fix(napi): fallback to copy arraybuffer if zero copy is not allowed (#1455)
This commit is contained in:
parent
b42410182e
commit
548f288722
2 changed files with 19 additions and 1 deletions
|
@ -316,7 +316,7 @@ macro_rules! impl_typed_array {
|
||||||
sys::napi_create_arraybuffer(env, length, ptr::null_mut(), &mut arraybuffer_value)
|
sys::napi_create_arraybuffer(env, length, ptr::null_mut(), &mut arraybuffer_value)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
unsafe {
|
let status = unsafe {
|
||||||
sys::napi_create_external_arraybuffer(
|
sys::napi_create_external_arraybuffer(
|
||||||
env,
|
env,
|
||||||
val_data as *mut c_void,
|
val_data as *mut c_void,
|
||||||
|
@ -325,6 +325,22 @@ macro_rules! impl_typed_array {
|
||||||
hint_ptr as *mut c_void,
|
hint_ptr as *mut c_void,
|
||||||
&mut arraybuffer_value,
|
&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"
|
"Create external arraybuffer failed"
|
||||||
|
|
|
@ -5,6 +5,7 @@ const {
|
||||||
readFileAsync,
|
readFileAsync,
|
||||||
callThreadsafeFunction,
|
callThreadsafeFunction,
|
||||||
withAbortController,
|
withAbortController,
|
||||||
|
createExternalTypedArray,
|
||||||
} = require('./index')
|
} = require('./index')
|
||||||
|
|
||||||
const FILE_CONTENT = readFileSync(__filename, 'utf8')
|
const FILE_CONTENT = readFileSync(__filename, 'utf8')
|
||||||
|
@ -43,6 +44,7 @@ async function main() {
|
||||||
value ===
|
value ===
|
||||||
Array.from({ length: 100 }, (_, i) => i + 1).reduce((a, b) => a + b),
|
Array.from({ length: 100 }, (_, i) => i + 1).reduce((a, b) => a + b),
|
||||||
)
|
)
|
||||||
|
console.info(createExternalTypedArray())
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue