Skip to content

Commit 6dad0ae

Browse files
committed
Added 'instanceof' binary operation to the API
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
1 parent 81ccd6a commit 6dad0ae

File tree

6 files changed

+198
-8
lines changed

6 files changed

+198
-8
lines changed

docs/02.API-REFERENCE.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ Enum that contains the supported binary operation types
286286
- JERRY_BIN_OP_LESS_EQUAL - less or equal relation (<=)
287287
- JERRY_BIN_OP_GREATER - greater relation (>)
288288
- JERRY_BIN_OP_GREATER_EQUAL - greater or equal relation (>=)
289+
- JERRY_BIN_OP_INSTANCEOF - instanceof operation
289290

290291

291292
## jerry_property_descriptor_t
@@ -1092,6 +1093,7 @@ jerry_get_global_object (void);
10921093
- [jerry_release_value](#jerry_release_value)
10931094
- [jerry_define_own_property](#jerry_define_own_property)
10941095

1096+
10951097
# Checker functions
10961098

10971099
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,
17411743
- error, if argument has an error flag or operation is unsuccessful or unsupported
17421744
- true/false, the result of the binary operation on the given operands otherwise
17431745

1744-
**Example**
1746+
**Example - JERRY_BIN_OP_EQUAL**
17451747

17461748
```c
17471749
{
@@ -1772,6 +1774,59 @@ jerry_binary_operation (jerry_binary_operation_t op,
17721774
}
17731775
```
17741776

1777+
**Example - JERRY_BIN_OP_INSTANCEOF**
1778+
1779+
[doctest]: # ()
1780+
1781+
```c
1782+
#include "jerryscript.h"
1783+
1784+
static jerry_value_t
1785+
my_constructor (const jerry_value_t func_val,
1786+
const jerry_value_t this_val,
1787+
const jerry_value_t argv[],
1788+
const jerry_length_t argc)
1789+
{
1790+
return jerry_create_undefined ();
1791+
}
1792+
1793+
int
1794+
main (void)
1795+
{
1796+
jerry_init (JERRY_INIT_EMPTY);
1797+
1798+
jerry_value_t base_obj = jerry_create_object ();
1799+
jerry_value_t constructor = jerry_create_external_function (my_constructor);
1800+
1801+
/* External functions does not have a prototype by default, so we need to create one */
1802+
jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) ("prototype"));
1803+
jerry_release_value (jerry_set_property (constructor, prototype_str, base_obj));
1804+
jerry_release_value (prototype_str);
1805+
1806+
/* Construct the instance. */
1807+
jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
1808+
1809+
/* Call the API function of 'instanceof'. */
1810+
jerry_value_t is_instance = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
1811+
instance_val,
1812+
constructor);
1813+
if (!jerry_value_is_error (is_instance)
1814+
&& jerry_get_boolean_value (is_instance) == true)
1815+
{
1816+
/* ... */
1817+
}
1818+
1819+
/* Free all of the jerry values and cleanup the engine. */
1820+
jerry_release_value (base_obj);
1821+
jerry_release_value (constructor);
1822+
jerry_release_value (instance_val);
1823+
jerry_release_value (is_instance);
1824+
1825+
jerry_cleanup ();
1826+
return 0;
1827+
}
1828+
```
1829+
17751830
**See also**
17761831
- [jerry_binary_operation_t](#jerry_binary_operation_t)
17771832

jerry-core/api/jerry.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,17 @@ jerry_binary_operation (jerry_binary_operation_t op, /**< operation */
947947
{
948948
return jerry_return (opfunc_relation (lhs, rhs, true, true));
949949
}
950+
case JERRY_BIN_OP_INSTANCEOF:
951+
{
952+
if (!ecma_is_value_object (lhs)
953+
|| !ecma_op_is_callable (rhs))
954+
{
955+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
956+
}
957+
958+
ecma_object_t *proto_obj_p = ecma_get_object_from_value (rhs);
959+
return jerry_return (ecma_op_object_has_instance (proto_obj_p, lhs));
960+
}
950961
default:
951962
{
952963
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Unsupported binary operation")));

jerry-core/ecma/operations/ecma-function-object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ ecma_op_implicit_class_constructor_has_instance (ecma_object_t *func_obj_p, /**<
391391
/**
392392
* 15.3.5.3 implementation of [[HasInstance]] for Function objects
393393
*
394-
* @return ecma value
394+
* @return true/false - if arguments are valid
395+
* error - otherwise
395396
* Returned value must be freed with ecma_free_value
396397
*/
397398
ecma_value_t

jerry-core/include/jerryscript-core.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,13 @@ typedef struct jerry_context_t jerry_context_t;
313313
*/
314314
typedef enum
315315
{
316-
JERRY_BIN_OP_EQUAL = 0u, /**< equal comparison (==) */
317-
JERRY_BIN_OP_STRICT_EQUAL, /**< strict equal comparison (===) */
318-
JERRY_BIN_OP_LESS, /**< less relation (<) */
319-
JERRY_BIN_OP_LESS_EQUAL, /**< less or equal relation (<=) */
320-
JERRY_BIN_OP_GREATER, /**< greater relation (>) */
321-
JERRY_BIN_OP_GREATER_EQUAL /**< greater or equal relation (>=)*/
316+
JERRY_BIN_OP_EQUAL = 0u, /**< equal comparison (==) */
317+
JERRY_BIN_OP_STRICT_EQUAL, /**< strict equal comparison (===) */
318+
JERRY_BIN_OP_LESS, /**< less relation (<) */
319+
JERRY_BIN_OP_LESS_EQUAL, /**< less or equal relation (<=) */
320+
JERRY_BIN_OP_GREATER, /**< greater relation (>) */
321+
JERRY_BIN_OP_GREATER_EQUAL, /**< greater or equal relation (>=)*/
322+
JERRY_BIN_OP_INSTANCEOF /**< instanceof operation */
322323
} jerry_binary_operation_t;
323324

324325
/**
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "jerryscript.h"
17+
18+
#include "test-common.h"
19+
20+
#define T(lhs, rhs, res) \
21+
{ lhs, rhs, res }
22+
23+
typedef struct
24+
{
25+
jerry_value_t lhs;
26+
jerry_value_t rhs;
27+
bool expected;
28+
} test_entry_t;
29+
30+
static jerry_value_t
31+
my_constructor (const jerry_value_t func_val, /**< function */
32+
const jerry_value_t this_val, /**< this */
33+
const jerry_value_t argv[], /**< arguments */
34+
const jerry_length_t argc) /**< number of arguments */
35+
{
36+
(void) func_val;
37+
(void) this_val;
38+
(void) argv;
39+
(void) argc;
40+
return jerry_create_undefined ();
41+
} /* my_constructor */
42+
43+
int
44+
main (void)
45+
{
46+
TEST_INIT ();
47+
48+
jerry_init (JERRY_INIT_EMPTY);
49+
50+
jerry_value_t base_obj = jerry_create_object ();
51+
jerry_value_t constructor = jerry_create_external_function (my_constructor);
52+
53+
jerry_value_t no_proto_instance_val = jerry_construct_object (constructor, NULL, 0);
54+
55+
jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) "prototype");
56+
jerry_value_t res = jerry_set_property (constructor, prototype_str, base_obj);
57+
jerry_release_value (prototype_str);
58+
TEST_ASSERT (!jerry_value_is_error (res));
59+
jerry_release_value (res);
60+
61+
jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
62+
63+
jerry_value_t error = jerry_create_error_from_value (base_obj, false);
64+
65+
test_entry_t bool_tests[] =
66+
{
67+
T (jerry_acquire_value (instance_val), jerry_acquire_value (constructor), true),
68+
T (jerry_acquire_value (no_proto_instance_val), jerry_acquire_value (constructor), false),
69+
T (jerry_acquire_value (base_obj), jerry_acquire_value (constructor), false)
70+
};
71+
72+
for (uint32_t idx = 0; idx < sizeof (bool_tests) / sizeof (test_entry_t); idx++)
73+
{
74+
jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
75+
bool_tests[idx].lhs,
76+
bool_tests[idx].rhs);
77+
TEST_ASSERT (!jerry_value_is_error (result));
78+
TEST_ASSERT (jerry_get_boolean_value (result) == bool_tests[idx].expected);
79+
jerry_release_value (bool_tests[idx].lhs);
80+
jerry_release_value (bool_tests[idx].rhs);
81+
jerry_release_value (result);
82+
}
83+
84+
test_entry_t error_tests[] =
85+
{
86+
T (jerry_acquire_value (constructor), jerry_acquire_value (instance_val), true),
87+
T (jerry_create_undefined (), jerry_acquire_value (constructor), true),
88+
T (jerry_acquire_value (instance_val), jerry_create_undefined (), true),
89+
T (jerry_acquire_value (instance_val), jerry_acquire_value (base_obj), true),
90+
T (jerry_acquire_value (error), jerry_acquire_value (constructor), true),
91+
T (jerry_acquire_value (instance_val), jerry_acquire_value (error), true),
92+
T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_string ((const jerry_char_t *) ""), true),
93+
T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_number (5.0), true),
94+
T (jerry_create_number (5.0), jerry_create_string ((const jerry_char_t *) ""), true),
95+
T (jerry_create_array (1), jerry_create_array (1), true),
96+
T (jerry_create_array (1), jerry_create_object (), true),
97+
T (jerry_create_object (), jerry_create_array (1), true),
98+
T (jerry_create_null (), jerry_create_object (), true),
99+
T (jerry_create_object (), jerry_create_string ((const jerry_char_t *) ""), true)
100+
};
101+
102+
for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++)
103+
{
104+
jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
105+
error_tests[idx].lhs,
106+
error_tests[idx].rhs);
107+
TEST_ASSERT (jerry_value_is_error (result) == error_tests[idx].expected);
108+
jerry_release_value (error_tests[idx].lhs);
109+
jerry_release_value (error_tests[idx].rhs);
110+
jerry_release_value (result);
111+
}
112+
113+
jerry_release_value (base_obj);
114+
jerry_release_value (constructor);
115+
jerry_release_value (error);
116+
jerry_release_value (instance_val);
117+
jerry_release_value (no_proto_instance_val);
118+
119+
jerry_cleanup ();
120+
121+
return 0;
122+
} /* main */

0 commit comments

Comments
 (0)