获取node.js或V8中闭包的所有值 [英] 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('');
}
我可以将实际的代码作为<$ c $的字符串获取c> g ,带 g.toString()
。但是,这显然不会(显然)获得 f
和 x
- 关闭的成员g
(对不起,如果我没有正确使用这些术语。)
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
如果我必须使用C ++进行与V8的特殊交互,那没关系 - 虽然以某种方式在纯JavaScript中这样做是最好的。
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!
推荐答案
我找到了关于V8闭包实现的讨论具有启发性。听起来像您正在寻找的C ++对象是上下文 。
I found this discussion of V8 closure implementations enlightening. It sounds like the C++ object you're looking for is a Context.
这篇关于获取node.js或V8中闭包的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!