• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "test-common.h"
18 #include "jerryscript-port.h"
19 #include "jerryscript-port-default.h"
20 #include <gtest/gtest.h>
21 
22 static const char instanceof_source[] = "var x = function(o, c) {return (o instanceof c);}; x";
23 
24 static jerry_value_t
external_function(const jerry_value_t function_obj,const jerry_value_t this_arg,const jerry_value_t args_p[],const jerry_size_t args_count)25 external_function (const jerry_value_t function_obj,
26                    const jerry_value_t this_arg,
27                    const jerry_value_t args_p[],
28                    const jerry_size_t args_count)
29 {
30   (void) function_obj;
31   (void) this_arg;
32   (void) args_p;
33   (void) args_count;
34 
35   return jerry_create_undefined ();
36 } /* external_function */
37 
38 static void
test_instanceof(jerry_value_t instanceof,jerry_value_t constructor)39 test_instanceof (jerry_value_t instanceof,
40                  jerry_value_t constructor)
41 {
42   jerry_value_t instance = jerry_construct_object (constructor, NULL, 0);
43   jerry_value_t args[2] =
44   {
45     instance, constructor
46   };
47 
48   jerry_value_t undefined = jerry_create_undefined ();
49   jerry_value_t result = jerry_call_function (instanceof, undefined, args, 2);
50   jerry_release_value (undefined);
51 
52   TEST_ASSERT (!jerry_value_is_error (result));
53   TEST_ASSERT (jerry_value_is_boolean (result));
54 
55   TEST_ASSERT (jerry_get_boolean_value (result));
56 
57   jerry_release_value (instance);
58   jerry_release_value (result);
59 } /* test_instanceof */
60 
61 class NativeInstanceofTest : public testing::Test{
62 public:
SetUpTestCase()63     static void SetUpTestCase()
64     {
65         GTEST_LOG_(INFO) << "NativeInstanceofTest SetUpTestCase";
66     }
67 
TearDownTestCase()68     static void TearDownTestCase()
69     {
70         GTEST_LOG_(INFO) << "NativeInstanceofTest TearDownTestCase";
71     }
72 
SetUp()73     void SetUp() override {}
TearDown()74     void TearDown() override {}
75 
76 };
77 static void *
context_alloc_fn(size_t size,void * cb_data)78 context_alloc_fn (size_t size, void *cb_data)
79 {
80   (void) cb_data;
81   return malloc (size);
82 } /* context_alloc_fn */
83 HWTEST_F(NativeInstanceofTest, Test001, testing::ext::TestSize.Level1)
84 {
85   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
86   jerry_port_default_set_current_context (ctx_p);
87   jerry_init (JERRY_INIT_EMPTY);
88 
89   jerry_value_t instanceof = jerry_eval ((jerry_char_t *) instanceof_source, sizeof (instanceof_source) - 1, true);
90 
91   /* Test for a native-backed function. */
92   jerry_value_t constructor = jerry_create_external_function (external_function);
93 
94   test_instanceof (instanceof, constructor);
95   jerry_release_value (constructor);
96 
97   /* Test for a JS constructor. */
98   jerry_value_t global = jerry_get_global_object ();
99   jerry_value_t object_name = jerry_create_string ((jerry_char_t *) "Object");
100   constructor = jerry_get_property (global, object_name);
101   jerry_release_value (object_name);
102   jerry_release_value (global);
103 
104   test_instanceof (instanceof, constructor);
105   jerry_release_value (constructor);
106 
107   jerry_release_value (instanceof);
108 
109   jerry_cleanup ();
110   free (ctx_p);
111 }
112