Created
April 11, 2014 12:45
-
-
Save imposibrus/10465781 to your computer and use it in GitHub Desktop.
Accessing line number in V8 JavaScript (Chrome & Node.js)
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
// from http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js/11386493#11386493 | |
Object.defineProperty(global, '__stack', { | |
get: function(){ | |
var orig = Error.prepareStackTrace; | |
Error.prepareStackTrace = function(_, stack){ return stack; }; | |
var err = new Error; | |
Error.captureStackTrace(err, arguments.callee); | |
var stack = err.stack; | |
Error.prepareStackTrace = orig; | |
return stack; | |
} | |
}); | |
Object.defineProperty(global, '__line', { | |
get: function(){ | |
return __stack[1].getLineNumber(); | |
} | |
}); | |
console.log(__line); // show 21 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment