diff --git a/crates/c-api/src/extern.rs b/crates/c-api/src/extern.rs index 4a71807cd9..9f50f20f90 100644 --- a/crates/c-api/src/extern.rs +++ b/crates/c-api/src/extern.rs @@ -1,6 +1,7 @@ +use crate::host_ref::HostRef; use crate::wasm_externkind_t; use crate::{wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_memory_t, wasm_table_t}; -use wasmtime::{ExternType, Func, Global, HostRef, Memory, Table}; +use wasmtime::{ExternType, Func, Global, Memory, Table}; #[derive(Clone)] pub struct wasm_extern_t { diff --git a/crates/c-api/src/func.rs b/crates/c-api/src/func.rs index 855770dff3..5f62ec30b2 100644 --- a/crates/c-api/src/func.rs +++ b/crates/c-api/src/func.rs @@ -1,3 +1,4 @@ +use crate::host_ref::HostRef; use crate::{wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t}; use crate::{wasm_name_t, wasm_trap_t, wasmtime_error_t, ExternHost}; use anyhow::anyhow; @@ -5,7 +6,7 @@ use std::ffi::c_void; use std::panic::{self, AssertUnwindSafe}; use std::ptr; use std::str; -use wasmtime::{Caller, Extern, Func, HostRef, Trap}; +use wasmtime::{Caller, Extern, Func, Trap}; #[derive(Clone)] #[repr(transparent)] diff --git a/crates/c-api/src/global.rs b/crates/c-api/src/global.rs index 29e9477fa6..3d4f768ba1 100644 --- a/crates/c-api/src/global.rs +++ b/crates/c-api/src/global.rs @@ -1,7 +1,8 @@ +use crate::host_ref::HostRef; use crate::{handle_result, wasmtime_error_t}; use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, ExternHost}; use std::ptr; -use wasmtime::{Global, HostRef}; +use wasmtime::Global; #[derive(Clone)] #[repr(transparent)] diff --git a/crates/c-api/src/host_ref.rs b/crates/c-api/src/host_ref.rs new file mode 100644 index 0000000000..41ac6a16f1 --- /dev/null +++ b/crates/c-api/src/host_ref.rs @@ -0,0 +1,101 @@ +use std::any::Any; +use std::cell::{self, RefCell}; +use std::convert::TryFrom; +use std::marker::PhantomData; +use wasmtime::{ExternRef, Store}; + +/// Represents a piece of data located in the host environment. +#[derive(Debug)] +pub struct HostRef +where + T: 'static + Any, +{ + externref: ExternRef, + _phantom: PhantomData, +} + +impl HostRef +where + T: 'static + Any, +{ + /// Creates a new `HostRef` from `T`. + pub fn new(store: &Store, item: T) -> HostRef { + HostRef { + externref: ExternRef::new(store, RefCell::new(item)), + _phantom: PhantomData, + } + } + + /// Immutably borrows the wrapped data. + /// + /// # Panics + /// + /// Panics if the value is currently mutably borrowed. + pub fn borrow(&self) -> cell::Ref { + self.inner().borrow() + } + + /// Mutably borrows the wrapped data. + /// + /// # Panics + /// + /// Panics if the `HostRef` is already borrowed. + pub fn borrow_mut(&self) -> cell::RefMut { + self.inner().borrow_mut() + } + + /// Returns true if the two `HostRef`'s point to the same value (not just + /// values that compare as equal). + pub fn ptr_eq(&self, other: &HostRef) -> bool { + self.externref.ptr_eq(&other.externref) + } + + fn inner(&self) -> &RefCell { + self.externref + .data() + .downcast_ref::>() + .expect("`HostRef`s always wrap an `ExternRef` of `RefCell`") + } +} + +impl AsRef for HostRef { + fn as_ref(&self) -> &ExternRef { + &self.externref + } +} + +impl From> for ExternRef +where + T: 'static + Any, +{ + fn from(host: HostRef) -> ExternRef { + host.externref + } +} + +impl TryFrom for HostRef +where + T: 'static + Any, +{ + type Error = ExternRef; + + fn try_from(externref: ExternRef) -> Result { + if externref.data().is::>() { + Ok(HostRef { + externref, + _phantom: PhantomData, + }) + } else { + Err(externref) + } + } +} + +impl Clone for HostRef { + fn clone(&self) -> HostRef { + HostRef { + externref: self.externref.clone(), + _phantom: PhantomData, + } + } +} diff --git a/crates/c-api/src/instance.rs b/crates/c-api/src/instance.rs index a833f462eb..d974a5f2dd 100644 --- a/crates/c-api/src/instance.rs +++ b/crates/c-api/src/instance.rs @@ -1,9 +1,10 @@ +use crate::host_ref::HostRef; use crate::{wasm_extern_t, wasm_extern_vec_t, wasm_module_t, wasm_trap_t}; use crate::{wasm_store_t, wasmtime_error_t, ExternHost}; use anyhow::Result; use std::cell::RefCell; use std::ptr; -use wasmtime::{Extern, HostRef, Instance, Store, Trap}; +use wasmtime::{Extern, Instance, Store, Trap}; #[repr(C)] #[derive(Clone)] diff --git a/crates/c-api/src/lib.rs b/crates/c-api/src/lib.rs index 9a62dad2be..ae5eb75575 100644 --- a/crates/c-api/src/lib.rs +++ b/crates/c-api/src/lib.rs @@ -11,6 +11,7 @@ mod error; mod r#extern; mod func; mod global; +mod host_ref; mod instance; mod linker; mod memory; diff --git a/crates/c-api/src/linker.rs b/crates/c-api/src/linker.rs index 24c565c383..0152d2d49b 100644 --- a/crates/c-api/src/linker.rs +++ b/crates/c-api/src/linker.rs @@ -1,8 +1,9 @@ +use crate::host_ref::HostRef; use crate::{bad_utf8, handle_result, wasmtime_error_t}; use crate::{wasm_extern_t, wasm_store_t, ExternHost}; use crate::{wasm_func_t, wasm_instance_t, wasm_module_t, wasm_name_t, wasm_trap_t}; use std::str; -use wasmtime::{Extern, HostRef, Linker}; +use wasmtime::{Extern, Linker}; #[repr(C)] pub struct wasmtime_linker_t { diff --git a/crates/c-api/src/memory.rs b/crates/c-api/src/memory.rs index aa3e2e4db3..ed25b24bea 100644 --- a/crates/c-api/src/memory.rs +++ b/crates/c-api/src/memory.rs @@ -1,5 +1,6 @@ +use crate::host_ref::HostRef; use crate::{wasm_extern_t, wasm_memorytype_t, wasm_store_t, ExternHost}; -use wasmtime::{HostRef, Memory}; +use wasmtime::Memory; #[derive(Clone)] #[repr(transparent)] diff --git a/crates/c-api/src/module.rs b/crates/c-api/src/module.rs index 2349e20bef..605c3e7233 100644 --- a/crates/c-api/src/module.rs +++ b/crates/c-api/src/module.rs @@ -1,8 +1,9 @@ +use crate::host_ref::HostRef; use crate::{handle_result, wasmtime_error_t}; use crate::{wasm_byte_vec_t, wasm_exporttype_vec_t, wasm_importtype_vec_t}; use crate::{wasm_exporttype_t, wasm_importtype_t, wasm_store_t}; use std::ptr; -use wasmtime::{HostRef, Module}; +use wasmtime::Module; #[repr(C)] #[derive(Clone)] diff --git a/crates/c-api/src/table.rs b/crates/c-api/src/table.rs index e49daddef8..773164a59c 100644 --- a/crates/c-api/src/table.rs +++ b/crates/c-api/src/table.rs @@ -1,7 +1,8 @@ +use crate::host_ref::HostRef; use crate::{handle_result, wasm_func_t, wasm_ref_t, wasmtime_error_t}; use crate::{wasm_extern_t, wasm_store_t, wasm_tabletype_t, ExternHost}; use std::ptr; -use wasmtime::{HostRef, Table, Val}; +use wasmtime::{Table, Val}; #[derive(Clone)] #[repr(transparent)] diff --git a/crates/c-api/src/trap.rs b/crates/c-api/src/trap.rs index c7a906e3eb..633577573d 100644 --- a/crates/c-api/src/trap.rs +++ b/crates/c-api/src/trap.rs @@ -1,6 +1,7 @@ +use crate::host_ref::HostRef; use crate::{wasm_frame_vec_t, wasm_instance_t, wasm_name_t, wasm_store_t}; use once_cell::unsync::OnceCell; -use wasmtime::{HostRef, Store, Trap}; +use wasmtime::{Store, Trap}; #[repr(C)] #[derive(Clone)] diff --git a/crates/c-api/src/wasi.rs b/crates/c-api/src/wasi.rs index f1db5d6c6b..4a7ed6c30a 100644 --- a/crates/c-api/src/wasi.rs +++ b/crates/c-api/src/wasi.rs @@ -1,4 +1,5 @@ //! The WASI embedding API definitions for Wasmtime. +use crate::host_ref::HostRef; use crate::{wasm_extern_t, wasm_importtype_t, wasm_store_t, wasm_trap_t, ExternHost}; use anyhow::Result; use std::collections::HashMap; @@ -12,7 +13,7 @@ use wasi_common::{ old::snapshot_0::WasiCtxBuilder as WasiSnapshot0CtxBuilder, preopen_dir, WasiCtxBuilder as WasiPreview1CtxBuilder, }; -use wasmtime::{HostRef, Linker, Store, Trap}; +use wasmtime::{Linker, Store, Trap}; use wasmtime_wasi::{old::snapshot_0::Wasi as WasiSnapshot0, Wasi as WasiPreview1}; unsafe fn cstr_to_path<'a>(path: *const c_char) -> Option<&'a Path> { diff --git a/crates/wasmtime/src/lib.rs b/crates/wasmtime/src/lib.rs index e08fbb15f9..b31b806b49 100644 --- a/crates/wasmtime/src/lib.rs +++ b/crates/wasmtime/src/lib.rs @@ -29,7 +29,7 @@ pub use crate::func::*; pub use crate::instance::Instance; pub use crate::linker::*; pub use crate::module::Module; -pub use crate::r#ref::{ExternRef, HostRef}; +pub use crate::r#ref::ExternRef; pub use crate::runtime::*; pub use crate::trap::Trap; pub use crate::types::*; diff --git a/crates/wasmtime/src/ref.rs b/crates/wasmtime/src/ref.rs index b8bd3693e5..3f87dbd8a2 100644 --- a/crates/wasmtime/src/ref.rs +++ b/crates/wasmtime/src/ref.rs @@ -1,10 +1,8 @@ #![allow(missing_docs)] use std::any::Any; -use std::cell::{self, RefCell}; -use std::convert::TryFrom; +use std::cell::RefCell; use std::fmt; -use std::marker::PhantomData; use std::rc::{Rc, Weak}; use wasmtime_runtime::VMExternRef; @@ -81,99 +79,3 @@ impl fmt::Debug for ExternRef { .finish() } } - -/// Represents a piece of data located in the host environment. -#[derive(Debug)] -pub struct HostRef -where - T: 'static + Any, -{ - externref: ExternRef, - _phantom: PhantomData, -} - -impl HostRef -where - T: 'static + Any, -{ - /// Creates a new `HostRef` from `T`. - pub fn new(store: &crate::Store, item: T) -> HostRef { - HostRef { - externref: ExternRef::new(store, RefCell::new(item)), - _phantom: PhantomData, - } - } - - /// Immutably borrows the wrapped data. - /// - /// # Panics - /// - /// Panics if the value is currently mutably borrowed. - pub fn borrow(&self) -> cell::Ref { - self.inner().borrow() - } - - /// Mutably borrows the wrapped data. - /// - /// # Panics - /// - /// Panics if the `HostRef` is already borrowed. - pub fn borrow_mut(&self) -> cell::RefMut { - self.inner().borrow_mut() - } - - /// Returns true if the two `HostRef`'s point to the same value (not just - /// values that compare as equal). - pub fn ptr_eq(&self, other: &HostRef) -> bool { - self.externref.ptr_eq(&other.externref) - } - - fn inner(&self) -> &RefCell { - self.externref - .inner - .downcast_ref::>() - .expect("`HostRef`s always wrap an `ExternRef` of `RefCell`") - } -} - -impl AsRef for HostRef { - fn as_ref(&self) -> &ExternRef { - &self.externref - } -} - -impl From> for ExternRef -where - T: 'static + Any, -{ - fn from(host: HostRef) -> ExternRef { - host.externref - } -} - -impl TryFrom for HostRef -where - T: 'static + Any, -{ - type Error = ExternRef; - - fn try_from(externref: ExternRef) -> Result { - if externref.inner.is::>() { - Ok(HostRef { - externref, - _phantom: PhantomData, - }) - } else { - Err(externref) - } - } -} - -impl Clone for HostRef { - fn clone(&self) -> HostRef { - HostRef { - externref: self.externref.clone(), - _phantom: PhantomData, - } - } -}