• 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 
18 #include "test-common.h"
19 
20 #define T(lhs, rhs, res) \
21   { lhs, rhs, res }
22 
23 typedef struct
24 {
25   jerry_value_t lhs;
26   jerry_value_t rhs;
27   bool expected;
28 } test_entry_t;
29 
30 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)31 my_constructor (const jerry_value_t func_val, /**< function */
32                 const jerry_value_t this_val, /**< this */
33                 const jerry_value_t argv[], /**< arguments */
34                 const jerry_length_t argc) /**< number of arguments */
35 {
36   (void) func_val;
37   (void) this_val;
38   (void) argv;
39   (void) argc;
40   return jerry_create_undefined ();
41 } /* my_constructor */
42 
43 int
main(void)44 main (void)
45 {
46   TEST_INIT ();
47 
48   jerry_init (JERRY_INIT_EMPTY);
49 
50   jerry_value_t base_obj = jerry_create_object ();
51   jerry_value_t constructor = jerry_create_external_function (my_constructor);
52 
53   jerry_value_t no_proto_instance_val = jerry_construct_object (constructor, NULL, 0);
54 
55   jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) "prototype");
56   jerry_value_t res = jerry_set_property (constructor, prototype_str, base_obj);
57   jerry_release_value (prototype_str);
58   TEST_ASSERT (!jerry_value_is_error (res));
59   jerry_release_value (res);
60 
61   jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
62 
63   jerry_value_t error = jerry_create_error_from_value (base_obj, false);
64 
65   test_entry_t bool_tests[] =
66   {
67     T (jerry_acquire_value (instance_val), jerry_acquire_value (constructor), true),
68     T (jerry_acquire_value (no_proto_instance_val), jerry_acquire_value (constructor), false),
69     T (jerry_acquire_value (base_obj), jerry_acquire_value (constructor), false)
70   };
71 
72   for (uint32_t idx = 0; idx < sizeof (bool_tests) / sizeof (test_entry_t); idx++)
73   {
74     jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
75                                                    bool_tests[idx].lhs,
76                                                    bool_tests[idx].rhs);
77     TEST_ASSERT (!jerry_value_is_error (result));
78     TEST_ASSERT (jerry_get_boolean_value (result) == bool_tests[idx].expected);
79     jerry_release_value (bool_tests[idx].lhs);
80     jerry_release_value (bool_tests[idx].rhs);
81     jerry_release_value (result);
82   }
83 
84   test_entry_t error_tests[] =
85   {
86     T (jerry_acquire_value (constructor), jerry_acquire_value (instance_val), true),
87     T (jerry_create_undefined (), jerry_acquire_value (constructor), true),
88     T (jerry_acquire_value (instance_val), jerry_create_undefined (), true),
89     T (jerry_acquire_value (instance_val), jerry_acquire_value (base_obj), true),
90     T (jerry_acquire_value (error), jerry_acquire_value (constructor), true),
91     T (jerry_acquire_value (instance_val), jerry_acquire_value (error), true),
92     T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_string ((const jerry_char_t *) ""), true),
93     T (jerry_create_string ((const jerry_char_t *) ""), jerry_create_number (5.0), true),
94     T (jerry_create_number (5.0), jerry_create_string ((const jerry_char_t *) ""), true),
95     T (jerry_create_array (1), jerry_create_array (1), true),
96     T (jerry_create_array (1), jerry_create_object (), true),
97     T (jerry_create_object (), jerry_create_array (1), true),
98     T (jerry_create_null (), jerry_create_object (), true),
99     T (jerry_create_object (), jerry_create_string ((const jerry_char_t *) ""), true)
100   };
101 
102   for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++)
103   {
104     jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
105                                                    error_tests[idx].lhs,
106                                                    error_tests[idx].rhs);
107     TEST_ASSERT (jerry_value_is_error (result) == error_tests[idx].expected);
108     jerry_release_value (error_tests[idx].lhs);
109     jerry_release_value (error_tests[idx].rhs);
110     jerry_release_value (result);
111   }
112 
113   jerry_release_value (base_obj);
114   jerry_release_value (constructor);
115   jerry_release_value (error);
116   jerry_release_value (instance_val);
117   jerry_release_value (no_proto_instance_val);
118 
119   jerry_cleanup ();
120 
121   return 0;
122 } /* main */
123