Created
December 12, 2016 05:58
-
-
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)
This file contains hidden or 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
/** | |
* Bla bla bla... | |
* some comments.. | |
*/ | |
require('./stack'); | |
console.log('now: ', __lineno); | |
console.log('now: ', __lineno); |
This file contains hidden or 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
// @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