0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

chore(ext/ffi): Improve FFI static handling (#14924)

This commit is contained in:
Aapo Alasuutari 2022-06-23 05:49:07 +03:00 committed by GitHub
parent 1e3713c3bc
commit a232b4011a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1194,7 +1194,7 @@ fn op_ffi_get_static<'scope>(
) -> Result<serde_v8::Value<'scope>, AnyError> { ) -> Result<serde_v8::Value<'scope>, AnyError> {
let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?; let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;
let data_ptr = resource.get_static(name)? as *const u8; let data_ptr = resource.get_static(name)?;
Ok(match static_type { Ok(match static_type {
NativeType::Void => { NativeType::Void => {
@ -1202,68 +1202,79 @@ fn op_ffi_get_static<'scope>(
} }
NativeType::U8 => { NativeType::U8 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const u8) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const u8) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> =
serde_v8::from_v8(scope, number.into())? v8::Integer::new_from_unsigned(scope, result as u32).into();
number.into()
} }
NativeType::I8 => { NativeType::I8 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const i8) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const i8) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> =
serde_v8::from_v8(scope, number.into())? v8::Integer::new(scope, result as i32).into();
number.into()
} }
NativeType::U16 => { NativeType::U16 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const u16) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const u16) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> =
serde_v8::from_v8(scope, number.into())? v8::Integer::new_from_unsigned(scope, result as u32).into();
number.into()
} }
NativeType::I16 => { NativeType::I16 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const i16) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const i16) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> =
serde_v8::from_v8(scope, number.into())? v8::Integer::new(scope, result as i32).into();
number.into()
} }
NativeType::U32 => { NativeType::U32 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const u32) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const u32) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> =
serde_v8::from_v8(scope, number.into())? v8::Integer::new_from_unsigned(scope, result).into();
number.into()
} }
NativeType::I32 => { NativeType::I32 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const i32) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const i32) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> = v8::Integer::new(scope, result).into();
serde_v8::from_v8(scope, number.into())? number.into()
} }
NativeType::U64 => { NativeType::U64 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const u64) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const u64) };
let big_int = v8::BigInt::new_from_u64(scope, result); let big_int: v8::Local<v8::Value> =
serde_v8::from_v8(scope, big_int.into())? v8::BigInt::new_from_u64(scope, result).into();
big_int.into()
} }
NativeType::I64 => { NativeType::I64 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const i64) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const i64) };
let big_int = v8::BigInt::new_from_i64(scope, result); let big_int: v8::Local<v8::Value> =
serde_v8::from_v8(scope, big_int.into())? v8::BigInt::new_from_i64(scope, result).into();
big_int.into()
} }
NativeType::USize => { NativeType::USize => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const usize) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const usize) };
let big_int = v8::BigInt::new_from_u64(scope, result as u64); let big_int: v8::Local<v8::Value> =
serde_v8::from_v8(scope, big_int.into())? v8::BigInt::new_from_u64(scope, result as u64).into();
big_int.into()
} }
NativeType::ISize => { NativeType::ISize => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const isize) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const isize) };
let big_int = v8::BigInt::new_from_i64(scope, result as i64); let big_int: v8::Local<v8::Value> =
serde_v8::from_v8(scope, big_int.into())? v8::BigInt::new_from_i64(scope, result as i64).into();
big_int.into()
} }
NativeType::F32 => { NativeType::F32 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const f32) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const f32) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> =
serde_v8::from_v8(scope, number.into())? v8::Number::new(scope, result as f64).into();
number.into()
} }
NativeType::F64 => { NativeType::F64 => {
let result = unsafe { ptr::read_unaligned(data_ptr as *const f64) }; let result = unsafe { ptr::read_unaligned(data_ptr as *const f64) };
let number = v8::Number::new(scope, result as f64); let number: v8::Local<v8::Value> = v8::Number::new(scope, result).into();
serde_v8::from_v8(scope, number.into())? number.into()
} }
NativeType::Pointer | NativeType::Function => { NativeType::Pointer | NativeType::Function => {
let result = data_ptr as *const u8 as u64; let result = data_ptr as u64;
let big_int = v8::BigInt::new_from_u64(scope, result); let big_int: v8::Local<v8::Value> =
serde_v8::from_v8(scope, big_int.into())? v8::BigInt::new_from_u64(scope, result).into();
big_int.into()
} }
}) })
} }