Skip to content

Instantly share code, notes, and snippets.

@ww24
Created February 6, 2014 05:51
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 ww24/8838967 to your computer and use it in GitHub Desktop.
Save ww24/8838967 to your computer and use it in GitHub Desktop.
V8 で行番号を取得する。
// test1
console.log(getLineNumber());
// test2
var test = (function test() {
console.log(getLineNumber());
return test;
})();
var obj = {
hoge: test,
fuga: function () {
console.log(getLineNumber());
}
};
// test3
obj.hoge();
// test4
obj.fuga();
function customPrepareStackTrace(error, structuredStackTrace) {
var trace = structuredStackTrace[0];
return {
// method name
name: trace.getMethodName() || trace.getFunctionName() || "<anonymous>",
// file name
file: trace.getFileName(),
// line number
line: trace.getLineNumber(),
// column number
column: trace.getColumnNumber()
};
}
function getLineNumber() {
var original = Error.prepareStackTrace,
error = {};
Error.captureStackTrace(error, getLineNumber);
Error.prepareStackTrace = customPrepareStackTrace;
var stack = error.stack;
Error.prepareStackTrace = original;
return stack;
}
@ww24
Copy link
Author

ww24 commented Feb 6, 2014

example の結果

{ name: '<anonymous>',
  file: 'example.js',
  line: 2,
  column: 13 }
{ name: 'test',
  file: 'example.js',
  line: 6,
  column: 15 }
{ name: 'hoge',
  file: 'example.js',
  line: 6,
  column: 15 }
{ name: 'fuga',
  file: 'example.js',
  line: 13,
  column: 17 }

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment