J'ai implémenté un module addon C++ qui crée un tampon en utilisant la mémoire unsigned char(uint8_t)
et l'envoie à node.js.comment livrer C++ array à node.js en utilisant l'addon natif v8
---- 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 en 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);
}
La livraison de l'objet à node.js a réussi, et je m'attendais à ce que les données du tableau existent dans l'objet reçu mais que cet objet soit vide.
Qu'est-ce qui ne va pas?, Y a-t-il une erreur dans le code? Comment fournir un tableau C++ à node.js en utilisant l'objet v8::External
? Ou connaissez-vous un autre moyen de fournir un tableau C++ à node.js?
De plus, je veux éviter les fonctions de copie (memcpy(), noeud :: tampon de copie() etc ..)