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 "config.h"
17 #include "jerryscript.h"
18
19 #include "test-common.h"
20
21 /** Test in Proxy on C side. Equivalent test code in JS:
22
23 var demo = 0.0;
24
25 var target = {};
26 var handler = {
27 get: function (target, name, recv) {
28 assert (typeof (target) === 'object');
29 assert (name === 'value');
30 assert (typeof (recv) === 'object');
31 return demo++;
32 }
33
34 set: function (target, name, value, recv) {
35 assert (typeof (target) === 'object');
36 assert (name === 'value');
37 assert (typeof (recv) === 'object');
38 demo = 55;
39 return demo;
40 }
41 };
42
43 var pdemo = new Proxy(target, handler);
44
45 assert (pdemo.value === 1.0);
46 assert (pdemo.value === 1.0);
47 assert (pdemo.value === 2.0);
48
49 pdemo.value = 55;
50
51 assert (pdemo.value === 56);
52
53 pdemo.value = 12;
54
55 assert (pdemo.value === 13);
56 */
57
58 static int demo_value = 0;
59
60 static jerry_value_t
handler_get(const jerry_value_t function_obj,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_count)61 handler_get (const jerry_value_t function_obj, /**< function object */
62 const jerry_value_t this_val, /**< this arg */
63 const jerry_value_t args_p[], /**< function arguments */
64 const jerry_length_t args_count) /**< number of function arguments */
65 {
66 JERRY_UNUSED (function_obj);
67 JERRY_UNUSED (this_val);
68
69 TEST_ASSERT (args_count == 3);
70 TEST_ASSERT (jerry_value_is_object (args_p[0])); /* target */
71 TEST_ASSERT (jerry_value_is_string (args_p[1])); /* P */
72 TEST_ASSERT (jerry_value_is_object (args_p[2])); /* receiver */
73
74 const char expected[] = "value";
75 char buffer[10];
76 jerry_size_t copied = jerry_string_to_char_buffer (args_p[1], (jerry_char_t *) buffer, 10);
77
78 TEST_ASSERT (copied == 5);
79 TEST_ASSERT (strncmp (expected, buffer, 5) == 0);
80
81 demo_value++;
82
83 return jerry_create_number (demo_value);
84 } /* handler_get */
85
86 static jerry_value_t
handler_set(const jerry_value_t function_obj,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_count)87 handler_set (const jerry_value_t function_obj, /**< function object */
88 const jerry_value_t this_val, /**< this arg */
89 const jerry_value_t args_p[], /**< function arguments */
90 const jerry_length_t args_count) /**< number of function arguments */
91 {
92 JERRY_UNUSED (function_obj);
93 JERRY_UNUSED (this_val);
94 JERRY_UNUSED (args_p);
95 JERRY_UNUSED (args_count);
96
97 TEST_ASSERT (args_count == 4);
98 TEST_ASSERT (jerry_value_is_object (args_p[0])); /* target */
99 TEST_ASSERT (jerry_value_is_string (args_p[1])); /* P */
100 TEST_ASSERT (jerry_value_is_number (args_p[2])); /* V */
101 TEST_ASSERT (jerry_value_is_object (args_p[3])); /* receiver */
102
103 const char expected[] = "value";
104 char buffer[10];
105 jerry_size_t copied = jerry_string_to_char_buffer (args_p[1], (jerry_char_t *) buffer, 10);
106
107 TEST_ASSERT (copied == 5);
108 TEST_ASSERT (strncmp (expected, buffer, 5) == 0);
109
110 TEST_ASSERT (jerry_value_is_number (args_p[2]));
111 demo_value = (int) jerry_get_number_value (args_p[2]);
112
113 return jerry_create_number (demo_value);
114 } /* handler_set */
115
116 static void
set_property(jerry_value_t target,const char * name_p,jerry_value_t value)117 set_property (jerry_value_t target, /**< target object */
118 const char *name_p, /**< name of the property */
119 jerry_value_t value) /**< value of the property */
120 {
121 jerry_value_t name_val = jerry_create_string ((const jerry_char_t *) name_p);
122 jerry_value_t result_val = jerry_set_property (target, name_val, value);
123
124 TEST_ASSERT (jerry_value_is_boolean (result_val));
125 TEST_ASSERT (jerry_get_boolean_value (result_val));
126 jerry_release_value (name_val);
127 } /* set_property */
128
129 static jerry_value_t
get_property(jerry_value_t target,const char * name_p)130 get_property (jerry_value_t target, /**< target object */
131 const char *name_p) /**< name of the property */
132 {
133 jerry_value_t name_val = jerry_create_string ((const jerry_char_t *) name_p);
134 jerry_value_t result_val = jerry_get_property (target, name_val);
135
136 TEST_ASSERT (!jerry_value_is_error (result_val));
137 jerry_release_value (name_val);
138 return result_val;
139 } /* get_property */
140
141 static void
set_function(jerry_value_t target,const char * name_p,jerry_external_handler_t handler_p)142 set_function (jerry_value_t target, /**< target object */
143 const char *name_p, /**< name of the function */
144 jerry_external_handler_t handler_p) /**< function callback */
145 {
146 jerry_value_t function_val = jerry_create_external_function (handler_p);
147 set_property (target, name_p, function_val);
148 jerry_release_value (function_val);
149 } /* set_function */
150
151 int
main(void)152 main (void)
153 {
154 TEST_INIT ();
155
156 if (!jerry_is_feature_enabled (JERRY_FEATURE_PROXY))
157 {
158 printf ("Skipping test, Proxy not enabled\n");
159 return 0;
160 }
161
162 jerry_init (JERRY_INIT_EMPTY);
163
164 jerry_value_t handler = jerry_create_object ();
165 {
166 set_function (handler, "get", handler_get);
167 set_function (handler, "set", handler_set);
168 }
169
170 jerry_value_t target = jerry_create_object ();
171 jerry_value_t proxy = jerry_create_proxy (target, handler);
172 {
173 jerry_value_t global = jerry_get_global_object ();
174 set_property (global, "pdemo", proxy);
175 jerry_release_value (global);
176 }
177
178 const jerry_char_t get_value_src[] = TEST_STRING_LITERAL ("pdemo.value");
179 jerry_value_t parsed_get_code_val = jerry_parse (NULL,
180 0,
181 get_value_src,
182 sizeof (get_value_src) - 1,
183 JERRY_PARSE_NO_OPTS);
184 TEST_ASSERT (!jerry_value_is_error (parsed_get_code_val));
185
186 {
187 jerry_value_t res = jerry_run (parsed_get_code_val);
188 TEST_ASSERT (jerry_value_is_number (res));
189 TEST_ASSERT (jerry_get_number_value (res) == 1.0);
190 jerry_release_value (res);
191 }
192
193 {
194 jerry_value_t res = get_property (proxy, "value");
195 TEST_ASSERT (jerry_value_is_number (res));
196 TEST_ASSERT (jerry_get_number_value (res) == 2.0);
197 jerry_release_value (res);
198 }
199
200 {
201 jerry_value_t res = jerry_run (parsed_get_code_val);
202 TEST_ASSERT (jerry_value_is_number (res));
203 TEST_ASSERT (jerry_get_number_value (res) == 3.0);
204 jerry_release_value (res);
205 }
206
207 const jerry_char_t set_value_src[] = TEST_STRING_LITERAL ("pdemo.value = 55");
208 jerry_value_t parsed_set_code_val = jerry_parse (NULL,
209 0,
210 set_value_src,
211 sizeof (set_value_src) - 1,
212 JERRY_PARSE_NO_OPTS);
213 TEST_ASSERT (!jerry_value_is_error (parsed_set_code_val));
214
215 {
216 jerry_value_t res = jerry_run (parsed_set_code_val);
217 TEST_ASSERT (jerry_value_is_number (res));
218 TEST_ASSERT (jerry_get_number_value (res) == 55);
219 jerry_release_value (res);
220 }
221
222 {
223 jerry_value_t res = jerry_run (parsed_get_code_val);
224 TEST_ASSERT (jerry_value_is_number (res));
225 TEST_ASSERT (jerry_get_number_value (res) == 56);
226 jerry_release_value (res);
227 }
228
229 {
230 jerry_value_t new_value = jerry_create_number (12);
231 set_property (proxy, "value", new_value);
232 jerry_release_value (new_value);
233 }
234
235 {
236 jerry_value_t res = get_property (proxy, "value");
237 TEST_ASSERT (jerry_value_is_number (res));
238 TEST_ASSERT (jerry_get_number_value (res) == 13.0);
239 jerry_release_value (res);
240 }
241
242 jerry_release_value (parsed_set_code_val);
243 jerry_release_value (parsed_get_code_val);
244 jerry_release_value (proxy);
245 jerry_release_value (target);
246 jerry_release_value (handler);
247
248 jerry_cleanup ();
249 return 0;
250 } /* main */
251