node 調用c++方法解析共用體

// 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));

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章