1

as a follow up to the question how-to-determine-source-information-of-callback-in-v8, I have the following question.

If I look at the properties of a function, I see that it has a name, a length etc. Would it be possible to automatically add a property to all functions by hacking the constructor of the 'Function' object? If so, how should this be done? I would like to add a property called 'source_location'

function foo() {
}

console.log(foo.name); //works out of the box
console.log(foo.source_location); //can I make this work?
Community
  • 1
  • 1
Corno
  • 5,448
  • 4
  • 25
  • 41
  • 1
    but `console.log` shows source location already ? – c69 Jan 18 '12 at 13:03
  • does it? can you give an example? – Corno Jan 18 '12 at 13:58
  • [Chrome](http://gyazo.com/69282b23aa0e4fe96958a7120e9bd644.png) , [Opera](http://gyazo.com/d47e3e0848b613349ead9ece61278428.png), same with Firefox and IE9. – c69 Jan 18 '12 at 14:28
  • [`Function.caller`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/caller) is non-standard but is definitely part of V8 so should be available to you. – clockworkgeek Jan 18 '12 at 20:30
  • 1
    That's not a solution to the question I asked. I want to know the source_location of a function. not a caller. In the example I gave, there isn't even a caller. – Corno Jan 19 '12 at 07:40

1 Answers1

0

You could use v8's debugger object.

Source

// test.js

function bar() {
}

function foo() {
}

foo.source_location = debug.Debug.findFunctionSourceLocation(foo);

console.log(foo.name);
console.log(foo.source_location);
console.log(foo.source_location.script.name);

Execute

node --expose-debug-as=debug test.js

Output

foo 
{ script: {}, // script object 
  position: 106,
  line: 5,
  column: 12,
  start: 94,
  end: 110 }
/home/skomski/test.js

References

http://code.google.com/p/v8/source/browse/trunk/src/debug-debugger.js#586

Skomski
  • 4,827
  • 21
  • 30