[proxies] Support proxies in JSON.parse and JSON.stringify.
This CL tries to correctly support the following: - stringifying a proxy, - stringifying with a proxy as replacer (callable or arraylike), - stringifying with a replacer that returns a proxy, - parsing with a callable proxy as reviver, - parsing with a reviver that inserts proxies into the object, - and whatever else you can imagine. This also fixes some bugs observable without proxies. BUG=v8:3139,v8:1543 LOG=n Review URL: https://codereview.chromium.org/1515133002 Cr-Commit-Position: refs/heads/master@{#32843}
This commit is contained in:
parent
973bc260c5
commit
1596b015ff
@ -364,17 +364,41 @@ void Bootstrapper::DetachGlobal(Handle<Context> env) {
|
||||
|
||||
namespace {
|
||||
|
||||
Handle<JSFunction> InstallFunction(Handle<JSObject> target,
|
||||
Handle<Name> property_name,
|
||||
Handle<JSFunction> function,
|
||||
Handle<String> function_name,
|
||||
PropertyAttributes attributes = DONT_ENUM) {
|
||||
void InstallFunction(Handle<JSObject> target, Handle<Name> property_name,
|
||||
Handle<JSFunction> function, Handle<String> function_name,
|
||||
PropertyAttributes attributes = DONT_ENUM) {
|
||||
JSObject::AddProperty(target, property_name, function, attributes);
|
||||
if (target->IsJSGlobalObject()) {
|
||||
function->shared()->set_instance_class_name(*function_name);
|
||||
}
|
||||
function->shared()->set_native(true);
|
||||
return function;
|
||||
}
|
||||
|
||||
|
||||
static void InstallFunction(Handle<JSObject> target,
|
||||
Handle<JSFunction> function, Handle<Name> name,
|
||||
PropertyAttributes attributes = DONT_ENUM) {
|
||||
Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked();
|
||||
InstallFunction(target, name, function, name_string, attributes);
|
||||
}
|
||||
|
||||
|
||||
static Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name,
|
||||
InstanceType type, int instance_size,
|
||||
MaybeHandle<JSObject> maybe_prototype,
|
||||
Builtins::Name call,
|
||||
bool strict_function_map = false) {
|
||||
Factory* factory = isolate->factory();
|
||||
Handle<Code> call_code(isolate->builtins()->builtin(call));
|
||||
Handle<JSObject> prototype;
|
||||
static const bool kReadOnlyPrototype = false;
|
||||
static const bool kInstallConstructor = false;
|
||||
return maybe_prototype.ToHandle(&prototype)
|
||||
? factory->NewFunction(name, call_code, prototype, type,
|
||||
instance_size, kReadOnlyPrototype,
|
||||
kInstallConstructor, strict_function_map)
|
||||
: factory->NewFunctionWithoutPrototype(name, call_code,
|
||||
strict_function_map);
|
||||
}
|
||||
|
||||
|
||||
@ -384,21 +408,12 @@ Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name,
|
||||
Builtins::Name call,
|
||||
PropertyAttributes attributes,
|
||||
bool strict_function_map = false) {
|
||||
Isolate* isolate = target->GetIsolate();
|
||||
Factory* factory = isolate->factory();
|
||||
Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked();
|
||||
Handle<Code> call_code(isolate->builtins()->builtin(call));
|
||||
Handle<JSObject> prototype;
|
||||
static const bool kReadOnlyPrototype = false;
|
||||
static const bool kInstallConstructor = false;
|
||||
Handle<JSFunction> function =
|
||||
maybe_prototype.ToHandle(&prototype)
|
||||
? factory->NewFunction(name_string, call_code, prototype, type,
|
||||
instance_size, kReadOnlyPrototype,
|
||||
kInstallConstructor, strict_function_map)
|
||||
: factory->NewFunctionWithoutPrototype(name_string, call_code,
|
||||
strict_function_map);
|
||||
return InstallFunction(target, name, function, name_string, attributes);
|
||||
CreateFunction(target->GetIsolate(), name_string, type, instance_size,
|
||||
maybe_prototype, call, strict_function_map);
|
||||
InstallFunction(target, name, function, name_string, attributes);
|
||||
return function;
|
||||
}
|
||||
|
||||
|
||||
@ -1028,17 +1043,31 @@ void Genesis::HookUpGlobalObject(Handle<JSGlobalObject> global_object) {
|
||||
}
|
||||
|
||||
|
||||
static void SimpleInstallFunction(Handle<JSObject> base, Handle<Name> name,
|
||||
Builtins::Name call, int len, bool adapt) {
|
||||
static Handle<JSFunction> SimpleCreateFunction(Isolate* isolate,
|
||||
Handle<String> name,
|
||||
Builtins::Name call, int len,
|
||||
bool adapt) {
|
||||
Handle<JSFunction> fun =
|
||||
InstallFunction(base, name, JS_OBJECT_TYPE, JSObject::kHeaderSize,
|
||||
MaybeHandle<JSObject>(), call, DONT_ENUM);
|
||||
CreateFunction(isolate, name, JS_OBJECT_TYPE, JSObject::kHeaderSize,
|
||||
MaybeHandle<JSObject>(), call);
|
||||
if (adapt) {
|
||||
fun->shared()->set_internal_formal_parameter_count(len);
|
||||
} else {
|
||||
fun->shared()->DontAdaptArguments();
|
||||
}
|
||||
fun->shared()->set_length(len);
|
||||
return fun;
|
||||
}
|
||||
|
||||
|
||||
static Handle<JSFunction> SimpleInstallFunction(Handle<JSObject> base,
|
||||
Handle<String> name,
|
||||
Builtins::Name call, int len,
|
||||
bool adapt) {
|
||||
Handle<JSFunction> fun =
|
||||
SimpleCreateFunction(base->GetIsolate(), name, call, len, adapt);
|
||||
InstallFunction(base, fun, name, DONT_ENUM);
|
||||
return fun;
|
||||
}
|
||||
|
||||
|
||||
@ -1146,9 +1175,10 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
|
||||
initial_strong_map->set_is_strong();
|
||||
CacheInitialJSArrayMaps(native_context(), initial_strong_map);
|
||||
|
||||
SimpleInstallFunction(array_function,
|
||||
isolate->factory()->InternalizeUtf8String("isArray"),
|
||||
Builtins::kArrayIsArray, 1, true);
|
||||
Handle<JSFunction> is_arraylike = SimpleInstallFunction(
|
||||
array_function, isolate->factory()->InternalizeUtf8String("isArray"),
|
||||
Builtins::kArrayIsArray, 1, true);
|
||||
native_context()->set_is_arraylike(*is_arraylike);
|
||||
}
|
||||
|
||||
{ // --- N u m b e r ---
|
||||
@ -2157,9 +2187,23 @@ void Genesis::InitializeGlobal_harmony_regexp_subclass() {
|
||||
|
||||
|
||||
void Genesis::InitializeGlobal_harmony_reflect() {
|
||||
Factory* factory = isolate()->factory();
|
||||
|
||||
// We currently use some of the Reflect functions internally, even when
|
||||
// the --harmony-reflect flag is not given.
|
||||
|
||||
Handle<JSFunction> define_property =
|
||||
SimpleCreateFunction(isolate(), factory->defineProperty_string(),
|
||||
Builtins::kReflectDefineProperty, 3, true);
|
||||
native_context()->set_reflect_define_property(*define_property);
|
||||
|
||||
Handle<JSFunction> delete_property =
|
||||
SimpleCreateFunction(isolate(), factory->deleteProperty_string(),
|
||||
Builtins::kReflectDeleteProperty, 2, true);
|
||||
native_context()->set_reflect_delete_property(*delete_property);
|
||||
|
||||
if (!FLAG_harmony_reflect) return;
|
||||
|
||||
Factory* factory = isolate()->factory();
|
||||
Handle<JSGlobalObject> global(JSGlobalObject::cast(
|
||||
native_context()->global_object()));
|
||||
Handle<String> reflect_string = factory->NewStringFromStaticChars("Reflect");
|
||||
@ -2167,10 +2211,9 @@ void Genesis::InitializeGlobal_harmony_reflect() {
|
||||
factory->NewJSObject(isolate()->object_function(), TENURED);
|
||||
JSObject::AddProperty(global, reflect_string, reflect, DONT_ENUM);
|
||||
|
||||
SimpleInstallFunction(reflect, factory->defineProperty_string(),
|
||||
Builtins::kReflectDefineProperty, 3, true);
|
||||
SimpleInstallFunction(reflect, factory->deleteProperty_string(),
|
||||
Builtins::kReflectDeleteProperty, 2, true);
|
||||
InstallFunction(reflect, define_property, factory->defineProperty_string());
|
||||
InstallFunction(reflect, delete_property, factory->deleteProperty_string());
|
||||
|
||||
SimpleInstallFunction(reflect, factory->get_string(),
|
||||
Builtins::kReflectGet, 2, false);
|
||||
SimpleInstallFunction(reflect, factory->getOwnPropertyDescriptor_string(),
|
||||
|
@ -78,12 +78,15 @@ enum BindingFlags {
|
||||
// Factory::NewContext.
|
||||
|
||||
#define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \
|
||||
V(IS_ARRAYLIKE, JSFunction, is_arraylike) \
|
||||
V(CONCAT_ITERABLE_TO_ARRAY_INDEX, JSFunction, concat_iterable_to_array) \
|
||||
V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site) \
|
||||
V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error) \
|
||||
V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error) \
|
||||
V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply) \
|
||||
V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct) \
|
||||
V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \
|
||||
V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \
|
||||
V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments) \
|
||||
V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable)
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
// Imports
|
||||
|
||||
var GlobalJSON = global.JSON;
|
||||
var GlobalSet = global.Set;
|
||||
var InternalArray = utils.InternalArray;
|
||||
var MakeTypeError;
|
||||
var MaxSimple;
|
||||
@ -30,27 +31,33 @@ utils.Import(function(from) {
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
function CreateDataProperty(o, p, v) {
|
||||
var desc = {value: v, enumerable: true, writable: true, configurable: true};
|
||||
return %reflect_define_property(o, p, desc);
|
||||
}
|
||||
|
||||
|
||||
function InternalizeJSONProperty(holder, name, reviver) {
|
||||
var val = holder[name];
|
||||
if (IS_OBJECT(val) && val !== null) {
|
||||
if (IS_ARRAY(val)) {
|
||||
var length = val.length;
|
||||
if (IS_SPEC_OBJECT(val)) {
|
||||
if (%is_arraylike(val)) {
|
||||
var length = TO_LENGTH(val.length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
var newElement =
|
||||
InternalizeJSONProperty(val, %_NumberToString(i), reviver);
|
||||
if (IS_UNDEFINED(newElement)) {
|
||||
delete val[i];
|
||||
%reflect_delete_property(val, i);
|
||||
} else {
|
||||
val[i] = newElement;
|
||||
CreateDataProperty(val, i, newElement);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var p of ObjectKeys(val)) {
|
||||
var newElement = InternalizeJSONProperty(val, p, reviver);
|
||||
if (IS_UNDEFINED(newElement)) {
|
||||
delete val[p];
|
||||
%reflect_delete_property(val, p);
|
||||
} else {
|
||||
val[p] = newElement;
|
||||
CreateDataProperty(val, p, newElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -74,7 +81,7 @@ function SerializeArray(value, replacer, stack, indent, gap) {
|
||||
var stepback = indent;
|
||||
indent += gap;
|
||||
var partial = new InternalArray();
|
||||
var len = value.length;
|
||||
var len = TO_LENGTH(value.length);
|
||||
for (var i = 0; i < len; i++) {
|
||||
var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack,
|
||||
indent, gap);
|
||||
@ -106,27 +113,23 @@ function SerializeObject(value, replacer, stack, indent, gap) {
|
||||
if (IS_ARRAY(replacer)) {
|
||||
var length = replacer.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (HAS_OWN_PROPERTY(replacer, i)) {
|
||||
var p = replacer[i];
|
||||
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
||||
if (!IS_UNDEFINED(strP)) {
|
||||
var member = %QuoteJSONString(p) + ":";
|
||||
if (gap != "") member += " ";
|
||||
member += strP;
|
||||
partial.push(member);
|
||||
}
|
||||
var p = replacer[i];
|
||||
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
||||
if (!IS_UNDEFINED(strP)) {
|
||||
var member = %QuoteJSONString(p) + ":";
|
||||
if (gap != "") member += " ";
|
||||
member += strP;
|
||||
partial.push(member);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var p in value) {
|
||||
if (HAS_OWN_PROPERTY(value, p)) {
|
||||
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
||||
if (!IS_UNDEFINED(strP)) {
|
||||
var member = %QuoteJSONString(p) + ":";
|
||||
if (gap != "") member += " ";
|
||||
member += strP;
|
||||
partial.push(member);
|
||||
}
|
||||
for (var p of ObjectKeys(value)) {
|
||||
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
||||
if (!IS_UNDEFINED(strP)) {
|
||||
var member = %QuoteJSONString(p) + ":";
|
||||
if (gap != "") member += " ";
|
||||
member += strP;
|
||||
partial.push(member);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -166,7 +169,7 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
|
||||
return "null";
|
||||
} else if (IS_SPEC_OBJECT(value) && !IS_CALLABLE(value)) {
|
||||
// Non-callable object. If it's a primitive wrapper, it must be unwrapped.
|
||||
if (IS_ARRAY(value)) {
|
||||
if (%is_arraylike(value)) {
|
||||
return SerializeArray(value, replacer, stack, indent, gap);
|
||||
} else if (IS_NUMBER_WRAPPER(value)) {
|
||||
value = TO_NUMBER(value);
|
||||
@ -185,14 +188,13 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
|
||||
|
||||
|
||||
function JSONStringify(value, replacer, space) {
|
||||
if (%_ArgumentsLength() == 1) {
|
||||
if (%_ArgumentsLength() == 1 && !%_IsJSProxy(value)) {
|
||||
return %BasicJSONStringify(value);
|
||||
}
|
||||
if (IS_ARRAY(replacer)) {
|
||||
// Deduplicate replacer array items.
|
||||
if (!IS_CALLABLE(replacer) && %is_arraylike(replacer)) {
|
||||
var property_list = new InternalArray();
|
||||
var seen_properties = { __proto__: null };
|
||||
var length = replacer.length;
|
||||
var seen_properties = new GlobalSet();
|
||||
var length = TO_LENGTH(replacer.length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
var v = replacer[i];
|
||||
var item;
|
||||
@ -205,9 +207,9 @@ function JSONStringify(value, replacer, space) {
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if (!seen_properties[item]) {
|
||||
if (!seen_properties.has(item)) {
|
||||
property_list.push(item);
|
||||
seen_properties[item] = true;
|
||||
seen_properties.add(item);
|
||||
}
|
||||
}
|
||||
replacer = property_list;
|
||||
|
@ -25,7 +25,13 @@
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Flags: --harmony-proxies
|
||||
// Flags: --harmony-proxies --harmony-reflect
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// JSON.stringify
|
||||
|
||||
|
||||
function testStringify(expected, object) {
|
||||
// Test fast case that bails out to slow case.
|
||||
@ -34,23 +40,30 @@ function testStringify(expected, object) {
|
||||
assertEquals(expected, JSON.stringify(object, undefined, 0));
|
||||
}
|
||||
|
||||
// Test serializing a proxy, function proxy and objects that contain them.
|
||||
|
||||
// Test serializing a proxy, a function proxy, and objects that contain them.
|
||||
|
||||
var handler1 = {
|
||||
get: function(target, name) {
|
||||
return name.toUpperCase();
|
||||
},
|
||||
enumerate: function(target) {
|
||||
ownKeys: function() {
|
||||
return ['a', 'b', 'c'];
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: true };
|
||||
getOwnPropertyDescriptor: function() {
|
||||
return { enumerable: true, configurable: true };
|
||||
}
|
||||
}
|
||||
|
||||
var proxy1 = new Proxy({}, handler1);
|
||||
testStringify('{"a":"A","b":"B","c":"C"}', proxy1);
|
||||
|
||||
var proxy_fun = Proxy.createFunction(handler1, function() { return 1; });
|
||||
var proxy_fun = new Proxy(() => {}, handler1);
|
||||
assertTrue(typeof(proxy_fun) === 'function');
|
||||
testStringify(undefined, proxy_fun);
|
||||
testStringify('[1,null]', [1, proxy_fun]);
|
||||
|
||||
handler1.apply = function() { return 666; };
|
||||
testStringify(undefined, proxy_fun);
|
||||
testStringify('[1,null]', [1, proxy_fun]);
|
||||
|
||||
@ -63,17 +76,19 @@ testStringify('{"a":123,"b":{"a":"A","b":"B","c":"C"},"c":true}', parent1b);
|
||||
var parent1c = [123, proxy1, true];
|
||||
testStringify('[123,{"a":"A","b":"B","c":"C"},true]', parent1c);
|
||||
|
||||
|
||||
// Proxy with side effect.
|
||||
|
||||
var handler2 = {
|
||||
get: function(target, name) {
|
||||
delete parent2.c;
|
||||
return name.toUpperCase();
|
||||
},
|
||||
enumerate: function(target) {
|
||||
ownKeys: function() {
|
||||
return ['a', 'b', 'c'];
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: true };
|
||||
getOwnPropertyDescriptor: function() {
|
||||
return { enumerable: true, configurable: true };
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,17 +99,21 @@ assertEquals(expected2, JSON.stringify(parent2));
|
||||
parent2.c = "remove"; // Revert side effect.
|
||||
assertEquals(expected2, JSON.stringify(parent2, undefined, 0));
|
||||
|
||||
// Proxy with a get function that uses the first argument.
|
||||
|
||||
// Proxy with a get function that uses the receiver argument.
|
||||
|
||||
var handler3 = {
|
||||
get: function(target, name) {
|
||||
if (name == 'valueOf') return function() { return "proxy" };
|
||||
return name + "(" + target + ")";
|
||||
get: function(target, name, receiver) {
|
||||
if (name == 'valueOf' || name === Symbol.toPrimitive) {
|
||||
return function() { return "proxy" };
|
||||
};
|
||||
if (typeof name !== 'symbol') return name + "(" + receiver + ")";
|
||||
},
|
||||
enumerate: function(target) {
|
||||
ownKeys: function() {
|
||||
return ['a', 'b', 'c'];
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: true };
|
||||
getOwnPropertyDescriptor: function() {
|
||||
return { enumerable: true, configurable: true };
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,13 +122,18 @@ var parent3 = { x: 123, y: proxy3 }
|
||||
testStringify('{"x":123,"y":{"a":"a(proxy)","b":"b(proxy)","c":"c(proxy)"}}',
|
||||
parent3);
|
||||
|
||||
|
||||
// Empty proxy.
|
||||
|
||||
var handler4 = {
|
||||
get: function(target, name) {
|
||||
return 0;
|
||||
},
|
||||
enumerate: function(target) {
|
||||
return [];
|
||||
return [][Symbol.iterator]();
|
||||
},
|
||||
has: function() {
|
||||
return true;
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: false };
|
||||
@ -120,14 +144,19 @@ var proxy4 = new Proxy({}, handler4);
|
||||
testStringify('{}', proxy4);
|
||||
testStringify('{"a":{}}', { a: proxy4 });
|
||||
|
||||
|
||||
// Proxy that provides a toJSON function that uses this.
|
||||
|
||||
var handler5 = {
|
||||
get: function(target, name) {
|
||||
if (name == 'z') return 97000;
|
||||
return function(key) { return key.charCodeAt(0) + this.z; };
|
||||
},
|
||||
enumerate: function(target) {
|
||||
return ['toJSON', 'z'];
|
||||
return ['toJSON', 'z'][Symbol.iterator]();
|
||||
},
|
||||
has: function() {
|
||||
return true;
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: true };
|
||||
@ -137,13 +166,18 @@ var handler5 = {
|
||||
var proxy5 = new Proxy({}, handler5);
|
||||
testStringify('{"a":97097}', { a: proxy5 });
|
||||
|
||||
|
||||
// Proxy that provides a toJSON function that returns undefined.
|
||||
|
||||
var handler6 = {
|
||||
get: function(target, name) {
|
||||
return function(key) { return undefined; };
|
||||
},
|
||||
enumerate: function(target) {
|
||||
return ['toJSON'];
|
||||
return ['toJSON'][Symbol.iterator]();
|
||||
},
|
||||
has: function() {
|
||||
return true;
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: true };
|
||||
@ -154,7 +188,9 @@ var proxy6 = new Proxy({}, handler6);
|
||||
testStringify('[1,null,true]', [1, proxy6, true]);
|
||||
testStringify('{"a":1,"c":true}', {a: 1, b: proxy6, c: true});
|
||||
|
||||
|
||||
// Object containing a proxy that changes the parent's properties.
|
||||
|
||||
var handler7 = {
|
||||
get: function(target, name) {
|
||||
delete parent7.a;
|
||||
@ -162,11 +198,11 @@ var handler7 = {
|
||||
parent7.e = "5";
|
||||
return name.toUpperCase();
|
||||
},
|
||||
enumerate: function(target) {
|
||||
ownKeys: function() {
|
||||
return ['a', 'b', 'c'];
|
||||
},
|
||||
getOwnPropertyDescriptor: function(target, name) {
|
||||
return { enumerable: true };
|
||||
getOwnPropertyDescriptor: function() {
|
||||
return { enumerable: true, configurable: true };
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,3 +212,299 @@ assertEquals('{"a":"1","b":{"a":"A","b":"B","c":"C"},"d":"4"}',
|
||||
JSON.stringify(parent7));
|
||||
assertEquals('{"b":{"a":"A","b":"B","c":"C"},"d":"4","e":"5"}',
|
||||
JSON.stringify(parent7));
|
||||
|
||||
|
||||
// (Proxy handler to log trap calls)
|
||||
|
||||
var log = [];
|
||||
var logger = {};
|
||||
var handler = new Proxy({}, logger);
|
||||
|
||||
logger.get = function(t, trap, r) {
|
||||
return function() {
|
||||
log.push([trap, ...arguments]);
|
||||
return Reflect[trap](...arguments);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Object is a callable proxy
|
||||
|
||||
log.length = 0;
|
||||
var target = () => 42;
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(typeof proxy === 'function');
|
||||
|
||||
assertEquals(undefined, JSON.stringify(proxy));
|
||||
assertEquals(1, log.length)
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["get", target, "toJSON", proxy], log[0]);
|
||||
|
||||
|
||||
// Object is a non-callable non-arraylike proxy
|
||||
|
||||
log.length = 0;
|
||||
var target = {foo: 42}
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertFalse(Array.isArray(proxy));
|
||||
|
||||
assertEquals('{"foo":42}', JSON.stringify(proxy));
|
||||
assertEquals(4, log.length)
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(
|
||||
["get", target, "toJSON", proxy], log[0]);
|
||||
assertEquals(
|
||||
["ownKeys", target], log[1]); // EnumerableOwnNames
|
||||
assertEquals(
|
||||
["getOwnPropertyDescriptor", target, "foo"], log[2]); // EnumerableOwnNames
|
||||
assertEquals(
|
||||
["get", target, "foo", proxy], log[3]);
|
||||
|
||||
|
||||
// Object is an arraylike proxy
|
||||
|
||||
log.length = 0;
|
||||
var target = [42];
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(Array.isArray(proxy));
|
||||
|
||||
assertEquals('[42]', JSON.stringify(proxy));
|
||||
assertEquals(3, log.length)
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["get", target, "toJSON", proxy], log[0]);
|
||||
assertEquals(["get", target, "length", proxy], log[1]);
|
||||
assertEquals(["get", target, "0", proxy], log[2]);
|
||||
|
||||
|
||||
// Replacer is a callable proxy
|
||||
|
||||
log.length = 0;
|
||||
var object = {0: "foo", 1: 666};
|
||||
var target = (key, val) => key == "1" ? val + 42 : val;
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(typeof proxy === 'function');
|
||||
|
||||
assertEquals('{"0":"foo","1":708}', JSON.stringify(object, proxy));
|
||||
assertEquals(3, log.length)
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(4, log[0].length)
|
||||
assertEquals("apply", log[0][0]);
|
||||
assertEquals("", log[0][3][0]);
|
||||
assertEquals({0: "foo", 1: 666}, log[0][3][1]);
|
||||
assertEquals(4, log[1].length)
|
||||
assertEquals("apply", log[1][0]);
|
||||
assertEquals(["0", "foo"], log[1][3]);
|
||||
assertEquals(4, log[2].length)
|
||||
assertEquals("apply", log[2][0]);
|
||||
assertEquals(["1", 666], log[2][3]);
|
||||
|
||||
|
||||
// Replacer is an arraylike proxy
|
||||
|
||||
log.length = 0;
|
||||
var object = {0: "foo", 1: 666};
|
||||
var target = [0];
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(Array.isArray(proxy));
|
||||
|
||||
assertEquals('{"0":"foo"}', JSON.stringify(object, proxy));
|
||||
assertEquals(2, log.length)
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["get", target, "length", proxy], log[0]);
|
||||
assertEquals(["get", target, "0", proxy], log[1]);
|
||||
|
||||
|
||||
// Replacer is an arraylike proxy and object is an array
|
||||
|
||||
log.length = 0;
|
||||
var object = ["foo", 42];
|
||||
var target = [0];
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(Array.isArray(proxy));
|
||||
|
||||
assertEquals('["foo",42]', JSON.stringify(object, proxy));
|
||||
assertEquals(2, log.length);
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["get", target, "length", proxy], log[0]);
|
||||
assertEquals(["get", target, "0", proxy], log[1]);
|
||||
|
||||
|
||||
// Replacer is an arraylike proxy with a non-trivial length
|
||||
|
||||
var getTrap = function(t, key) {
|
||||
if (key === "length") return {[Symbol.toPrimitive]() {return 42}};
|
||||
if (key === "41") return "foo";
|
||||
if (key === "42") return "bar";
|
||||
};
|
||||
var target = [];
|
||||
var proxy = new Proxy(target, {get: getTrap});
|
||||
assertTrue(Array.isArray(proxy));
|
||||
var object = {foo: true, bar: 666};
|
||||
assertEquals('{"foo":true}', JSON.stringify(object, proxy));
|
||||
|
||||
|
||||
// Replacer is an arraylike proxy with a bogus length
|
||||
|
||||
var getTrap = function(t, key) {
|
||||
if (key === "length") return Symbol();
|
||||
if (key === "41") return "foo";
|
||||
if (key === "42") return "bar";
|
||||
};
|
||||
var target = [];
|
||||
var proxy = new Proxy(target, {get: getTrap});
|
||||
assertTrue(Array.isArray(proxy));
|
||||
var object = {foo: true, bar: 666};
|
||||
assertThrows(() => JSON.stringify(object, proxy), TypeError);
|
||||
|
||||
|
||||
// Replacer returns a non-callable non-arraylike proxy
|
||||
|
||||
log.length = 0;
|
||||
var object = ["foo", 42];
|
||||
var target = {baz: 5};
|
||||
var proxy = new Proxy(target, handler);
|
||||
var replacer = (key, val) => key === "1" ? proxy : val;
|
||||
|
||||
assertEquals('["foo",{"baz":5}]', JSON.stringify(object, replacer));
|
||||
assertEquals(3, log.length);
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["ownKeys", target], log[0]);
|
||||
assertEquals(["getOwnPropertyDescriptor", target, "baz"], log[1]);
|
||||
|
||||
|
||||
// Replacer returns an arraylike proxy
|
||||
|
||||
log.length = 0;
|
||||
var object = ["foo", 42];
|
||||
var target = ["bar"];
|
||||
var proxy = new Proxy(target, handler);
|
||||
var replacer = (key, val) => key === "1" ? proxy : val;
|
||||
|
||||
assertEquals('["foo",["bar"]]', JSON.stringify(object, replacer));
|
||||
assertEquals(2, log.length);
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["get", target, "length", proxy], log[0]);
|
||||
assertEquals(["get", target, "0", proxy], log[1]);
|
||||
|
||||
|
||||
// Replacer returns an arraylike proxy with a non-trivial length
|
||||
|
||||
var getTrap = function(t, key) {
|
||||
if (key === "length") return {[Symbol.toPrimitive]() {return 3}};
|
||||
if (key === "2") return "baz";
|
||||
if (key === "3") return "bar";
|
||||
};
|
||||
var target = [];
|
||||
var proxy = new Proxy(target, {get: getTrap});
|
||||
var replacer = (key, val) => key === "goo" ? proxy : val;
|
||||
var object = {foo: true, goo: false};
|
||||
assertEquals('{"foo":true,"goo":[null,null,"baz"]}',
|
||||
JSON.stringify(object, replacer));
|
||||
|
||||
|
||||
// Replacer returns an arraylike proxy with a bogus length
|
||||
|
||||
var getTrap = function(t, key) {
|
||||
if (key === "length") return Symbol();
|
||||
if (key === "2") return "baz";
|
||||
if (key === "3") return "bar";
|
||||
};
|
||||
var target = [];
|
||||
var proxy = new Proxy(target, {get: getTrap});
|
||||
var replacer = (key, val) => key === "goo" ? proxy : val;
|
||||
var object = {foo: true, goo: false};
|
||||
assertThrows(() => JSON.stringify(object, replacer));
|
||||
|
||||
|
||||
// Replacer returns a callable proxy
|
||||
|
||||
log.length = 0;
|
||||
var target = () => 666;
|
||||
var proxy = new Proxy(target, handler);
|
||||
var replacer = (key, val) => key === "1" ? proxy : val;
|
||||
|
||||
assertEquals('["foo",null]', JSON.stringify(["foo", 42], replacer));
|
||||
assertEquals(0, log.length);
|
||||
|
||||
assertEquals('{"0":"foo"}', JSON.stringify({0: "foo", 1: 42}, replacer));
|
||||
assertEquals(0, log.length);
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// JSON.parse
|
||||
|
||||
|
||||
// Reviver is a callable proxy
|
||||
|
||||
log.length = 0;
|
||||
var target = () => 42;
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(typeof proxy === "function");
|
||||
|
||||
assertEquals(42, JSON.parse("[true, false]", proxy));
|
||||
assertEquals(3, log.length);
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(4, log[0].length);
|
||||
assertEquals("apply", log[0][0]);
|
||||
assertEquals(["0", true], log[0][3]);
|
||||
assertEquals(4, log[1].length);
|
||||
assertEquals("apply", log[1][0]);
|
||||
assertEquals(["1", false], log[1][3]);
|
||||
assertEquals(4, log[2].length);
|
||||
assertEquals("apply", log[2][0]);
|
||||
assertEquals(["", [42, 42]], log[2][3]);
|
||||
|
||||
|
||||
// Reviver plants a non-arraylike proxy into a yet-to-be-visited property
|
||||
|
||||
log.length = 0;
|
||||
var target = {baz: 42};
|
||||
var proxy = new Proxy(target, handler);
|
||||
var reviver = function(p, v) {
|
||||
if (p === "baz") return 5;
|
||||
if (p === "foo") this.bar = proxy;
|
||||
return v;
|
||||
}
|
||||
|
||||
assertEquals({foo: 0, bar: proxy}, JSON.parse('{"foo":0,"bar":1}', reviver));
|
||||
assertEquals(4, log.length);
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["ownKeys", target], log[0]);
|
||||
assertEquals(["getOwnPropertyDescriptor", target, "baz"], log[1]);
|
||||
assertEquals(["get", target, "baz", proxy], log[2]);
|
||||
assertEquals(["defineProperty", target, "baz",
|
||||
{value: 5, configurable: true, writable: true, enumerable: true}], log[3]);
|
||||
|
||||
|
||||
// Reviver plants an arraylike proxy into a yet-to-be-visited property
|
||||
|
||||
log.length = 0;
|
||||
var target = [42];
|
||||
var proxy = new Proxy(target, handler);
|
||||
assertTrue(Array.isArray(proxy));
|
||||
var reviver = function(p, v) {
|
||||
if (p === "0") return undefined;
|
||||
if (p === "foo") this.bar = proxy;
|
||||
return v;
|
||||
}
|
||||
|
||||
var result = JSON.parse('{"foo":0,"bar":1}', reviver);
|
||||
assertEquals({foo: 0, bar: proxy}, result);
|
||||
assertSame(result.bar, proxy);
|
||||
assertEquals(3, log.length);
|
||||
for (var i in log) assertSame(target, log[i][1]);
|
||||
|
||||
assertEquals(["get", target, "length", proxy], log[0]);
|
||||
assertEquals(["get", target, "0", proxy], log[1]);
|
||||
assertEquals(["deleteProperty", target, "0"], log[2]);
|
||||
|
@ -489,3 +489,32 @@ assertTrue(Object.prototype.isPrototypeOf(o2));
|
||||
|
||||
var json = '{"stuff before slash\\\\stuff after slash":"whatever"}';
|
||||
TestStringify(json, JSON.parse(json));
|
||||
|
||||
|
||||
// https://bugs.chromium.org/p/v8/issues/detail?id=3139
|
||||
|
||||
reviver = function(p, v) {
|
||||
if (p == "a") {
|
||||
this.b = { get x() {return null}, set x(_){throw 666} }
|
||||
}
|
||||
return v;
|
||||
}
|
||||
assertEquals({a: 0, b: {x: null}}, JSON.parse('{"a":0,"b":1}', reviver));
|
||||
|
||||
|
||||
// Make sure a failed [[Delete]] doesn't throw
|
||||
|
||||
reviver = function(p, v) {
|
||||
Object.freeze(this);
|
||||
return p === "" ? v : undefined;
|
||||
}
|
||||
assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver));
|
||||
|
||||
|
||||
// Make sure a failed [[DefineProperty]] doesn't throw
|
||||
|
||||
reviver = function(p, v) {
|
||||
Object.freeze(this);
|
||||
return p === "" ? v : 42;
|
||||
}
|
||||
assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver));
|
||||
|
@ -88,7 +88,6 @@
|
||||
# Proxy tests rely on non ES6 version of Proxies
|
||||
# TODO(neis,cbruni): figure out which Proxy tests can be reused
|
||||
'harmony/proxies-example-membrane': [SKIP],
|
||||
'harmony/proxies-json': [SKIP],
|
||||
'harmony/proxies-symbols': [SKIP],
|
||||
'harmony/proxies-with-unscopables': [SKIP],
|
||||
'strong/load-proxy': [SKIP],
|
||||
@ -800,6 +799,8 @@
|
||||
'global-vars-with': [SKIP],
|
||||
'instanceof-2': [SKIP],
|
||||
'json': [SKIP],
|
||||
'json-replacer-number-wrapper-tostring': [SKIP],
|
||||
'json-replacer-order': [SKIP],
|
||||
'math-floor-of-div-minus-zero': [SKIP],
|
||||
'math-min-max': [SKIP],
|
||||
'messages': [SKIP],
|
||||
@ -854,6 +855,7 @@
|
||||
'regress/regress-2163': [SKIP],
|
||||
'regress/regress-2318': [SKIP],
|
||||
'regress/regress-2339': [SKIP],
|
||||
'regress/regress-2374': [SKIP],
|
||||
'regress/regress-2444': [SKIP],
|
||||
'regress/regress-244': [SKIP],
|
||||
'regress/regress-2593': [SKIP],
|
||||
@ -866,6 +868,7 @@
|
||||
'regress/regress-2825': [SKIP],
|
||||
'regress/regress-286': [SKIP],
|
||||
'regress/regress-298269': [SKIP],
|
||||
'regress/regress-3135': [SKIP],
|
||||
'regress/regress-3176': [SKIP],
|
||||
'regress/regress-318420': [SKIP],
|
||||
'regress/regress-320532': [SKIP],
|
||||
@ -926,6 +929,7 @@
|
||||
'regress/regress-69': [SKIP],
|
||||
'regress/regress-70066': [SKIP],
|
||||
'regress/regress-747': [SKIP],
|
||||
'regress/regress-753': [SKIP],
|
||||
'regress/regress-806473': [SKIP],
|
||||
'regress/regress-842017': [SKIP],
|
||||
'regress/regress-84234': [SKIP],
|
||||
@ -953,6 +957,7 @@
|
||||
'regress/regress-crbug-245480': [SKIP],
|
||||
'regress/regress-crbug-349079': [SKIP],
|
||||
'regress/regress-crbug-350864': [SKIP],
|
||||
'regress/regress-crbug-351262': [SKIP],
|
||||
'regress/regress-crbug-352058': [SKIP],
|
||||
'regress/regress-353551': [SKIP],
|
||||
'regress/regress-crbug-357137': [SKIP],
|
||||
@ -997,6 +1002,7 @@
|
||||
'regress/regress-handle-illegal-redeclaration': [SKIP],
|
||||
'regress/regress-inline-class-constructor': [SKIP],
|
||||
'regress/regress-inlining-function-literal-context': [SKIP],
|
||||
'regress/regress-latin-1': [SKIP],
|
||||
'regress/regress-lazy-deopt-reloc': [SKIP],
|
||||
'regress/regress-map-invalidation-2': [SKIP],
|
||||
'regress/regress-opt-after-debug-deopt': [SKIP],
|
||||
|
@ -566,6 +566,7 @@
|
||||
'built-ins/Array/prototype/toString/S15.4.4.2_A1_T4': [SKIP],
|
||||
'built-ins/Date/15.9.1.15-1': [SKIP],
|
||||
'built-ins/Date/prototype/toISOString/15.9.5.43-0-13': [SKIP],
|
||||
'built-ins/JSON/stringify/*': [SKIP],
|
||||
'built-ins/Object/defineProperty/15.2.3.6-4-625gs': [SKIP],
|
||||
'built-ins/Object/prototype/hasOwnProperty/S15.2.4.5_A12': [SKIP],
|
||||
'built-ins/Object/prototype/isPrototypeOf/S15.2.4.6_A12': [SKIP],
|
||||
|
Loading…
Reference in New Issue
Block a user