OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 // Flags: --do-expression-parsing --harmony-sloppy-let | |
6 | |
7 // This feature is not currently on the standards track. --do-expression-parsing | |
rossberg
2015/10/13 10:44:33
It is, though still at stage 0.
caitp (gmail)
2015/10/13 15:06:00
Acknowledged.
| |
8 // exists for the testing of the internal implementation, intended to be used | |
9 // to desugar certain sequences more easily. It may be removed in the future. | |
10 | |
11 function returnValue(v) { return v; } | |
12 function MyError() {} | |
13 | |
14 function TestBasic() { | |
rossberg
2015/10/13 10:44:33
The most important thing to test is all sorts of s
caitp (gmail)
2015/10/13 15:06:00
For every case I've tried, it seems to work (altho
rossberg
2015/10/15 09:58:27
Some other scenarios to test:
- Uses of break and
caitp (gmail)
2015/10/15 11:24:17
Ah yes, I'll add more
| |
15 // Looping and lexical declarations | |
16 assertEquals(512, returnValue(do { | |
17 let n = 2; | |
18 for (let i = 0; i < 4; i++) n <<= 2; | |
19 })); | |
20 | |
21 // Strings do the right thing | |
22 assertEquals("spooky halloween", returnValue(do { | |
23 "happy halloween".replace('happy', 'spooky'); | |
24 })); | |
25 | |
26 // Do expressions with no completion produce an undefined value | |
27 assertEquals(undefined, returnValue(do {})); | |
28 assertEquals(undefined, returnValue(do { var x = 99; })); | |
29 assertEquals(undefined, returnValue(do { function f() {}; })); | |
30 assertEquals(undefined, returnValue(do { let z = 33; })); | |
31 | |
32 // Propagation of exception | |
33 assertThrows(function() { | |
34 (do { | |
35 throw new MyError(); | |
36 "potatoes"; | |
37 }); | |
38 }, MyError); | |
39 | |
40 // Real-world use case for desugaring | |
41 var array = [1, 2, 3, 4, 5], iterable = [6, 7, 8,9]; | |
42 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], do { | |
43 for (var element of iterable) array.push(element); | |
44 array; | |
45 }); | |
46 | |
47 // Nested do-expressions | |
48 assertEquals(125, do { (do { (do { 5 * 5 * 5 }) }) }); | |
49 | |
50 // Directives are not honoured | |
51 (do { | |
52 "use strict"; | |
53 foo = 80; | |
54 assertEquals(foo, 80); | |
55 }); | |
56 } | |
57 TestBasic(); | |
OLD | NEW |