2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-08-06 17:28:10 -04:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2022-06-28 05:23:36 -04:00
|
|
|
use deno_core::futures::channel::mpsc;
|
2021-08-06 17:28:10 -04:00
|
|
|
use deno_core::include_js_files;
|
2022-06-20 07:06:04 -04:00
|
|
|
use deno_core::v8;
|
2021-08-06 17:28:10 -04:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::OpState;
|
2022-12-12 09:14:20 -05:00
|
|
|
|
2021-10-05 08:50:00 -04:00
|
|
|
use std::cell::RefCell;
|
2022-07-24 09:08:13 -04:00
|
|
|
use std::mem::size_of;
|
2021-12-17 04:42:45 -05:00
|
|
|
use std::os::raw::c_char;
|
2022-07-24 09:08:13 -04:00
|
|
|
use std::os::raw::c_short;
|
2021-10-13 13:04:44 -04:00
|
|
|
use std::path::Path;
|
2021-12-15 09:41:49 -05:00
|
|
|
use std::ptr;
|
2021-08-06 17:28:10 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-12-12 09:14:20 -05:00
|
|
|
mod call;
|
|
|
|
mod callback;
|
|
|
|
mod dlfcn;
|
|
|
|
mod ir;
|
|
|
|
mod repr;
|
|
|
|
mod r#static;
|
|
|
|
mod symbol;
|
|
|
|
mod turbocall;
|
|
|
|
|
|
|
|
use call::op_ffi_call_nonblocking;
|
|
|
|
use call::op_ffi_call_ptr;
|
|
|
|
use call::op_ffi_call_ptr_nonblocking;
|
|
|
|
use callback::op_ffi_unsafe_callback_create;
|
|
|
|
use callback::op_ffi_unsafe_callback_ref;
|
|
|
|
use callback::op_ffi_unsafe_callback_unref;
|
|
|
|
use dlfcn::op_ffi_load;
|
|
|
|
use dlfcn::ForeignFunction;
|
|
|
|
use r#static::op_ffi_get_static;
|
|
|
|
use repr::*;
|
|
|
|
use symbol::NativeType;
|
|
|
|
use symbol::Symbol;
|
2022-07-11 21:03:05 -04:00
|
|
|
|
2022-07-24 09:08:13 -04:00
|
|
|
#[cfg(not(target_pointer_width = "64"))]
|
|
|
|
compile_error!("platform not supported");
|
|
|
|
|
|
|
|
const _: () = {
|
|
|
|
assert!(size_of::<c_char>() == 1);
|
|
|
|
assert!(size_of::<c_short>() == 2);
|
|
|
|
assert!(size_of::<*const ()>() == 8);
|
|
|
|
};
|
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
thread_local! {
|
2022-06-28 05:23:36 -04:00
|
|
|
static LOCAL_ISOLATE_POINTER: RefCell<*const v8::Isolate> = RefCell::new(ptr::null());
|
2022-06-20 07:06:04 -04:00
|
|
|
}
|
|
|
|
|
2022-12-12 09:14:20 -05:00
|
|
|
pub(crate) const MAX_SAFE_INTEGER: isize = 9007199254740991;
|
|
|
|
pub(crate) const MIN_SAFE_INTEGER: isize = -9007199254740991;
|
2022-07-24 06:41:11 -04:00
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
pub struct Unstable(pub bool);
|
|
|
|
|
|
|
|
fn check_unstable(state: &OpState, api_name: &str) {
|
|
|
|
let unstable = state.borrow::<Unstable>();
|
|
|
|
|
|
|
|
if !unstable.0 {
|
|
|
|
eprintln!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Unstable API '{api_name}'. The --unstable flag must be provided."
|
2021-08-06 17:28:10 -04:00
|
|
|
);
|
|
|
|
std::process::exit(70);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:29:54 -04:00
|
|
|
pub fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
|
|
|
|
let state = state.borrow();
|
|
|
|
check_unstable(&state, api_name)
|
|
|
|
}
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
pub trait FfiPermissions {
|
2021-12-15 09:41:49 -05:00
|
|
|
fn check(&mut self, path: Option<&Path>) -> Result<(), AnyError>;
|
2021-08-06 17:28:10 -04:00
|
|
|
}
|
|
|
|
|
2022-12-12 09:14:20 -05:00
|
|
|
pub(crate) type PendingFfiAsyncWork = Box<dyn FnOnce()>;
|
2021-08-06 17:28:10 -04:00
|
|
|
|
2022-12-12 09:14:20 -05:00
|
|
|
pub(crate) struct FfiState {
|
|
|
|
pub(crate) async_work_sender: mpsc::UnboundedSender<PendingFfiAsyncWork>,
|
|
|
|
pub(crate) async_work_receiver: mpsc::UnboundedReceiver<PendingFfiAsyncWork>,
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
pub fn init<P: FfiPermissions + 'static>(unstable: bool) -> Extension {
|
2023-01-08 17:48:46 -05:00
|
|
|
Extension::builder(env!("CARGO_PKG_NAME"))
|
2023-02-07 16:09:50 -05:00
|
|
|
.esm(include_js_files!("00_ffi.js",))
|
2021-08-06 17:28:10 -04:00
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_ffi_load::decl::<P>(),
|
|
|
|
op_ffi_get_static::decl(),
|
|
|
|
op_ffi_call_nonblocking::decl(),
|
2022-03-25 07:29:54 -04:00
|
|
|
op_ffi_call_ptr::decl::<P>(),
|
|
|
|
op_ffi_call_ptr_nonblocking::decl::<P>(),
|
2022-03-14 13:44:15 -04:00
|
|
|
op_ffi_ptr_of::decl::<P>(),
|
2022-07-23 13:11:06 -04:00
|
|
|
op_ffi_get_buf::decl::<P>(),
|
2022-03-14 13:44:15 -04:00
|
|
|
op_ffi_buf_copy_into::decl::<P>(),
|
|
|
|
op_ffi_cstr_read::decl::<P>(),
|
2022-09-04 23:26:52 -04:00
|
|
|
op_ffi_read_bool::decl::<P>(),
|
2022-03-14 13:44:15 -04:00
|
|
|
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>(),
|
2022-07-24 06:41:11 -04:00
|
|
|
op_ffi_read_i64::decl::<P>(),
|
2022-03-14 13:44:15 -04:00
|
|
|
op_ffi_read_f32::decl::<P>(),
|
|
|
|
op_ffi_read_f64::decl::<P>(),
|
2022-06-20 07:06:04 -04:00
|
|
|
op_ffi_unsafe_callback_create::decl::<P>(),
|
2022-06-28 05:23:36 -04:00
|
|
|
op_ffi_unsafe_callback_ref::decl(),
|
2022-10-15 09:49:46 -04:00
|
|
|
op_ffi_unsafe_callback_unref::decl(),
|
2021-08-06 17:28:10 -04:00
|
|
|
])
|
2022-06-28 05:23:36 -04:00
|
|
|
.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
|
|
|
|
})
|
2021-08-06 17:28:10 -04:00
|
|
|
.state(move |state| {
|
|
|
|
// Stolen from deno_webgpu, is there a better option?
|
|
|
|
state.put(Unstable(unstable));
|
2022-06-28 05:23:36 -04:00
|
|
|
|
|
|
|
let (async_work_sender, async_work_receiver) =
|
|
|
|
mpsc::unbounded::<PendingFfiAsyncWork>();
|
|
|
|
|
|
|
|
state.put(FfiState {
|
|
|
|
async_work_receiver,
|
|
|
|
async_work_sender,
|
|
|
|
});
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|