Skip to content

Commit bebf377

Browse files
author
yangguo@chromium.org
committed
Fix JSON.stringify for objects with interceptor handlers.
BUG=161028 Review URL: https://chromiumcodereview.appspot.com/11348209 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13041 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
1 parent bbb6d45 commit bebf377

2 files changed

Lines changed: 28 additions & 33 deletions

File tree

src/json-stringifier.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ class BasicJsonStringifier BASE_EMBEDDED {
7676
}
7777
}
7878

79-
Handle<Object> GetProperty(Handle<JSObject> object,
80-
Handle<String> key);
81-
8279
Handle<Object> ApplyToJsonFunction(Handle<Object> object,
8380
Handle<Object> key);
8481

@@ -262,34 +259,6 @@ void BasicJsonStringifier::Append_(const Char* chars) {
262259
}
263260

264261

265-
Handle<Object> BasicJsonStringifier::GetProperty(Handle<JSObject> object,
266-
Handle<String> key) {
267-
LookupResult lookup(isolate_);
268-
object->LocalLookupRealNamedProperty(*key, &lookup);
269-
if (!lookup.IsProperty()) return factory_->undefined_value();
270-
switch (lookup.type()) {
271-
case NORMAL: {
272-
Object* value = lookup.holder()->GetNormalizedProperty(&lookup);
273-
ASSERT(!value->IsTheHole());
274-
return Handle<Object>(value, isolate_);
275-
}
276-
case FIELD: {
277-
Object* value = lookup.holder()->FastPropertyAt(
278-
lookup.GetFieldIndex().field_index());
279-
ASSERT(!value->IsTheHole());
280-
return Handle<Object>(value, isolate_);
281-
}
282-
case CONSTANT_FUNCTION:
283-
return Handle<Object>(lookup.GetConstantFunction(), isolate_);
284-
default: {
285-
PropertyAttributes attr;
286-
return Object::GetProperty(object, object, &lookup, key, &attr);
287-
}
288-
}
289-
return Handle<Object>::null();
290-
}
291-
292-
293262
Handle<Object> BasicJsonStringifier::ApplyToJsonFunction(
294263
Handle<Object> object, Handle<Object> key) {
295264
LookupResult lookup(isolate_);
@@ -400,8 +369,8 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeGeneric(
400369
bool deferred_comma,
401370
bool deferred_key) {
402371
Handle<JSObject> builtins(isolate_->native_context()->builtins());
403-
Handle<JSFunction> builtin = Handle<JSFunction>::cast(
404-
v8::internal::GetProperty(builtins, "JSONSerializeAdapter"));
372+
Handle<JSFunction> builtin =
373+
Handle<JSFunction>::cast(GetProperty(builtins, "JSONSerializeAdapter"));
405374

406375
Handle<Object> argv[] = { key, object };
407376
bool has_exception = false;

test/cctest/test-accessors.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,29 @@ THREADED_TEST(HandleScopeSegment) {
453453
"result;"))->Run();
454454
CHECK_EQ(100, result->Int32Value());
455455
}
456+
457+
458+
v8::Handle<v8::Array> JSONStringifyEnumerator(const AccessorInfo& info) {
459+
v8::Handle<v8::Array> array = v8::Array::New(1);
460+
array->Set(0, v8_str("regress"));
461+
return array;
462+
}
463+
464+
465+
v8::Handle<v8::Value> JSONStringifyGetter(Local<String> name,
466+
const AccessorInfo& info) {
467+
return v8_str("crbug-161028");
468+
}
469+
470+
471+
THREADED_TEST(JSONStringifyNamedInterceptorObject) {
472+
v8::HandleScope scope;
473+
LocalContext env;
474+
475+
v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New();
476+
obj->SetNamedPropertyHandler(
477+
JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator);
478+
env->Global()->Set(v8_str("obj"), obj->NewInstance());
479+
v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}");
480+
CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected));
481+
}

0 commit comments

Comments
 (0)