bytecode-cache.js (1712B)
1 function evalWithCache(code, ctx) { 2 ctx = ctx || {}; 3 ctx = Object.create(ctx, { 4 fileName: { value: "evalWithCacheCode.js" }, 5 lineNumber: { value: 0 } 6 }); 7 code = code instanceof Object ? code : cacheEntry(code); 8 9 var collectDelazifications = ctx.collectDelazifications || false; 10 11 // We create a new global ... 12 if (!("global" in ctx)) 13 ctx.global = newGlobal({newCompartment: ctx.newCompartment}); 14 15 var ctx_save = Object.create(ctx, { 16 saveBytecodeWithDelazifications: { value: true } 17 }); 18 19 // Fetch the verification function from the evaluation context. This function 20 // is used to assert the state of the script/function after each run of the 21 // evaluate function. 22 var checkAfter = ctx.checkAfter || function(ctx) {}; 23 24 // The generation counter is used to represent environment variations which 25 // might cause the program to run differently, and thus to have a different 26 // set of functions executed. 27 ctx.global.generation = 0; 28 var res1 = evaluate(code, ctx_save); 29 checkAfter(ctx); 30 31 ctx.global.generation = 1; 32 var res2 = evaluate(code, Object.create(ctx_save, {loadBytecode: { value: true } })); 33 checkAfter(ctx); 34 35 ctx.global.generation = 2; 36 var res3 = evaluate(code, Object.create(ctx, {loadBytecode: { value: true } })); 37 checkAfter(ctx); 38 39 ctx.global.generation = 3; 40 var res0 = evaluate(code, ctx); 41 checkAfter(ctx); 42 43 if (ctx.assertEqResult) { 44 assertEq(res0, res1); 45 assertEq(res0, res2); 46 assertEq(res0, res3); 47 } 48 49 if (ctx.checkFrozen) { 50 assertEq(Object.isFrozen(res0), Object.isFrozen(res1)); 51 assertEq(Object.isFrozen(res0), Object.isFrozen(res2)); 52 assertEq(Object.isFrozen(res0), Object.isFrozen(res3)); 53 } 54 }