I implemented a c++ addon module that creates some buffer using unsigned char(uint8_t)
memory and delivers it to node.js.
---- 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)
---- received.js in Node.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);
}
Delivering object to node.js is successful, and I expected the array data to exist in the received object but that object is empty.
What's wrong?, is there a mistake in the code? How to deliver a c++ array to node.js using v8::External
object? Or, do you know another way to deliver c++ array to node.js?
Additionally, I want to avoid copy functions (memcpy(), node::Buffer::Copy() etc..)