且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

V8 FunctionTemplate类实例

更新时间:2023-11-13 13:01:16

你正试图绑定两个东西:实例和方法来调用它,并使它看起来像一个函数指针。这不幸的是不工作在C ++。您只能将指针绑定到平滑函数或 static 方法。所以图像你添加一个静态的RegisterCB方法并注册它作为回调:

You're trying to bind two things at once: the instance and the method to invoke on it, and have it look like a function pointer. That unfortunately doesn't work in C++. You can only bind a pointer to a plain function or a static method. So image you add a static "RegisterCB" method and register it as the callback:

static Handle<Value> RegisterCB(const Arguments& args);
...FunctionTemplate::New(&PluginManager::RegisterCB)...

现在你从哪里得到pluginManagerInstance?为此,V8中的大多数回调注册apis都有一个附加的data参数,它将被传递回回调。 FunctionTemplate :: New也是如此。所以你其实想要这样绑定:

Now where do you get the pluginManagerInstance from? For this purpose, most callback-registration apis in V8 have an additional "data" parameter that will get passed back to the callback. So does FunctionTemplate::New. So you actually want to bind it like this:

...FunctionTemplate::New(&PluginManager::RegisterCB,
                         External::Wrap(pluginManagerInstance))...

.Data(),你可以委托给实际的方法:

The data is then available through args.Data() and you can delegate to the actual method:

return ((PluginManager*)External::Unwrap(args.Data())->Register(args);

一些宏。