0

Is it possible to determine the source information (file, line number, column number) of a callback in v8?

function foo(callback) {
    var x = callback();
    if (typeof x !== "string") {
        //hmmm, x is not as expected, I want to know more about the callback.
    }
}
Corno
  • 5,448
  • 4
  • 25
  • 41

2 Answers2

1

callsite helps with this.

It exposes __stack global variable which can be used to get the stack trace. And __line which is the current line number.

fent
  • 17,861
  • 15
  • 87
  • 91
  • I don't see how. Can you post an example? – Corno Jan 15 '12 at 21:17
  • Actually I'm not sure if it can do what you want. What do you want exactly? To know what file the callback was defined and what line number? – fent Jan 15 '12 at 22:12
  • yes, that's what I want. If it is necessary to hack the constructor of 'Function' to add the source info to all functions, then that would be okay, but I don't know how. – Corno Jan 15 '12 at 22:16
  • It's amazing this answer doesn't have more attention. – james_womack Jul 06 '12 at 23:31
0

I'm not aware that you can do this in JavaScript, but if you can debug it, it should show you the file/line etc..

I guess doing that would depend exactly on your setup and I've never done it myself - it looks like node (uses v8) has some sort of support for that .

Might be useful?: v8 DebuggerProtocol

Adam
  • 5,091
  • 5
  • 32
  • 49
  • I might indeed use this in the following way: if x is not as expected, first have a debugger statement and then call the same callback again. – Corno Jan 15 '12 at 18:27
  • Apparently, there's some support in IDEs too, like Cloud9 (web based). Not tried it to be honest but could be worth a shot. http://stackoverflow.com/questions/5652972/ide-and-debugger-for-node-js – Adam Jan 15 '12 at 19:19