summaryrefslogtreecommitdiff
path: root/ext/ffi/symbol.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-12-12 06:14:20 -0800
committerGitHub <noreply@github.com>2022-12-12 14:14:20 +0000
commita2db70a8d0820722695e9094c8dbc888bde1ffa3 (patch)
tree6885e2aee424ab0264a8b01df9b0bafbcfe85792 /ext/ffi/symbol.rs
parent890b0653104388bfef60ca7ebd0af758caf5f0be (diff)
refactor(ext/ffi): split into multiple parts (#16950)
- [x] `dlfcn.rs` - `dlopen()`-related code. - [x] `turbocall.rs` - Call trampoline JIT compiler. - [x] `repr.rs` - Pointer representation. Home of the UnsafePointerView ops. - [x] `symbol.rs` - Function symbol related code. - [x] `callback.rs` - Home of `Deno.UnsafeCallback` ops. - [x] `ir.rs` - Intermediate representation for values. Home of the `NativeValue` type. - [x] `call.rs` - Generic call ops. Home to everything related to calling FFI symbols. - [x] `static.rs` - static symbol support I find easier to work with this setup, I eventually want to expand TurboCall to unroll type conversion loop in generic calls, generate code for individual symbols (lazy function pointers), etc.
Diffstat (limited to 'ext/ffi/symbol.rs')
-rw-r--r--ext/ffi/symbol.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/ext/ffi/symbol.rs b/ext/ffi/symbol.rs
new file mode 100644
index 000000000..0248c1fff
--- /dev/null
+++ b/ext/ffi/symbol.rs
@@ -0,0 +1,63 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+/// Defines the accepted types that can be used as
+/// parameters and return values in FFI.
+#[derive(Clone, Copy, Debug, serde::Deserialize, Eq, PartialEq)]
+#[serde(rename_all = "lowercase")]
+pub enum NativeType {
+ Void,
+ Bool,
+ U8,
+ I8,
+ U16,
+ I16,
+ U32,
+ I32,
+ U64,
+ I64,
+ USize,
+ ISize,
+ F32,
+ F64,
+ Pointer,
+ Buffer,
+ Function,
+}
+
+impl From<NativeType> for libffi::middle::Type {
+ fn from(native_type: NativeType) -> Self {
+ match native_type {
+ NativeType::Void => libffi::middle::Type::void(),
+ NativeType::U8 | NativeType::Bool => libffi::middle::Type::u8(),
+ NativeType::I8 => libffi::middle::Type::i8(),
+ NativeType::U16 => libffi::middle::Type::u16(),
+ NativeType::I16 => libffi::middle::Type::i16(),
+ NativeType::U32 => libffi::middle::Type::u32(),
+ NativeType::I32 => libffi::middle::Type::i32(),
+ NativeType::U64 => libffi::middle::Type::u64(),
+ NativeType::I64 => libffi::middle::Type::i64(),
+ NativeType::USize => libffi::middle::Type::usize(),
+ NativeType::ISize => libffi::middle::Type::isize(),
+ NativeType::F32 => libffi::middle::Type::f32(),
+ NativeType::F64 => libffi::middle::Type::f64(),
+ NativeType::Pointer | NativeType::Buffer | NativeType::Function => {
+ libffi::middle::Type::pointer()
+ }
+ }
+ }
+}
+
+#[derive(Clone)]
+pub struct Symbol {
+ pub cif: libffi::middle::Cif,
+ pub ptr: libffi::middle::CodePtr,
+ pub parameter_types: Vec<NativeType>,
+ pub result_type: NativeType,
+ pub can_callback: bool,
+}
+
+#[allow(clippy::non_send_fields_in_send_ty)]
+// SAFETY: unsafe trait must have unsafe implementation
+unsafe impl Send for Symbol {}
+// SAFETY: unsafe trait must have unsafe implementation
+unsafe impl Sync for Symbol {}