Skip to content

Instantly share code, notes, and snippets.

@ryanhanwu
Forked from bmeck/__stack.js
Created April 20, 2012 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhanwu/2426625 to your computer and use it in GitHub Desktop.
Save ryanhanwu/2426625 to your computer and use it in GitHub Desktop.
stack trace api fun for the masses.
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;
}});
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