Reviewers: Kasper Lund, Description: Make global variable initialization consistent with Firefox and Safari behavior. Only force the introduction of a variable directly on the global object if there is an explicit initial value in a variable declaration.
BUG=http://crbug.com/12548 Please review this at http://codereview.chromium.org/151191 SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/ Affected files: M src/runtime.cc M test/cctest/test-api.cc M test/cctest/test-decls.cc Index: test/cctest/test-api.cc =================================================================== --- test/cctest/test-api.cc (revision 2324) +++ test/cctest/test-api.cc (working copy) @@ -7024,3 +7024,20 @@ calling_context1.Clear(); calling_context2.Clear(); } + + +// Check that a variable declaration with no explicit initialization +// value does not shadow an existing property in the prototype chain. +// +// This is consistent with Firefox and Safari. +// +// See http://crbug.com/12548. +THREADED_TEST(InitGlobalVarInProtoChain) { + v8::HandleScope scope; + LocalContext context; + // Introduce a variable in the prototype chain. + CompileRun("__proto__.x = 42"); + v8::Handle<v8::Value> result = CompileRun("var x; x"); + CHECK(!result->IsUndefined()); + CHECK_EQ(42, result->Int32Value()); +} Index: test/cctest/test-decls.cc =================================================================== --- test/cctest/test-decls.cc (revision 2324) +++ test/cctest/test-decls.cc (working copy) @@ -534,10 +534,10 @@ { ExistsInPrototypeContext context; context.Check("var x; x", + 1, // get 0, - 0, 1, // declaration - EXPECT_RESULT, Undefined()); + EXPECT_EXCEPTION); } { ExistsInPrototypeContext context; Index: src/runtime.cc =================================================================== --- src/runtime.cc (revision 2325) +++ src/runtime.cc (working copy) @@ -769,17 +769,23 @@ PropertyAttributes attributes = DONT_DELETE; // Lookup the property locally in the global object. If it isn't - // there, we add the property and take special precautions to always - // add it as a local property even in case of callbacks in the - // prototype chain (this rules out using SetProperty). - // We have IgnoreAttributesAndSetLocalProperty for this. + // there, there is a property with this name in the prototype chain. + // We follow Safari and Firefox behavior and only set the property + // locally if there is an explicit initialization value that we have + // to assign to the property. When adding the property we take + // special precautions to always add it as a local property even in + // case of callbacks in the prototype chain (this rules out using + // SetProperty). We have IgnoreAttributesAndSetLocalProperty for + // this. LookupResult lookup; global->LocalLookup(*name, &lookup); if (!lookup.IsProperty()) { - Object* value = (assign) ? args[1] : Heap::undefined_value(); - return global->IgnoreAttributesAndSetLocalProperty(*name, - value, - attributes); + if (assign) { + return global->IgnoreAttributesAndSetLocalProperty(*name, + args[1], + attributes); + } + return Heap::undefined_value(); } // Determine if this is a redeclaration of something read-only. --~--~---------~--~----~------------~-------~--~----~ v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev -~----------~----~----~----~------~----~------~--~---
