withSourceHook.js (1917B)
1 // |jit-test| skip-if: typeof withSourceHook !== 'function' 2 3 // Check that withSourceHook passes URLs, propagates exceptions, and 4 // properly restores the original source hooks. 5 6 load(libdir + 'asserts.js'); 7 8 var log = ''; 9 10 // Establish an outermost source hook. 11 withSourceHook(function (url) { 12 log += 'o'; 13 assertEq(url, 'outer'); 14 return '(function outer() { 3; })'; 15 }, function () { 16 log += 'O'; 17 // Verify that withSourceHook propagates exceptions thrown by source hooks. 18 assertThrowsValue(function () { 19 // Establish a source hook that throws. 20 withSourceHook(function (url) { 21 log += 'm'; 22 assertEq(url, 'middle'); 23 throw 'borborygmus'; // middle 24 }, function () { 25 log += 'M'; 26 // Establish an innermost source hook that does not throw, 27 // and verify that it is in force. 28 assertEq(withSourceHook(function (url) { 29 log += 'i'; 30 assertEq(url, 'inner'); 31 return '(function inner() { 1; })'; 32 }, function () { 33 log += 'I'; 34 return evaluate('(function inner() { 2; })', 35 { fileName: 'inner', sourceIsLazy: true }) 36 .toString(); 37 }), 38 'function inner() { 1; }'); 39 // Verify that the source hook that throws has been reinstated. 40 evaluate('(function middle() { })', 41 { fileName: 'middle', sourceIsLazy: true }) 42 .toString(); 43 }); 44 }, 'borborygmus'); 45 46 // Verify that the outermost source hook has been restored. 47 assertEq(evaluate('(function outer() { 4; })', 48 { fileName: 'outer', sourceIsLazy: true }) 49 .toString(), 50 'function outer() { 3; }'); 51 }); 52 53 assertEq(log, 'OMIimo');