Skip to content

Commit

Permalink
Merge pull request #95 from gpetrov/master-next
Browse files Browse the repository at this point in the history
Use the new ArrayBuffer API for Electron 13
  • Loading branch information
maxbrunsfeld authored Apr 19, 2022
2 parents fc99954 + 4cdc668 commit aefa0dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@
]
}
],
'variables': { 'runtime%': 'node' },
'conditions': [
['runtime=="electron"', { 'defines': ['NODE_RUNTIME_ELECTRON=1'] }],
]
}
14 changes: 13 additions & 1 deletion src/conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ void InitConversions(Local<Object> exports) {
end_position_key.Reset(Nan::Persistent<String>(Nan::New("endPosition").ToLocalChecked()));

point_transfer_buffer = static_cast<uint32_t *>(malloc(2 * sizeof(uint32_t)));
auto js_point_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), point_transfer_buffer, 2 * sizeof(uint32_t));

#if _MSC_VER && NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 89
// this is a terrible thing we have to do because of https://github.com/electron/electron/issues/29893
v8::Local<v8::Object> bufferView;
bufferView = node::Buffer::New(Isolate::GetCurrent(), point_transfer_buffer, 0, 2 * sizeof(uint32_t)).ToLocalChecked();
auto js_point_transfer_buffer = node::Buffer::Data(bufferView);
#elif V8_MAJOR_VERSION >= 8
auto backing_store = ArrayBuffer::NewBackingStore(point_transfer_buffer, 2 * sizeof(uint32_t), BackingStore::EmptyDeleter, nullptr);
auto js_point_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), std::move(backing_store));
#else
auto js_point_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), point_transfer_buffer, 2 * sizeof(uint32_t));
#endif

Nan::Set(exports, Nan::New("pointTransferArray").ToLocalChecked(), Uint32Array::New(js_point_transfer_buffer, 0, 2));
}

Expand Down
14 changes: 13 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ static inline void setup_transfer_buffer(uint32_t node_count) {
}
transfer_buffer_length = new_length;
transfer_buffer = static_cast<uint32_t *>(malloc(transfer_buffer_length * sizeof(uint32_t)));
auto js_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), transfer_buffer, transfer_buffer_length * sizeof(uint32_t));

#if _MSC_VER && NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 89
// this is a terrible thing we have to do because of https://github.com/electron/electron/issues/29893
v8::Local<v8::Object> bufferView;
bufferView = node::Buffer::New(Isolate::GetCurrent(), transfer_buffer, 0, transfer_buffer_length * sizeof(uint32_t)).ToLocalChecked();
auto js_point_transfer_buffer = node::Buffer::Data(bufferView);
#elif V8_MAJOR_VERSION >= 8
auto backing_store = ArrayBuffer::NewBackingStore(transfer_buffer, transfer_buffer_length * sizeof(uint32_t), BackingStore::EmptyDeleter, nullptr);
auto js_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), std::move(backing_store));
#else
auto js_transfer_buffer = ArrayBuffer::New(Isolate::GetCurrent(), transfer_buffer, transfer_buffer_length * sizeof(uint32_t));
#endif

Nan::Set(
Nan::New(module_exports),
Nan::New("nodeTransferArray").ToLocalChecked(),
Expand Down

0 comments on commit aefa0dd

Please sign in to comment.