How to create and return ArrayBuffer?
I expected the following code to work but it seems not to be supported:
#include <nan.h>
using v8::ArrayBuffer;
static void *buffer;
static size_t length;
...
Local<ArrayBuffer> abuf = NanNew<ArrayBuffer>(buffer, length);
NanReturnValue(abuf);
Then, how can we create and return ArrayBuffer using nan?
does it compile or does it throw in node? what's the error message? Do you assign the module correctly as described here?
It doesn't compile. Error message is:
error: no matching function for call to 'New'
return NanIntern::Factory<T>::New(arg0, arg1);
But it compiles if I write like this:
Local<ArrayBuffer> abuf = ArrayBuffer::New(v8::Isolate::GetCurrent(), buffer, length);
NanReturnValue(abuf);
Then I can receive a JS object with correct "byteLength" property but it seems to be an empty object.
I'm using nan 1.8.4 + node 0.12.7 + Mac OS X 10.10.4
but it seems to be an empty object.
I think the ArrayBuffer is GCed. I can read the buffer when I receive it from the native addon but once I pass it to another function, it is gone (byteLength=undefined.) I tried the Persistent handle, but the situation didn't change.
static Persistent<ArrayBuffer> persistentBuf;
Local<ArrayBuffer> abuf = ArrayBuffer::New(v8::Isolate::GetCurrent(), buffer, length);
NanAssignPersistent(persistentBuf, abuf);
NanReturnValue(abuf);
NAN does not directly support typed arrays as they are not available for all supported V8 versions. Use Node's Buffer instead.
Or use the correct routines straight from V8 if you absolutely require typed arrays and do not care about veraion compativility.
On July 17, 2015 9:04:05 PM EEST, Kuu Miyazaki [email protected] wrote:
I expected the following code to work but it seems not to be supported:
static void *buffer; static size_t length; ... Local<ArrayBuffer> abuf = NanNew<ArrayBuffer>(buffer, length); NanReturnValue(buf);Then, how can we create and return ArrayBuffer using nan?
Reply to this email directly or view it on GitHub: https://github.com/nodejs/nan/issues/379
Thanks for the explanation.
Now I can create/return a Buffer like this:
Local<Object> buf = node::Buffer::New(v8::Isolate::GetCurrent(), (const char *) buffer, length);
NanReturnValue(buf);
You can also create the Buffer with NanBufferHandle or something similarly named. Don't have the exact name in front of me now. That makes it cross-version.
On July 18, 2015 10:59:55 AM EEST, Kuu Miyazaki [email protected] wrote:
Thanks for the explanation.
Now I can create/return a Buffer like this:
Local<Object> buf = node::Buffer::New(v8::Isolate::GetCurrent(), (const char *) buffer, length); NanReturnValue(buf);
Reply to this email directly or view it on GitHub: https://github.com/nodejs/nan/issues/379#issuecomment-122510272
Cool. It's working!
Local<Object> buf = NanNewBufferHandle((char*)buffer, length);
NanReturnValue(buf);