I have an array of bytes defined as
unsigned char ptr = new unsigned char[1024];
fillWithSomething(ptr, 1024);
Then, I need to store the ptr
variable in a Local< Array>
variable of V8 Engine or another kind of V8 data type.
Currently, I am converting the array of bytes into a string in order to store in a Local< String>
variable, but this approach is inefficient to my application.
Thanks in advance.
UPDATED (thanks to Vyacheslav Egorov)
I test the solution with an external array but I can't use it on my node.js server code. I have the following code (on my extension side C++):
Handle<Object> array = Object::New();
array->SetIndexedPropertiesToExternalArrayData(getBytes(), kExternalUnsignedByteArray, bytesSize);
return array;
My question is, How I can use the array
variable in my server code (javascript) to call the function GetIndexedPropertiesExternalArrayData()
.
Thanks again
like Vyacheslav Egorov already answered
is the right answer. If you need more examples you can check out https://github.com/joyent/node they use lots of v8 functionallity.
just
grep -nrw ".*SetIndexedPropertiesToExternalArrayData.*" "."
in the /src folder of the project and you will get lots of examples forSetIndexedPropertiesToExternalArrayData
The most efficient way is to use external arrays:
Good example of external arrays API usage (including lifetime management) can be found in
d8.cc
: https://github.com/v8/v8/blob/7a0c55bd0d07135ce317f0e95909120eaafd5973/src/d8.cc#L394-L591