2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-08-06 17:28:10 -04:00
|
|
|
|
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;
|
2024-09-16 16:39:37 -04:00
|
|
|
use std::path::PathBuf;
|
2021-08-06 17:28:10 -04:00
|
|
|
|
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;
|
2023-05-07 06:31:01 -04:00
|
|
|
use callback::op_ffi_unsafe_callback_close;
|
2022-12-12 09:14:20 -05:00
|
|
|
use callback::op_ffi_unsafe_callback_create;
|
|
|
|
use callback::op_ffi_unsafe_callback_ref;
|
|
|
|
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
|
|
|
|
2024-10-14 18:05:49 -04:00
|
|
|
pub use call::CallError;
|
|
|
|
pub use callback::CallbackError;
|
2024-11-04 12:17:21 -05:00
|
|
|
use deno_permissions::PermissionCheckError;
|
2024-10-14 18:05:49 -04:00
|
|
|
pub use dlfcn::DlfcnError;
|
|
|
|
pub use ir::IRError;
|
|
|
|
pub use r#static::StaticError;
|
|
|
|
pub use repr::ReprError;
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2023-10-12 11:55:50 -04:00
|
|
|
pub const UNSTABLE_FEATURE_NAME: &str = "ffi";
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
pub trait FfiPermissions {
|
2024-11-04 12:17:21 -05:00
|
|
|
fn check_partial_no_path(&mut self) -> Result<(), PermissionCheckError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
|
|
|
fn check_partial_with_path(
|
|
|
|
&mut self,
|
|
|
|
path: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError>;
|
2021-08-06 17:28:10 -04:00
|
|
|
}
|
|
|
|
|
2024-06-06 23:37:53 -04:00
|
|
|
impl FfiPermissions for deno_permissions::PermissionsContainer {
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
fn check_partial_no_path(&mut self) -> Result<(), PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
deno_permissions::PermissionsContainer::check_ffi_partial_no_path(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn check_partial_with_path(
|
|
|
|
&mut self,
|
|
|
|
path: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
deno_permissions::PermissionsContainer::check_ffi_partial_with_path(
|
|
|
|
self, path,
|
|
|
|
)
|
2024-06-06 23:37:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
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>,
|
2023-10-05 09:35:21 -04:00
|
|
|
op_ffi_ptr_of_exact<P>,
|
2023-03-17 14:22:15 -04:00
|
|
|
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>,
|
2023-05-07 06:31:01 -04:00
|
|
|
op_ffi_unsafe_callback_close,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_ffi_unsafe_callback_ref,
|
|
|
|
],
|
|
|
|
esm = [ "00_ffi.js" ],
|
|
|
|
);
|