java
自动生成 JNI 代码
javascript
对 V8 的支持过低,仅能演示 hello-world
安装 swig
# winscoop install swig
手动编译
node-gyp 手动编译单个 C++ 文件
# hello.cc# 1. v8 包裹 C/C++ 代码#include <node.h>namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;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").ToLocalChecked());}void Initialize(Local<Object> exports) {NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)} // namespace demo# binding.gyp# 2. node_gyp 配置{"targets": [{"target_name": "addon","sources": [ "hello.cc" ]}]}# 3. node-gyp 编译# 4. hello.js 调用const addon = require('./build/Release/addon');console.log(addon.hello());# Prints: 'world'
swig 编译
暂时需要手动编译,swig 编译生成的 wrapper 文件不支持 v8 新语法
# 生成目标文件 example.java example_wrap.c exampleJNI.java
cd simple
# 编译生成 v8 包裹的 C++ 文件
swig -c++ -javascript -node example.i
# 编译 C++ 文件为 js
node-gyp configure build
