1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

chore(ext/ffi): migrate part of FFI to op2 (#20699)

This commit is contained in:
Matt Mastracci 2023-09-27 07:54:43 -06:00 committed by GitHub
parent 507f24c474
commit 8ef52b3e6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 74 deletions

View file

@ -11,9 +11,8 @@ use crate::ForeignFunction;
use deno_core::anyhow::anyhow;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::serde_json::Value;
use deno_core::serde_v8;
use deno_core::serde_v8::ExternalPointer;
use deno_core::unsync::spawn_blocking;
use deno_core::v8;
@ -273,14 +272,15 @@ fn ffi_call(
}
}
#[op(v8)]
pub fn op_ffi_call_ptr_nonblocking<'scope, FP>(
scope: &mut v8::HandleScope<'scope>,
#[op2(async)]
#[serde]
pub fn op_ffi_call_ptr_nonblocking<FP>(
scope: &mut v8::HandleScope,
state: Rc<RefCell<OpState>>,
pointer: *mut c_void,
def: ForeignFunction,
parameters: serde_v8::Value<'scope>,
out_buffer: Option<serde_v8::Value<'scope>>,
#[serde] def: ForeignFunction,
parameters: v8::Local<v8::Array>,
out_buffer: Option<v8::Local<v8::TypedArray>>,
) -> Result<impl Future<Output = Result<FfiValue, AnyError>>, AnyError>
where
FP: FfiPermissions + 'static,
@ -294,9 +294,6 @@ where
let symbol = PtrSymbol::new(pointer, &def)?;
let call_args = ffi_parse_args(scope, parameters, &def.parameters)?;
let out_buffer = out_buffer
.map(|v| v8::Local::<v8::TypedArray>::try_from(v.v8_value).unwrap());
let out_buffer_ptr = out_buffer_as_ptr(scope, out_buffer);
let join_handle = spawn_blocking(move || {
@ -321,16 +318,16 @@ where
}
/// A non-blocking FFI call.
#[op(v8)]
pub fn op_ffi_call_nonblocking<'scope>(
scope: &mut v8::HandleScope<'scope>,
#[op2(async)]
#[serde]
pub fn op_ffi_call_nonblocking(
scope: &mut v8::HandleScope,
state: Rc<RefCell<OpState>>,
rid: ResourceId,
symbol: String,
parameters: serde_v8::Value<'scope>,
out_buffer: Option<serde_v8::Value<'scope>>,
) -> Result<impl Future<Output = Result<FfiValue, AnyError>> + 'static, AnyError>
{
#[smi] rid: ResourceId,
#[string] symbol: String,
parameters: v8::Local<v8::Array>,
out_buffer: Option<v8::Local<v8::TypedArray>>,
) -> Result<impl Future<Output = Result<FfiValue, AnyError>>, AnyError> {
let symbol = {
let state = state.borrow();
let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;
@ -342,8 +339,6 @@ pub fn op_ffi_call_nonblocking<'scope>(
};
let call_args = ffi_parse_args(scope, parameters, &symbol.parameter_types)?;
let out_buffer = out_buffer
.map(|v| v8::Local::<v8::TypedArray>::try_from(v.v8_value).unwrap());
let out_buffer_ptr = out_buffer_as_ptr(scope, out_buffer);
let join_handle = spawn_blocking(move || {
@ -373,14 +368,15 @@ pub fn op_ffi_call_nonblocking<'scope>(
})
}
#[op(v8)]
pub fn op_ffi_call_ptr<FP, 'scope>(
scope: &mut v8::HandleScope<'scope>,
#[op2]
#[serde]
pub fn op_ffi_call_ptr<FP>(
scope: &mut v8::HandleScope,
state: Rc<RefCell<OpState>>,
pointer: *mut c_void,
def: ForeignFunction,
parameters: serde_v8::Value<'scope>,
out_buffer: Option<serde_v8::Value<'scope>>,
#[serde] def: ForeignFunction,
parameters: v8::Local<v8::Array>,
out_buffer: Option<v8::Local<v8::TypedArray>>,
) -> Result<FfiValue, AnyError>
where
FP: FfiPermissions + 'static,
@ -395,8 +391,6 @@ where
let symbol = PtrSymbol::new(pointer, &def)?;
let call_args = ffi_parse_args(scope, parameters, &def.parameters)?;
let out_buffer = out_buffer
.map(|v| v8::Local::<v8::TypedArray>::try_from(v.v8_value).unwrap());
let out_buffer_ptr = out_buffer_as_ptr(scope, out_buffer);
let result = ffi_call(

View file

@ -11,8 +11,7 @@ use crate::MIN_SAFE_INTEGER;
use deno_core::error::AnyError;
use deno_core::futures::channel::mpsc;
use deno_core::futures::task::AtomicWaker;
use deno_core::op;
use deno_core::serde_v8;
use deno_core::op2;
use deno_core::v8;
use deno_core::CancelFuture;
use deno_core::CancelHandle;
@ -506,10 +505,10 @@ unsafe fn do_ffi_callback(
};
}
#[op]
#[op2(async)]
pub fn op_ffi_unsafe_callback_ref(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<impl Future<Output = Result<(), AnyError>>, AnyError> {
let state = state.borrow();
let callback_resource =
@ -534,13 +533,13 @@ pub struct RegisterCallbackArgs {
result: NativeType,
}
#[op(v8)]
#[op2]
pub fn op_ffi_unsafe_callback_create<FP, 'scope>(
state: &mut OpState,
scope: &mut v8::HandleScope<'scope>,
args: RegisterCallbackArgs,
cb: serde_v8::Value<'scope>,
) -> Result<serde_v8::Value<'scope>, AnyError>
#[serde] args: RegisterCallbackArgs,
cb: v8::Local<v8::Function>,
) -> Result<v8::Local<'scope, v8::Value>, AnyError>
where
FP: FfiPermissions + 'static,
{
@ -548,9 +547,6 @@ where
let permissions = state.borrow_mut::<FP>();
permissions.check_partial(None)?;
let v8_value = cb.v8_value;
let cb = v8::Local::<v8::Function>::try_from(v8_value)?;
let thread_id: u32 = LOCAL_THREAD_ID.with(|s| {
let value = *s.borrow();
if value == 0 {
@ -622,14 +618,14 @@ where
array.set_index(scope, 1, ptr_local);
let array_value: v8::Local<v8::Value> = array.into();
Ok(array_value.into())
Ok(array_value)
}
#[op(v8)]
#[op2]
pub fn op_ffi_unsafe_callback_close(
state: &mut OpState,
scope: &mut v8::HandleScope,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<(), AnyError> {
// SAFETY: This drops the closure and the callback info associated with it.
// Any retained function pointers to the closure become dangling pointers.

View file

@ -8,7 +8,7 @@ use crate::turbocall;
use crate::FfiPermissions;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::serde_v8;
use deno_core::v8;
use deno_core::OpState;
@ -131,11 +131,12 @@ pub struct FfiLoadArgs {
symbols: HashMap<String, ForeignSymbol>,
}
#[op(v8)]
pub fn op_ffi_load<FP, 'scope>(
#[op2]
#[serde]
pub fn op_ffi_load<'scope, FP>(
scope: &mut v8::HandleScope<'scope>,
state: &mut OpState,
args: FfiLoadArgs,
#[serde] args: FfiLoadArgs,
) -> Result<(ResourceId, serde_v8::Value<'scope>), AnyError>
where
FP: FfiPermissions + 'static,

View file

@ -483,7 +483,7 @@ pub fn ffi_parse_function_arg(
pub fn ffi_parse_args<'scope>(
scope: &mut v8::HandleScope<'scope>,
args: serde_v8::Value<'scope>,
args: v8::Local<v8::Array>,
parameter_types: &[NativeType],
) -> Result<Vec<NativeValue>, AnyError>
where
@ -493,8 +493,6 @@ where
return Ok(vec![]);
}
let args = v8::Local::<v8::Array>::try_from(args.v8_value)
.map_err(|_| type_error("Invalid FFI parameters, expected Array"))?;
let mut ffi_args: Vec<NativeValue> =
Vec::with_capacity(parameter_types.len());

View file

@ -6,21 +6,21 @@ use crate::MAX_SAFE_INTEGER;
use crate::MIN_SAFE_INTEGER;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::serde_v8;
use deno_core::op2;
use deno_core::v8;
use deno_core::OpState;
use deno_core::ResourceId;
use std::ptr;
#[op(v8)]
#[op2]
pub fn op_ffi_get_static<'scope>(
scope: &mut v8::HandleScope<'scope>,
state: &mut deno_core::OpState,
rid: ResourceId,
name: String,
static_type: NativeType,
state: &mut OpState,
#[smi] rid: ResourceId,
#[string] name: String,
#[serde] static_type: NativeType,
optional: bool,
) -> Result<serde_v8::Value<'scope>, AnyError> {
) -> Result<v8::Local<'scope, v8::Value>, AnyError> {
let resource = state.resource_table.get::<DynamicLibraryResource>(rid)?;
let data_ptr = match resource.get_static(name) {
@ -28,7 +28,7 @@ pub fn op_ffi_get_static<'scope>(
Err(err) => {
if optional {
let null: v8::Local<v8::Value> = v8::null(scope).into();
return Ok(null.into());
return Ok(null);
} else {
Err(err)
}
@ -44,48 +44,48 @@ pub fn op_ffi_get_static<'scope>(
let result = unsafe { ptr::read_unaligned(data_ptr as *const bool) };
let boolean: v8::Local<v8::Value> =
v8::Boolean::new(scope, result).into();
boolean.into()
boolean
}
NativeType::U8 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const u8) };
let number: v8::Local<v8::Value> =
v8::Integer::new_from_unsigned(scope, result as u32).into();
number.into()
number
}
NativeType::I8 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const i8) };
let number: v8::Local<v8::Value> =
v8::Integer::new(scope, result as i32).into();
number.into()
number
}
NativeType::U16 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const u16) };
let number: v8::Local<v8::Value> =
v8::Integer::new_from_unsigned(scope, result as u32).into();
number.into()
number
}
NativeType::I16 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const i16) };
let number: v8::Local<v8::Value> =
v8::Integer::new(scope, result as i32).into();
number.into()
number
}
NativeType::U32 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const u32) };
let number: v8::Local<v8::Value> =
v8::Integer::new_from_unsigned(scope, result).into();
number.into()
number
}
NativeType::I32 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const i32) };
let number: v8::Local<v8::Value> = v8::Integer::new(scope, result).into();
number.into()
number
}
NativeType::U64 => {
// SAFETY: ptr is user provided
@ -95,7 +95,7 @@ pub fn op_ffi_get_static<'scope>(
} else {
v8::Number::new(scope, result as f64).into()
};
integer.into()
integer
}
NativeType::I64 => {
// SAFETY: ptr is user provided
@ -107,7 +107,7 @@ pub fn op_ffi_get_static<'scope>(
} else {
v8::Number::new(scope, result as f64).into()
};
integer.into()
integer
}
NativeType::USize => {
// SAFETY: ptr is user provided
@ -118,7 +118,7 @@ pub fn op_ffi_get_static<'scope>(
} else {
v8::Number::new(scope, result as f64).into()
};
integer.into()
integer
}
NativeType::ISize => {
// SAFETY: ptr is user provided
@ -129,25 +129,25 @@ pub fn op_ffi_get_static<'scope>(
} else {
v8::Number::new(scope, result as f64).into()
};
integer.into()
integer
}
NativeType::F32 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const f32) };
let number: v8::Local<v8::Value> =
v8::Number::new(scope, result as f64).into();
number.into()
number
}
NativeType::F64 => {
// SAFETY: ptr is user provided
let result = unsafe { ptr::read_unaligned(data_ptr as *const f64) };
let number: v8::Local<v8::Value> = v8::Number::new(scope, result).into();
number.into()
number
}
NativeType::Pointer | NativeType::Function | NativeType::Buffer => {
let external: v8::Local<v8::Value> =
v8::External::new(scope, data_ptr).into();
external.into()
external
}
NativeType::Struct(_) => {
return Err(type_error("Invalid FFI static type 'struct'"));