如何使用v8 native addon将c ++数组传递给node.js [英] how to deliver c++ array to node.js using v8 native addon
问题描述
我实现了一个c ++插件模块,它使用 unsigned char(uint8_t)
内存创建一些缓冲区并将其传递给node.js.
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 ----
---- 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 ----
---- 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);
}
将对象传递给node.js是成功的,我期望数组数据到存在于接收的对象中但该对象为空。
Delivering object to node.js is successful, and I expected the array data to exist in the received object but that object is empty.
出了什么问题?代码中是否有错误?如何使用 v8 ::外部
对象将c ++数组传递给node.js?或者,你知道另一种方法将c ++数组传递给node.js吗?
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?
另外,我想避免复制函数(memcpy(),node :: Buffer ::复制()等..)
Additionally, I want to avoid copy functions (memcpy(), node::Buffer::Copy() etc..)
推荐答案
简短的回答是:你不能使用 v8 :: External
将C ++对象公开给JavaScript。
The short answer is: you cannot use v8::External
s to expose C++ objects to JavaScript.
外部
的预期用例是将C ++对象与暴露于JS的对象相关联,方法是将它们存储在内部字段,只能从C ++访问。例如,您可以在某个对象的内部字段中存储 uint8_t
数组(包含在外部
中),然后在该对象从JS传递到您的一个C ++回调,从其内部字段获取外部
以检索原始 uint8_t
数组。但是将C ++数组直接作为对象属性暴露给JavaScript是没有魔力的。
The intended use case for External
s is to associate C++ objects with JS-exposed objects by storing them in "internal fields", which can only be accessed from C++. For example, you can store that uint8_t
array (wrapped in an External
) in some object's internal field, and then when that object is handed from JS to one of your C++ callbacks, get the External
from its internal field to retrieve the original uint8_t
array. But there is no magic to expose a C++ array directly as object properties to JavaScript.
为了在C ++和JavaScript之间快速共享二进制数据,节点:: Buffer
s可能就是你想要的。也许这个问题:将Node.js缓冲区传递给C ++插件是什么您正在寻找?这个问题:节点缓冲区到字符数组提供了双向转换的代码。
For fast sharing of binary data between C++ and JavaScript, node::Buffer
s are probably what you want. Maybe this question: Pass Node.js Buffer to C++ addon is what you're looking for? This question: Node Buffer to char array provides code for conversions in both directions.
这篇关于如何使用v8 native addon将c ++数组传递给node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!