Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Unified Diff: test/mjsunit/harmony/sloppy-implicit-block-function.js

Issue 1757543003: Restrict FunctionDeclarations in Statement position (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: ignition failure expectation Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/harmony/sloppy-implicit-block-function.js
diff --git a/test/mjsunit/harmony/sloppy-implicit-block-function.js b/test/mjsunit/harmony/sloppy-implicit-block-function.js
new file mode 100644
index 0000000000000000000000000000000000000000..9bdce9c693273b26dd615b27232150d6c3594c8c
--- /dev/null
+++ b/test/mjsunit/harmony/sloppy-implicit-block-function.js
@@ -0,0 +1,109 @@
+// Copyright 2016 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: --no-harmony-restrictive-declarations
+
+// ES#sec-functiondeclarations-in-ifstatement-statement-clauses
+// Annex B 3.4 FunctionDeclarations in IfStatement Statement Clauses
+// In sloppy mode, function declarations in if statements act like
+// they have a block around them. Prohibited in strict mode.
+(function() {
adamk 2016/03/02 00:33:45 Maybe add assertEquals(undefined, f) before the if
Dan Ehrenberg 2016/03/02 00:49:54 Done
+ if (false) function f() { };
adamk 2016/03/02 00:33:45 What about if (true)? I assume that results in the
Dan Ehrenberg 2016/03/02 00:49:54 Good point, added tests for this, and for the 'els
+ assertEquals(undefined, f);
+})();
+
+assertThrows(`
+ function() {
+ 'use strict';
+ if (true) function foo() {}
+ }
+`, SyntaxError);
adamk 2016/03/02 00:51:10 All these parsing tests will better-exercise the p
Dan Ehrenberg 2016/03/03 02:32:57 Oops, not sure how I completely forgot about the p
+
+// For legacy reasons, we also support these types of semantics as
+// the body of a for or with statement.
+(function() {
+ for (;false;) function f() { };
+ assertEquals(undefined, f);
+})();
+
+(function() {
+ for (var x in {}) function f() { };
+ assertEquals(undefined, f);
+})();
+
+(function() {
+ var x;
+ for (x in {}) function f() { };
+ assertEquals(undefined, f);
+})();
+
+(function() {
+ for (var i = 0; i < 1; i++) function f() { };
+ assertEquals('function', typeof f);
+})();
+
+(function() {
+ for (var x in {a: 1}) function f() { };
+ assertEquals('function', typeof f);
+})();
+
+(function() {
+ var x;
+ for (x in {a: 1}) function f() { };
+ assertEquals('function', typeof f);
+})();
+
+(function() {
+ with ({}) function f() { };
+ assertEquals('function', typeof f);
+})();
+
+(function() {
+ do function f() {} while (0);
+ assertEquals('function', typeof f);
+})();
+
+assertThrows(`
+ function() {
+ 'use strict';
+ for (;false;) function foo() {}
+ }
+`, SyntaxError);
+
+assertThrows(`
+ function() {
+ 'use strict';
+ for (var x in {}) function foo() {}
+ }
+`, SyntaxError);
+
+assertThrows(`
+ function() {
+ 'use strict';
+ var x;
+ for (x in {}) function foo() {}
+ }
+`, SyntaxError);
+
+assertThrows(`
+ function() {
+ 'use strict';
+ do function foo() {} while (0);
+ }
+`, SyntaxError);
+
+// Some contexts always throw for function declarations, even in sloppy mode
+
+assertThrows(`
+ function() {
+ try function foo() {} catch (e) {}
+ }
+`, SyntaxError);
+
+assertThrows(`
+ function() {
+ 'use strict';
+ try function foo() {} catch (e) {}
+ }
+`, SyntaxError);

Powered by Google App Engine
This is Rietveld 408576698