Not sure if you still need help with this, but ... expanding on what Ben
said:
Node Buffers are Uint8Arrays (ArrayBufferViews) in version 4.x and later
only.
Small node Buffers ([currently] <4096 bytes) use a shared buffer as a
backing store, which is why you see a greater byteLength. You need to
observe the byteOffset and byteLength to find the section of the shared
buffer that belongs to your buffer.
If you want something for Buffers compatible going back to node 0.10, use
Buffer::Data() from node_buffer.h, which takes care of the byteOffset for
you:
char* Data(Local<Value> val) {
CHECK(val->IsUint8Array());
Local<Uint8Array> ui = val.As<Uint8Array>();
ArrayBuffer::Contents ab_c = ui->Buffer()->GetContents();
return static_cast<char*>(ab_c.Data()) + ui->ByteOffset();
}
If you really want to handle them as Uint8Arrays, there's an example using
nan (native abstractions for node) to access typed array contents here:
https://github.com/Automattic/node-canvas/blob/a3492e3c3355b13433e1a3ddd5197f8bfa90f26c/src/ImageData.cc#L111
And an example without the nan macro here:
https://github.com/Automattic/node-canvas/blob/2884267de4a45cbbb6dfa717903d054cf8a7ab94/src/ImageData.cc#L107-L111
But using node's exported function should be preferable if you're actually
consuming Buffers.
-Zach
On Monday, July 4, 2016 at 3:00:08 AM UTC-7, Elia Mazzuoli wrote:
>
> Hi all, I'm trying to access data inside an ArrayBufferView. This is my
> simple javascript code:
>
> const myAddon = require('addon location');
>
> const myObj = myAddon.MyObj();
>
> const data = Buffer.from('test');
> myObj = doSomething(data);
>
> After that in my c++ I have this:
>
> void MyObj::doSomething(const v8::FunctionCallbackInfo<v8::Value> &args)
> {) {
>
> Local<v8::ArrayBufferView> dataBufferView =
> Local<v8::ArrayBufferView>::Cast(args[1]);
>
> std::cout << "Lenght on dataBufferView: " << dataBufferView->ByteLength()
> << std::endl;
>
> std::cout << "Byte offset on dataBufferView: " <<
> dataBufferView->ByteOffset() << std::endl;
>
> Local<ArrayBuffer> dataBuffer = dataBufferView->Buffer();
>
> std::cout << "Lenght dataBuffer: " << dataBuffer->ByteLength() <<
> std::endl;
>
> ArrayBuffer::Contents dataContent = dataBuffer->Externalize();
>
>
> uint8_t* startData = static_cast<uint8_t*>(dataContent.Data());
>
> std::cout << "Content of " <<dataContent.ByteLength() << " from js: ";
>
> for(unsigned int i = 0; i < dataContent->ByteLength(); i++) {
>
> std::cout << *(startData + i);
>
> }
>
> std::cout << std::endl;
>
>
> }
>
>
> What is strange in this simple code is that dataBuffer->ByteLength() is
> 8192 and if I print out the data content what I see is my full javascript
> code and some other binary data.
>
> So.. what is the right way to access data?
>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.