Added 'instanceof' binary operation to the API (#2751)

Added 'JERRY_BIN_OP_INSTANCEOF' to 'jerry_binary_operation_t' and
'jerry_binary_operation'. Added unit tests for this new operation
and updated the documentations.

JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
This commit is contained in:
László Langó
2019-02-13 12:49:28 +01:00
committed by GitHub
parent ed0662612d
commit 2d16705050
6 changed files with 198 additions and 8 deletions
+56 -1
View File
@@ -286,6 +286,7 @@ Enum that contains the supported binary operation types
- JERRY_BIN_OP_LESS_EQUAL - less or equal relation (<=)
- JERRY_BIN_OP_GREATER - greater relation (>)
- JERRY_BIN_OP_GREATER_EQUAL - greater or equal relation (>=)
- JERRY_BIN_OP_INSTANCEOF - instanceof operation
## jerry_property_descriptor_t
@@ -1092,6 +1093,7 @@ jerry_get_global_object (void);
- [jerry_release_value](#jerry_release_value)
- [jerry_define_own_property](#jerry_define_own_property)
# Checker functions
Functions to check the type of an API value ([jerry_value_t](#jerry_value_t)).
@@ -1741,7 +1743,7 @@ jerry_binary_operation (jerry_binary_operation_t op,
- error, if argument has an error flag or operation is unsuccessful or unsupported
- true/false, the result of the binary operation on the given operands otherwise
**Example**
**Example - JERRY_BIN_OP_EQUAL**
```c
{
@@ -1772,6 +1774,59 @@ jerry_binary_operation (jerry_binary_operation_t op,
}
```
**Example - JERRY_BIN_OP_INSTANCEOF**
[doctest]: # ()
```c
#include "jerryscript.h"
static jerry_value_t
my_constructor (const jerry_value_t func_val,
const jerry_value_t this_val,
const jerry_value_t argv[],
const jerry_length_t argc)
{
return jerry_create_undefined ();
}
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t base_obj = jerry_create_object ();
jerry_value_t constructor = jerry_create_external_function (my_constructor);
/* External functions does not have a prototype by default, so we need to create one */
jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) ("prototype"));
jerry_release_value (jerry_set_property (constructor, prototype_str, base_obj));
jerry_release_value (prototype_str);
/* Construct the instance. */
jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
/* Call the API function of 'instanceof'. */
jerry_value_t is_instance = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
instance_val,
constructor);
if (!jerry_value_is_error (is_instance)
&& jerry_get_boolean_value (is_instance) == true)
{
/* ... */
}
/* Free all of the jerry values and cleanup the engine. */
jerry_release_value (base_obj);
jerry_release_value (constructor);
jerry_release_value (instance_val);
jerry_release_value (is_instance);
jerry_cleanup ();
return 0;
}
```
**See also**
- [jerry_binary_operation_t](#jerry_binary_operation_t)