• 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 
21 int
main(void)22 main (void)
23 {
24   jerry_init (JERRY_INIT_EMPTY);
25 
26   jerry_value_t object = jerry_create_object ();
27 
28   jerry_value_t prop_name_1 = jerry_create_string ((const jerry_char_t *) "foo");
29   jerry_value_t prop_name_2 = jerry_create_string ((const jerry_char_t *) "non_hidden_prop");
30   jerry_value_t prop_name_3;
31 
32   if (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
33   {
34     jerry_value_t prop_name_3_desc = jerry_create_string ((const jerry_char_t *) "bar");
35     prop_name_3 = jerry_create_symbol (prop_name_3_desc);
36     jerry_release_value (prop_name_3_desc);
37   }
38   else
39   {
40     prop_name_3 = jerry_create_string ((const jerry_char_t *) "non_hidden_string_prop");
41   }
42 
43   jerry_value_t internal_prop_name_1 = jerry_create_string ((const jerry_char_t *) "hidden_foo");
44   jerry_value_t internal_prop_name_2 = jerry_create_string ((const jerry_char_t *) "hidden_prop");
45   jerry_value_t internal_prop_name_3;
46 
47   if (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
48   {
49     jerry_value_t internal_prop_name_3_desc = jerry_create_string ((const jerry_char_t *) "bar");
50     internal_prop_name_3 = jerry_create_symbol (internal_prop_name_3_desc);
51     jerry_release_value (internal_prop_name_3_desc);
52   }
53   else
54   {
55     internal_prop_name_3 = jerry_create_string ((const jerry_char_t *) "hidden_string_prop");
56   }
57 
58   jerry_value_t prop_value_1 = jerry_create_number (5.5);
59   jerry_value_t prop_value_2 = jerry_create_number (6.5);
60   jerry_value_t prop_value_3 = jerry_create_number (7.5);
61 
62   jerry_value_t internal_prop_value_1 = jerry_create_number (8.5);
63   jerry_value_t internal_prop_value_2 = jerry_create_number (9.5);
64   jerry_value_t internal_prop_value_3 = jerry_create_number (10.5);
65 
66   /* Test the normal [[Set]] method */
67   bool set_result_1 = jerry_set_property (object, prop_name_1, prop_value_1);
68   bool set_result_2 = jerry_set_property (object, prop_name_2, prop_value_2);
69   bool set_result_3 = jerry_set_property (object, prop_name_3, prop_value_3);
70 
71   TEST_ASSERT (set_result_1);
72   TEST_ASSERT (set_result_2);
73   TEST_ASSERT (set_result_3);
74 
75   /* Test the internal [[Set]] method */
76   bool set_internal_result_1 = jerry_set_internal_property (object, internal_prop_name_1, internal_prop_value_1);
77   bool set_internal_result_2 = jerry_set_internal_property (object, internal_prop_name_2, internal_prop_value_2);
78   bool set_internal_result_3 = jerry_set_internal_property (object, internal_prop_name_3, internal_prop_value_3);
79 
80   TEST_ASSERT (set_internal_result_1);
81   TEST_ASSERT (set_internal_result_2);
82   TEST_ASSERT (set_internal_result_3);
83 
84   /* Test the normal [[Has]] method. */
85   jerry_value_t has_result_1 = jerry_has_property (object, prop_name_1);
86   jerry_value_t has_result_2 = jerry_has_property (object, prop_name_2);
87   jerry_value_t has_result_3 = jerry_has_property (object, prop_name_3);
88   jerry_value_t has_result_4 = jerry_has_property (object, internal_prop_name_1);
89   jerry_value_t has_result_5 = jerry_has_property (object, internal_prop_name_2);
90   jerry_value_t has_result_6 = jerry_has_property (object, internal_prop_name_3);
91 
92   TEST_ASSERT (jerry_value_is_boolean (has_result_1) && jerry_get_boolean_value (has_result_1));
93   TEST_ASSERT (jerry_value_is_boolean (has_result_2) && jerry_get_boolean_value (has_result_2));
94   TEST_ASSERT (jerry_value_is_boolean (has_result_3) && jerry_get_boolean_value (has_result_3));
95   TEST_ASSERT (jerry_value_is_boolean (has_result_4) && !jerry_get_boolean_value (has_result_4));
96   TEST_ASSERT (jerry_value_is_boolean (has_result_5) && !jerry_get_boolean_value (has_result_5));
97   TEST_ASSERT (jerry_value_is_boolean (has_result_6) && !jerry_get_boolean_value (has_result_6));
98 
99   jerry_release_value (has_result_1);
100   jerry_release_value (has_result_2);
101   jerry_release_value (has_result_3);
102   jerry_release_value (has_result_4);
103   jerry_release_value (has_result_5);
104   jerry_release_value (has_result_6);
105 
106   /* Test the internal [[Has]] method. */
107   bool has_internal_result_1 = jerry_has_internal_property (object, prop_name_1);
108   bool has_internal_result_2 = jerry_has_internal_property (object, prop_name_2);
109   bool has_internal_result_3 = jerry_has_internal_property (object, prop_name_3);
110   bool has_internal_result_4 = jerry_has_internal_property (object, internal_prop_name_1);
111   bool has_internal_result_5 = jerry_has_internal_property (object, internal_prop_name_2);
112   bool has_internal_result_6 = jerry_has_internal_property (object, internal_prop_name_3);
113 
114   TEST_ASSERT (!has_internal_result_1);
115   TEST_ASSERT (!has_internal_result_2);
116   TEST_ASSERT (!has_internal_result_3);
117   TEST_ASSERT (has_internal_result_4);
118   TEST_ASSERT (has_internal_result_5);
119   TEST_ASSERT (has_internal_result_6);
120 
121   /* Test the normal [[Get]] method. */
122   jerry_value_t get_result_1 = jerry_get_property (object, prop_name_1);
123   jerry_value_t get_result_2 = jerry_get_property (object, prop_name_2);
124   jerry_value_t get_result_3 = jerry_get_property (object, prop_name_3);
125   jerry_value_t get_result_4 = jerry_get_property (object, internal_prop_name_1);
126   jerry_value_t get_result_5 = jerry_get_property (object, internal_prop_name_2);
127   jerry_value_t get_result_6 = jerry_get_property (object, internal_prop_name_3);
128 
129   TEST_ASSERT (jerry_value_is_number (get_result_1) && jerry_get_number_value (get_result_1) == 5.5);
130   TEST_ASSERT (jerry_value_is_number (get_result_2) && jerry_get_number_value (get_result_2) == 6.5);
131   TEST_ASSERT (jerry_value_is_number (get_result_3) && jerry_get_number_value (get_result_3) == 7.5);
132   TEST_ASSERT (jerry_value_is_undefined (get_result_4));
133   TEST_ASSERT (jerry_value_is_undefined (get_result_5));
134   TEST_ASSERT (jerry_value_is_undefined (get_result_6));
135 
136   jerry_release_value (get_result_1);
137   jerry_release_value (get_result_2);
138   jerry_release_value (get_result_3);
139   jerry_release_value (get_result_4);
140   jerry_release_value (get_result_5);
141   jerry_release_value (get_result_6);
142 
143   /* Test the internal [[Get]] method. */
144   jerry_value_t get_internal_result_1 = jerry_get_internal_property (object, prop_name_1);
145   jerry_value_t get_internal_result_2 = jerry_get_internal_property (object, prop_name_2);
146   jerry_value_t get_internal_result_3 = jerry_get_internal_property (object, prop_name_3);
147   jerry_value_t get_internal_result_4 = jerry_get_internal_property (object, internal_prop_name_1);
148   jerry_value_t get_internal_result_5 = jerry_get_internal_property (object, internal_prop_name_2);
149   jerry_value_t get_internal_result_6 = jerry_get_internal_property (object, internal_prop_name_3);
150 
151   TEST_ASSERT (jerry_value_is_undefined (get_internal_result_1));
152   TEST_ASSERT (jerry_value_is_undefined (get_internal_result_2));
153   TEST_ASSERT (jerry_value_is_undefined (get_internal_result_3));
154   TEST_ASSERT (jerry_value_is_number (get_internal_result_4) && jerry_get_number_value (get_internal_result_4) == 8.5);
155   TEST_ASSERT (jerry_value_is_number (get_internal_result_5) && jerry_get_number_value (get_internal_result_5) == 9.5);
156   TEST_ASSERT (jerry_value_is_number (get_internal_result_6) && jerry_get_number_value (get_internal_result_6) == 10.5);
157 
158   jerry_release_value (get_internal_result_1);
159   jerry_release_value (get_internal_result_2);
160   jerry_release_value (get_internal_result_3);
161   jerry_release_value (get_internal_result_4);
162   jerry_release_value (get_internal_result_5);
163   jerry_release_value (get_internal_result_6);
164 
165   /* Test the normal [[Delete]] method. */
166   bool delete_result_1 = jerry_delete_property (object, prop_name_1);
167   bool delete_result_2 = jerry_delete_property (object, prop_name_2);
168   bool delete_result_3 = jerry_delete_property (object, prop_name_3);
169   bool delete_result_4 = jerry_delete_property (object, internal_prop_name_1);
170   bool delete_result_5 = jerry_delete_property (object, internal_prop_name_2);
171   bool delete_result_6 = jerry_delete_property (object, internal_prop_name_3);
172 
173   TEST_ASSERT (delete_result_1);
174   TEST_ASSERT (delete_result_2);
175   TEST_ASSERT (delete_result_3);
176   TEST_ASSERT (delete_result_4);
177   TEST_ASSERT (delete_result_5);
178   TEST_ASSERT (delete_result_6);
179 
180   jerry_value_t has_after_delete_result_1 = jerry_has_property (object, prop_name_1);
181   jerry_value_t has_after_delete_result_2 = jerry_has_property (object, prop_name_2);
182   jerry_value_t has_after_delete_result_3 = jerry_has_property (object, prop_name_3);
183   bool has_after_delete_result_4 = jerry_has_internal_property (object, internal_prop_name_1);
184   bool has_after_delete_result_5 = jerry_has_internal_property (object, internal_prop_name_2);
185   bool has_after_delete_result_6 = jerry_has_internal_property (object, internal_prop_name_3);
186 
187   TEST_ASSERT (jerry_value_is_boolean (has_after_delete_result_1)
188                && !jerry_get_boolean_value (has_after_delete_result_1));
189   TEST_ASSERT (jerry_value_is_boolean (has_after_delete_result_2)
190                && !jerry_get_boolean_value (has_after_delete_result_2));
191   TEST_ASSERT (jerry_value_is_boolean (has_after_delete_result_3)
192                && !jerry_get_boolean_value (has_after_delete_result_3));
193   TEST_ASSERT (has_after_delete_result_4);
194   TEST_ASSERT (has_after_delete_result_5);
195   TEST_ASSERT (has_after_delete_result_6);
196 
197   jerry_release_value (has_after_delete_result_1);
198   jerry_release_value (has_after_delete_result_2);
199   jerry_release_value (has_after_delete_result_3);
200 
201   /* Test the internal [[Delete]] method. */
202   bool delete_internal_result_4 = jerry_delete_internal_property (object, internal_prop_name_1);
203   bool delete_internal_result_5 = jerry_delete_internal_property (object, internal_prop_name_2);
204   bool delete_internal_result_6 = jerry_delete_internal_property (object, internal_prop_name_3);
205 
206   TEST_ASSERT (delete_internal_result_4);
207   TEST_ASSERT (delete_internal_result_5);
208   TEST_ASSERT (delete_internal_result_6);
209 
210   bool has_after_internal_delete_result_1 = jerry_has_internal_property (object, internal_prop_name_1);
211   bool has_after_internal_delete_result_2 = jerry_has_internal_property (object, internal_prop_name_2);
212   bool has_after_internal_delete_result_3 = jerry_has_internal_property (object, internal_prop_name_3);
213 
214   TEST_ASSERT (!has_after_internal_delete_result_1);
215   TEST_ASSERT (!has_after_internal_delete_result_2);
216   TEST_ASSERT (!has_after_internal_delete_result_3);
217 
218   /* Cleanup */
219   jerry_release_value (prop_value_3);
220   jerry_release_value (prop_value_2);
221   jerry_release_value (prop_value_1);
222 
223   jerry_release_value (prop_name_3);
224   jerry_release_value (prop_name_2);
225   jerry_release_value (prop_name_1);
226 
227   jerry_release_value (internal_prop_value_3);
228   jerry_release_value (internal_prop_value_2);
229   jerry_release_value (internal_prop_value_1);
230 
231   jerry_release_value (internal_prop_name_3);
232   jerry_release_value (internal_prop_name_2);
233   jerry_release_value (internal_prop_name_1);
234 
235   jerry_release_value (object);
236 
237   jerry_cleanup ();
238 
239   return 0;
240 } /* main */
241