Ananthi Thangavel Ananthi Thangavel - 3 days ago 5
Javascript Question

how to send array buffer from and to in Native client and javascript

I want to send an array buffer from Javascript to a Native Client module and then I want to convert the array buffer to an integer pointer. I saw the earth example in the nacl-sdk directory. They are passing image data and convert it like so:

//javascript

var imageData = context.getImageData(0, 0, img.width, img.height);
// Send NaCl module the raw image data obtained from canvas.
common.naclModule.postMessage({'message' : 'texture',
'name' : name,
'width' : img.width,
'height' : img.height,
'data' : imageData.data.buffer});

//nativeclient
std::string name = dictionary.Get("name").AsString();
int width = dictionary.Get("width").AsInt();
int height = dictionary.Get("height").AsInt();
pp::VarArrayBuffer array_buffer(dictionary.Get("data"));
if (!name.empty() && !array_buffer.is_null()) {
if (width > 0 && height > 0) {
uint32_t* pixels = static_cast<uint32_t*>(array_buffer.Map());
SetTexture(name, width, height, pixels);
array_buffer.Unmap();


I am using eclipse debugging and I don't know how to check whether the array buffer was received correctly and whether I can pass pixels as a parameter to some function or I have to create them with
pixels = new uint32_t[size]
before passing.
More importantly, I need to know how to convert
uint32_t*
pixels to
VarArrayBuffer
and send it to Javascript using a dictionary and post the message and how to receive that in Javascript and handle the message as an
ArrayBuffer
value.

Answer

The simplest example of this is the ArrayBuffer example in the SDK (examples/api/var_array_buffer).

The memory for the ArrayBuffer is owned by the pp::VarArrayBuffer, so as long as you have a reference to that (and you haven't called pp::VarArrayBuffer::Unmap) you don't have to make a copy of the memory.

pp::Var variables are automatically reference counted, so you don't need to explicitly call AddRef.

Comments