Skip to content

Commit 6e31e64

Browse files
committed
Added new 'jerry_value_instanceof' API function.
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent a42f239 commit 6e31e64

File tree

5 files changed

+220
-1
lines changed

5 files changed

+220
-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_value_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_value_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_value_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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,40 @@ 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_value_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+
{
625+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (error_value_msg_p)));
626+
}
627+
628+
if (!ecma_is_value_object (value)
629+
|| !ecma_op_is_callable (prototype))
630+
{
631+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
632+
}
633+
634+
ecma_object_t *proto_obj_p = ecma_get_object_from_value (prototype);
635+
return jerry_return (ecma_op_object_has_instance (proto_obj_p, value));
636+
} /* jerry_value_instanceof */
637+
604638
/**
605639
* Check if the specified value is an abort value.
606640
*

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ jerry_value_t jerry_get_global_object (void);
343343
/**
344344
* Checker functions of 'jerry_value_t'.
345345
*/
346+
jerry_value_t jerry_value_instanceof(const jerry_value_t value,
347+
const jerry_value_t prototype);
346348
bool jerry_value_is_abort (const jerry_value_t value);
347349
bool jerry_value_is_array (const jerry_value_t value);
348350
bool jerry_value_is_boolean (const jerry_value_t value);

tests/unit-core/test-api-instaceof.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
static jerry_value_t
21+
my_constructor (const jerry_value_t func_val, /**< function */
22+
const jerry_value_t this_val, /**< this */
23+
const jerry_value_t argv[], /**< arguments */
24+
const jerry_length_t argc) /**< number of arguments */
25+
{
26+
(void) func_val;
27+
(void) this_val;
28+
(void) argv;
29+
(void) argc;
30+
return jerry_create_undefined ();
31+
} /* my_constructor */
32+
33+
int
34+
main (void)
35+
{
36+
TEST_INIT ();
37+
38+
jerry_init (JERRY_INIT_EMPTY);
39+
40+
jerry_value_t base_obj = jerry_create_object ();
41+
jerry_value_t constructor = jerry_create_external_function (my_constructor);
42+
43+
jerry_value_t no_proto_instance_val = jerry_construct_object (constructor, NULL, 0);
44+
45+
jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) ("prototype"));
46+
jerry_value_t res = jerry_set_property (constructor, prototype_str, base_obj);
47+
jerry_release_value (prototype_str);
48+
TEST_ASSERT (!jerry_value_is_error (res));
49+
jerry_release_value (res);
50+
51+
jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
52+
53+
/* Test the correct call */
54+
res = jerry_value_instanceof (instance_val, constructor);
55+
TEST_ASSERT (!jerry_value_is_error (res));
56+
TEST_ASSERT (jerry_get_boolean_value (res));
57+
jerry_release_value (res);
58+
59+
/* Test instance with different prototype */
60+
res = jerry_value_instanceof (no_proto_instance_val, constructor);
61+
TEST_ASSERT (!jerry_value_is_error (res));
62+
TEST_ASSERT (!jerry_get_boolean_value (res));
63+
jerry_release_value (res);
64+
65+
/* Test wrong argument order */
66+
res = jerry_value_instanceof (constructor, instance_val);
67+
TEST_ASSERT (jerry_value_is_error (res));
68+
jerry_release_value (res);
69+
70+
/* Test wrong arguments */
71+
res = jerry_value_instanceof (base_obj, constructor);
72+
TEST_ASSERT (!jerry_value_is_error (res));
73+
TEST_ASSERT (!jerry_get_boolean_value (res));
74+
jerry_release_value (res);
75+
76+
/* Test value is not an object */
77+
res = jerry_value_instanceof (jerry_create_undefined (), constructor);
78+
TEST_ASSERT (jerry_value_is_error (res));
79+
jerry_release_value (res);
80+
81+
/* Test prototype is not a function object */
82+
res = jerry_value_instanceof (instance_val, jerry_create_undefined ());
83+
TEST_ASSERT (jerry_value_is_error (res));
84+
jerry_release_value (res);
85+
86+
res = jerry_value_instanceof (instance_val, base_obj);
87+
TEST_ASSERT (jerry_value_is_error (res));
88+
jerry_release_value (res);
89+
90+
/* Test error argument */
91+
jerry_value_t error = jerry_create_error_from_value (base_obj, true);
92+
res = jerry_value_instanceof (error, constructor);
93+
TEST_ASSERT (jerry_value_is_error (res));
94+
jerry_release_value (res);
95+
96+
jerry_release_value (error);
97+
jerry_release_value (constructor);
98+
jerry_release_value (instance_val);
99+
jerry_release_value (no_proto_instance_val);
100+
101+
jerry_cleanup ();
102+
103+
return 0;
104+
} /* main */

0 commit comments

Comments
 (0)