1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "jerryscript.h"
17425bb815Sopenharmony_ci#include "test-common.h"
18425bb815Sopenharmony_ci#include "jerryscript-port.h"
19425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
20425bb815Sopenharmony_ci#include <gtest/gtest.h>
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_cistatic const char instanceof_source[] = "var x = function(o, c) {return (o instanceof c);}; x";
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_cistatic jerry_value_t
25425bb815Sopenharmony_ciexternal_function (const jerry_value_t function_obj,
26425bb815Sopenharmony_ci                   const jerry_value_t this_arg,
27425bb815Sopenharmony_ci                   const jerry_value_t args_p[],
28425bb815Sopenharmony_ci                   const jerry_size_t args_count)
29425bb815Sopenharmony_ci{
30425bb815Sopenharmony_ci  (void) function_obj;
31425bb815Sopenharmony_ci  (void) this_arg;
32425bb815Sopenharmony_ci  (void) args_p;
33425bb815Sopenharmony_ci  (void) args_count;
34425bb815Sopenharmony_ci
35425bb815Sopenharmony_ci  return jerry_create_undefined ();
36425bb815Sopenharmony_ci} /* external_function */
37425bb815Sopenharmony_ci
38425bb815Sopenharmony_cistatic void
39425bb815Sopenharmony_citest_instanceof (jerry_value_t instanceof,
40425bb815Sopenharmony_ci                 jerry_value_t constructor)
41425bb815Sopenharmony_ci{
42425bb815Sopenharmony_ci  jerry_value_t instance = jerry_construct_object (constructor, NULL, 0);
43425bb815Sopenharmony_ci  jerry_value_t args[2] =
44425bb815Sopenharmony_ci  {
45425bb815Sopenharmony_ci    instance, constructor
46425bb815Sopenharmony_ci  };
47425bb815Sopenharmony_ci
48425bb815Sopenharmony_ci  jerry_value_t undefined = jerry_create_undefined ();
49425bb815Sopenharmony_ci  jerry_value_t result = jerry_call_function (instanceof, undefined, args, 2);
50425bb815Sopenharmony_ci  jerry_release_value (undefined);
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_ci  TEST_ASSERT (!jerry_value_is_error (result));
53425bb815Sopenharmony_ci  TEST_ASSERT (jerry_value_is_boolean (result));
54425bb815Sopenharmony_ci
55425bb815Sopenharmony_ci  TEST_ASSERT (jerry_get_boolean_value (result));
56425bb815Sopenharmony_ci
57425bb815Sopenharmony_ci  jerry_release_value (instance);
58425bb815Sopenharmony_ci  jerry_release_value (result);
59425bb815Sopenharmony_ci} /* test_instanceof */
60425bb815Sopenharmony_ci
61425bb815Sopenharmony_ciclass NativeInstanceofTest : public testing::Test{
62425bb815Sopenharmony_cipublic:
63425bb815Sopenharmony_ci    static void SetUpTestCase()
64425bb815Sopenharmony_ci    {
65425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "NativeInstanceofTest SetUpTestCase";
66425bb815Sopenharmony_ci    }
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ci    static void TearDownTestCase()
69425bb815Sopenharmony_ci    {
70425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "NativeInstanceofTest TearDownTestCase";
71425bb815Sopenharmony_ci    }
72425bb815Sopenharmony_ci
73425bb815Sopenharmony_ci    void SetUp() override {}
74425bb815Sopenharmony_ci    void TearDown() override {}
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci};
77425bb815Sopenharmony_cistatic void *
78425bb815Sopenharmony_cicontext_alloc_fn (size_t size, void *cb_data)
79425bb815Sopenharmony_ci{
80425bb815Sopenharmony_ci  (void) cb_data;
81425bb815Sopenharmony_ci  return malloc (size);
82425bb815Sopenharmony_ci} /* context_alloc_fn */
83425bb815Sopenharmony_ciHWTEST_F(NativeInstanceofTest, Test001, testing::ext::TestSize.Level1)
84425bb815Sopenharmony_ci{
85425bb815Sopenharmony_ci  jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
86425bb815Sopenharmony_ci  jerry_port_default_set_current_context (ctx_p);
87425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
88425bb815Sopenharmony_ci
89425bb815Sopenharmony_ci  jerry_value_t instanceof = jerry_eval ((jerry_char_t *) instanceof_source, sizeof (instanceof_source) - 1, true);
90425bb815Sopenharmony_ci
91425bb815Sopenharmony_ci  /* Test for a native-backed function. */
92425bb815Sopenharmony_ci  jerry_value_t constructor = jerry_create_external_function (external_function);
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ci  test_instanceof (instanceof, constructor);
95425bb815Sopenharmony_ci  jerry_release_value (constructor);
96425bb815Sopenharmony_ci
97425bb815Sopenharmony_ci  /* Test for a JS constructor. */
98425bb815Sopenharmony_ci  jerry_value_t global = jerry_get_global_object ();
99425bb815Sopenharmony_ci  jerry_value_t object_name = jerry_create_string ((jerry_char_t *) "Object");
100425bb815Sopenharmony_ci  constructor = jerry_get_property (global, object_name);
101425bb815Sopenharmony_ci  jerry_release_value (object_name);
102425bb815Sopenharmony_ci  jerry_release_value (global);
103425bb815Sopenharmony_ci
104425bb815Sopenharmony_ci  test_instanceof (instanceof, constructor);
105425bb815Sopenharmony_ci  jerry_release_value (constructor);
106425bb815Sopenharmony_ci
107425bb815Sopenharmony_ci  jerry_release_value (instanceof);
108425bb815Sopenharmony_ci
109425bb815Sopenharmony_ci  jerry_cleanup ();
110425bb815Sopenharmony_ci  free (ctx_p);
111425bb815Sopenharmony_ci}
112