Skip to content

Commit

Permalink
fixes from node-ffi-napi#82
Browse files Browse the repository at this point in the history
  • Loading branch information
btsimonh committed Oct 28, 2024
1 parent b8cb3da commit 4524014
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class InstanceData final : public RefNapi::Instance {
if (it != pointer_to_orig_buffer.end())
ab = it->second.ab.Value();

if (ab.IsEmpty()) {
if (ab.IsEmpty() || (ab.ByteLength() < length)) {
// this ALWAYS creates an arraybuffer of 1Gbyte?
//length = std::max<size_t>(length, kMaxLength);
length = std::min<size_t>(length, kMaxLength);
Expand Down Expand Up @@ -168,8 +168,15 @@ class InstanceData final : public RefNapi::Instance {
*/

Value WrapPointer(Env env, char* ptr, size_t length) {
if (ptr == nullptr)
if (ptr == nullptr) {
length = 0;
} else if (length == 0) {
// If length is 0 N-API doesn't guarantee it will save/restore ptr normally.
// For example, see https://nodejs.org/api/n-api.html#napi_get_typedarray_info
// "[out] data: ... If the length of the array is 0, this may be NULL or any
// other pointer value."
length = 1;
}

InstanceData* data;
if (ptr != nullptr && (data = InstanceData::Get(env)) != nullptr) {
Expand Down

0 comments on commit 4524014

Please sign in to comment.