雖然現在大部分情況都是使用n-api來編寫插件,但是底層畢竟是v8(和libuv),使用v8編寫簡單的插件,同時熟悉v8的使用。
本文介紹在寫c++插件時,簡單又常用的寫法,其實本質上,寫插件的難處在于底層的能力和對libuv、v8的了解。話不多說,直接看代碼。
- #include <node.h>
- namespace demo {
- using v8::FunctionCallbackInfo;
- using v8::Isolate;
- using v8::Local;
- using v8::Object;
- using v8::String;
- using v8::Value;
- using v8::FunctionTemplate;
- using v8::Function;
- using v8::Number;
- using v8::MaybeLocal;
- using v8::Context;
- using v8::Int32;
- static int seq;
- // 定義一個工具函數,生成seq
- void GenSeq(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = args.GetIsolate();
- args.GetReturnValue().Set(Number::New(isolate, ++seq));
- }
- // 定義一個加法函數
- void Add(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = args.GetIsolate();
- int a = args[0].As<Int32>()->Value();
- int b = args[1].As<Int32>()->Value();
- args.GetReturnValue().Set(Number::New(isolate, a + b));
- }
- void Initialize(
- Local<Object> exports,
- Local<Value> module,
- Local<Context> context
- ) {
- Isolate* isolate = context->GetIsolate();
- // 新建一個函數模版
- Local<FunctionTemplate> func = FunctionTemplate::New(isolate);
- // 新建一個字符串表示函數名
- Local<String> zaylee = String::NewFromUtf8(isolate, "zaylee", v8::NewStringType::kNormal).ToLocalChecked();
- // 設置函數名
- func->SetClassName(zaylee);
- // 設置原型屬性
- func->PrototypeTemplate()->Set(isolate, "protoField", Number::New(isolate, 1));
- // 設置對象屬性
- func->InstanceTemplate()->Set(isolate, "instanceField", Number::New(isolate, 2));
- func->InstanceTemplate()->Set(isolate, "add", FunctionTemplate::New(isolate, Add));
- // 設置函數對象本身的屬性
- func->Set(isolate, "funcField", Number::New(isolate, 3));
- // 根據函數模版創建一個函數
- Local<Function> ret = func->GetFunction(context).ToLocalChecked();
- Local<String> Demo = String::NewFromUtf8(isolate, "Demo", v8::NewStringType::kNormal).ToLocalChecked();
- // 導出函數
- exports->Set(context, Demo, ret).Check();
- // 導出工具函數
- NODE_SET_METHOD(exports, "genSeq", GenSeq);
- }
- NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
- } // namespace demo
寫個測試例子
- const { Demo, genSeq } = require('./build/Release/test.node');
- const demo = new Demo();
- console.log('demo對象:', demo, '\n');
- console.log('原型屬性:', demo.protoField, '\n');
- console.log('執行add方法:', demo.add(1,2), '\n');
- console.log('執行seq方法:', genSeq(), genSeq(), '\n');
最后編寫編譯配置
- {
- "targets": [
- {
- "target_name": "test",
- "sources": [ "./test.cc" ]
- }
- ]
- }
看起來非常簡單,大概的流程如下
- npm install -g node-gyp
- node-gyp configure
- node-gyp build
- node test.js
拓展nodejs的方式很多,插件是一種,直接修改內核也是一種,之前有介紹過如何修改內核,有興趣的同學也可以看一下。
原文地址:https://mp.weixin.qq.com/s/oJDHYoPuCR7iy7XGp6eHTw