调用不可用函数时的回退回调



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

my_object.ThisFunctionDoesNotExists(2, 4);

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

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

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

使用--harmony_proxies参数启用此功能。从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);

最新更新