forbidden-loop-closure.js
1.94 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
"use strict";
var arr = [];
// fresh x per iteration but semantics not determined yet
// in ES6 spec draft (transfer in particular). Also inconsistent
// between VM implementations.
// once ES6 nails down the semantics (and VM's catch up) we'll
// revisit
// note v8 bug https://code.google.com/p/v8/issues/detail?id=2560
// also see other/v8-bug.js
for (let x = 0; x < 3; x++) {
arr.push(function() { return x; });
}
for (let z, x = 0; x < 3; x++) {
arr.push(function() { return x; });
}
// as a consequence of the above, defs is unable to transform
// the code below (even though it is the output of an earlier
// defs transformation). we should be able to detect this case
// (and pass it through unmodified) but is it worth the effort?
for (let x = 0; x < 3; x++) {(function(){
let y = x;
arr.push(function() { return y; });
}).call(this);}
// return is not allowed inside the loop body because the IIFE would break it
(function() {
for (let x = 0; x < 3; x++) {
let y = x;
return 1;
arr.push(function() { return y; });
}
})();
// break is not allowed inside the loop body because the IIFE would break it
for (let x = 0; x < 3; x++) {
let y = x;
break;
arr.push(function() { return y; });
}
// continue is not allowed inside the loop body because the IIFE would break it
for (let x = 0; x < 3; x++) {
let y = x;
continue;
arr.push(function() { return y; });
}
// arguments is not allowed inside the loop body because the IIFE would break it
// (and I don't want to re-apply outer arguments in the inserted IIFE)
for (let x = 0; x < 3; x++) {
let y = x;
arguments[0];
arr.push(function() { return y; });
}
// continue is not allowed inside the loop body because the IIFE would break it
for (let x = 0; x < 3; x++) {
let y = x;
var z = 1;
arr.push(function() { return y; });
}
// TODO block-less loops (is that even applicable?)
arr.forEach(function(f) {
console.log(f());
});