Reviewers: arv,
Description:
[strong] make function and class declarations lexical & immutable
[email protected]
BUG=
Please review this at https://codereview.chromium.org/932283003/
Base URL: https://chromium.googlesource.com/v8/v8.git@master
Affected files (+40, -19 lines):
M src/parser.cc
D test/mjsunit/strong/arguments.js
A test/mjsunit/strong/classes.js
A + test/mjsunit/strong/functions.js
Index: test/mjsunit/strong/arguments.js
diff --git a/test/mjsunit/strong/arguments.js
b/test/mjsunit/strong/arguments.js
deleted file mode 100644
index
b40b661ccf2243d565a42b3aa8567ddab3486536..0000000000000000000000000000000000000000
--- a/test/mjsunit/strong/arguments.js
+++ /dev/null
@@ -1,16 +0,0 @@
-// Copyright 2015 the V8 project authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Flags: --strong-mode
-
-(function NoArguments() {
- assertThrows("'use strong'; arguments", SyntaxError);
- assertThrows("'use strong'; function f() { arguments }", SyntaxError);
- assertThrows("'use strong'; let f = function() { arguments }",
SyntaxError);
- assertThrows("'use strong'; let f = () => arguments", SyntaxError);
- // The following are strict mode errors already.
- assertThrows("'use strong'; let arguments", SyntaxError);
- assertThrows("'use strong'; function f(arguments) {}", SyntaxError);
- assertThrows("'use strong'; let f = (arguments) => {}", SyntaxError);
-})();
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index
84a94efbfc6a991bb99b5b6d0a1a038e70a0e1cf..985a90f8dc5f0154a1829680e99f37cb2e008085
100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1975,6 +1975,7 @@ Statement* Parser::ParseFunctionDeclaration(
// In ES6, a function behaves as a lexical binding, except in
// a script scope, or the initial scope of eval or another function.
VariableMode mode =
+ is_strong(language_mode()) ? CONST :
allow_harmony_scoping() && is_strict(language_mode()) &&
!(scope_->is_script_scope() || scope_->is_eval_scope() ||
scope_->is_function_scope())
@@ -2018,13 +2019,15 @@ Statement*
Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names,
ClassLiteral* value = ParseClassLiteral(name, scanner()->location(),
is_strict_reserved, pos,
CHECK_OK);
- VariableProxy* proxy = NewUnresolved(name, LET);
+ VariableMode mode = is_strong(language_mode()) ? CONST : LET;
+ VariableProxy* proxy = NewUnresolved(name, mode);
Declaration* declaration =
- factory()->NewVariableDeclaration(proxy, LET, scope_, pos);
+ factory()->NewVariableDeclaration(proxy, mode, scope_, pos);
Declare(declaration, true, CHECK_OK);
proxy->var()->set_initializer_position(pos);
- Token::Value init_op = Token::INIT_LET;
+ Token::Value init_op =
+ is_strong(language_mode()) ? Token::INIT_CONST : Token::INIT_LET;
Assignment* assignment = factory()->NewAssignment(init_op, proxy, value,
pos);
Statement* assignment_statement =
factory()->NewExpressionStatement(assignment,
RelocInfo::kNoPosition);
Index: test/mjsunit/strong/classes.js
diff --git a/test/mjsunit/strong/classes.js b/test/mjsunit/strong/classes.js
new file mode 100644
index
0000000000000000000000000000000000000000..3c7caf5f84e4a5d31ac3519d975816a4be05ae8b
--- /dev/null
+++ b/test/mjsunit/strong/classes.js
@@ -0,0 +1,17 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --strong-mode
+
+'use strong';
+
+class C {}
+
+(function ImmutableClassBindings() {
+ class D {}
+ assertThrows(function(){ eval("C = 0") }, TypeError);
+ assertThrows(function(){ eval("D = 0") }, TypeError);
+ assertEquals('function', typeof C);
+ assertEquals('function', typeof D);
+})();
Index: test/mjsunit/strong/functions.js
diff --git a/test/mjsunit/strong/arguments.js
b/test/mjsunit/strong/functions.js
similarity index 64%
rename from test/mjsunit/strong/arguments.js
rename to test/mjsunit/strong/functions.js
index
b40b661ccf2243d565a42b3aa8567ddab3486536..4869ac6dfaef35befff01bac577b6bb57576936e
100644
--- a/test/mjsunit/strong/arguments.js
+++ b/test/mjsunit/strong/functions.js
@@ -4,6 +4,8 @@
// Flags: --strong-mode
+'use strong';
+
(function NoArguments() {
assertThrows("'use strong'; arguments", SyntaxError);
assertThrows("'use strong'; function f() { arguments }", SyntaxError);
@@ -14,3 +16,18 @@
assertThrows("'use strong'; function f(arguments) {}", SyntaxError);
assertThrows("'use strong'; let f = (arguments) => {}", SyntaxError);
})();
+
+function g() {}
+
+(function LexicalFunctionBindings(global) {
+ assertEquals('function', typeof g);
+ assertEquals(undefined, global.g);
+})(this);
+
+(function ImmutableFunctionBindings() {
+ function f() {}
+ assertThrows(function(){ eval("g = 0") }, TypeError);
+ assertThrows(function(){ eval("f = 0") }, TypeError);
+ assertEquals('function', typeof g);
+ assertEquals('function', typeof f);
+})();
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.