Skip to content

Instantly share code, notes, and snippets.

@junosuarez
Created October 4, 2012 02:49
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 junosuarez/3831191 to your computer and use it in GitHub Desktop.
Save junosuarez/3831191 to your computer and use it in GitHub Desktop.
V8 reflection
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