三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 293|回复: 0
打印 上一主题 下一主题

C++插件-包装对象工厂

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-8-8 08:10:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
当你想创建本地对象,又不想在 JavaScript 中严格的使用 new 初始化的时候,以下方法非常实用。
  1. var obj = addon.createObject();
  2. // instead of:
  3. // var obj = new addon.Object();
复制代码
在 addon.cc 中注册 createObject 方法:
  1. // addon.cc
  2. #include <node.h>
  3. #include "myobject.h"

  4. using namespace v8;

  5. void CreateObject(const FunctionCallbackInfo<Value>& args) {
  6.   Isolate* isolate = Isolate::GetCurrent();
  7.   HandleScope scope(isolate);
  8.   MyObject::NewInstance(args);
  9. }

  10. void InitAll(Handle<Object> exports, Handle<Object> module) {
  11.   MyObject::Init();

  12.   NODE_SET_METHOD(module, "exports", CreateObject);
  13. }

  14. NODE_MODULE(addon, InitAll)
复制代码
在 myobject.h 中有静态方法 NewInstance,他能实例化对象 (它就像 JavaScript 的 new):
  1. // myobject.h
  2. #ifndef MYOBJECT_H
  3. #define MYOBJECT_H

  4. #include <node.h>
  5. #include <node_object_wrap.h>

  6. class MyObject : public node::ObjectWrap {
  7. public:
  8.   static void Init();
  9.   static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);

  10. private:
  11.   explicit MyObject(double value = 0);
  12.   ~MyObject();

  13.   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
  14.   static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
  15.   static v8::Persistent<v8::Function> constructor;
  16.   double value_;
  17. };

  18. #endif
复制代码
这个实现方法和 myobject.cc 类似:
  1. // myobject.cc
  2. #include <node.h>
  3. #include "myobject.h"

  4. using namespace v8;

  5. Persistent<Function> MyObject::constructor;

  6. MyObject::MyObject(double value) : value_(value) {
  7. }

  8. MyObject::~MyObject() {
  9. }

  10. void MyObject::Init() {
  11.   Isolate* isolate = Isolate::GetCurrent();
  12.   // Prepare constructor template
  13.   Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
  14.   tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
  15.   tpl->InstanceTemplate()->SetInternalFieldCount(1);

  16.   // Prototype
  17.   NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);

  18.   constructor.Reset(isolate, tpl->GetFunction());
  19. }

  20. void MyObject::New(const FunctionCallbackInfo<Value>& args) {
  21.   Isolate* isolate = Isolate::GetCurrent();
  22.   HandleScope scope(isolate);

  23.   if (args.IsConstructCall()) {
  24.     // Invoked as constructor: `new MyObject(...)`
  25.     double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
  26.     MyObject* obj = new MyObject(value);
  27.     obj->Wrap(args.This());
  28.     args.GetReturnValue().Set(args.This());
  29.   } else {
  30.     // Invoked as plain function `MyObject(...)`, turn into construct call.
  31.     const int argc = 1;
  32.     Local<Value> argv[argc] = { args[0] };
  33.     Local<Function> cons = Local<Function>::New(isolate, constructor);
  34.     args.GetReturnValue().Set(cons->NewInstance(argc, argv));
  35.   }
  36. }

  37. void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
  38.   Isolate* isolate = Isolate::GetCurrent();
  39.   HandleScope scope(isolate);

  40.   const unsigned argc = 1;
  41.   Handle<Value> argv[argc] = { args[0] };
  42.   Local<Function> cons = Local<Function>::New(isolate, constructor);
  43.   Local<Object> instance = cons->NewInstance(argc, argv);

  44.   args.GetReturnValue().Set(instance);
  45. }

  46. void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
  47.   Isolate* isolate = Isolate::GetCurrent();
  48.   HandleScope scope(isolate);

  49.   MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
  50.   obj->value_ += 1;

  51.   args.GetReturnValue().Set(Number::New(isolate, obj->value_));
  52. }
复制代码
测试:
  1. // test.js
  2. var createObject = require('./build/Release/addon');

  3. var obj = createObject(10);
  4. console.log( obj.plusOne() ); // 11
  5. console.log( obj.plusOne() ); // 12
  6. console.log( obj.plusOne() ); // 13

  7. var obj2 = createObject(20);
  8. console.log( obj2.plusOne() ); // 21
  9. console.log( obj2.plusOne() ); // 22
  10. console.log( obj2.plusOne() ); // 23
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-6-27 00:21 , Processed in 0.027701 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表