diff --git a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs index 1fefe88e..214bc0fb 100644 --- a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs @@ -316,7 +316,7 @@ macro_rules! impl_typed_array { sys::napi_create_arraybuffer(env, length, ptr::null_mut(), &mut arraybuffer_value) } } else { - unsafe { + let status = unsafe { sys::napi_create_external_arraybuffer( env, val_data as *mut c_void, @@ -325,6 +325,22 @@ macro_rules! impl_typed_array { hint_ptr as *mut c_void, &mut arraybuffer_value, ) + }; + if status == napi_sys::Status::napi_no_external_buffers_allowed { + let hint = unsafe { Box::from_raw(hint_ptr) }; + let mut underlying_data = ptr::null_mut(); + let status = unsafe { + sys::napi_create_arraybuffer( + env, + length, + &mut underlying_data, + &mut arraybuffer_value, + ) + }; + unsafe { std::ptr::swap(hint.data, underlying_data as *mut _) }; + status + } else { + status } }, "Create external arraybuffer failed" diff --git a/examples/napi/electron.js b/examples/napi/electron.js index 41e4d5c4..6d3ce1e1 100644 --- a/examples/napi/electron.js +++ b/examples/napi/electron.js @@ -5,6 +5,7 @@ const { readFileAsync, callThreadsafeFunction, withAbortController, + createExternalTypedArray, } = require('./index') const FILE_CONTENT = readFileSync(__filename, 'utf8') @@ -43,6 +44,7 @@ async function main() { value === Array.from({ length: 100 }, (_, i) => i + 1).reduce((a, b) => a + b), ) + console.info(createExternalTypedArray()) process.exit(0) }