Commit b95dbc3f authored by ager@chromium.org's avatar ager@chromium.org

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
Review URL: http://codereview.chromium.org/151191

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2326 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e75a173f
......@@ -769,17 +769,23 @@ static Object* Runtime_InitializeVarGlobal(Arguments args) {
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.
......
......@@ -7024,3 +7024,20 @@ THREADED_TEST(GetCallingContext) {
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());
}
......@@ -534,10 +534,10 @@ TEST(ExistsInPrototype) {
{ ExistsInPrototypeContext context;
context.Check("var x; x",
0,
1, // get
0,
1, // declaration
EXPECT_RESULT, Undefined());
EXPECT_EXCEPTION);
}
{ ExistsInPrototypeContext context;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment