Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created December 12, 2016 05:58
Show Gist options
  • Save mooyoul/1d27223fdcdffb8095cc42e10bb2abbf to your computer and use it in GitHub Desktop.
Save mooyoul/1d27223fdcdffb8095cc42e10bb2abbf to your computer and use it in GitHub Desktop.
Getting line number on Runtime in Node.js (or other V8 related projects)
/**
* Bla bla bla...
* some comments..
*/
require('./stack');
console.log('now: ', __lineno);
console.log('now: ', __lineno);
// @see https://github.com/v8/v8/wiki/Stack-Trace-API
Object.defineProperty(global, '__lineno', {
get: () => {
const orgFn = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const e = new Error('dummy');
const stack = e.stack;
Error.captureStackTrace(e, arguments.callee);
Error.prepareStackTrace = orgFn;
return stack[1].getLineNumber();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment