// 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;
union data
{
float a;
unsigned char b[4];
long longval;
};
// 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")));
return;
}
// Check the argument types
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
// Perform the operation
double value = args[0]->NumberValue() + args[1]->NumberValue();
Local<Number> num = Number::New(isolate, value);
args.GetReturnValue().Set(num);
}
void Test (const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
data c;
c.b[0] = args[0]->NumberValue();
c.b[1] = args[1]->NumberValue();
c.b[2] = args[2]->NumberValue();
c.b[3] = args[3]->NumberValue();
args.GetReturnValue().Set(c.a);
}
void Init(Local<Object> exports) {
NODE_SET_METHOD(exports, "add", Add);
NODE_SET_METHOD(exports, "test", Test);
}
// 暴露接口
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
}
# 执行命令
npm install
node-gyp configure
node-gyp build
修改过引用文件后需要重新npm install
node-gyp configure // 根据平台,生成项目构建文件,如:MakeFile
node-gyp build // 根据构建文件,生成原生 Node.js 插件文件,这一步之后就可以将生成的插件复制出来到处用啦,但是注意:不能跨平台。
node-gyp clean // 清除 configure 和 build 生成的文件
node-gyp rebuild // 按顺序运行 clean、configure、build 命令
// 其他的可以用到的时候查文档 ...
共三个文件:package.json,democpp_v8.cc,binding.gyp
package.json
{
"name": "test-cpp-module",
"version": "0.1.0",
"private": true,
"gypfile": true,
"dependencies": {
"node-addon-api": "^3.0.0"
}
}
democpp_v8.cc
// 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;
union data
{
float a;
unsigned char b[4];
long longval;
};
void Test (const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
data c;
c.b[0] = args[0]->NumberValue();
c.b[1] = args[1]->NumberValue();
c.b[2] = args[2]->NumberValue();
c.b[3] = args[3]->NumberValue();
args.GetReturnValue().Set(c.a);
}
void Init(Local<Object> exports) {
NODE_SET_METHOD(exports, "test", Test);
}
// 暴露接口
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
}
binding.gyp
{
"targets": [
{
"target_name": "democpp",
"sources": [
"democpp_v8.cc"
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")"
],
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"],
"defines": ["NAPI_CPP_EXCEPTIONS"],
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES"
}
}
]
}
执行如下命令:
# 执行命令
npm install
node-gyp configure
node-gyp build
修改过引用文件后需要重新npm install
node-gyp configure // 根据平台,生成项目构建文件,如:MakeFile
node-gyp build // 根据构建文件,生成原生 Node.js 插件文件,这一步之后就可以将生成的插件复制出来到处用啦,但是注意:不能跨平台。
node-gyp clean // 清除 configure 和 build 生成的文件
node-gyp rebuild // 按顺序运行 clean、configure、build 命令
// 其他的可以用到的时候查文档 ...
democpp=require("./build/Release/democpp.node");
console.log(democpp.add(1, 2));
console.log(democpp.test(0,0,200,66));