Revision: 11333
Author: [email protected]
Date: Mon Apr 16 06:20:50 2012
Log: Implement ES5 erratum: global declarations shadow inherited
properties.
I also discovered that our treatment of const declarations is inconsistent
when inside a global eval under 'with' (i.e., when created by
DeclareContextSlots). That is,
var x;
eval("const x = 9")
and
var x;
eval("with({}) const x = 9")
differ (the former assigns 9, the latter throws). This appears to be an
oversight from earlier changes to our const semantics (the latter shouldn't
throw either). Fixing this is a separate issue, though (and one that doesn't
seem quite worthwhile).
[email protected]
BUG=v8:1991,80591
TEST=
Review URL: https://chromiumcodereview.appspot.com/10067010
http://code.google.com/p/v8/source/detail?r=11333
Modified:
/branches/bleeding_edge/src/contexts.h
/branches/bleeding_edge/src/objects.cc
/branches/bleeding_edge/src/parser.cc
/branches/bleeding_edge/src/runtime.cc
/branches/bleeding_edge/test/cctest/test-api.cc
/branches/bleeding_edge/test/cctest/test-decls.cc
/branches/bleeding_edge/test/mjsunit/declare-locally.js
/branches/bleeding_edge/test/mjsunit/mjsunit.js
/branches/bleeding_edge/test/mjsunit/regress/regress-1119.js
/branches/bleeding_edge/test/mjsunit/regress/regress-115452.js
/branches/bleeding_edge/test/mjsunit/regress/regress-1170.js
=======================================
--- /branches/bleeding_edge/src/contexts.h Mon Feb 20 06:02:59 2012
+++ /branches/bleeding_edge/src/contexts.h Mon Apr 16 06:20:50 2012
@@ -397,7 +397,7 @@
GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
- // Lookup the the slot called name, starting with the current context.
+ // Lookup the slot called name, starting with the current context.
// There are three possibilities:
//
// 1) result->IsContext():
=======================================
--- /branches/bleeding_edge/src/objects.cc Fri Apr 13 02:35:18 2012
+++ /branches/bleeding_edge/src/objects.cc Mon Apr 16 06:20:50 2012
@@ -3025,7 +3025,6 @@
String* name,
Object* value,
PropertyAttributes attributes) {
-
// Make sure that the top context does not change when doing callbacks or
// interceptor calls.
AssertNoContextChange ncc;
@@ -3094,7 +3093,6 @@
return ConvertDescriptorToFieldAndMapTransition(name, value,
attributes);
case HANDLER:
UNREACHABLE();
- return value;
}
UNREACHABLE(); // keep the compiler happy
return value;
=======================================
--- /branches/bleeding_edge/src/parser.cc Mon Apr 16 04:48:20 2012
+++ /branches/bleeding_edge/src/parser.cc Mon Apr 16 06:20:50 2012
@@ -2254,7 +2254,7 @@
// Global variable declarations must be compiled in a specific
// way. When the script containing the global variable declaration
// is entered, the global variable must be declared, so that if it
- // doesn't exist (not even in a prototype of the global object) it
+ // doesn't exist (on the global object itself, see ES5 errata) it
// gets created with an initial undefined value. This is handled
// by the declarations part of the function representing the
// top-level global code; see Runtime::DeclareGlobalVariable. If
=======================================
--- /branches/bleeding_edge/src/runtime.cc Mon Apr 16 04:48:20 2012
+++ /branches/bleeding_edge/src/runtime.cc Mon Apr 16 06:20:50 2012
@@ -1298,8 +1298,9 @@
if (is_var || is_const) {
// Lookup the property in the global object, and don't set the
// value of the variable if the property is already there.
+ // Do the lookup locally only, see ES5 errata.
LookupResult lookup(isolate);
- global->Lookup(*name, &lookup);
+ global->LocalLookup(*name, &lookup);
if (lookup.IsProperty()) {
// We found an existing property. Unless it was an interceptor
// that claims the property is absent, skip this declaration.
@@ -1336,40 +1337,28 @@
LanguageMode language_mode = DeclareGlobalsLanguageMode::decode(flags);
- // Safari does not allow the invocation of callback setters for
- // function declarations. To mimic this behavior, we do not allow
- // the invocation of setters for function values. This makes a
- // difference for global functions with the same names as event
- // handlers such as "function onload() {}". Firefox does call the
- // onload setter in those case and Safari does not. We follow
- // Safari for compatibility.
- if (is_function || is_module) {
- if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) {
- // Do not overwrite READ_ONLY properties.
- if (lookup.GetAttributes() & READ_ONLY) {
- if (language_mode != CLASSIC_MODE) {
- Handle<Object> args[] = { name };
- return isolate->Throw(*isolate->factory()->NewTypeError(
- "strict_cannot_assign", HandleVector(args,
ARRAY_SIZE(args))));
- }
- continue;
- }
- // Do not change DONT_DELETE to false from true.
- attr |= lookup.GetAttributes() & DONT_DELETE;
- }
- PropertyAttributes attributes =
static_cast<PropertyAttributes>(attr);
-
- RETURN_IF_EMPTY_HANDLE(
- isolate,
- JSObject::SetLocalPropertyIgnoreAttributes(global, name, value,
- attributes));
+ if (!lookup.IsProperty() || is_function || is_module) {
+ // If the local property exists, check that we can reconfigure it
+ // as required for function declarations.
+ if (lookup.IsProperty() && lookup.IsDontDelete()) {
+ if (lookup.IsReadOnly() || lookup.IsDontEnum() ||
+ lookup.type() == CALLBACKS) {
+ return ThrowRedeclarationError(
+ isolate, is_function ? "function" : "module", name);
+ }
+ // If the existing property is not configurable, keep its
attributes.
+ attr = lookup.GetAttributes();
+ }
+ // Define or redefine own property.
+ RETURN_IF_EMPTY_HANDLE(isolate,
+ JSObject::SetLocalPropertyIgnoreAttributes(
+ global, name, value, static_cast<PropertyAttributes>(attr)));
} else {
- RETURN_IF_EMPTY_HANDLE(
- isolate,
- JSReceiver::SetProperty(global, name, value,
- static_cast<PropertyAttributes>(attr),
- language_mode == CLASSIC_MODE
- ? kNonStrictMode : kStrictMode));
+ // Do a [[Put]] on the existing (own) property.
+ RETURN_IF_EMPTY_HANDLE(isolate,
+ JSObject::SetProperty(
+ global, name, value, static_cast<PropertyAttributes>(attr),
+ language_mode == CLASSIC_MODE ? kNonStrictMode :
kStrictMode));
}
}
@@ -1402,6 +1391,8 @@
if (attributes != ABSENT) {
// The name was declared before; check for conflicting re-declarations.
+ // Note: this is actually inconsistent with what happens for globals
(where
+ // we silently ignore such declarations).
if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) {
// Functions are not read-only.
ASSERT(mode != READ_ONLY || initial_value->IsTheHole());
@@ -1464,9 +1455,14 @@
return ThrowRedeclarationError(isolate, "const", name);
}
}
- RETURN_IF_EMPTY_HANDLE(
- isolate,
- JSReceiver::SetProperty(object, name, value, mode,
kNonStrictMode));
+ if (object->IsJSGlobalObject()) {
+ // Define own property on the global object.
+ RETURN_IF_EMPTY_HANDLE(isolate,
+ JSObject::SetLocalPropertyIgnoreAttributes(object, name, value,
mode));
+ } else {
+ RETURN_IF_EMPTY_HANDLE(isolate,
+ JSReceiver::SetProperty(object, name, value, mode,
kNonStrictMode));
+ }
}
return isolate->heap()->undefined_value();
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc Fri Apr 13 02:38:00 2012
+++ /branches/bleeding_edge/test/cctest/test-api.cc Mon Apr 16 06:20:50 2012
@@ -12441,19 +12441,15 @@
// 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.
+// value does shadow an existing property in the prototype chain.
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");
+ v8::Handle<v8::Value> result = CompileRun("var x = 43; x");
CHECK(!result->IsUndefined());
- CHECK_EQ(42, result->Int32Value());
+ CHECK_EQ(43, result->Int32Value());
}
=======================================
--- /branches/bleeding_edge/test/cctest/test-decls.cc Thu Sep 15 05:00:30
2011
+++ /branches/bleeding_edge/test/cctest/test-decls.cc Mon Apr 16 06:20:50
2012
@@ -535,17 +535,17 @@
{ ExistsInPrototypeContext context;
context.Check("var x; x",
- 1, // get
+ 0, // get
0,
- 1, // declaration
- EXPECT_EXCEPTION);
+ 0, // declaration
+ EXPECT_RESULT, Undefined());
}
{ ExistsInPrototypeContext context;
context.Check("var x = 0; x",
0,
0,
- 1, // declaration
+ 0, // declaration
EXPECT_RESULT, Number::New(0));
}
@@ -553,7 +553,7 @@
context.Check("const x; x",
0,
0,
- 1, // declaration
+ 0, // declaration
EXPECT_RESULT, Undefined());
}
@@ -561,7 +561,7 @@
context.Check("const x = 0; x",
0,
0,
- 1, // declaration
+ 0, // declaration
EXPECT_RESULT, Number::New(0));
}
}
@@ -589,7 +589,7 @@
context.Check("if (false) { var x = 0; }; x",
0,
0,
- 1, // declaration
+ 0, // declaration
EXPECT_RESULT, Undefined());
}
}
=======================================
--- /branches/bleeding_edge/test/mjsunit/declare-locally.js Tue Dec 7
03:01:02 2010
+++ /branches/bleeding_edge/test/mjsunit/declare-locally.js Mon Apr 16
06:20:50 2012
@@ -36,8 +36,8 @@
this.__proto__.foo = 42;
this.__proto__.bar = 87;
-eval("assertEquals(42, foo); var foo = 87;");
+eval("assertEquals(undefined, foo); var foo = 87;");
assertEquals(87, foo);
-eval("assertEquals(87, bar); const bar = 42;");
+eval("assertEquals(undefined, bar); const bar = 42;");
assertEquals(42, bar);
=======================================
--- /branches/bleeding_edge/test/mjsunit/mjsunit.js Thu Mar 8 04:49:24 2012
+++ /branches/bleeding_edge/test/mjsunit/mjsunit.js Mon Apr 16 06:20:50 2012
@@ -75,7 +75,7 @@
// Checks that the found value is false.
var assertFalse;
-// Checks that the found value is null. Kept for historical compatability,
+// Checks that the found value is null. Kept for historical compatibility,
// please just use assertEquals(null, expected).
var assertNull;
=======================================
--- /branches/bleeding_edge/test/mjsunit/regress/regress-1119.js Tue May 31
01:08:42 2011
+++ /branches/bleeding_edge/test/mjsunit/regress/regress-1119.js Mon Apr 16
06:20:50 2012
@@ -28,17 +28,17 @@
// Test runtime declaration of properties with var which are intercepted
// by JS accessors.
-__proto__.__defineSetter__("x", function() { hasBeenInvoked = true; });
-__proto__.__defineSetter__("y", function() { throw 'exception'; });
+this.__defineSetter__("x", function() { hasBeenInvoked = true; });
+this.__defineSetter__("y", function() { throw 'exception'; });
var hasBeenInvoked = false;
eval("try { } catch (e) { var x = false; }");
assertTrue(hasBeenInvoked);
-var exception;
+// This has to run in global scope, so cannot use assertThrows...
try {
eval("try { } catch (e) { var y = false; }");
+ assertUnreachable();
} catch (e) {
- exception = e;
-}
-assertEquals('exception', exception);
+ assertEquals('exception', e);
+}
=======================================
--- /branches/bleeding_edge/test/mjsunit/regress/regress-115452.js Wed Mar
14 06:51:00 2012
+++ /branches/bleeding_edge/test/mjsunit/regress/regress-115452.js Mon Apr
16 06:20:50 2012
@@ -27,22 +27,19 @@
// Test that a function declaration cannot overwrite a read-only property.
-print(0)
function foobl() {}
assertTrue(typeof this.foobl == "function");
assertTrue(Object.getOwnPropertyDescriptor(this, "foobl").writable);
-print(1)
Object.defineProperty(this, "foobl", {value: 1, writable: false});
assertSame(1, this.foobl);
assertFalse(Object.getOwnPropertyDescriptor(this, "foobl").writable);
-print(2)
-eval("function foobl() {}");
+// This has to run in global scope, so cannot use assertThrows...
+try {
+ eval("function foobl() {}"); // Should throw.
+ assertUnreachable();
+} catch (e) {
+ assertInstanceof(e, TypeError);
+}
assertSame(1, this.foobl);
-assertFalse(Object.getOwnPropertyDescriptor(this, "foobl").writable);
-
-print(3)
-eval("function foobl() {}");
-assertSame(1, this.foobl);
-assertFalse(Object.getOwnPropertyDescriptor(this, "foobl").writable);
=======================================
--- /branches/bleeding_edge/test/mjsunit/regress/regress-1170.js Thu Sep 15
05:00:30 2011
+++ /branches/bleeding_edge/test/mjsunit/regress/regress-1170.js Mon Apr 16
06:20:50 2012
@@ -27,46 +27,70 @@
var setter_value = 0;
-__proto__.__defineSetter__("a", function(v) { setter_value = v; });
+this.__defineSetter__("a", function(v) { setter_value = v; });
eval("var a = 1");
assertEquals(1, setter_value);
-assertFalse(this.hasOwnProperty("a"));
+assertFalse("value" in Object.getOwnPropertyDescriptor(this, "a"));
eval("with({}) { eval('var a = 2') }");
assertEquals(2, setter_value);
-assertFalse(this.hasOwnProperty("a"));
+assertFalse("value" in Object.getOwnPropertyDescriptor(this, "a"));
// Function declarations are treated specially to match Safari. We do
// not call setters for them.
+this.__defineSetter__("a", function(v) { assertUnreachable(); });
eval("function a() {}");
-assertTrue(this.hasOwnProperty("a"));
-
-__proto__.__defineSetter__("b", function(v) { assertUnreachable(); });
-var exception = false;
+assertTrue("value" in Object.getOwnPropertyDescriptor(this, "a"));
+
+this.__defineSetter__("b", function(v) { setter_value = v; });
try {
- eval("const b = 23");
+ eval("const b = 3");
} catch(e) {
- exception = true;
- assertTrue(/TypeError/.test(e));
-}
-assertFalse(exception);
-
-exception = false;
+ assertUnreachable();
+}
+assertEquals(3, setter_value);
+
try {
eval("with({}) { eval('const b = 23') }");
} catch(e) {
- exception = true;
- assertTrue(/TypeError/.test(e));
-}
-assertTrue(exception);
-
-__proto__.__defineSetter__("c", function(v) { throw 42; });
-exception = false;
+ assertInstanceof(e, TypeError);
+}
+
+this.__defineSetter__("c", function(v) { throw 42; });
try {
eval("var c = 1");
+ assertUnreachable();
} catch(e) {
- exception = true;
assertEquals(42, e);
- assertFalse(this.hasOwnProperty("c"));
-}
-assertTrue(exception);
+ assertFalse("value" in Object.getOwnPropertyDescriptor(this, "c"));
+}
+
+
+
+
+__proto__.__defineSetter__("aa", function(v) { assertUnreachable(); });
+eval("var aa = 1");
+assertTrue(this.hasOwnProperty("aa"));
+
+__proto__.__defineSetter__("bb", function(v) { assertUnreachable(); });
+eval("with({}) { eval('var bb = 2') }");
+assertTrue(this.hasOwnProperty("bb"));
+
+// Function declarations are treated specially to match Safari. We do
+// not call setters for them.
+__proto__.__defineSetter__("cc", function(v) { assertUnreachable(); });
+eval("function cc() {}");
+assertTrue(this.hasOwnProperty("cc"));
+
+__proto__.__defineSetter__("dd", function(v) { assertUnreachable(); });
+try {
+ eval("const dd = 23");
+} catch(e) {
+ assertUnreachable();
+}
+
+try {
+ eval("with({}) { eval('const dd = 23') }");
+} catch(e) {
+ assertInstanceof(e, TypeError);
+}
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev