Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all values of the closure in node.js or V8

For example, if we assume the following code:

var f = function() { return 'hello world' };
var x = 10;
var y = 314;

var g = function() {
    var buf = [], xx = x;
    while (xx--)
        buf.append(f() + ' ');
    return buf.join('');
}

I can get the actual "code" as a string of g with g.toString(). However, this does not (obviously) get f and x—members of the closure of g (sorry if I'm not quite using these terms correctly.)

Is there some way to query a function for what its closure contains? Ideally I could get an object like:

{ 'f' : f, 'x': x } // note that `y` is not here

If I have to drop into C++ for special interactions with V8, that's okay—although somehow doing this in pure JavaScript would be best.


I know that this is a bit of an odd question—but I do have a legitimate reason for wanting this!

like image 966
Aaron Yodaiken Avatar asked Aug 15 '11 03:08

Aaron Yodaiken


1 Answers

I found this discussion of V8 closure implementations enlightening. It sounds like the C++ object you're looking for is a Context.

like image 154
hurrymaplelad Avatar answered Oct 18 '22 21:10

hurrymaplelad