You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today I thought I'd share a method on how to efficiently work with data structures when making Node.js addons. Specifically, I will show you how to represent a struct in JavaScript and how to access and manipulate this data from v8. Because there is no official documentation for v8 except the reference material generated from v8.h, this post is meant to bridge the language gap between JavaScript and C/C++.
Data storage
Typed arrays were introduced a few years ago and they provide us with a way of accessing and manipulating raw binary data. The different types of typed arrays and their C equivalents are shown in the below table (derived from the MDN page):
It's good to know that the above are only different views on the underlying ArrayBuffer storage. We can initialize typed arrays using regular arrays, like this:
varview=newInt8Array([1,2,3]);
We can also initialize them by allocating memory using an ArrayBuffer:
Note: the last argument of setInt8 specifies the endianness of the system. A value of true means little endian.
In C++, the Int8Array in the first two examples can be represented with an int8_t array. The same applies to the other types. Just look up the C equivalent in the table above.
int8_t data[] = {1, 2, 3};
The DataView in the last example can effectively be represented as a struct:
typedefstruct {
int8_t a;
int16_t b;
} dataView;
So we have established some common ground between JavaScript and C++. The remaining question is how do we read this data into a Node.js addon?
Data access
Let's set up a simple example. If you have no idea what a Node.js addon is I suggest you read this guide.
#include<node.h>typedefstruct {
int8_t a;
int16_t b;
} dataView;
voidaccessInt8Array(const v8::FunctionCallbackInfo<v8::Value>& info) {
// make sure the first argument is an Int8Arrayassert(info[0]->IsInt8Array());
// read first argument as an Int8Array.
v8::Local<v8::Int8Array> view = info[0].As<v8::Int8Array>();
// get contents as a void pointervoid *data = view->Buffer()->GetContents().Data();
// create a pointer to int8_t and typecastint8_t *contents = static_cast<int8_t*>(data);
// multiply all elements by 2for (int i = 0; i < view->Length(); i++)
contents[i] *= 2;
}
voidaccessDataView(const v8::FunctionCallbackInfo<v8::Value>& info) {
assert(info[0]->IsDataView());
v8::Local<v8::DataView> view = info[0].As<v8::DataView>();
// check size to make sure the data is compatible with our structassert(view->ByteLength() == sizeof(dataView));
void *data = view->Buffer()->GetContents().Data();
dataView *contents = static_cast<dataView*>(data);
// multiply both integers by 2
contents->a *= 2;
contents->b *= 2;
}
voidinit(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "accessInt8Array", accessInt8Array);
NODE_SET_METHOD(exports, "accessDataView", accessDataView);
}
NODE_MODULE(addon, init);
Note: If you want to have full control over memory and avoid unexpected garbage collection, you should replace GetContents() with Externalize(). You will then lose the ability to manipulate the contents directly and the responsibility to free() the memory lies on you.
Finally, we create a binding.gyp file, compile with node-gyp configure rebuild and try it out!
Notice how I didn’t use free. Setting the last argument to v8::ArrayBufferCreationMode::kInternalized hands over responsibility of freeing the memory to the GC.
Today I thought I'd share a method on how to efficiently work with data structures when making Node.js addons. Specifically, I will show you how to represent a
struct
in JavaScript and how to access and manipulate this data from v8. Because there is no official documentation for v8 except the reference material generated from v8.h, this post is meant to bridge the language gap between JavaScript and C/C++.Data storage
Typed arrays were introduced a few years ago and they provide us with a way of accessing and manipulating raw binary data. The different types of typed arrays and their C equivalents are shown in the below table (derived from the MDN page):
Int8Array
int8_t
Uint8Array
uint8_t
Uint8ClampedArray
uint8_t
Int16Array
int16_t
Uint16Array
uint16_t
Int32Array
int32_t
Uint32Array
uint32_t
Float32Array
float
Float64Array
double
It's good to know that the above are only different views on the underlying
ArrayBuffer
storage. We can initialize typed arrays using regular arrays, like this:We can also initialize them by allocating memory using an
ArrayBuffer
:More importantly, we can use the
DataView
interface to read and write arbitrary data to anArrayBuffer
:Note: the last argument of
setInt8
specifies the endianness of the system. A value oftrue
means little endian.In C++, the
Int8Array
in the first two examples can be represented with anint8_t
array. The same applies to the other types. Just look up the C equivalent in the table above.The
DataView
in the last example can effectively be represented as astruct
:So we have established some common ground between JavaScript and C++. The remaining question is how do we read this data into a Node.js addon?
Data access
Let's set up a simple example. If you have no idea what a Node.js addon is I suggest you read this guide.
Note: If you want to have full control over memory and avoid unexpected garbage collection, you should replace
GetContents()
withExternalize()
. You will then lose the ability to manipulate the contents directly and the responsibility tofree()
the memory lies on you.Finally, we create a
binding.gyp
file, compile withnode-gyp configure rebuild
and try it out!Now we can work with practically any kind of binary data in both JavaScript and C++.
Success!
The text was updated successfully, but these errors were encountered: