fix(napi): use ptr::copy to create TypedArray in electron fallback mode (#1548)

This commit is contained in:
LongYinan 2023-03-29 14:03:32 +08:00 committed by GitHub
parent 0a0aa36c28
commit a0b6e2b263
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -301,12 +301,11 @@ macro_rules! impl_typed_array {
return Ok(napi_value); return Ok(napi_value);
} }
let mut arraybuffer_value = ptr::null_mut(); let mut arraybuffer_value = ptr::null_mut();
let ratio = mem::size_of::<$rust_type>() / mem::size_of::<u8>(); let ratio = mem::size_of::<$rust_type>();
let length = val.length * ratio;
let val_data = val.data;
let val_length = val.length; let val_length = val.length;
let length = val_length * ratio;
let val_data = val.data;
val.drop_in_vm.store(true, Ordering::Release); val.drop_in_vm.store(true, Ordering::Release);
let hint_ptr = Box::into_raw(Box::new(val));
check_status!( check_status!(
if length == 0 { if length == 0 {
// Rust uses 0x1 as the data pointer for empty buffers, // Rust uses 0x1 as the data pointer for empty buffers,
@ -316,13 +315,14 @@ 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 {
let hint_ptr = Box::into_raw(Box::new(val));
let status = unsafe { let status = unsafe {
sys::napi_create_external_arraybuffer( sys::napi_create_external_arraybuffer(
env, env,
val_data as *mut c_void, val_data.cast(),
length, length,
Some(finalizer::<$rust_type, $name>), Some(finalizer::<$rust_type, $name>),
hint_ptr as *mut c_void, hint_ptr.cast(),
&mut arraybuffer_value, &mut arraybuffer_value,
) )
}; };
@ -337,7 +337,7 @@ macro_rules! impl_typed_array {
&mut arraybuffer_value, &mut arraybuffer_value,
) )
}; };
unsafe { std::ptr::swap(hint.data, underlying_data as *mut _) }; unsafe { std::ptr::copy(hint.data.cast(), underlying_data, length) };
status status
} else { } else {
status status