|
以下会创建一个 C++对象的包装MyObject,这样 他就能再 JavaScript 中用 new 实例化。首先在addon.cc中准备主要模块:
- // addon.cc
- #include <node.h>
- #include "myobject.h"
- using namespace v8;
- void InitAll(Handle<Object> exports) {
- MyObject::Init(exports);
- }
- NODE_MODULE(addon, InitAll)
复制代码 接着在 myobject.h 创建包装,它继承自 node::ObjectWrap:
- // myobject.h
- #ifndef MYOBJECT_H
- #define MYOBJECT_H
- #include <node.h>
- #include <node_object_wrap.h>
- class MyObject : public node::ObjectWrap {
- public:
- static void Init(v8::Handle<v8::Object> exports);
- private:
- explicit MyObject(double value = 0);
- ~MyObject();
- static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
- static v8::Persistent<v8::Function> constructor;
- double value_;
- };
- #endif
复制代码 在 myobject.cc 中实现各种暴露的方法,通过给构造函数添加 prototype 属性来暴露 plusOne 方法:
- // myobject.cc
- #include "myobject.h"
- using namespace v8;
- Persistent<Function> MyObject::constructor;
- MyObject::MyObject(double value) : value_(value) {
- }
- MyObject::~MyObject() {
- }
- void MyObject::Init(Handle<Object> exports) {
- Isolate* isolate = Isolate::GetCurrent();
- // Prepare constructor template
- Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
- tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
- tpl->InstanceTemplate()->SetInternalFieldCount(1);
- // Prototype
- NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
- constructor.Reset(isolate, tpl->GetFunction());
- exports->Set(String::NewFromUtf8(isolate, "MyObject"),
- tpl->GetFunction());
- }
- void MyObject::New(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = Isolate::GetCurrent();
- HandleScope scope(isolate);
- if (args.IsConstructCall()) {
- // Invoked as constructor: `new MyObject(...)`
- double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
- MyObject* obj = new MyObject(value);
- obj->Wrap(args.This());
- args.GetReturnValue().Set(args.This());
- } else {
- // Invoked as plain function `MyObject(...)`, turn into construct call.
- const int argc = 1;
- Local<Value> argv[argc] = { args[0] };
- Local<Function> cons = Local<Function>::New(isolate, constructor);
- args.GetReturnValue().Set(cons->NewInstance(argc, argv));
- }
- }
- void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = Isolate::GetCurrent();
- HandleScope scope(isolate);
- MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
- obj->value_ += 1;
- args.GetReturnValue().Set(Number::New(isolate, obj->value_));
- }
复制代码 测试:
- // test.js
- var addon = require('./build/Release/addon');
- var obj = new addon.MyObject(10);
- console.log( obj.plusOne() ); // 11
- console.log( obj.plusOne() ); // 12
- console.log( obj.plusOne() ); // 13
复制代码
|
|