throw-exception-stack.js (2448B)
1 // Simple tests for getExceptionInfo behavior. 2 function testTestingFunction() { 3 let vals = [{}, 1, "foo", null, undefined]; 4 for (let v of vals) { 5 let thrower = () => { throw v; }; 6 let info = getExceptionInfo(thrower); 7 assertEq(info.exception, v); 8 assertEq(info.stack.includes("thrower@"), true); 9 } 10 11 // Returns null if there was no exception. 12 assertEq(getExceptionInfo(() => 123), null); 13 14 // OOM exceptions don't have a stack trace. 15 let info = getExceptionInfo(throwOutOfMemory); 16 assertEq(info.exception, "out of memory"); 17 assertEq(info.stack, null); 18 } 19 testTestingFunction(); 20 21 /** 22 * Check that the expected number of stack traces are generated for a given 23 * global where 100 "throws" are generated 24 */ 25 function assertStacksCount(global, expectedStacksCount) { 26 global.evaluate("(" + function(_expectedStacksCount) { 27 let thrower = () => { throw 123; }; 28 for (let i = 0; i < 100; i++) { 29 let info = getExceptionInfo(thrower); 30 assertEq(info.exception, 123); 31 // NOTE: if this ever gets increased, update the tests above too! 32 if (i <= _expectedStacksCount) { 33 assertEq(info.stack.includes("thrower@"), true); 34 } else { 35 assertEq(info.stack, null); 36 } 37 } 38 } + `)(${expectedStacksCount})`); 39 } 40 41 // Debuggee globals always get an exception stack. 42 function testDebuggee() { 43 let g = newGlobal({newCompartment: true}); 44 let dbg = new Debugger(g); 45 assertStacksCount(g, 100); 46 } 47 testDebuggee(); 48 49 // Globals with trusted principals always get an exception stack. 50 function testTrustedPrincipals() { 51 let g = newGlobal({newCompartment: true, systemPrincipal: true}); 52 assertStacksCount(g, 100); 53 } 54 testTrustedPrincipals(); 55 56 // In normal cases, a stack is captured only for the first 50 exceptions per realm. 57 function testNormal() { 58 let g = newGlobal(); 59 assertStacksCount(g, 50); 60 } 61 testNormal(); 62 63 // Non debuggee with unlimited stacks capturing enabled should always get a stack. 64 function testEnableUnlimitedStacksCapturing() { 65 let dbg = new Debugger(); 66 let g = newGlobal(); 67 dbg.enableUnlimitedStacksCapturing(g); 68 assertStacksCount(g, 100); 69 70 dbg.disableUnlimitedStacksCapturing(g); 71 assertStacksCount(g, 50); 72 73 dbg.enableUnlimitedStacksCapturing(g); 74 assertStacksCount(g, 100); 75 } 76 testEnableUnlimitedStacksCapturing();