2

是否可以设置当用户想要调用不存在的函数时调用的回退回调?例如

my_object.ThisFunctionDoesNotExists(2, 4);

现在我希望调用一个函数,其中第一个参数是名称和一个带有传递参数的堆栈(或类似的东西)。澄清一下,回退回调应该是一个 C++ 函数。

4

1 回答 1

1

假设您的问题是关于从标签推断的嵌入式 V8 引擎,您可以使用 Harmony Proxies 功能:

var A = Proxy.create({
    get: function (proxy, name) {
        return function (param) {
            console.log(name, param);
        }
    }
});

A.hello('world');  // hello world

使用--harmony_proxiesparam 启用此功能。从 C++ 代码:

static const char v8_flags[] = "--harmony_proxies";
v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);

另一种方式:

有一个v8::ObjectTemplate调用方法,SetNamedPropertyHandler因此您可以拦截属性访问。例如:

void GetterCallback(v8::Local<v8::String> property,
    const v8::PropertyCallbackInfo<v8::Value>& info)
{
    // This will be called on property read
    // You can return function here to call it
}
...

object_template->SetNamedPropertyHandler(GetterCallback);
于 2014-01-15T10:47:35.250 回答