Skip to content

Commit 9c7fa59

Browse files
committed
Add Proxy support for JSON.stringify
JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
1 parent 4be9ffd commit 9c7fa59

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-json.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,13 +1260,33 @@ ecma_builtin_json_serialize_property (ecma_json_stringify_context_t *context_p,
12601260
/* 11. */
12611261
if (ecma_is_value_object (value) && !ecma_op_is_callable (value))
12621262
{
1263+
ecma_value_t is_array = ecma_is_value_array (value);
1264+
1265+
#if ENABLED (JERRY_ES2015)
1266+
if (ECMA_IS_VALUE_ERROR (is_array))
1267+
{
1268+
return is_array;
1269+
}
1270+
#endif /* ENABLED (JERRY_ES2015) */
1271+
12631272
ecma_object_t *obj_p = ecma_get_object_from_value (value);
12641273

12651274
ecma_value_t ret_value;
1275+
12661276
/* 10.a */
1267-
if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_ARRAY)
1277+
if (ecma_is_value_true (is_array))
12681278
{
1269-
ret_value = ecma_builtin_json_serialize_array (context_p, obj_p);
1279+
ecma_object_t *array_obj_p = obj_p;
1280+
1281+
#if ENABLED (JERRY_ES2015)
1282+
if (ECMA_OBJECT_IS_PROXY (obj_p))
1283+
{
1284+
ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) obj_p;
1285+
array_obj_p = ecma_get_object_from_value (proxy_obj_p->target);
1286+
}
1287+
#endif /* ENABLED (JERRY_ES2015) */
1288+
1289+
ret_value = ecma_builtin_json_serialize_array (context_p, array_obj_p);
12701290
}
12711291
/* 10.b */
12721292
else

tests/jerry/json-stringify.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,7 @@ assert (JSON.stringify (object, null, undefined) == '{"a":2}');
212212
assert (JSON.stringify (object, null, new Boolean (true)) == '{"a":2}');
213213
assert (JSON.stringify (object, null, [1, 2, 3] ) == '{"a":2}');
214214
assert (JSON.stringify (object, null, { "a": 3 }) == '{"a":2}');
215+
216+
// Test with proxy
217+
assert(JSON.stringify(new Proxy(['foo'], {})) === '["foo"]');
218+
assert(JSON.stringify(new Proxy({0:"foo"}, {})) === '{"0":"foo"}');

0 commit comments

Comments
 (0)