forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashbang.js
More file actions
110 lines (105 loc) · 4.82 KB
/
Copy pathHashbang.js
File metadata and controls
110 lines (105 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
let invalidScripts = [
[" #!\n", "Hashbang must be the first token (even before whitespace)"],
["\n#!\n", "Hashbang must be the first token (even before whitespace)"],
["##!\n", "Hashbang token is '#!'"],
[";#!\n", "Hashbang must be the first token"],
["'use strict'#!\n", "Hashbang must come before 'use strict'"],
["#!\n#!\n", "Only one hashbang may exist because it has to be the first token"],
["function foo() {\n#!\n}", "Hashbang must be the first token in the script (not local function)"],
["function foo() {#!\n}", "Hashbang must be the first token in the script (not local function)"],
["#\\041\n", "Hashbang can't be made of encoded characters"],
["#\\u0021\n", "Hashbang can't be made of encoded characters"],
["#\\u{21}\n", "Hashbang can't be made of encoded characters"],
["#\\x21\n", "Hashbang can't be made of encoded characters"],
["\\043!\n", "Hashbang can't be made of encoded characters"],
["\\u0023!\n", "Hashbang can't be made of encoded characters"],
["\\u{23}!\n", "Hashbang can't be made of encoded characters"],
["\\x23!\n", "Hashbang can't be made of encoded characters"],
["\\u0023\\u0021\n", "Hashbang can't be made of encoded characters"],
["Function('#!\n','')", "Hashbang is not valid in function evaluator contexts"],
["new Function('#!\n','')", "Hashbang is not valid in function evaluator contexts"],
["{\n#!\n}\n", "Hashbang not valid in block"],
["#!/*\nthrow 123;\n*/\nthrow 456;", "Hashbang comments out a single line"],
["\\\\ single line comment\n#! hashbang\n", "Single line comment may not preceed hashbang"],
["/**/#! hashbang\n", "Multi-line comment may not preceed hashbang"],
["/**/\n#! hashbang\n", "Multi-line comment may not preceed hashbang"],
];
var tests = [
{
name: "Valid hashbang in ordinary script",
body: function () {
assert.areEqual(2, WScript.LoadScript("#! throw 'error';\nthis.prop=2;").prop);
assert.areEqual(3, WScript.LoadScript("#! throw 'error'\u{000D}this.prop=3;").prop);
assert.areEqual(4, WScript.LoadScript("#! throw 'error'\u{2028}this.prop=4;").prop);
assert.areEqual(5, WScript.LoadScript("#! throw 'error'\u{2029}this.prop=5;").prop);
}
},
{
name: "Valid hashbang in module script",
body: function () {
WScript.RegisterModuleSource('module_hashbang_valid.js', "#! export default 123;\n export default 456;");
testRunner.LoadModule(`
import {default as prop} from 'module_hashbang_valid.js';
assert.areEqual(456, prop);
`, 'samethread', false, false);
}
},
{
name: "Valid hashbang in eval",
body: function () {
assert.areEqual(undefined, eval('#!'));
assert.areEqual(undefined, eval('#!\n'));
assert.areEqual(1, eval('#!\n1'));
assert.areEqual(undefined, eval('#!2\n'));
}
},
{
name: "Valid hashbang in indirect eval",
body: function () {
let _eval = eval;
assert.areEqual(undefined, _eval('#!'));
assert.areEqual(undefined, _eval('#!\n'));
assert.areEqual(1, _eval('#!\n1'));
assert.areEqual(undefined, _eval('#!2\n'));
}
},
{
name: "Invalid hashbang in ordinary script",
body: function () {
for (a of invalidScripts) {
assert.throws(()=>WScript.LoadScript(a[0]), SyntaxError, a[1]);
}
}
},
{
name: "Invalid hashbang in module script",
body: function () {
for (a of invalidScripts) {
assert.throws(()=>WScript.LoadModule(a[0]), SyntaxError, a[1]);
}
}
},
{
name: "Invalid hashbang in eval",
body: function () {
for (a of invalidScripts) {
assert.throws(()=>eval(a[0]), SyntaxError, a[1]);
}
}
},
{
name: "Invalid hashbang in indirect eval",
body: function () {
let _eval = eval;
for (a of invalidScripts) {
assert.throws(()=>_eval(a[0]), SyntaxError, a[1]);
}
}
},
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });