mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
chore(ext/ffi): Improve FFI static handling (#14924)
This commit is contained in:
parent
1e3713c3bc
commit
a232b4011a
1 changed files with 39 additions and 28 deletions
|
@ -1194,7 +1194,7 @@ fn op_ffi_get_static<'scope>(
|
|||
) -> Result<serde_v8::Value<'scope>, AnyError> {
|
||||
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 {
|
||||
NativeType::Void => {
|
||||
|
@ -1202,68 +1202,79 @@ fn op_ffi_get_static<'scope>(
|
|||
}
|
||||
NativeType::U8 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const u8) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> =
|
||||
v8::Integer::new_from_unsigned(scope, result as u32).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::I8 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const i8) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> =
|
||||
v8::Integer::new(scope, result as i32).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::U16 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const u16) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> =
|
||||
v8::Integer::new_from_unsigned(scope, result as u32).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::I16 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const i16) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> =
|
||||
v8::Integer::new(scope, result as i32).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::U32 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const u32) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> =
|
||||
v8::Integer::new_from_unsigned(scope, result).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::I32 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const i32) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> = v8::Integer::new(scope, result).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::U64 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const u64) };
|
||||
let big_int = v8::BigInt::new_from_u64(scope, result);
|
||||
serde_v8::from_v8(scope, big_int.into())?
|
||||
let big_int: v8::Local<v8::Value> =
|
||||
v8::BigInt::new_from_u64(scope, result).into();
|
||||
big_int.into()
|
||||
}
|
||||
NativeType::I64 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const i64) };
|
||||
let big_int = v8::BigInt::new_from_i64(scope, result);
|
||||
serde_v8::from_v8(scope, big_int.into())?
|
||||
let big_int: v8::Local<v8::Value> =
|
||||
v8::BigInt::new_from_i64(scope, result).into();
|
||||
big_int.into()
|
||||
}
|
||||
NativeType::USize => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const usize) };
|
||||
let big_int = v8::BigInt::new_from_u64(scope, result as u64);
|
||||
serde_v8::from_v8(scope, big_int.into())?
|
||||
let big_int: v8::Local<v8::Value> =
|
||||
v8::BigInt::new_from_u64(scope, result as u64).into();
|
||||
big_int.into()
|
||||
}
|
||||
NativeType::ISize => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const isize) };
|
||||
let big_int = v8::BigInt::new_from_i64(scope, result as i64);
|
||||
serde_v8::from_v8(scope, big_int.into())?
|
||||
let big_int: v8::Local<v8::Value> =
|
||||
v8::BigInt::new_from_i64(scope, result as i64).into();
|
||||
big_int.into()
|
||||
}
|
||||
NativeType::F32 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const f32) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> =
|
||||
v8::Number::new(scope, result as f64).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::F64 => {
|
||||
let result = unsafe { ptr::read_unaligned(data_ptr as *const f64) };
|
||||
let number = v8::Number::new(scope, result as f64);
|
||||
serde_v8::from_v8(scope, number.into())?
|
||||
let number: v8::Local<v8::Value> = v8::Number::new(scope, result).into();
|
||||
number.into()
|
||||
}
|
||||
NativeType::Pointer | NativeType::Function => {
|
||||
let result = data_ptr as *const u8 as u64;
|
||||
let big_int = v8::BigInt::new_from_u64(scope, result);
|
||||
serde_v8::from_v8(scope, big_int.into())?
|
||||
let result = data_ptr as u64;
|
||||
let big_int: v8::Local<v8::Value> =
|
||||
v8::BigInt::new_from_u64(scope, result).into();
|
||||
big_int.into()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue