Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 1 | // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | |
| 6 | function doTest(scripts, expectedError) { |
| 7 | var realm = Realm.create(); |
| 8 | |
| 9 | for (var i = 0; i < scripts.length - 1; i++) { |
| 10 | Realm.eval(realm, scripts[i]); |
| 11 | } |
| 12 | assertThrows(function() { |
| 13 | Realm.eval(realm, scripts[scripts.length - 1]); |
| 14 | }, Realm.eval(realm, expectedError)); |
| 15 | |
| 16 | Realm.dispose(realm); |
| 17 | } |
| 18 | |
| 19 | var tests = [ |
| 20 | { |
| 21 | // ES#sec-globaldeclarationinstantiation 5.a: |
| 22 | // If envRec.HasVarDeclaration(name) is true, throw a SyntaxError |
| 23 | // exception. |
| 24 | scripts: [ |
| 25 | "var a;", |
| 26 | "let a;", |
| 27 | ], |
| 28 | expectedError: "SyntaxError", |
| 29 | }, |
| 30 | { |
| 31 | // ES#sec-globaldeclarationinstantiation 6.a: |
| 32 | // If envRec.HasLexicalDeclaration(name) is true, throw a SyntaxError |
| 33 | // exception. |
| 34 | scripts: [ |
| 35 | "let a;", |
| 36 | "var a;", |
| 37 | ], |
| 38 | expectedError: "SyntaxError", |
| 39 | }, |
| 40 | { |
| 41 | // ES#sec-globaldeclarationinstantiation 5.b: |
| 42 | // If envRec.HasLexicalDeclaration(name) is true, throw a SyntaxError |
| 43 | // exception. |
| 44 | scripts: [ |
| 45 | "let a;", |
| 46 | "let a;", |
| 47 | ], |
| 48 | expectedError: "SyntaxError", |
| 49 | }, |
| 50 | { |
| 51 | // ES#sec-evaldeclarationinstantiation 5.a.i.1: |
| 52 | // If varEnvRec.HasLexicalDeclaration(name) is true, throw a SyntaxError |
| 53 | // exception. |
| 54 | scripts: [ |
| 55 | 'let a; eval("var a;");', |
| 56 | ], |
| 57 | expectedError: "SyntaxError", |
| 58 | }, |
| 59 | { |
| 60 | // ES#sec-evaldeclarationinstantiation 5.a.i.1: |
| 61 | // If varEnvRec.HasLexicalDeclaration(name) is true, throw a SyntaxError |
| 62 | // exception. |
| 63 | scripts: [ |
| 64 | 'let a; eval("function a() {}");', |
| 65 | ], |
| 66 | expectedError: "SyntaxError", |
| 67 | }, |
| 68 | { |
| 69 | // ES#sec-evaldeclarationinstantiation 5.d.ii.2.a.i: |
| 70 | // Throw a SyntaxError exception. |
| 71 | scripts: [ |
| 72 | '(function() { let a; eval("var a;"); })();', |
| 73 | ], |
| 74 | expectedError: "SyntaxError", |
| 75 | }, |
| 76 | { |
| 77 | // ES#sec-evaldeclarationinstantiation 5.d.ii.2.a.i: |
| 78 | // Throw a SyntaxError exception. |
| 79 | scripts: [ |
| 80 | '(function() { let a; eval("function a() {}"); })();', |
| 81 | ], |
| 82 | expectedError: "SyntaxError", |
| 83 | }, |
| 84 | { |
| 85 | // ES#sec-globaldeclarationinstantiation 5.d: |
| 86 | // If hasRestrictedGlobal is true, throw a SyntaxError exception. |
| 87 | scripts: [ |
| 88 | 'let NaN;', |
| 89 | ], |
| 90 | expectedError: "SyntaxError", |
| 91 | }, |
| 92 | { |
| 93 | // ES#sec-globaldeclarationinstantiation 5.d: |
| 94 | // If hasRestrictedGlobal is true, throw a SyntaxError exception. |
| 95 | scripts: [ |
| 96 | 'function NaN() {}', |
| 97 | ], |
| 98 | expectedError: "SyntaxError", |
| 99 | }, |
| 100 | |
| 101 | { |
| 102 | // ES#sec-evaldeclarationinstantiation 8.a.iv.1.b: |
| 103 | // If fnDefinable is false, throw a TypeError exception. |
| 104 | scripts: [ |
| 105 | 'eval("function NaN() {}");', |
| 106 | ], |
| 107 | expectedError: "TypeError", |
| 108 | }, |
| 109 | { |
| 110 | // ES#sec-evaldeclarationinstantiation 8.a.iv.1.b: |
| 111 | // If fnDefinable is false, throw a TypeError exception. |
| 112 | scripts: [ |
| 113 | ` |
| 114 | let a; |
| 115 | try { |
| 116 | eval("function a() {}"); |
| 117 | } catch (e) {} |
| 118 | eval("function NaN() {}"); |
| 119 | `, |
| 120 | ], |
| 121 | expectedError: "TypeError", |
| 122 | }, |
| 123 | { |
| 124 | // ES#sec-evaldeclarationinstantiation 8.a.iv.1.b: |
| 125 | // If fnDefinable is false, throw a TypeError exception. |
| 126 | scripts: [ |
| 127 | ` |
| 128 | eval(" |
| 129 | function f() { |
| 130 | function b() { |
| 131 | (0, eval)('function NaN() {}'); |
| 132 | } |
| 133 | b(); |
| 134 | } |
| 135 | f(); |
| 136 | "); |
| 137 | `.replace(/"/g, '`'), |
| 138 | ], |
| 139 | expectedError: "TypeError", |
| 140 | }, |
| 141 | ]; |
| 142 | |
| 143 | tests.forEach(function(test) { |
| 144 | doTest(test.scripts, test.expectedError); |
| 145 | }); |