Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | load(libdir + 'bytecode-cache.js'); |
michael@0 | 2 | var test = ""; |
michael@0 | 3 | var checkAfter; |
michael@0 | 4 | |
michael@0 | 5 | // code a function which has both used and unused inner functions. |
michael@0 | 6 | test = (function () { |
michael@0 | 7 | function f(x) { |
michael@0 | 8 | function ifTrue() { |
michael@0 | 9 | return true; |
michael@0 | 10 | }; |
michael@0 | 11 | function ifFalse() { |
michael@0 | 12 | return false; |
michael@0 | 13 | }; |
michael@0 | 14 | |
michael@0 | 15 | if (x) return ifTrue(); |
michael@0 | 16 | else return ifFalse(); |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | return f.toSource() + "; f(true)"; |
michael@0 | 20 | })(); |
michael@0 | 21 | evalWithCache(test, { assertEqBytecode: true, assertEqResult : true }); |
michael@0 | 22 | |
michael@0 | 23 | // code a function which uses different inner functions based on the generation. |
michael@0 | 24 | test = (function () { |
michael@0 | 25 | function f(x) { |
michael@0 | 26 | function ifTrue() { |
michael@0 | 27 | return true; |
michael@0 | 28 | }; |
michael@0 | 29 | function ifFalse() { |
michael@0 | 30 | return false; |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | if (x) return ifTrue(); |
michael@0 | 34 | else return ifFalse(); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | return f.toSource() + "; f((generation % 2) == 0)"; |
michael@0 | 38 | })(); |
michael@0 | 39 | evalWithCache(test, { }); |
michael@0 | 40 | |
michael@0 | 41 | // Code a function which has an enclosing scope. |
michael@0 | 42 | test = (function () { |
michael@0 | 43 | function f() { |
michael@0 | 44 | var upvar = ""; |
michael@0 | 45 | function g() { upvar += ""; return upvar; } |
michael@0 | 46 | return g; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | return f.toSource() + "; f()();"; |
michael@0 | 50 | })(); |
michael@0 | 51 | evalWithCache(test, { assertEqBytecode: true, assertEqResult : true }); |
michael@0 | 52 | |
michael@0 | 53 | // Code a lazy function which has an enclosing scope. |
michael@0 | 54 | test = (function () { |
michael@0 | 55 | function f() { |
michael@0 | 56 | var upvar = ""; |
michael@0 | 57 | function g() { upvar += ""; return upvar; } |
michael@0 | 58 | return g; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | return f.toSource() + "; f();"; |
michael@0 | 62 | })(); |
michael@0 | 63 | evalWithCache(test, { assertEqBytecode: true }); |
michael@0 | 64 | |
michael@0 | 65 | // (basic/bug535930) Code an enclosing scope which is a Call object. |
michael@0 | 66 | test = (function () { |
michael@0 | 67 | return "(" + (function () { |
michael@0 | 68 | p = function () { |
michael@0 | 69 | Set() |
michael@0 | 70 | }; |
michael@0 | 71 | var Set = function () {}; |
michael@0 | 72 | for (var x = 0; x < 5; x++) { |
michael@0 | 73 | Set = function (z) { |
michael@0 | 74 | return function () { |
michael@0 | 75 | [z] |
michael@0 | 76 | } |
michael@0 | 77 | } (x) |
michael@0 | 78 | } |
michael@0 | 79 | }).toSource() + ")()"; |
michael@0 | 80 | })(); |
michael@0 | 81 | evalWithCache(test, { assertEqBytecode: true }); |
michael@0 | 82 | |
michael@0 | 83 | // Code an arrow function, and execute it. |
michael@0 | 84 | test = (function () { |
michael@0 | 85 | function f() { |
michael@0 | 86 | var g = (a) => a + a; |
michael@0 | 87 | return g; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | return f.toSource() + "; f()(1);"; |
michael@0 | 91 | })(); |
michael@0 | 92 | evalWithCache(test, { assertEqBytecode: true, assertEqResult : true }); |
michael@0 | 93 | |
michael@0 | 94 | // Code an arrow function, and do not execute it. |
michael@0 | 95 | test = (function () { |
michael@0 | 96 | function f() { |
michael@0 | 97 | var g = (a) => a + a; |
michael@0 | 98 | return g; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | return f.toSource() + "; f();"; |
michael@0 | 102 | })(); |
michael@0 | 103 | evalWithCache(test, { assertEqBytecode: true }); |
michael@0 | 104 | |
michael@0 | 105 | // Ensure that decoded functions can be relazified. |
michael@0 | 106 | test = "function f() { }; f();" |
michael@0 | 107 | + "assertEq(isLazyFunction(f), false);" |
michael@0 | 108 | + "var expect = isRelazifiableFunction(f);"; |
michael@0 | 109 | checkAfter = function (ctx) { |
michael@0 | 110 | gc(ctx.global.f); // relazify f, if possible. |
michael@0 | 111 | evaluate("assertEq(isLazyFunction(f), expect);", ctx); |
michael@0 | 112 | }; |
michael@0 | 113 | evalWithCache(test, { |
michael@0 | 114 | assertEqBytecode: true, // Check that we re-encode the same thing. |
michael@0 | 115 | assertEqResult: true, // The function should remain relazifiable, if it was |
michael@0 | 116 | // during the first run. |
michael@0 | 117 | checkAfter: checkAfter // Check that relazifying the restored function works |
michael@0 | 118 | // if the original was relazifiable. |
michael@0 | 119 | }); |
michael@0 | 120 | |
michael@0 | 121 | // Ensure that decoded functions can be relazified, even if they have free |
michael@0 | 122 | // variables. |
michael@0 | 123 | test = "function f() { return isRelazifiableFunction(f) }; var expect = f();" |
michael@0 | 124 | + "assertEq(isLazyFunction(f), false);" |
michael@0 | 125 | + "expect"; |
michael@0 | 126 | checkAfter = function (ctx) { |
michael@0 | 127 | gc(ctx.global.f); // relazify f, if possible. |
michael@0 | 128 | evaluate("assertEq(isLazyFunction(f), expect);", ctx); |
michael@0 | 129 | }; |
michael@0 | 130 | evalWithCache(test, { |
michael@0 | 131 | assertEqBytecode: true, // Check that we re-encode the same thing. |
michael@0 | 132 | assertEqResult: true, // The function should remain relazifiable, if it was |
michael@0 | 133 | // during the first run. |
michael@0 | 134 | checkAfter: checkAfter // Check that relazifying the restored function works |
michael@0 | 135 | // if the original was relazifiable. |
michael@0 | 136 | }); |