diff options
Diffstat (limited to 'ext/ffi/lib.rs')
-rw-r--r-- | ext/ffi/lib.rs | 168 |
1 files changed, 78 insertions, 90 deletions
diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 2b90cd1c4..1fd01c9d2 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -2,10 +2,7 @@ use deno_core::error::AnyError; use deno_core::futures::channel::mpsc; -use deno_core::include_js_files; use deno_core::v8; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use std::cell::RefCell; @@ -82,92 +79,83 @@ pub(crate) struct FfiState { pub(crate) async_work_receiver: mpsc::UnboundedReceiver<PendingFfiAsyncWork>, } -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"]) -} - -fn ops<P: FfiPermissions + 'static>( - ext: &mut ExtensionBuilder, - unstable: bool, -) -> &mut ExtensionBuilder { - ext - .ops(vec![ - op_ffi_load::decl::<P>(), - op_ffi_get_static::decl(), - op_ffi_call_nonblocking::decl(), - op_ffi_call_ptr::decl::<P>(), - op_ffi_call_ptr_nonblocking::decl::<P>(), - op_ffi_ptr_create::decl::<P>(), - op_ffi_ptr_equals::decl::<P>(), - op_ffi_ptr_of::decl::<P>(), - op_ffi_ptr_offset::decl::<P>(), - op_ffi_ptr_value::decl::<P>(), - op_ffi_get_buf::decl::<P>(), - op_ffi_buf_copy_into::decl::<P>(), - op_ffi_cstr_read::decl::<P>(), - op_ffi_read_bool::decl::<P>(), - op_ffi_read_u8::decl::<P>(), - op_ffi_read_i8::decl::<P>(), - op_ffi_read_u16::decl::<P>(), - op_ffi_read_i16::decl::<P>(), - op_ffi_read_u32::decl::<P>(), - op_ffi_read_i32::decl::<P>(), - op_ffi_read_u64::decl::<P>(), - op_ffi_read_i64::decl::<P>(), - op_ffi_read_f32::decl::<P>(), - op_ffi_read_f64::decl::<P>(), - op_ffi_read_ptr::decl::<P>(), - op_ffi_unsafe_callback_create::decl::<P>(), - op_ffi_unsafe_callback_ref::decl(), - ]) - .event_loop_middleware(|op_state_rc, _cx| { - // FFI callbacks coming in from other threads will call in and get queued. - let mut maybe_scheduling = false; - - let mut work_items: Vec<PendingFfiAsyncWork> = vec![]; - - { - let mut op_state = op_state_rc.borrow_mut(); - let ffi_state = op_state.borrow_mut::<FfiState>(); - - while let Ok(Some(async_work_fut)) = - ffi_state.async_work_receiver.try_next() - { - // Move received items to a temporary vector so that we can drop the `op_state` borrow before we do the work. - work_items.push(async_work_fut); - maybe_scheduling = true; - } - - drop(op_state); - } - while let Some(async_work_fut) = work_items.pop() { - async_work_fut(); - } - - maybe_scheduling - }) - .state(move |state| { - // Stolen from deno_webgpu, is there a better option? - state.put(Unstable(unstable)); - - let (async_work_sender, async_work_receiver) = - mpsc::unbounded::<PendingFfiAsyncWork>(); - - state.put(FfiState { - async_work_receiver, - async_work_sender, - }); - }) -} - -pub fn init_ops_and_esm<P: FfiPermissions + 'static>( - unstable: bool, -) -> Extension { - ops::<P>(&mut ext(), unstable) - .esm(include_js_files!("00_ffi.js",)) - .build() -} +deno_core::extension!(deno_ffi, + deps = [ deno_web ], + parameters = [P: FfiPermissions], + ops = [ + op_ffi_load<P>, + op_ffi_get_static, + op_ffi_call_nonblocking, + op_ffi_call_ptr<P>, + op_ffi_call_ptr_nonblocking<P>, + op_ffi_ptr_create<P>, + op_ffi_ptr_equals<P>, + op_ffi_ptr_of<P>, + op_ffi_ptr_offset<P>, + op_ffi_ptr_value<P>, + op_ffi_get_buf<P>, + op_ffi_buf_copy_into<P>, + op_ffi_cstr_read<P>, + op_ffi_read_bool<P>, + op_ffi_read_u8<P>, + op_ffi_read_i8<P>, + op_ffi_read_u16<P>, + op_ffi_read_i16<P>, + op_ffi_read_u32<P>, + op_ffi_read_i32<P>, + op_ffi_read_u64<P>, + op_ffi_read_i64<P>, + op_ffi_read_f32<P>, + op_ffi_read_f64<P>, + op_ffi_read_ptr<P>, + op_ffi_unsafe_callback_create<P>, + op_ffi_unsafe_callback_ref, + ], + esm = [ "00_ffi.js" ], + config = { + unstable: bool, + }, + state = |state, unstable| { + // Stolen from deno_webgpu, is there a better option? + state.put(Unstable(unstable)); + + let (async_work_sender, async_work_receiver) = + mpsc::unbounded::<PendingFfiAsyncWork>(); + + state.put(FfiState { + async_work_receiver, + async_work_sender, + }); + }, + event_loop_middleware = event_loop_middleware, +); + +fn event_loop_middleware( + op_state_rc: Rc<RefCell<OpState>>, + _cx: &mut std::task::Context, +) -> bool { + // FFI callbacks coming in from other threads will call in and get queued. + let mut maybe_scheduling = false; + + let mut work_items: Vec<PendingFfiAsyncWork> = vec![]; + + { + let mut op_state = op_state_rc.borrow_mut(); + let ffi_state = op_state.borrow_mut::<FfiState>(); + + while let Ok(Some(async_work_fut)) = + ffi_state.async_work_receiver.try_next() + { + // Move received items to a temporary vector so that we can drop the `op_state` borrow before we do the work. + work_items.push(async_work_fut); + maybe_scheduling = true; + } + + drop(op_state); + } + while let Some(async_work_fut) = work_items.pop() { + async_work_fut(); + } -pub fn init_ops<P: FfiPermissions + 'static>(unstable: bool) -> Extension { - ops::<P>(&mut ext(), unstable).build() + maybe_scheduling } |