Browse Source

Merge branch 'json-stringify-fastpath-proxy'

pull/198/merge
Sami Vaarala 9 years ago
parent
commit
ba2c5bc532
  1. 8
      src/duk_bi_json.c
  2. 41
      tests/ecmascript/test-bi-json-enc-fastpath.js

8
src/duk_bi_json.c

@ -2145,9 +2145,13 @@ DUK_LOCAL duk_bool_t duk__json_stringify_fast_value(duk_json_enc_ctx *js_ctx, du
/* If object has a .toJSON() property, we can't be certain /* If object has a .toJSON() property, we can't be certain
* that it wouldn't mutate any value arbitrarily, so bail * that it wouldn't mutate any value arbitrarily, so bail
* out of the fast path. * out of the fast path.
*
* If an object is a Proxy we also can't avoid side effects
* so abandon.
*/ */
if (duk_hobject_hasprop_raw(js_ctx->thr, obj, DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr))) { if (duk_hobject_hasprop_raw(js_ctx->thr, obj, DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr)) ||
DUK_DD(DUK_DDPRINT("object has a .toJSON property, abort fast path")); DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj)) {
DUK_DD(DUK_DDPRINT("object has a .toJSON property or object is a Proxy, abort fast path"));
goto abort_fastpath; goto abort_fastpath;
} }

41
tests/ecmascript/test-bi-json-enc-fastpath.js

@ -264,3 +264,44 @@ try {
} catch (e) { } catch (e) {
print(e.stack || e); print(e.stack || e);
} }
/*===
proxy test
["begin",{"foo":123},{"foo":345},{"foo":456},{"foo":567},"end"]
===*/
/* A Proxy object causes side effects and should cause abandonment of the
* fast path.
*/
function jsonStringifyFastPathProxyTest() {
var myValue;
var target = { foo: 'bar' };
// side effects chosen so that a restart will generate the same
// result value sequence
var p1 = new Proxy(target, {
get: function() { myValue = 234; return 123; }
});
var p2 = new Proxy(target, {
get: function() { var ret = myValue; myValue = 345; return ret; }
});
var p3 = new Proxy(target, {
get: function() { var ret = myValue; myValue = 456; return ret; }
});
var p4 = new Proxy(target, {
get: function() { var ret = myValue; myValue = 567; return ret; }
});
var obj = [ 'begin', p1, p2, p3, p4, 'end' ];
myValue = 100;
print(JSON.stringify(obj));
}
try {
print('proxy test');
jsonStringifyFastPathProxyTest();
} catch (e) {
print(e.name);
}

Loading…
Cancel
Save