Skip to content

Commit c1bddc7

Browse files
committed
Added new 'jerry_value_instanceof' API function.
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
1 parent 924f4bb commit c1bddc7

File tree

5 files changed

+234
-1
lines changed

5 files changed

+234
-1
lines changed

docs/02.API-REFERENCE.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,84 @@ jerry_get_global_object (void);
10841084

10851085
Functions to check the type of an API value ([jerry_value_t](#jerry_value_t)).
10861086

1087+
1088+
## jerry_instanceof
1089+
1090+
**Summary**
1091+
1092+
Returns whether the given `jerry_value_t` is an instance of the given prototype.
1093+
1094+
**Prototype**
1095+
1096+
```c
1097+
jerry_value_t
1098+
jerry_instanceof (const jerry_value_t value, const jerry_value_t prototype);
1099+
```
1100+
1101+
- `value` - api value of instance object
1102+
- `prototype` - api value of prototype
1103+
- return value
1104+
- error, if value argument is not an object or prototype argument is not a function
1105+
- true, if the specified value is an instance of the given prototype
1106+
- false, otherwise
1107+
1108+
**Example**
1109+
1110+
[doctest]: # ()
1111+
1112+
```c
1113+
#include "jerryscript.h"
1114+
1115+
static jerry_value_t
1116+
my_constructor (const jerry_value_t func_val,
1117+
const jerry_value_t this_val,
1118+
const jerry_value_t argv[],
1119+
const jerry_length_t argc)
1120+
{
1121+
return jerry_create_undefined ();
1122+
}
1123+
1124+
int
1125+
main (void)
1126+
{
1127+
jerry_init (JERRY_INIT_EMPTY);
1128+
1129+
jerry_value_t base_obj = jerry_create_object ();
1130+
jerry_value_t constructor = jerry_create_external_function (my_constructor);
1131+
1132+
/* External functions does not have a prototype by default, so we need to create one */
1133+
jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) ("prototype"));
1134+
jerry_release_value (jerry_set_property (constructor, prototype_str, base_obj));
1135+
jerry_release_value (prototype_str);
1136+
1137+
/* Construct the instance. */
1138+
jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
1139+
1140+
/* Call the API function of 'instanceof'. */
1141+
jerry_value_t is_instance = jerry_instanceof (instance_val, constructor);
1142+
if (!jerry_value_is_error (is_instance)
1143+
&& jerry_get_boolean_value (is_instance) == true)
1144+
{
1145+
/* ... */
1146+
}
1147+
1148+
/* Free all of the jerry values and cleanup the engine. */
1149+
jerry_release_value (base_obj);
1150+
jerry_release_value (constructor);
1151+
jerry_release_value (instance_val);
1152+
jerry_release_value (is_instance);
1153+
1154+
jerry_cleanup ();
1155+
return 0;
1156+
}
1157+
```
1158+
1159+
**See also**
1160+
1161+
- [jerry_construct_object](#jerry_construct_object)
1162+
- [jerry_create_external_function](#jerry_create_external_function)
1163+
1164+
10871165
## jerry_value_is_abort
10881166

10891167
**Summary**

jerry-core/api/jerry.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,41 @@ jerry_get_global_object (void)
601601
return ecma_make_object_value (global_obj_p);
602602
} /* jerry_get_global_object */
603603

604+
/**
605+
* Check if the specified value is an instance of the given prototype.
606+
*
607+
* See also ECMA-262 v5.1, 11.8.6
608+
*
609+
* Note:
610+
* returned value must be freed with jerry_release_value, when it is no longer needed.
611+
*
612+
* @return true - if the specified value is an instance of the given prototype
613+
* false - otherwise
614+
* error - if value argument is not an object or prototype argument is not a function.
615+
*
616+
*/
617+
jerry_value_t
618+
jerry_instanceof (const jerry_value_t value, /**< object value */
619+
const jerry_value_t prototype) /**< prototpye function */
620+
{
621+
jerry_assert_api_available ();
622+
623+
if (ecma_is_value_error_reference (value)
624+
|| ecma_is_value_error_reference (prototype))
625+
{
626+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (error_value_msg_p)));
627+
}
628+
629+
if (!ecma_is_value_object (value)
630+
|| !ecma_op_is_callable (prototype))
631+
{
632+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
633+
}
634+
635+
ecma_object_t *proto_obj_p = ecma_get_object_from_value (prototype);
636+
return jerry_return (ecma_op_object_has_instance (proto_obj_p, value));
637+
} /* jerry_instanceof */
638+
604639
/**
605640
* Check if the specified value is an abort value.
606641
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ ecma_op_arrow_function_get_compiled_code (ecma_arrow_function_t *arrow_function_
327327
/**
328328
* 15.3.5.3 implementation of [[HasInstance]] for Function objects
329329
*
330-
* @return ecma value
330+
* @return true/false - if arguments are valid
331+
* error - otherwise
331332
* Returned value must be freed with ecma_free_value
332333
*/
333334
ecma_value_t

jerry-core/include/jerryscript-core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ jerry_value_t jerry_get_global_object (void);
343343
/**
344344
* Checker functions of 'jerry_value_t'.
345345
*/
346+
jerry_value_t jerry_instanceof (const jerry_value_t value, const jerry_value_t prototype);
346347
bool jerry_value_is_abort (const jerry_value_t value);
347348
bool jerry_value_is_array (const jerry_value_t value);
348349
bool jerry_value_is_boolean (const jerry_value_t value);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_instanceof (bool_tests[idx].lhs, bool_tests[idx].rhs);
75+
TEST_ASSERT (!jerry_value_is_error (result));
76+
TEST_ASSERT (jerry_get_boolean_value (result) == bool_tests[idx].expected);
77+
jerry_release_value (bool_tests[idx].lhs);
78+
jerry_release_value (bool_tests[idx].rhs);
79+
jerry_release_value (result);
80+
}
81+
82+
test_entry_t error_tests[] =
83+
{
84+
T (jerry_acquire_value (constructor), jerry_acquire_value (instance_val), true),
85+
T (jerry_create_undefined (), jerry_acquire_value (constructor), true),
86+
T (jerry_acquire_value (instance_val), jerry_create_undefined (), true),
87+
T (jerry_acquire_value (instance_val), jerry_acquire_value (base_obj), true),
88+
T (jerry_acquire_value (error), jerry_acquire_value (constructor), true),
89+
T (jerry_acquire_value (instance_val), jerry_acquire_value (error), true),
90+
T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_string ((const jerry_char_t *) ""), true),
91+
T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_number (5.0), true),
92+
T (jerry_create_number (5.0), jerry_create_string ((const jerry_char_t *) ""), true),
93+
T (jerry_create_array (1), jerry_create_array (1), true),
94+
T (jerry_create_array (1), jerry_create_object (), true),
95+
T (jerry_create_object (), jerry_create_array (1), true),
96+
T (jerry_create_null (), jerry_create_object (), true),
97+
T (jerry_create_object (), jerry_create_string ((const jerry_char_t *) ""), true)
98+
};
99+
100+
for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++)
101+
{
102+
jerry_value_t result = jerry_instanceof (error_tests[idx].lhs, error_tests[idx].rhs);
103+
TEST_ASSERT (jerry_value_is_error (result) == error_tests[idx].expected);
104+
jerry_release_value (error_tests[idx].lhs);
105+
jerry_release_value (error_tests[idx].rhs);
106+
jerry_release_value (result);
107+
}
108+
109+
jerry_release_value (base_obj);
110+
jerry_release_value (constructor);
111+
jerry_release_value (error);
112+
jerry_release_value (instance_val);
113+
jerry_release_value (no_proto_instance_val);
114+
115+
jerry_cleanup ();
116+
117+
return 0;
118+
} /* main */

0 commit comments

Comments
 (0)