V8 FunctionTemplate类实例 [英] V8 FunctionTemplate Class Instance

查看:255
本文介绍了V8 FunctionTemplate类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

class PluginManager
{
public:
    Handle<Value> Register(const Arguments& args);
    Handle<ObjectTemplate> GetObjectTemplate();
};  

我想从JavaScript访问Register方法。我把它添加到全局对象像这样:

I want the Register method to be accessible from JavaScript. I add it to the global object like this:

PluginManager pluginManagerInstance;

global->Set(String::New("register"), FunctionTemplate::New(pluginManagerInstance.Register)); 

它会引发以下错误:


'PluginManager :: Register':function
调用缺少参数列表;使用
'& PluginManager :: Register'创建一个
指向成员的指针

'PluginManager::Register': function call missing argument list; use '&PluginManager::Register' to create a pointer to member

那,但它也不工作。这不正确,因为我想要它调用 pluginManagerInstance 的Register方法。

I tried to do that, but it doesn't work either. And it's not correct, because I want it to call the Register method of the pluginManagerInstance.

除了使Register方法为静态或全局外,任何想法?

Except for making the Register method static or global, any ideas?

b $ b

推荐答案

你正试图绑定两个东西:实例和方法来调用它,并使它看起来像一个函数指针。这不幸的是不工作在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);

一些宏。

这篇关于V8 FunctionTemplate类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