completion.js 7.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


function assertUndef(x) {
  assertEquals(undefined, x);
}


11 12 13 14 15 16 17 18 19 20
// ClassDeclaration

assertUndef(eval('class C {}'));
assertUndef(eval('class C {m() {}}'));
assertUndef(eval('class C extends null {}'));
assertEquals(42, eval('42; class C {}'));
assertEquals(42, eval('42; class C {m() {}}'));
assertEquals(42, eval('42; class C extends null {}'));


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
// IfStatement [13.6.7]

assertUndef(eval('42; if (true) ; else 0;'));  // ES5: 42
assertUndef(eval('42; if (true) ;'));  // ES5: 42
assertUndef(eval('42; if (false) 0;'));  // ES5: 42

assertEquals(1, eval('42; if (true) 1;'));
assertEquals(1, eval('42; if (true) 1; else 0;'));
assertEquals(0, eval('42; if (false) 1; else 0;'));


// IterationStatement [13.7]

assertUndef(eval('42; do ; while (false);'));  // ES5: 42
assertUndef(eval('42; var x = 1; do ; while (x--);'));  // ES5: 42
assertUndef(eval('42; while (false) 0;'));  // ES5: 42
assertUndef(eval('42; while (true) break;'));  // ES5: 42
assertUndef(eval('42; bla: while (true) break bla;'));  // ES5: 42
assertUndef(eval('42; var x = 1; while (x--) ;'));  // ES5: 42
assertUndef(eval('42; for (; false; ) 0;'));  // ES5: 42
assertUndef(eval('42; for (var x = 1; x; x--) ;'));  // ES5: 42
assertUndef(eval('42; for (var x in ["foo", "bar"]) ;'));
assertUndef(eval('42; for (var x of ["foo", "bar"]) ;'));
assertUndef(eval('42; for (let x = 1; x; x--) ;'));
assertUndef(eval('42; for (let x in ["foo", "bar"]) ;'));
assertUndef(eval('42; for (let x of ["foo", "bar"]) ;'));
assertUndef(eval('42; for (const x in ["foo", "bar"]) ;'));
assertUndef(eval('42; for (const x of ["foo", "bar"]) ;'));

assertEquals(1, eval('42; var x = 10; do x--; while (x);'));
assertEquals(1, eval('42; var x = 10; while (x) x--;'));
assertEquals(1, eval('42; for (var x = 10; x; x--) x;'));
assertEquals(1, eval('42; for (var x = 10; x; --x) x;'));
assertEquals(1, eval('42; for (let x = 10; x; --x) x;'));
assertEquals(1, eval('42; var y = 2; for (var x in ["foo", "bar"]) y--;'));
assertEquals(1, eval('42; var y = 2; for (const x in ["foo", "bar"]) y--;'));
assertEquals(1, eval('42; var y = 2; for (let x in ["foo", "bar"]) y--;'));
assertEquals(1, eval('42; var y = 2; for (var x of ["foo", "bar"]) y--;'));
assertEquals(1, eval('42; var y = 2; for (const x of ["foo", "bar"]) y--;'));
assertEquals(1, eval('42; var y = 2; for (let x of ["foo", "bar"]) y--;'));


// WithStatement [13.11.7]

assertUndef(eval('42; with ({}) ;'));  // ES5: 42

assertEquals(1, eval('42; with ({}) 1;'));


// SwitchStatement [13.12.11]

assertUndef(eval('42; switch (0) {};'));  // ES5: 42
assertUndef(eval('42; switch (0) { case 1: 1; };'));  // ES5: 42
assertUndef(eval('42; switch (0) { case 0: ; };'));  // ES5: 42
assertUndef(eval('42; switch (0) { default: ; };'));  // ES5: 42
assertUndef(eval('42; switch (0) { case 0: break; }'));  // ES5: 42

assertEquals(1, eval('42; switch (0) { case 0: 1; }'));
assertEquals(1, eval('42; switch (0) { case 0: 1; break; }'));
assertEquals(1, eval('42; switch (0) { case 0: 1; case 666: break; }'));
assertEquals(2, eval('42; switch (0) { case 0: 1; case 666: 2; break; }'));


// TryStatement [13.15.8]

assertUndef(eval('42; try { } catch(e) { };'));  // ES5: 42
assertUndef(eval('42; try { } catch(e) { 0; };'));  // ES5: 42
assertUndef(eval('42; try { throw "" } catch(e) { };'));  // ES5: 42
assertUndef(eval('42; try { throw "" } catch(e) { } finally { };'));  // ES5: 42
assertUndef(eval('42; try { } finally { 666 };'));  // ES5: 42


// Some combinations

