C++ 插件#

插件Addons)是使用 C++ 编写的动态链接共享对象。require() 函数可以像加载普通 Node.js 模块一样加载插件。插件为 JavaScript 和 C/C++ 库之间提供了一个接口。

实现插件有三种选择:

除非需要直接访问未由 Node-API 暴露的功能,
否则请使用 Node-API。有关 Node-API 的更多信息,请参阅 使用 Node-API 的 C/C++ 插件

在不使用 Node-API 的情况下,实现插件会变得更加复杂,需要
了解多个组件和 API:

  • V8:Node.js 用来提供 JavaScript 实现的 C++ 库。它提供了创建对象、调用函数等机制。V8 的 API 主要记录在 v8.h 头文件(位于 Node.js 源码树的 deps/v8/include/v8.h)中,也可在线查阅。

  • libuv:实现 Node.js 事件循环、工作线程以及平台所有异步行为的 C 库。它还作为一个跨平台的抽象库,为所有主流操作系统提供了对许多常见系统任务(如与文件系统、套接字、定时器和系统事件交互)的简便、类似 POSIX 的访问方式。libuv 还提供了一种类似于 POSIX 线程的线程抽象,供需要超越标准事件循环的更复杂的异步插件使用。插件作者应避免通过 I/O 或其他耗时任务阻塞事件循环,而应通过 libuv 将工作卸载到非阻塞系统操作、工作线程或自定义的 libuv 线程中。

  • 内部 Node.js 库:Node.js 本身导出了一些 C++ API 供插件使用,其中最重要的是 node::ObjectWrap 类。

  • 其他静态链接的库(包括 OpenSSL):这些其他库位于 Node.js 源码树的 deps/ 目录中。只有 libuv、OpenSSL、V8 和 zlib 的符号被 Node.js 有意地重新导出,并且插件可以在不同程度上使用它们。更多信息请参阅链接到 Node.js 附带的库

以下所有示例都可下载,并可用作开发插件的起点。

Hello world#

这个 "Hello world" 示例是一个用 C++ 编写的简单插件,它等同于以下 JavaScript 代码:

module.exports.hello = () => 'world'; 

首先,创建文件 hello.cc

// hello.cc
#include <node.h>

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::String;
using v8::Value;

void Method(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(String::NewFromUtf8(
      isolate, "world", NewStringType::kNormal).ToLocalChecked());
}

void Initialize(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

}  // namespace demo 

所有 Node.js 插件都必须导出一个遵循以下模式的初始化函数:

void Initialize(Local<Object> exports);
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) 

NODE_MODULE 后面没有分号,因为它不是一个函数(参见 node.h)。

module_name 必须与最终二进制文件的文件名(不包括 .node 后缀)相匹配。

hello.cc 示例中,初始化函数是 Initialize,插件模块名是 addon

使用 node-gyp 构建插件时,将宏 NODE_GYP_MODULE_NAME 作为 NODE_MODULE() 的第一个参数,可以确保最终二进制文件的名称会被传递给 NODE_MODULE()

使用 NODE_MODULE() 定义的插件不能同时在多个上下文或多个线程中加载。

上下文感知插件#

在某些环境中,Node.js 插件可能需要在多个上下文中被多次加载。例如,Electron 运行时在单个进程中运行多个 Node.js 实例。每个实例都有自己的 require() 缓存,因此每个实例都需要一个原生插件在通过 require() 加载时能正确运行。这意味着插件必须支持多次初始化。

可以通过使用宏 NODE_MODULE_INITIALIZER 来构建一个上下文感知的插件,该宏会扩展为一个 Node.js 在加载插件时期望找到的函数名。因此,插件可以像下面的例子一样进行初始化:

using namespace v8;

extern "C" NODE_MODULE_EXPORT void
NODE_MODULE_INITIALIZER(Local<Object> exports,
                        Local<Value> module,
                        Local<Context> context) {
  /* Perform addon initialization steps here. */
} 

另一个选择是使用宏 NODE_MODULE_INIT(),它也会构建一个上下文感知的插件。与用于围绕给定插件初始化函数构建插件的 NODE_MODULE() 不同,NODE_MODULE_INIT() 用作此类初始化器的声明,后面跟着函数体。

在调用 NODE_MODULE_INIT() 后的函数体内,可以使用以下三个变量:

  • Local<Object> exports,
  • Local<Value> module,以及
  • Local<Context> context

构建上下文感知的插件需要仔细管理全局静态数据以确保稳定性和正确性。由于插件可能被多次加载,甚至可能来自不同的线程,因此插件中存储的任何全局静态数据都必须得到妥善保护,并且不能包含任何对 JavaScript 对象的持久引用。原因是 JavaScript 对象只在一个上下文中有效,当从错误的上下文或从创建它们的线程以外的线程访问时,很可能会导致崩溃。

可以通过执行以下步骤来构造上下文感知的插件以避免全局静态数据:

  • 定义一个类,用于存放每个插件实例的数据,并拥有一个形式如下的静态成员:
    static void DeleteInstance(void* data) {
      // Cast `data` to an instance of the class and delete it.
    } 
  • 在插件初始化器中,使用 new 关键字在堆上分配该类的一个实例。
  • 调用 node::AddEnvironmentCleanupHook(),将上面创建的实例和指向 DeleteInstance() 的指针传递给它。这将确保在环境拆卸时实例被删除。
  • 将类的实例存储在 v8::External 中,并且
  • 通过将 v8::External 传递给 v8::FunctionTemplate::New()v8::Function::New()(用于创建原生支持的 JavaScript 函数),将其传递给所有暴露给 JavaScript 的方法。v8::FunctionTemplate::New()v8::Function::New() 的第三个参数接受 v8::External,并使其在原生回调中通过 v8::FunctionCallbackInfo::Data() 方法可用。

这将确保每个插件实例的数据能够到达每个可以从 JavaScript 调用的绑定。每个插件实例的数据也必须传递到插件可能创建的任何异步回调中。

以下示例演示了上下文感知插件的实现:

#include <node.h>

using namespace v8;

class AddonData {
 public:
  explicit AddonData(Isolate* isolate):
      call_count(0) {
    // Ensure this per-addon-instance data is deleted at environment cleanup.
    node::AddEnvironmentCleanupHook(isolate, DeleteInstance, this);
  }

  // Per-addon data.
  int call_count;

  static void DeleteInstance(void* data) {
    delete static_cast<AddonData*>(data);
  }
};

static void Method(const v8::FunctionCallbackInfo<v8::Value>& info) {
  // Retrieve the per-addon-instance data.
  AddonData* data =
      reinterpret_cast<AddonData*>(info.Data().As<External>()->Value());
  data->call_count++;
  info.GetReturnValue().Set((double)data->call_count);
}

// Initialize this addon to be context-aware.
NODE_MODULE_INIT(/* exports, module, context */) {
  Isolate* isolate = Isolate::GetCurrent();

  // Create a new instance of `AddonData` for this instance of the addon and
  // tie its life cycle to that of the Node.js environment.
  AddonData* data = new AddonData(isolate);

  // Wrap the data in a `v8::External` so we can pass it to the method we
  // expose.
  Local<External> external = External::New(isolate, data);

  // Expose the method `Method` to JavaScript, and make sure it receives the
  // per-addon-instance data we created above by passing `external` as the
  // third parameter to the `FunctionTemplate` constructor.
  exports->Set(context,
               String::NewFromUtf8(isolate, "method").ToLocalChecked(),
               FunctionTemplate::New(isolate, Method, external)
                  ->GetFunction(context).ToLocalChecked()).FromJust();
} 
Worker 支持#

为了能从多个 Node.js 环境(例如主线程和 Worker 线程)中加载,一个插件需要:

  • 是一个 Node-API 插件,或者
  • 如上所述,使用 NODE_MODULE_INIT() 声明为上下文感知。

为了支持 Worker 线程,插件需要清理它们在线程退出时可能分配的任何资源。这可以通过使用 AddEnvironmentCleanupHook() 函数来实现:

void AddEnvironmentCleanupHook(v8::Isolate* isolate,
                               void (*fun)(void* arg),
                               void* arg); 

此函数添加一个钩子,该钩子将在给定的 Node.js 实例关闭之前运行。如有必要,可以使用具有相同签名的 RemoveEnvironmentCleanupHook() 在运行前移除这些钩子。回调函数以后进先出的顺序运行。

如果需要,还有另一对 AddEnvironmentCleanupHook()RemoveEnvironmentCleanupHook() 的重载,其中清理钩子接受一个回调函数。这可用于关闭异步资源,例如插件注册的任何 libuv 句柄。

下面的 addon.cc 使用了 AddEnvironmentCleanupHook

// addon.cc
#include <node.h>
#include <assert.h>
#include <stdlib.h>

using node::AddEnvironmentCleanupHook;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;

// Note: In a real-world application, do not rely on static/global data.
static char cookie[] = "yum yum";
static int cleanup_cb1_called = 0;
static int cleanup_cb2_called = 0;

static void cleanup_cb1(void* arg) {
  Isolate* isolate = static_cast<Isolate*>(arg);
  HandleScope scope(isolate);
  Local<Object> obj = Object::New(isolate);
  assert(!obj.IsEmpty());  // assert VM is still alive
  assert(obj->IsObject());
  cleanup_cb1_called++;
}

static void cleanup_cb2(void* arg) {
  assert(arg == static_cast<void*>(cookie));
  cleanup_cb2_called++;
}

static void sanity_check(void*) {
  assert(cleanup_cb1_called == 1);
  assert(cleanup_cb2_called == 1);
}

// Initialize this addon to be context-aware.
NODE_MODULE_INIT(/* exports, module, context */) {
  Isolate* isolate = Isolate::GetCurrent();

  AddEnvironmentCleanupHook(isolate, sanity_check, nullptr);
  AddEnvironmentCleanupHook(isolate, cleanup_cb2, cookie);
  AddEnvironmentCleanupHook(isolate, cleanup_cb1, isolate);
} 

在 JavaScript 中通过运行以下代码进行测试:

// test.js
require('./build/Release/addon'); 

构建#

源代码编写完成后,必须将其编译成二进制的 addon.node 文件。为此,在项目的顶层创建一个名为 binding.gyp 的文件,使用类似 JSON 的格式描述模块的构建配置。该文件由 node-gyp 使用,这是一个专门用于编译 Node.js 插件的工具。

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [ "hello.cc" ]
    }
  ]
} 

node-gyp 工具的一个版本随 Node.js 一起捆绑和分发,作为 npm 的一部分。这个版本不直接提供给开发者使用,仅用于支持使用 npm install 命令来编译和安装插件。希望直接使用 node-gyp 的开发者可以通过命令 npm install -g node-gyp 来安装它。更多信息,包括特定于平台的要求,请参阅 node-gyp安装说明

创建 binding.gyp 文件后,使用 node-gyp configure 为当前平台生成相应的项目构建文件。这将在 build/ 目录中生成一个 Makefile(在 Unix 平台上)或一个 vcxproj 文件(在 Windows 上)。

接下来,调用 node-gyp build 命令来生成已编译的 addon.node 文件。该文件将被放置在 build/Release/ 目录中。

使用 npm install 安装 Node.js 插件时,npm 会使用其自己捆绑的 node-gyp 版本来执行同样的操作,按需为用户平台生成插件的编译版本。

构建完成后,可以通过在 Node.js 中将 require() 指向构建好的 addon.node 模块来使用这个二进制插件:

// hello.js
const addon = require('./build/Release/addon');

console.log(addon.hello());
// Prints: 'world' 

由于编译后的插件二进制文件的确切路径可能因编译方式而异(例如,有时可能在 ./build/Debug/ 中),插件可以使用 bindings 包来加载编译好的模块。

虽然 bindings 包在定位插件模块方面的实现更为复杂,但它本质上使用的是一种类似于以下的 try…catch 模式:

try {
  return require('./build/Release/addon.node');
} catch (err) {
  return require('./build/Debug/addon.node');
} 

链接到 Node.js 附带的库#

Node.js 使用了静态链接的库,如 V8、libuv 和 OpenSSL。所有插件都必须链接到 V8,并且也可以链接到任何其他依赖项。通常,这只需包含适当的 #include <...> 语句(例如 #include <v8.h>)即可,node-gyp 会自动定位相应的头文件。但是,有几点需要注意:

  • node-gyp 运行时,它会检测 Node.js 的具体发布版本,并下载完整的源码 tarball 或仅下载头文件。如果下载了完整的源码,插件将可以完全访问 Node.js 的全部依赖项。但是,如果只下载了 Node.js 的头文件,那么将只有 Node.js 导出的符号可用。

  • node-gyp 运行时可以使用 --nodedir 标志指向一个本地的 Node.js 源码镜像。使用此选项,插件将可以访问完整的依赖项集合。

使用 require() 加载插件#

编译后的插件二进制文件的扩展名是 .node(而不是 .dll.so)。require() 函数被编写为会查找带有 .node 文件扩展名的文件,并将它们初始化为动态链接库。

调用 require() 时,通常可以省略 .node 扩展名,Node.js 仍然会找到并初始化该插件。但需要注意的一点是,Node.js 会首先尝试定位和加载恰好共享相同基本名称的模块或 JavaScript 文件。例如,如果与二进制文件 addon.node 在同一目录中存在一个文件 addon.js,那么 require('addon') 将优先加载 addon.js 文件。

Node.js 的原生抽象#

本文档中展示的每个示例都直接使用 Node.js 和 V8 API 来实现插件。V8 API 可能会,并且已经,在不同的 V8 版本之间(以及 Node.js 的主要版本之间)发生巨大变化。每次变化,插件可能都需要更新和重新编译才能继续工作。Node.js 的发布计划旨在最小化此类变化的频率和影响,但 Node.js 几乎无法保证 V8 API 的稳定性。

Node.js 的原生抽象(或 nan)提供了一套工具,建议插件开发者使用,以保持在 V8 和 Node.js 过去和未来版本之间的兼容性。请参阅 nan示例以了解其使用方法。

Node-API#

稳定性:2 - 稳定

Node-API 是一个用于构建原生插件的 API。它独立于底层的 JavaScript 运行时(例如 V8),并作为 Node.js 本身的一部分进行维护。此 API 将在不同版本的 Node.js 之间保持应用程序二进制接口(ABI)稳定。它旨在使插件免受底层 JavaScript 引擎变化的影响,并允许为某一版本编译的模块在更高版本的 Node.js 上无需重新编译即可运行。插件的构建/打包方式与本文档中概述的方法/工具相同(node-gyp 等)。唯一的区别是原生代码使用的 API 集合。 вместо V8 或 Node.js 的原生抽象 API,使用的是 Node-API 中可用的函数。

创建和维护一个受益于 Node-API 提供的 ABI 稳定性的插件,需要考虑某些实现上的注意事项

要在上述 "Hello world" 示例中使用 Node-API,请将 hello.cc 的内容替换为以下内容。所有其他说明保持不变。

// hello.cc using Node-API
#include <node_api.h>

namespace demo {

napi_value Method(napi_env env, napi_callback_info args) {
  napi_value greeting;
  napi_status status;

  status = napi_create_string_utf8(env, "world", NAPI_AUTO_LENGTH, &greeting);
  if (status != napi_ok) return nullptr;
  return greeting;
}

napi_value init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value fn;

  status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
  if (status != napi_ok) return nullptr;

  status = napi_set_named_property(env, exports, "hello", fn);
  if (status != napi_ok) return nullptr;
  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, init)

}  // namespace demo 

可用的函数及其使用方法记录在 使用 Node-API 的 C/C++ 插件 中。

插件示例#

以下是一些旨在帮助开发者入门的示例插件。这些示例使用了 V8 API。有关各种 V8 调用的帮助,请参考在线 V8 参考,以及 V8 的嵌入者指南,其中解释了句柄、作用域、函数模板等几个概念。

以下每个示例都使用下面的 binding.gyp 文件:

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [ "addon.cc" ]
    }
  ]
} 

如果存在多个 .cc 文件,只需将附加的文件名添加到 sources 数组中即可:

"sources": ["addon.cc", "myexample.cc"] 

一旦 binding.gyp 文件准备就绪,就可以使用 node-gyp 配置和构建示例插件:

node-gyp configure build 

函数参数#

插件通常会暴露出可以从 Node.js 内部运行的 JavaScript 访问的对象和函数。当从 JavaScript 调用函数时,输入参数和返回值必须在 C/C++ 代码之间进行映射。

以下示例演示了如何读取从 JavaScript 传递的函数参数以及如何返回结果:

// addon.cc
#include <node.h>

namespace demo {

using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

// This is the implementation of the "add" method
// Input arguments are passed using the
// const FunctionCallbackInfo<Value>& args struct
void Add(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();

  // Check the number of arguments passed.
  if (args.Length() < 2) {
    // Throw an Error that is passed back to JavaScript
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate,
                            "Wrong number of arguments").ToLocalChecked()));
    return;
  }

  // Check the argument types
  if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate,
                            "Wrong arguments").ToLocalChecked()));
    return;
  }

  // Perform the operation
  double value =
      args[0].As<Number>()->Value() + args[1].As<Number>()->Value();
  Local<Number> num = Number::New(isolate, value);

  // Set the return value (using the passed in
  // FunctionCallbackInfo<Value>&)
  args.GetReturnValue().Set(num);
}

void Init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "add", Add);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

}  // namespace demo 

编译后,该示例插件可以在 Node.js 中被 require 并使用:

// test.js
const addon = require('./build/Release/addon');

console.log('This should be eight:', addon.add(3, 5)); 

回调函数#

在插件中,将 JavaScript 函数传递给 C++ 函数并在其中执行是一种常见做法。以下示例演示了如何调用此类回调函数:

// addon.cc
#include <node.h>

namespace demo {

using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::String;
using v8::Value;

void RunCallback(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();
  Local<Function> cb = Local<Function>::Cast(args[0]);
  const unsigned argc = 1;
  Local<Value> argv[argc] = {
      String::NewFromUtf8(isolate,
                          "hello world").ToLocalChecked() };
  cb->Call(context, Null(isolate), argc, argv).ToLocalChecked();
}

void Init(Local<Object> exports, Local<Object> module) {
  NODE_SET_METHOD(module, "exports", RunCallback);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

}  // namespace demo 

此示例使用了 Init() 的双参数形式,它接收完整的 module 对象作为第二个参数。这允许插件用单个函数完全覆盖 exports,而不是将该函数作为 exports 的一个属性来添加。

要测试它,请运行以下 JavaScript:

// test.js
const addon = require('./build/Release/addon');

addon((msg) => {
  console.log(msg);
// Prints: 'hello world'
}); 

在此示例中,回调函数是同步调用的。

对象工厂#

插件可以在 C++ 函数内部创建并返回新对象,如以下示例所示。一个对象被创建并返回,其带有一个 msg 属性,该属性回显传递给 createObject() 的字符串:

// addon.cc
#include <node.h>

namespace demo {

using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void CreateObject(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  Local<Object> obj = Object::New(isolate);
  obj->Set(context,
           String::NewFromUtf8(isolate,
                               "msg").ToLocalChecked(),
                               args[0]->ToString(context).ToLocalChecked())
           .FromJust();

  args.GetReturnValue().Set(obj);
}

void Init(Local<Object> exports, Local<Object> module) {
  NODE_SET_METHOD(module, "exports", CreateObject);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

}  // namespace demo 

在 JavaScript 中测试它:

// test.js
const addon = require('./build/Release/addon');

const obj1 = addon('hello');
const obj2 = addon('world');
console.log(obj1.msg, obj2.msg);
// Prints: 'hello world' 

函数工厂#

另一个常见场景是创建包装了 C++ 函数的 JavaScript 函数,并将它们返回给 JavaScript:

// addon.cc
#include <node.h>

namespace demo {

using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void MyFunction(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  args.GetReturnValue().Set(String::NewFromUtf8(
      isolate, "hello world").ToLocalChecked());
}

void CreateFunction(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();

  Local<Context> context = isolate->GetCurrentContext();
  Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
  Local<Function> fn = tpl->GetFunction(context).ToLocalChecked();

  // omit this to make it anonymous
  fn->SetName(String::NewFromUtf8(
      isolate, "theFunction").ToLocalChecked());

  args.GetReturnValue().Set(fn);
}

void Init(Local<Object> exports, Local<Object> module) {
  NODE_SET_METHOD(module, "exports", CreateFunction);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

}  // namespace demo 

测试:

// test.js
const addon = require('./build/Release/addon');

const fn = addon();
console.log(fn());
// Prints: 'hello world' 

包装 C++ 对象#

也可以包装 C++ 对象/类,以便可以使用 JavaScript 的 new 操作符创建新实例:

// addon.cc
#include <node.h>
#include "myobject.h"

namespace demo {

using v8::Local;
using v8::Object;

void InitAll(Local<Object> exports) {
  MyObject::Init(exports);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)

}  // namespace demo 

然后,在 myobject.h 中,包装类继承自 node::ObjectWrap

// myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H

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

namespace demo {

class MyObject : public node::ObjectWrap {
 public:
  static void Init(v8::Local<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);

  double value_;
};

}  // namespace demo

#endif 

myobject.cc 中,实现需要暴露的各种方法。在下面的代码中,通过将 plusOne() 方法添加到构造函数的原型中来暴露它:

// myobject.cc
#include "myobject.h"

namespace demo {

using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;

MyObject::MyObject(double value) : value_(value) {
}

MyObject::~MyObject() {
}

void MyObject::Init(Local<Object> exports) {
  Isolate* isolate = Isolate::GetCurrent();
  Local<Context> context = isolate->GetCurrentContext();

  Local<ObjectTemplate> addon_data_tpl = ObjectTemplate::New(isolate);
  addon_data_tpl->SetInternalFieldCount(1);  // 1 field for the MyObject::New()
  Local<Object> addon_data =
      addon_data_tpl->NewInstance(context).ToLocalChecked();

  // Prepare constructor template
  Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New, addon_data);
  tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject").ToLocalChecked());
  tpl->InstanceTemplate()->SetInternalFieldCount(1);

  // Prototype
  NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);

  Local<Function> constructor = tpl->GetFunction(context).ToLocalChecked();
  addon_data->SetInternalField(0, constructor);
  exports->Set(context, String::NewFromUtf8(
      isolate, "MyObject").ToLocalChecked(),
      constructor).FromJust();
}

void MyObject::New(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  if (args.IsConstructCall()) {
    // Invoked as constructor: `new MyObject(...)`
    double value = args[0]->IsUndefined() ?
        0 : args[0]->NumberValue(context).FromMaybe(0);
    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 =
        args.Data().As<Object>()->GetInternalField(0)
            .As<Value>().As<Function>();
    Local<Object> result =
        cons->NewInstance(context, argc, argv).ToLocalChecked();
    args.GetReturnValue().Set(result);
  }
}

void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();

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

  args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}

}  // namespace demo 

要构建此示例,必须将 myobject.cc 文件添加到 binding.gyp 中:

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [
        "addon.cc",
        "myobject.cc"
      ]
    }
  ]
} 

用以下代码测试它:

// test.js
const addon = require('./build/Release/addon');

const obj = new addon.MyObject(10);
console.log(obj.plusOne());
// Prints: 11
console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13 

包装对象的析构函数将在对象被垃圾回收时运行。为了测试析构函数,可以使用一些命令行标志来强制进行垃圾回收。这些标志由底层的 V8 JavaScript 引擎提供。它们随时可能更改或被移除。Node.js 或 V8 均未对其进行文档记录,并且它们绝不应在测试之外使用。

在进程或工作线程关闭期间,JS 引擎不会调用析构函数。因此,用户有责任跟踪这些对象并确保正确销毁以避免资源泄漏。

包装对象的工厂#

或者,可以使用工厂模式来避免使用 JavaScript 的 new 操作符显式创建对象实例:

const obj = addon.createObject();
// instead of:
// const obj = new addon.Object(); 

首先,在 addon.cc 中实现 createObject() 方法:

// addon.cc
#include <node.h>
#include "myobject.h"

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void CreateObject(const FunctionCallbackInfo<Value>& args) {
  MyObject::NewInstance(args);
}

void InitAll(Local<Object> exports, Local<Object> module) {
  MyObject::Init();

  NODE_SET_METHOD(module, "exports", CreateObject);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)

}  // namespace demo 

myobject.h 中,添加了静态方法 NewInstance() 来处理对象的实例化。此方法替代了在 JavaScript 中使用 new 的方式:

// myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H

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

namespace demo {

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

 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::Global<v8::Function> constructor;
  double value_;
};

}  // namespace demo

#endif 

myobject.cc 中的实现与前一个示例类似:

// myobject.cc
#include <node.h>
#include "myobject.h"

namespace demo {

using node::AddEnvironmentCleanupHook;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Global;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

// Warning! This is not thread-safe, this addon cannot be used for worker
// threads.
Global<Function> MyObject::constructor;

MyObject::MyObject(double value) : value_(value) {
}

MyObject::~MyObject() {
}

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

  // Prototype
  NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);

  Local<Context> context = isolate->GetCurrentContext();
  constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());

  AddEnvironmentCleanupHook(isolate, [](void*) {
    constructor.Reset();
  }, nullptr);
}

void MyObject::New(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  if (args.IsConstructCall()) {
    // Invoked as constructor: `new MyObject(...)`
    double value = args[0]->IsUndefined() ?
        0 : args[0]->NumberValue(context).FromMaybe(0);
    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);
    Local<Object> instance =
        cons->NewInstance(context, argc, argv).ToLocalChecked();
    args.GetReturnValue().Set(instance);
  }
}

void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();

  const unsigned argc = 1;
  Local<Value> argv[argc] = { args[0] };
  Local<Function> cons = Local<Function>::New(isolate, constructor);
  Local<Context> context = isolate->GetCurrentContext();
  Local<Object> instance =
      cons->NewInstance(context, argc, argv).ToLocalChecked();

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

void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();

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

  args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}

}  // namespace demo 

再次强调,要构建此示例,必须将 myobject.cc 文件添加到 binding.gyp 中:

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [
        "addon.cc",
        "myobject.cc"
      ]
    }
  ]
} 

用以下代码测试它:

// test.js
const createObject = require('./build/Release/addon');

const obj = createObject(10);
console.log(obj.plusOne());
// Prints: 11
console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13

const obj2 = createObject(20);
console.log(obj2.plusOne());
// Prints: 21
console.log(obj2.plusOne());
// Prints: 22
console.log(obj2.plusOne());
// Prints: 23 

传递包装对象#

除了包装和返回 C++ 对象,还可以通过使用 Node.js 辅助函数 node::ObjectWrap::Unwrap 来解包并传递包装对象。以下示例展示了一个函数 add(),它可以接受两个 MyObject 对象作为输入参数:

// addon.cc
#include <node.h>
#include <node_object_wrap.h>
#include "myobject.h"

namespace demo {

using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

void CreateObject(const FunctionCallbackInfo<Value>& args) {
  MyObject::NewInstance(args);
}

void Add(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
      args[0]->ToObject(context).ToLocalChecked());
  MyObject* obj2 = node::ObjectWrap::Unwrap<MyObject>(
      args[1]->ToObject(context).ToLocalChecked());

  double sum = obj1->value() + obj2->value();
  args.GetReturnValue().Set(Number::New(isolate, sum));
}

void InitAll(Local<Object> exports) {
  MyObject::Init();

  NODE_SET_METHOD(exports, "createObject", CreateObject);
  NODE_SET_METHOD(exports, "add", Add);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)

}  // namespace demo 

myobject.h 中,添加了一个新的公共方法,以便在解包对象后访问私有值。

// myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H

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

namespace demo {

class MyObject : public node::ObjectWrap {
 public:
  static void Init();
  static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
  inline double value() const { return value_; }

 private:
  explicit MyObject(double value = 0);
  ~MyObject();

  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
  static v8::Global<v8::Function> constructor;
  double value_;
};

}  // namespace demo

#endif 

myobject.cc 的实现与之前的版本类似:

// myobject.cc
#include <node.h>
#include "myobject.h"

namespace demo {

using node::AddEnvironmentCleanupHook;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Global;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

// Warning! This is not thread-safe, this addon cannot be used for worker
// threads.
Global<Function> MyObject::constructor;

MyObject::MyObject(double value) : value_(value) {
}

MyObject::~MyObject() {
}

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

  Local<Context> context = isolate->GetCurrentContext();
  constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());

  AddEnvironmentCleanupHook(isolate, [](void*) {
    constructor.Reset();
  }, nullptr);
}

void MyObject::New(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  if (args.IsConstructCall()) {
    // Invoked as constructor: `new MyObject(...)`
    double value = args[0]->IsUndefined() ?
        0 : args[0]->NumberValue(context).FromMaybe(0);
    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);
    Local<Object> instance =
        cons->NewInstance(context, argc, argv).ToLocalChecked();
    args.GetReturnValue().Set(instance);
  }
}

void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();

  const unsigned argc = 1;
  Local<Value> argv[argc] = { args[0] };
  Local<Function> cons = Local<Function>::New(isolate, constructor);
  Local<Context> context = isolate->GetCurrentContext();
  Local<Object> instance =
      cons->NewInstance(context, argc, argv).ToLocalChecked();

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

}  // namespace demo 

用以下代码测试它:

// test.js
const addon = require('./build/Release/addon');

const obj1 = addon.createObject(10);
const obj2 = addon.createObject(20);
const result = addon.add(obj1, obj2);

console.log(result);
// Prints: 30