Nodejs Addons. C++ to JS type conversion

34 views
Skip to first unread message

HungryHippo

unread,
Mar 17, 2016, 12:16:51 PM3/17/16
to nodejs
hi, i'm really struggling to work with Nan and conversions of C++ to V8 types.

How would i convert this format ?  std::vector<uint8_t> data; 


info.GetReturnValue().Set(Nan::New<v8::String>(convertedData));


thanks for any help :-) 



Ben Noordhuis

unread,
Mar 19, 2016, 2:35:17 AM3/19/16
to nod...@googlegroups.com
if (data.empty()) {
info.GetReturnValue().SetEmptyString();
} else {
// Assumes |data| contains valid UTF-8.
const char* chars = reinterpret_cast<const char*>(&data[0]);
info.GetReturnValue().Set(Nan::New(chars, data.size()).ToLocalChecked());
}

HungryHippo

unread,
Mar 19, 2016, 10:26:25 AM3/19/16
to nodejs
thanks Ben, 

I found setting up the addon quite straightforward but all the type conversions of the javascript inputs to c++ and c++ outputs to v8 was the tricky bit. 
Reply all
Reply to author
Forward
0 new messages