From fd47929742453b4c718179f19587f3ddd8af83ef Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 30 Apr 2019 17:36:04 +0800 Subject: [PATCH] Add core API jerry_value_instanceof JerryScript-DCO-1.0-Signed-off-by: legendecas legendecas@gmail.com --- jerry-core/api/jerry.c | 15 +++++++++++++++ jerry-core/include/jerryscript-core.h | 1 + tests/unit-core/test-api.c | 1 + 3 files changed, 17 insertions(+) diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 0664e86612..cf9cc0f1bb 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -807,6 +807,21 @@ jerry_value_is_undefined (const jerry_value_t value) /**< api value */ return ecma_is_value_undefined (value); } /* jerry_value_is_undefined */ +/** + * Perform instanceof check on the specified two operands. + * + * @return true - if the left hand side is instance of right hand side, + * false - otherwise. + */ +bool +jerry_value_instanceof (const jerry_value_t value, const jerry_value_t proto) +{ + jerry_assert_api_available (); + + ecma_value_t ret = ecma_op_object_has_instance (ecma_get_object_from_value (proto), value); + return ecma_is_value_true (ret); +} + /** * Perform the base type of the JavaScript value. * diff --git a/jerry-core/include/jerryscript-core.h b/jerry-core/include/jerryscript-core.h index 60805eee35..a309830185 100644 --- a/jerry-core/include/jerryscript-core.h +++ b/jerry-core/include/jerryscript-core.h @@ -372,6 +372,7 @@ bool jerry_value_is_promise (const jerry_value_t value); bool jerry_value_is_string (const jerry_value_t value); bool jerry_value_is_symbol (const jerry_value_t value); bool jerry_value_is_undefined (const jerry_value_t value); +bool jerry_value_instanceof(const jerry_value_t value, const jerry_value_t proto); /** * JerryScript API value type information. diff --git a/tests/unit-core/test-api.c b/tests/unit-core/test-api.c index 09ec226c05..0850e91043 100644 --- a/tests/unit-core/test-api.c +++ b/tests/unit-core/test-api.c @@ -450,6 +450,7 @@ main (void) val_a = get_property (global_obj_val, "a"); TEST_ASSERT (!jerry_value_is_error (val_a)); TEST_ASSERT (jerry_value_is_object (val_a)); + TEST_ASSERT (jerry_value_instanceof (val_a, val_A)); /* Get a.t */ res = get_property (val_a, "t");