• 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 "jerryscript-port.h"
18 #include "jerryscript-port-default.h"
19 #include "test-common.h"
20 #include <gtest/gtest.h>
21 
22 #define T(lhs, rhs, res) \
23   { lhs, rhs, res }
24 
25 typedef struct
26 {
27   jerry_value_t lhs;
28   jerry_value_t rhs;
29   bool expected;
30 } test_entry_t;
31 
32 static jerry_value_t
my_constructor(const jerry_value_t func_val,const jerry_value_t this_val,const jerry_value_t argv[],const jerry_length_t argc)33 my_constructor (const jerry_value_t func_val, /**< function */
34                 const jerry_value_t this_val, /**< this */
35                 const jerry_value_t argv[], /**< arguments */
36                 const jerry_length_t argc) /**< number of arguments */
37 {
38   (void) func_val;
39   (void) this_val;
40   (void) argv;
41   (void) argc;
42   return jerry_create_undefined ();
43 } /* my_constructor */
44 
45 class ApiBinaryOperationsInstanceofTest : public testing::Test{
46 public:
SetUpTestCase()47     static void SetUpTestCase()
48     {
49         GTEST_LOG_(INFO) << "ApiBinaryOperationsInstanceofTest SetUpTestCase";
50     }
51 
TearDownTestCase()52     static void TearDownTestCase()
53     {
54         GTEST_LOG_(INFO) << "ApiBinaryOperationsInstanceofTest TearDownTestCase";
55     }
56 
SetUp()57     void SetUp() override {}
TearDown()58     void TearDown() override {}
59 
60 };
61 
62 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)63 static void* context_alloc_fn(size_t size, void* cb_data)
64 {
65     (void)cb_data;
66     size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
67     return malloc(newSize);
68 }
69 HWTEST_F(ApiBinaryOperationsInstanceofTest, Test001, testing::ext::TestSize.Level1)
70 {
71   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
72   jerry_port_default_set_current_context (ctx_p);
73   TEST_INIT ();
74 
75   jerry_init (JERRY_INIT_EMPTY);
76 
77   jerry_value_t base_obj = jerry_create_object ();
78   jerry_value_t constructor = jerry_create_external_function (my_constructor);
79 
80   jerry_value_t no_proto_instance_val = jerry_construct_object (constructor, NULL, 0);
81 
82   jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) "prototype");
83   jerry_value_t res = jerry_set_property (constructor, prototype_str, base_obj);
84   jerry_release_value (prototype_str);
85   TEST_ASSERT (!jerry_value_is_error (res));
86   jerry_release_value (res);
87 
88   jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
89 
90   jerry_value_t error = jerry_create_error_from_value (base_obj, false);
91 
92   test_entry_t bool_tests[] =
93   {
94     T (jerry_acquire_value (instance_val), jerry_acquire_value (constructor), true),
95     T (jerry_acquire_value (no_proto_instance_val), jerry_acquire_value (constructor), false),
96     T (jerry_acquire_value (base_obj), jerry_acquire_value (constructor), false)
97   };
98 
99   for (uint32_t idx = 0; idx < sizeof (bool_tests) / sizeof (test_entry_t); idx++)
100   {
101     jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
102                                                    bool_tests[idx].lhs,
103                                                    bool_tests[idx].rhs);
104     TEST_ASSERT (!jerry_value_is_error (result));
105     TEST_ASSERT (jerry_get_boolean_value (result) == bool_tests[idx].expected);
106     jerry_release_value (bool_tests[idx].lhs);
107     jerry_release_value (bool_tests[idx].rhs);
108     jerry_release_value (result);
109   }
110 
111   test_entry_t error_tests[] =
112   {
113     T (jerry_acquire_value (constructor), jerry_acquire_value (instance_val), true),
114     T (jerry_create_undefined (), jerry_acquire_value (constructor), true),
115     T (jerry_acquire_value (instance_val), jerry_create_undefined (), true),
116     T (jerry_acquire_value (instance_val), jerry_acquire_value (base_obj), true),
117     T (jerry_acquire_value (error), jerry_acquire_value (constructor), true),
118     T (jerry_acquire_value (instance_val), jerry_acquire_value (error), true),
119     T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_string ((const jerry_char_t *) ""), true),
120     T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_number (5.0), true),
121     T (jerry_create_number (5.0), jerry_create_string ((const jerry_char_t *) ""), true),
122     T (jerry_create_array (1), jerry_create_array (1), true),
123     T (jerry_create_array (1), jerry_create_object (), true),
124     T (jerry_create_object (), jerry_create_array (1), true),
125     T (jerry_create_null (), jerry_create_object (), true),
126     T (jerry_create_object (), jerry_create_string ((const jerry_char_t *) ""), true)
127   };
128 
129   for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++)
130   {
131     jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
132                                                    error_tests[idx].lhs,
133                                                    error_tests[idx].rhs);
134     TEST_ASSERT (jerry_value_is_error (result) == error_tests[idx].expected);
135     jerry_release_value (error_tests[idx].lhs);
136     jerry_release_value (error_tests[idx].rhs);
137     jerry_release_value (result);
138   }
139 
140   jerry_release_value (base_obj);
141   jerry_release_value (constructor);
142   jerry_release_value (error);
143   jerry_release_value (instance_val);
144   jerry_release_value (no_proto_instance_val);
145 
146   jerry_cleanup ();
147   free (ctx_p);
148 }
149