| if (typeof Element == "undefined" && $vm) |
| Element = $vm.Element; |
| |
| if (!this.alert) { |
| debug = print; |
| description = print; |
| } |
| |
| description( |
| 'This test checks stack trace corectness in special cases.' |
| ); |
| |
| function stackTraceLineFor(stackTrace, frameIndex) { |
| var i = frameIndex; |
| var indexOfAt = stackTrace[i].indexOf('@') |
| var indexOfLastSlash = stackTrace[i].lastIndexOf('/'); |
| if (indexOfLastSlash == -1) |
| indexOfLastSlash = indexOfAt |
| var functionName = stackTrace[i].substring(0, indexOfAt); |
| var fileName = stackTrace[i].substring(indexOfLastSlash + 1); |
| return functionName + " at " + fileName; |
| } |
| |
| function printStack(stackTrace) { |
| debug("--> Stack Trace:") |
| stackTrace = stackTrace.split("\n"); |
| var length = Math.min(stackTrace.length, 100); |
| for (var i = 0; i < length; i++) |
| debug(" " + i + " " + stackTraceLineFor(stackTrace, i)); |
| debug(''); |
| } |
| |
| function dumpPattern(pattern) { |
| for (var i = 0; i < pattern.length; i++) |
| debug(" " + i + " " + pattern[i]); |
| } |
| |
| function matchesPatternAtLine(pattern, patternIndex, traceLine) { |
| var patternLine = pattern[patternIndex]; |
| return traceLine.slice(0, patternLine.length) == patternLine; |
| } |
| |
| function matchPattern(pattern, traceLine) { |
| for (var i = 0; i < pattern.length; i++) { |
| if (matchesPatternAtLine(pattern, i, traceLine)) |
| return i; |
| } |
| return -1; |
| } |
| |
| function checkStackForPattern(stackTrace, pattern) { |
| stackTrace = stackTrace.split("\n"); |
| var length = Math.min(stackTrace.length, 100); |
| |
| // Get the match in the pattern for the first line: |
| var firstStackTraceLine = stackTraceLineFor(stackTrace, 0); |
| var patternIndex = matchPattern(pattern, firstStackTraceLine); |
| if (patternIndex < 0) { |
| debug("--> Stack Trace FAILED to match pattern:") |
| dumpPattern(pattern); |
| debug(''); |
| return; |
| } |
| |
| for (var i = 1; i < length; i++) { |
| patternIndex = ++patternIndex % pattern.length; |
| var traceLine = stackTraceLineFor(stackTrace, i); |
| if (!matchesPatternAtLine(pattern, patternIndex, traceLine)) { |
| debug("--> Stack Trace FAILED to match pattern:") |
| dumpPattern(pattern); |
| debug(''); |
| return; |
| } |
| } |
| |
| debug("--> Stack Trace matches pattern:") |
| dumpPattern(pattern); |
| debug(''); |
| } |
| |
| function hostThrower() { Element.prototype.appendChild.call({ }, [{ }]); } |
| function callbacker(f) { [0].map(f); } |
| function outer(errorName) { inner(errorName); } |
| function inner(errorName) { throw new Error("Error in " + errorName); } |
| function evaler(code) { eval(code); } |
| function normalOuter() { normalInner(); } |
| function normalInner() { if(thisVarDoesntExist) failIfTrue("shouldFailBeforeThis") }; |
| function scripterInner() { htmlInner(); } |
| function scripterOuter() { htmlOuter(); } |
| // Expected functions in stack trace |
| // Normal Case |
| try { normalOuter() } catch (e) { printStack(e.stack) } // normalOuter -> normalInner |
| |
| // Eval Case |
| try { evaler("inner('inner eval');"); } catch (e) { printStack(e.stack) } // evaler -> eval -> inner |
| try { evaler("outer('outer eval');"); } catch (e) { printStack(e.stack) } // evaler -> eval -> outer -> inner |
| |
| // Function Callback Case |
| try { callbacker(inner('inner map')); } catch (e) { printStack(e.stack); } // callbacker -> map -> inner |
| try { callbacker(outer('outer map')); } catch (e) { printStack(e.stack); } // callbacker -> map -> outer -> inner |
| |
| // Host Code Case |
| try { hostThrower(); } catch (e) { printStack(e.stack); } // hostThrower |
| |
| try { scripterInner(); } catch (e) { printStack(e.stack) } // program -> scripter -> inner |
| try { scripterOuter(); } catch (e) { printStack(e.stack) } // program -> scripter -> outer -> inner |
| |
| function selfRecursive1() { selfRecursive1(); |
| } |
| |
| |
| try { selfRecursive1(); } catch (e) { printStack(e.stack) } // selfRecursive1 -> selfRecursive1 -> selfRecursive1 -> selfRecursive1 ... |
| |
| function selfRecursive2() { |
| // A little work to make the DFG kick in |
| for (var i = 0; i < 10; i++) { |
| if (i == 9) |
| selfRecursive2(); |
| } |
| } |
| |
| try { selfRecursive2(); } catch (e) { printStack(e.stack) } // selfRecursive2 -> selfRecursive2 -> selfRecursive2 -> selfRecursive2 ... |
| |
| function selfRecursive3() { |
| eval("selfRecursive3()"); |
| } |
| |
| try { |
| selfRecursive3(); |
| } catch (e) { |
| var pattern = [ |
| "eval code at ", |
| "eval at [native code]", |
| "selfRecursive3 at stack-trace.js" |
| ]; |
| checkStackForPattern(e.stack, pattern); |
| } |
| |
| var callCount = 0; |
| |
| function throwError() { |
| throw new Error(); |
| } |
| |
| var object = { |
| get getter1() { |
| var o = { |
| valueOf: function() { |
| throwError() |
| } |
| }; |
| +o; |
| }, |
| get getter2() { |
| var o = { |
| valueOf: throwError |
| }; |
| +o; |
| }, |
| get getter3() { |
| var o1 = { |
| valueOf: throwError |
| }; |
| var o2 = { |
| valueOf: function () { |
| throwError(); |
| } |
| }; |
| if (callCount == 9998) |
| +o1; |
| if (callCount == 9999) |
| +o2; |
| }, |
| nonInlineable : function () { |
| if (0) return [arguments, function(){}]; |
| ++callCount; |
| if (callCount == 1) { |
| this.getter1; |
| } else if (callCount == 2) { |
| this.getter2; |
| } else { |
| this.getter3; |
| } |
| }, |
| inlineable : function () { |
| this.nonInlineable(); |
| } |
| } |
| |
| function yetAnotherInlinedCall(o) { |
| o.inlineable(); |
| } |
| |
| function makeInlinableCall(o) { |
| for (var i = 0; i < 10000; i++) { |
| new yetAnotherInlinedCall(o); |
| } |
| } |
| for (var k = 0; k < 4; k++) { |
| try { |
| function g() { |
| var j = 0; |
| for (var i = 0; i < 1000; i++) { |
| j++; |
| makeInlinableCall(object); |
| } |
| } |
| [1].map(g); |
| } catch (e) { |
| printStack(e.stack); |
| } |
| } |
| |
| function h() { |
| if (callCount++ == 1000) |
| throw new Error(); |
| if (callCount > 1000) { |
| [].map.apply(undefined, throwError); |
| } |
| } |
| |
| function mapTest(a) { |
| a.map(h); |
| } |
| |
| function mapTestDriver() { |
| var a = [1,2,3]; |
| for (var i = 0; i < 2000; i++) |
| mapTest(a); |
| } |
| |
| try { |
| callCount = 0; |
| mapTestDriver() |
| } catch(e) { |
| printStack(e.stack); |
| } |
| |
| try { |
| mapTestDriver() |
| } catch(e) { |
| printStack(e.stack); |
| } |
| |
| var dfgFunctionShouldThrow = false; |
| function dfgFunction() { |
| if (dfgFunctionShouldThrow) { |
| dfgFunctionShouldThrow = false; |
| throwError(); |
| } |
| } |
| |
| for (var k = 0; k < 1000; k++) |
| dfgFunction(); |
| |
| try { |
| dfgFunctionShouldThrow = true; |
| [1,2,3,4].map(dfgFunction); |
| } catch (e) { |
| printStack(e.stack); |
| } |
| |
| try { |
| var o = { }; |
| o.__defineGetter__("g", dfgFunction); |
| function f(o) { |
| o.g; |
| } |
| for (var k = 0; k < 1000; k++) |
| f(o); |
| |
| dfgFunctionShouldThrow = true; |
| f(o); |
| |
| } catch (e) { |
| printStack(e.stack); |
| } |
| |
| var someValue = null; |
| |
| function callNonCallable() { |
| someValue(); |
| } |
| |
| for (var i = 0; i < 100; i++) { |
| try { |
| callNonCallable(); |
| } catch (e) { |
| } |
| } |
| |
| function dfgTest(f) { |
| dfgCount = 0; |
| while (dfgCount++ < 1000) { |
| try { |
| f(); |
| } catch (e) { |
| printStack(e.stack) |
| return; |
| } |
| } |
| } |
| |
| function inlineableThrow() { |
| if (dfgCount > 500) throw new Error(); |
| } |
| |
| var dfgThing = { |
| get willThrow() { |
| if (dfgCount > 500) |
| throw new Error(); |
| }, |
| get willThrowEventually() { |
| inlineableThrow(); |
| }, |
| willThrowFunc: function () { if (dfgCount > 500) throw new Error(); }, |
| willThrowEventuallyFunc: function () { inlineableThrow(); } |
| } |
| dfgThing.__defineGetter__("hostWillThrow", hostThrower); |
| |
| function dfg1() { |
| dfgThing.willThrow |
| } |
| |
| function dfg2() { |
| dfg1(); |
| } |
| |
| function dfg3() { |
| dfg2(); |
| } |
| |
| function dfg4() { |
| dfgThing.willThrowFunc(); |
| } |
| |
| function dfg5() { |
| dfg4(); |
| } |
| |
| function dfg6() { |
| dfg5(); |
| } |
| |
| function dfg7() { |
| dfgThing.willThrowEventually |
| } |
| |
| function dfg8() { |
| dfg7(); |
| } |
| |
| function dfg9() { |
| dfg8(); |
| } |
| |
| function dfga() { |
| dfgThing.willThrowEventuallyFunc(); |
| } |
| |
| function dfgb() { |
| dfga(); |
| } |
| |
| function dfgc() { |
| dfgb(); |
| } |
| |
| dfgTest(dfg3) |
| dfgTest(dfg6) |
| dfgTest(dfg9) |
| dfgTest(dfgc) |
| |
| successfullyParsed = true; |