使用Node.js附加组件实例化和返回C ++包装的对象

use*_*681 5 node.js

我正在使用Node 0.12并尝试创建插件。

我有两个包装好的c ++类,A和B。在类A的一个方法中,我想返回包装的B实例。因此,例如,类A具有如下所示的缩写接口。在A类的方法Api中,我想实例化并返回B类包装的对象。我尝试使用NewInstance(),但不确定如何在NewInstance()为类B定义的方法中调用构造函数。我们将不胜感激您提供的任何帮助。

先感谢您。

-乔恩

class A : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);

private:
explicit A(Connection *connection);
~B();

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Api(const v8::FunctionCallbackInfo<v8::Value>& args);

static v8::Persistent<v8::Function> constructor;

Connection* _connection;
};

#endif

class B : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);


private:

~B();

static v8::Persistent<v8::Function> constructor;
// The method that will construct & return the new JavaScript object

Event* _event;
};

#endif
Run Code Online (Sandbox Code Playgroud)

这是我尝试做的事情NewInstance()

void B::NewInstance(const v8::internal::Arguments& args) {
 Isolate* isolate = Isolate::GetCurrent();
 HandleScope scope(isolate);

 const unsigned argc = 1;
 Local<Function> cons = Local<Function>::New(isolate, constructor);
 Local<Object> instance = cons->NewInstance(argc, argv);

 args.GetReturnValue().Set(instance);
}
Run Code Online (Sandbox Code Playgroud)