Created
October 4, 2012 02:49
-
-
Save junosuarez/3831191 to your computer and use it in GitHub Desktop.
V8 reflection
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 reflection = { | |
getCallStack: function getCallStack() { | |
var old = Error.prepareStackTrace, e = {}; | |
Error.prepareStackTrace = function (e, cs) { return cs; }; | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(e, getCallStack); | |
e = e.stack; | |
e.shift(); | |
Error.prepareStackTrace = old; | |
} | |
return e; | |
}, | |
fnInfo: function (callsite) { | |
return { | |
name: function () { return callsite.getFunctionName(); }, | |
module: function () { return urlToModuleName(callsite.getScriptNameOrSourceURL()); }, | |
params: function () { return getParams(callsite.getFunction()); } | |
}; | |
}, | |
getCallingModule: function () { | |
var stack = reflection.getCallStack(); | |
if (stack.length) { | |
stack.shift(); | |
return reflection.fnInfo(stack[0]).module(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment