nan icon indicating copy to clipboard operation
nan copied to clipboard

How to create and return ArrayBuffer?

Open kuu opened this issue 11 years ago • 7 comments

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?

kuu avatar Jul 17 '15 18:07 kuu

does it compile or does it throw in node? what's the error message? Do you assign the module correctly as described here?

bbuecherl avatar Jul 17 '15 18:07 bbuecherl

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

kuu avatar Jul 17 '15 19:07 kuu

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);

kuu avatar Jul 18 '15 05:07 kuu

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

kkoopa avatar Jul 18 '15 07:07 kkoopa

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);

kuu avatar Jul 18 '15 07:07 kuu

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

kkoopa avatar Jul 18 '15 08:07 kkoopa

Cool. It's working!

Local<Object> buf = NanNewBufferHandle((char*)buffer, length);
NanReturnValue(buf);

kuu avatar Jul 18 '15 08:07 kuu