unsigned char(uint8_t)
メモリを使用してバッファを作成し、node.jsに配信するC++アドオンモジュールを実装しました。v8ネイティブアドオンを使用してnode.jsにC++配列を渡す方法
---- C++ addon.cpp ----
void get_frame_buffer(const FunctionCallbackInfo<Value>& args){
Isolate* isolate = helper.get_current_isolate();
if (!args[0]->IsUint32()&& !args[1]->IsUint32())
{
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
int width = args[0]->Uint32Value();
int height = args[1]->Uint32Value();
uint8_t* buf = new uint8_t[width * height * 4];
int length = width * height;
memset(buf, 0, sizeof(buf));
for (int i = 0; i < length; i++)
{
buf[i * 4 + 0] = 128;
buf[i * 4 + 1] = 128;
buf[i * 4 + 2] = 128;
buf[i * 4 + 3] = 255;
}
Local<External> ext = External::New(isolate, buf);
Local<Object> obj = ext->ToObject();
args.GetReturnValue().Set(obj);
}
void Init(Local<Object> exports)
{
NODE_SET_METHOD(exports, "GetFrameBuffer", get_frame_buffer);
}
NODE_MODULE(glAddonNode, Init)
のNode.jsで
---- ---- received.js
const module = require('./addon.js');
var Received = function()
{
this._module = new module.Module;
}
Received.prototype.get_frame_buffer = function(width, height)
{
var c++_buf = this._module.GetFrameBuffer(width, height);
// c++_buf is received from c++ addon
// but c++_buf is null
console.log(c++_buf);
}
オブジェクトをnode.jsに渡すことは成功です。配列データが受け取ったオブジェクトに存在すると予想しましたが、そのオブジェクトは空です。
何が問題なのですか?コードに間違いがありますか? v8::External
オブジェクトを使用してnode.jsにC ++配列を渡すには?あるいは、node.jsにC ++配列を渡す別の方法を知っていますか?
また、私はコピー機能を避けたい(memcpyを()、ノード::バッファ::コピー()などが...)