assertUndef(eval('42; switch (0) { case 0: if (true) break; }'));  // ES5: 42
assertUndef(eval('42; switch (0) { case 0: 1; if (true) ; }'));  // ES5: 1
assertUndef(eval('42; switch (0) { case 0: 1; try { break } catch(e) { }; }'));  // ES5: 1

assertEquals(0, eval('42; switch (0) { case 0: 0; case 1: break; }'));
assertEquals(0, eval('42; while (1) { 0; break; }'))
assertEquals(0, eval('42; bla: while (1) { 0; break bla; }'))
assertEquals(0, eval('42; while (1) { with ({}) { 0; break; } }'))
assertEquals(0, eval('42; while (1) { try { 0; break } catch(e) {666} }'))
assertEquals(0, eval(
    '42; while (1) { try { 0; break } catch(e) {666} finally {666} }'))
assertEquals(0, eval(
    '42; while (1) { try { throw "" } catch(e) {666} finally {0; break} }'))
assertEquals(0, eval(
    '42; while (1) { try { throw "" } catch(e) {0; break} finally {666} }'))
assertEquals(0, eval(
    '42; while (1) { try { 666 } finally {0; break} }'));
assertEquals(0, eval(
    '42; while (1) { try { 666; break } finally {0; break} }'));
assertEquals(0, eval(
    '42; lab: try { 666; break lab } finally {0; break lab}'));
assertEquals(undefined, eval(
  'var b = 1; ' +
  'outer: while (1) { while (1) { if (b--) 42; else break outer; }; 666 }'));

assertUndef(eval('42; switch (0) { case 0: 1; if (true) break; }'));  // ES5: 1

122 123 124 125 126 127 128 129 130 131 132 133 134 135
assertUndef(eval('a: while(true) { do { 0 } while(false); switch(1) { case 0: 1; case 1: break a; }; 0 }'));
assertUndef(eval('a: while(true) { do { 0 } while(false); try {} finally { break a }; 0 }'));
assertUndef(eval('a: while(true) { b: while(true) { 0; break b; }; switch(1) { case 1: break a; }; 2 }'));
assertUndef(eval('a: while(true) { b: while(true) { 0; break b; }; while (true) { break a; }; 2 }'));
assertUndef(eval('while (true) { 20; a:{ break a; }  with ({}) break; 30; }'));
assertEquals(42, eval('a: while(true) { switch(0) { case 0: 42; case 1: break a; }; 33 }'));

assertUndef(eval(
  'for (var i = 0; i < 2; ++i) { if (i) { try {} finally { break; } } 0; }'
));
assertUndef(eval(
  'for (var i = 0; i < 2; ++i) { if (i) { try {} finally { continue; } } 0; }'
));

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


////////////////////////////////////////////////////////////////////////////////
//
// The following are copied from webkit/eval-throw-return and adapted.

function throwFunc() {
  throw "";
}

function throwOnReturn(){
  1;
  return throwFunc();
}

function twoFunc() {
  2;
}

assertEquals(1, eval("1;"));
assertUndef(eval("1; try { foo = [2,3,throwFunc(), 4]; } catch (e){}"));
assertUndef(eval("1; try { 2; throw ''; } catch (e){}"));
assertUndef(eval("1; try { 2; throwFunc(); } catch (e){}"));
assertEquals(3, eval("1; try { 2; throwFunc(); } catch (e){3;} finally {}"));
assertEquals(3, eval("1; try { 2; throwFunc(); } catch (e){3;} finally {4;}"));
assertUndef(eval("function blah() { 1; }; blah();"));
assertUndef(eval("var x = 1;"));
assertEquals(1, eval("if (true) { 1; } else { 2; }"));
assertEquals(2, eval("if (false) { 1; } else { 2; }"));
assertUndef(eval("try{1; if (true) { 2; throw ''; } else { 2; }} catch(e){}"));
assertEquals(2, eval("1; var i = 0; do { ++i; 2; } while(i!=1);"));
assertUndef(eval(
    "try{1; var i = 0; do { ++i; 2; throw ''; } while (i!=1);} catch(e){}"));
assertUndef(eval("1; try{2; throwOnReturn();} catch(e){}"));
assertUndef(eval("1; twoFunc();"));
assertEquals(2, eval("1; with ( { a: 0 } ) { 2; }"));
172 173 174 175 176 177 178

// https://bugs.chromium.org/p/chromium/issues/detail?id=787698
assertEquals(42, eval("try {42} catch (_) {} finally {}"));
assertEquals(42, eval("try {42} catch (_) {} finally {43}"));
assertEquals(42, eval("foo: try {42} catch (_) {} finally {}"));
assertEquals(42, eval("foo: try {42} catch (_) {} finally {43}"));
assertEquals(43, eval("foo: try {42} catch (_) {} finally {43; break foo}"));