1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/xdr/lazy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +load(libdir + 'bytecode-cache.js'); 1.5 +var test = ""; 1.6 +var checkAfter; 1.7 + 1.8 +// code a function which has both used and unused inner functions. 1.9 +test = (function () { 1.10 + function f(x) { 1.11 + function ifTrue() { 1.12 + return true; 1.13 + }; 1.14 + function ifFalse() { 1.15 + return false; 1.16 + }; 1.17 + 1.18 + if (x) return ifTrue(); 1.19 + else return ifFalse(); 1.20 + } 1.21 + 1.22 + return f.toSource() + "; f(true)"; 1.23 +})(); 1.24 +evalWithCache(test, { assertEqBytecode: true, assertEqResult : true }); 1.25 + 1.26 +// code a function which uses different inner functions based on the generation. 1.27 +test = (function () { 1.28 + function f(x) { 1.29 + function ifTrue() { 1.30 + return true; 1.31 + }; 1.32 + function ifFalse() { 1.33 + return false; 1.34 + }; 1.35 + 1.36 + if (x) return ifTrue(); 1.37 + else return ifFalse(); 1.38 + } 1.39 + 1.40 + return f.toSource() + "; f((generation % 2) == 0)"; 1.41 +})(); 1.42 +evalWithCache(test, { }); 1.43 + 1.44 +// Code a function which has an enclosing scope. 1.45 +test = (function () { 1.46 + function f() { 1.47 + var upvar = ""; 1.48 + function g() { upvar += ""; return upvar; } 1.49 + return g; 1.50 + } 1.51 + 1.52 + return f.toSource() + "; f()();"; 1.53 +})(); 1.54 +evalWithCache(test, { assertEqBytecode: true, assertEqResult : true }); 1.55 + 1.56 +// Code a lazy function which has an enclosing scope. 1.57 +test = (function () { 1.58 + function f() { 1.59 + var upvar = ""; 1.60 + function g() { upvar += ""; return upvar; } 1.61 + return g; 1.62 + } 1.63 + 1.64 + return f.toSource() + "; f();"; 1.65 +})(); 1.66 +evalWithCache(test, { assertEqBytecode: true }); 1.67 + 1.68 +// (basic/bug535930) Code an enclosing scope which is a Call object. 1.69 +test = (function () { 1.70 + return "(" + (function () { 1.71 + p = function () { 1.72 + Set() 1.73 + }; 1.74 + var Set = function () {}; 1.75 + for (var x = 0; x < 5; x++) { 1.76 + Set = function (z) { 1.77 + return function () { 1.78 + [z] 1.79 + } 1.80 + } (x) 1.81 + } 1.82 + }).toSource() + ")()"; 1.83 +})(); 1.84 +evalWithCache(test, { assertEqBytecode: true }); 1.85 + 1.86 +// Code an arrow function, and execute it. 1.87 +test = (function () { 1.88 + function f() { 1.89 + var g = (a) => a + a; 1.90 + return g; 1.91 + } 1.92 + 1.93 + return f.toSource() + "; f()(1);"; 1.94 +})(); 1.95 +evalWithCache(test, { assertEqBytecode: true, assertEqResult : true }); 1.96 + 1.97 +// Code an arrow function, and do not execute it. 1.98 +test = (function () { 1.99 + function f() { 1.100 + var g = (a) => a + a; 1.101 + return g; 1.102 + } 1.103 + 1.104 + return f.toSource() + "; f();"; 1.105 +})(); 1.106 +evalWithCache(test, { assertEqBytecode: true }); 1.107 + 1.108 +// Ensure that decoded functions can be relazified. 1.109 +test = "function f() { }; f();" 1.110 + + "assertEq(isLazyFunction(f), false);" 1.111 + + "var expect = isRelazifiableFunction(f);"; 1.112 +checkAfter = function (ctx) { 1.113 + gc(ctx.global.f); // relazify f, if possible. 1.114 + evaluate("assertEq(isLazyFunction(f), expect);", ctx); 1.115 +}; 1.116 +evalWithCache(test, { 1.117 + assertEqBytecode: true, // Check that we re-encode the same thing. 1.118 + assertEqResult: true, // The function should remain relazifiable, if it was 1.119 + // during the first run. 1.120 + checkAfter: checkAfter // Check that relazifying the restored function works 1.121 + // if the original was relazifiable. 1.122 +}); 1.123 + 1.124 +// Ensure that decoded functions can be relazified, even if they have free 1.125 +// variables. 1.126 +test = "function f() { return isRelazifiableFunction(f) }; var expect = f();" 1.127 + + "assertEq(isLazyFunction(f), false);" 1.128 + + "expect"; 1.129 +checkAfter = function (ctx) { 1.130 + gc(ctx.global.f); // relazify f, if possible. 1.131 + evaluate("assertEq(isLazyFunction(f), expect);", ctx); 1.132 +}; 1.133 +evalWithCache(test, { 1.134 + assertEqBytecode: true, // Check that we re-encode the same thing. 1.135 + assertEqResult: true, // The function should remain relazifiable, if it was 1.136 + // during the first run. 1.137 + checkAfter: checkAfter // Check that relazifying the restored function works 1.138 + // if the original was relazifiable. 1.139 +});