-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests-arguments-wild.html
234 lines (198 loc) · 6.24 KB
/
tests-arguments-wild.html
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!doctype html>
<!-- see Issue #12 -->
<head>
<title>Some tests against wild semantics of .arguments in current implementations</title>
<script>
function run() {
function print(x) {
document.getElementById('output').appendChild(document.createTextNode(x + '\n'));
}
function assertEq(a, b, msg) {
if (!Object.is(a, b))
throw new EvalError(`Expected ${b} got ${a} ${msg || ''}`);
}
function test(name, f) {
print(`------ ${name}`);
try {
f();
print(`passed!`);
}
catch (e) {
print(e);
}
print('');
}
test('basic', _ => {
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0);
})
test('contains-arguments', _ => {
print("- Refers to the implicit `arguments` binding.")
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
if (false) arguments;
a = 1;
assertEq(arguments[0], 1);
}
f(0);
});
test('contains-delete-arguments', _ => {
print("- Explicitly refers to the implicit `arguments` binding, but only for deleting it.")
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
delete arguments
a = 1
assertEq(f.arguments[0], 0);
}
f(0);
});
test('contains-direct-eval', _ => {
print("- Contains direct eval.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
if (false) eval("");
a = 1
assertEq(f.arguments[0], 0);
}
f(0);
});
test('trigger-direct-eval-with-side-effect', _ => {
var b;
print("- Contains direct eval with a non-static string and with side-effect.");
function f(a) {
eval(`b = ${ Math.random() };`);
a = 1
assertEq(f.arguments[0], 0);
}
f(0);
});
test('contains-with-statement', _ => {
print("- Contains a with statement.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
a = 1;
if (false) with ({}) { }
assertEq(f.arguments[0], 0);
}
f(0);
})
test('parameter-has-default', _ => {
print("- The parameter has a default value.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a = 0) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0);
})
test('other-parameter-has-default', _ => {
print("- Another parameter has a default value.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, b = 0) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0);
})
test('other-parameter-has-default-bis', _ => {
print("- Another parameter has a default value.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, b = 0) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0, 0);
})
test('has-rest-parameter', _ => {
print("- There is a rest parameter.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, ...b) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0);
})
test('has-rest-parameter-bis', _ => {
print("- There is a rest parameter, that will receive a value.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, ...b) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0, 0);
})
test('is-constructed', _ => {
print("- Invoked via `new`.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, ...b) {
a = 1;
assertEq(f.arguments[0], 0);
}
new f(0);
})
test('parameter-closed-other', _ => {
print("- The modified parameter is closed over.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
a = 1;
assertEq(f.arguments[0], 0);
() => a;
}
f(0);
})
test('other-parameter-closed-other', _ => {
print("- Another parameter is closed over.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, b) {
b = 1;
assertEq(f.arguments[1], 0);
() => a;
}
f(0, 0);
})
test('missing-argument', _ => {
print("- Call with less arguments than parameters.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a, b) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0);
})
test('argument-in-excess', _ => {
print("- Call with more arguments than parameters.");
print("Testing whether f.arguments[0] reflects the original value.");
function f(a) {
a = 1;
assertEq(f.arguments[0], 0);
}
f(0, 0);
})
test('many-executions', _ => {
print("Testing whether the result remains consistent after many executions.");
var expected = (function g(a) {
a = 1;
return g.arguments[0];
})(0);
function f(a, expected) {
a = 1;
assertEq(f.arguments[0], expected, `at iteration ${i}`)
}
print(`Will expect ${expected}`)
for (var i = 0; i < 1e6; ++i) {
f(0, expected);
}
})
}
</script>
<body>
<p>These tests check whether <code>f.arguments[i]</code> reflects the original value
of the <var>i</var>-th argument as it was passed to <var>f</var>, in various settings.
<p>See Issue #12 on the Github repo.
<p><button onclick="run()">Run the tests</button>
<hr>
<p><output id="output" style="display: block; white-space:pre"></output>