Created
October 20, 2010 15:42
-
-
Save bmeck/636657 to your computer and use it in GitHub Desktop.
stack trace api fun for the masses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stack_holder = new Error | |
function stackPrepare(e,stacks) { | |
var stack = stacks | |
for(var p in stack[0]) { | |
stack[p] = stack[0][p] | |
} | |
stack.find = function(fn) { | |
console.log(stack) | |
for(var i=0;i<stack.length;i++) { | |
if(stack[i].getFunction() === fn) { | |
return i | |
} | |
} | |
return -1 | |
} | |
return stack | |
}; | |
Object.defineProperty(global,'__stack', {get:function getStack(){ | |
var old = Error.prepareStackTrace; | |
Error.prepareStackTrace = stackPrepare; | |
Error.captureStackTrace(stack_holder, getStack); | |
var stack = stack_holder.stack; | |
Error.prepareStackTrace = old; | |
return stack; | |
}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
return (function() { | |
console.log(__stack.find(foo)) | |
})() | |
} | |
console.log(foo()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment