




























Node.js Native Addons/Node.js 原生扩展插件, 是指用C / C++等底层语言编写的模块,然后通过 Node.js 提供的接口编译成可被 JavaScript 直接调用的扩展插件。
C++ AddonsAddons are dynamically-linked shared objects that can be loaded via the require() function as ordinary Node.js modules.
Addons provide a foreign function interface between JavaScript and native code.
There are three options for implementing addons:
Node-API (recommended ✅)nan (Native Abstractions for Node.js)https://nodejs.org/api/addons.html
Node-API(原名 N-API)是一个用于构建原生插件的 API。它独立于底层 JavaScript 运行时(例如 V8),并作为 Node.js 的一部分进行维护。
该 API 将在各个 Node.js 版本之间保持应用程序二进制接口 (ABI) 的稳定性。
其目的是使插件免受底层 JavaScript 引擎变更的影响,并允许为某一主要版本编译的模块无需重新编译即可在后续的 Node.js 主要版本上运行。ABI 稳定性指南提供了更详细的解释。
https://nodejs.org/api/n-api.html
Node-API is a stable C API built into Node.js that lets C/C++ code create, read, and manipulate JavaScript values as if they were created by JavaScript itself.
It was introduced experimentally in Node.js 8.0.0 and became stable (no longer behind a flag) in the Node.js 8/10 timeframe.
Because Node-API is part of Node.js itself, it requires no additional installation.


https://nodejs.org/learn/node-api
https://nodejs.org/zh-cn/learn/node-api
Node-API 的一个重要搭档是 npm 包 node-addon-api。
它将 C API 封装在一个符合 C++ 惯用语的 C++ 对象模型中,减少了样板代码,并使一些常见模式(例如将 C++ 对象封装成 JavaScript 对象)更加符合人体工程学。
它保留了 Node-API 的全部 ABI 稳定性保证。
$ npm i node-addon-api
https://www.npmjs.com/package/node-addon-api
https://github.com/nodejs/node-addon-api
Native Abstractions for Node.js / Node.js 的原生抽象
// the equivalent of the following JavaScript code
module.exports.hello = () => 'world';
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);
}
// N.B.: no semi-colon, this is not a function
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
}
// namespace demo
Makefile, hello.cc => > hello.node
NODEJS_DEV_ROOT ?= $(shell dirname "$(command -v node)")/..
CXXFLAGS = -std=c++23 -I$(NODEJS_DEV_ROOT)/include/node -fPIC -shared -Wl,-undefined,dynamic_lookup
# target source
hello.node: hello.cc
$(CXX) $(CXXFLAGS) -o $@ $<
$ make
$ node -p 'require("./hello.node").hello()'
# world
.cpp 和 .cc 本质上没有语言层面的区别,只是 C++ 源文件的不同命名习惯。

$ g++ main.cpp
# or
$ g++ main.cc
CMake

C files → compiled with C compiler (e.g. gcc)
C++ files → compiled with C++ compiler (e.g. g++ / clang++)


©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。