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 /* foo string */
22 #define STRING_FOO ((const jerry_char_t *) "foo")
23
24 /* bar string */
25 #define STRING_BAR ((const jerry_char_t *) "bar")
26
27 /* Symbol(bar) desciptive string */
28 #define SYMBOL_DESCIPTIVE_STRING_BAR "Symbol(bar)"
29
30 class SymbolTest : public testing::Test{
31 public:
SetUpTestCase()32 static void SetUpTestCase()
33 {
34 GTEST_LOG_(INFO) << "SymbolTest SetUpTestCase";
35 }
36
TearDownTestCase()37 static void TearDownTestCase()
38 {
39 GTEST_LOG_(INFO) << "SymbolTest TearDownTestCase";
40 }
41
SetUp()42 void SetUp() override {}
TearDown()43 void TearDown() override {}
44
45 };
46
47 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)48 static void* context_alloc_fn(size_t size, void* cb_data)
49 {
50 (void)cb_data;
51 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
52 return malloc(newSize);
53 }
54
55 HWTEST_F(SymbolTest, Test001, testing::ext::TestSize.Level1)
56 {
57 if (!jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
58 {
59 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Symbol support is disabled!\n");
60 }
61 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
62 jerry_port_default_set_current_context (ctx_p);
63 jerry_init (JERRY_INIT_EMPTY);
64
65 jerry_value_t object = jerry_create_object ();
66
67 /* Test for that each symbol is unique independently from their descriptor strings */
68 jerry_value_t symbol_desc_1 = jerry_create_string (STRING_FOO);
69 jerry_value_t symbol_desc_2 = jerry_create_string (STRING_FOO);
70
71 jerry_value_t symbol_1 = jerry_create_symbol (symbol_desc_1);
72 TEST_ASSERT (!jerry_value_is_error (symbol_1));
73 TEST_ASSERT (jerry_value_is_symbol (symbol_1));
74
75 jerry_value_t symbol_2 = jerry_create_symbol (symbol_desc_2);
76 TEST_ASSERT (!jerry_value_is_error (symbol_2));
77 TEST_ASSERT (jerry_value_is_symbol (symbol_2));
78
79 /* The descriptor strings are no longer needed */
80 jerry_release_value (symbol_desc_1);
81 jerry_release_value (symbol_desc_2);
82
83 jerry_value_t value_1 = jerry_create_number (1);
84 jerry_value_t value_2 = jerry_create_number (2);
85
86 jerry_value_t result_val = jerry_set_property (object, symbol_1, value_1);
87 TEST_ASSERT (jerry_value_is_boolean (result_val));
88 TEST_ASSERT (jerry_get_boolean_value (jerry_has_property (object, symbol_1)));
89 TEST_ASSERT (jerry_get_boolean_value (jerry_has_own_property (object, symbol_1)));
90
91 result_val = jerry_set_property (object, symbol_2, value_2);
92 TEST_ASSERT (jerry_value_is_boolean (result_val));
93 TEST_ASSERT (jerry_get_boolean_value (jerry_has_property (object, symbol_2)));
94 TEST_ASSERT (jerry_get_boolean_value (jerry_has_own_property (object, symbol_2)));
95
96 jerry_value_t get_value_1 = jerry_get_property (object, symbol_1);
97 TEST_ASSERT (jerry_get_number_value (get_value_1) == jerry_get_number_value (value_1));
98 jerry_release_value (get_value_1);
99
100 jerry_value_t get_value_2 = jerry_get_property (object, symbol_2);
101 TEST_ASSERT (jerry_get_number_value (get_value_2) == jerry_get_number_value (value_2));
102 jerry_release_value (get_value_2);
103
104 /* Test delete / has_{own}_property */
105 TEST_ASSERT (jerry_delete_property (object, symbol_1));
106 TEST_ASSERT (!jerry_get_boolean_value (jerry_has_property (object, symbol_1)));
107 TEST_ASSERT (!jerry_get_boolean_value (jerry_has_own_property (object, symbol_1)));
108
109 jerry_release_value (value_1);
110 jerry_release_value (symbol_1);
111
112 /* Test {get, define}_own_property_descriptor */
113 jerry_property_descriptor_t prop_desc;
114 TEST_ASSERT (jerry_get_own_property_descriptor (object, symbol_2, &prop_desc));
115 TEST_ASSERT (prop_desc.is_value_defined == true);
116 TEST_ASSERT (value_2 == prop_desc.value);
117 TEST_ASSERT (jerry_get_number_value (value_2) == jerry_get_number_value (prop_desc.value));
118 TEST_ASSERT (prop_desc.is_writable == true);
119 TEST_ASSERT (prop_desc.is_enumerable == true);
120 TEST_ASSERT (prop_desc.is_configurable == true);
121 TEST_ASSERT (prop_desc.is_get_defined == false);
122 TEST_ASSERT (jerry_value_is_undefined (prop_desc.getter));
123 TEST_ASSERT (prop_desc.is_set_defined == false);
124 TEST_ASSERT (jerry_value_is_undefined (prop_desc.setter));
125 jerry_free_property_descriptor_fields (&prop_desc);
126
127 /* Modify the descriptor fields */
128 jerry_init_property_descriptor_fields (&prop_desc);
129 jerry_value_t value_3 = jerry_create_string (STRING_BAR);
130
131 prop_desc.is_value_defined = true;
132 prop_desc.value = jerry_acquire_value (value_3);
133 prop_desc.is_writable_defined = true;
134 prop_desc.is_writable = false;
135 prop_desc.is_enumerable_defined = true;
136 prop_desc.is_enumerable = false;
137 prop_desc.is_configurable_defined = true;
138 prop_desc.is_configurable = false;
139 TEST_ASSERT (jerry_get_boolean_value (jerry_define_own_property (object, symbol_2, &prop_desc)));
140 jerry_free_property_descriptor_fields (&prop_desc);
141
142 /* Check the modified fields */
143 TEST_ASSERT (jerry_get_own_property_descriptor (object, symbol_2, &prop_desc));
144 TEST_ASSERT (prop_desc.is_value_defined == true);
145 TEST_ASSERT (value_3 == prop_desc.value);
146 TEST_ASSERT (jerry_value_is_string (prop_desc.value));
147 TEST_ASSERT (prop_desc.is_writable_defined == true);
148 TEST_ASSERT (prop_desc.is_writable == false);
149 TEST_ASSERT (prop_desc.is_enumerable_defined == true);
150 TEST_ASSERT (prop_desc.is_enumerable == false);
151 TEST_ASSERT (prop_desc.is_configurable_defined == true);
152 TEST_ASSERT (prop_desc.is_configurable == false);
153 TEST_ASSERT (prop_desc.is_get_defined == false);
154 TEST_ASSERT (jerry_value_is_undefined (prop_desc.getter));
155 TEST_ASSERT (prop_desc.is_set_defined == false);
156 TEST_ASSERT (jerry_value_is_undefined (prop_desc.setter));
157 jerry_free_property_descriptor_fields (&prop_desc);
158
159 jerry_release_value (value_3);
160 jerry_release_value (value_2);
161 jerry_release_value (symbol_2);
162 jerry_release_value (object);
163
164 /* Test creating symbol with a symbol description */
165 jerry_value_t empty_symbol_desc = jerry_create_string ((const jerry_char_t *) "");
166
167 jerry_value_t empty_symbol = jerry_create_symbol (empty_symbol_desc);
168 TEST_ASSERT (!jerry_value_is_error (empty_symbol));
169 TEST_ASSERT (jerry_value_is_symbol (empty_symbol));
170
171 jerry_release_value (empty_symbol_desc);
172
173 jerry_value_t symbol_symbol = jerry_create_symbol (empty_symbol);
174 TEST_ASSERT (!jerry_value_is_symbol (symbol_symbol));
175 TEST_ASSERT (jerry_value_is_error (symbol_symbol));
176
177 jerry_value_t error_obj = jerry_get_value_from_error (symbol_symbol, true);
178
179 TEST_ASSERT (jerry_get_error_type (error_obj) == JERRY_ERROR_TYPE);
180
181 jerry_release_value (error_obj);
182 jerry_release_value (empty_symbol);
183
184 /* Test symbol to string operation with symbol argument */
185 jerry_value_t bar_symbol_desc = jerry_create_string (STRING_BAR);
186
187 jerry_value_t bar_symbol = jerry_create_symbol (bar_symbol_desc);
188 TEST_ASSERT (!jerry_value_is_error (bar_symbol));
189 TEST_ASSERT (jerry_value_is_symbol (bar_symbol));
190
191 jerry_release_value (bar_symbol_desc);
192
193 jerry_value_t bar_symbol_string = jerry_get_symbol_descriptive_string (bar_symbol);
194 TEST_ASSERT (jerry_value_is_string (bar_symbol_string));
195
196 jerry_size_t bar_symbol_string_size = jerry_get_string_size (bar_symbol_string);
197 TEST_ASSERT (bar_symbol_string_size == (sizeof (SYMBOL_DESCIPTIVE_STRING_BAR) - 1));
198 JERRY_VLA (jerry_char_t, str_buff, bar_symbol_string_size);
199
200 jerry_string_to_char_buffer (bar_symbol_string, str_buff, bar_symbol_string_size);
201 TEST_ASSERT (memcmp (str_buff, SYMBOL_DESCIPTIVE_STRING_BAR, sizeof (SYMBOL_DESCIPTIVE_STRING_BAR) - 1) == 0);
202
203 jerry_release_value (bar_symbol_string);
204 jerry_release_value (bar_symbol);
205
206 /* Test symbol to string operation with non-symbol argument */
207 jerry_value_t null_value = jerry_create_null ();
208 jerry_value_t to_string_value = jerry_get_symbol_descriptive_string (null_value);
209 TEST_ASSERT (jerry_value_is_error (to_string_value));
210
211 error_obj = jerry_get_value_from_error (to_string_value, true);
212
213 TEST_ASSERT (jerry_get_error_type (error_obj) == JERRY_ERROR_TYPE);
214
215 jerry_release_value (error_obj);
216 jerry_release_value (null_value);
217
218 jerry_cleanup ();
219 free(ctx_p);
220 }
221