wasmtime: Rip out incomplete/incorrect externref "host info" support
Better to be loud that we don't support attaching arbitrary host info to `externref`s than to limp along and pretend we do support it. Supporting it properly won't reuse any of this code anyways.
This commit is contained in:
@@ -355,10 +355,9 @@ impl Table {
|
||||
Some(unsafe { from_checked_anyfunc(f, &self.instance.store) })
|
||||
}
|
||||
runtime::TableElement::ExternRef(None) => Some(Val::ExternRef(None)),
|
||||
runtime::TableElement::ExternRef(Some(x)) => Some(Val::ExternRef(Some(ExternRef {
|
||||
inner: x,
|
||||
store: self.instance.store.weak(),
|
||||
}))),
|
||||
runtime::TableElement::ExternRef(Some(x)) => {
|
||||
Some(Val::ExternRef(Some(ExternRef { inner: x })))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
53
crates/wasmtime/src/ref.rs
Normal file → Executable file
53
crates/wasmtime/src/ref.rs
Normal file → Executable file
@@ -1,34 +1,22 @@
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use std::any::Any;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
use std::rc::{Rc, Weak};
|
||||
use wasmtime_runtime::VMExternRef;
|
||||
|
||||
/// Represents an opaque reference to any data within WebAssembly.
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ExternRef {
|
||||
pub(crate) inner: VMExternRef,
|
||||
pub(crate) store: Weak<crate::runtime::StoreInner>,
|
||||
}
|
||||
|
||||
impl ExternRef {
|
||||
/// Creates a new instance of `ExternRef` wrapping the given value.
|
||||
pub fn new<T>(store: &crate::Store, value: T) -> ExternRef
|
||||
pub fn new<T>(value: T) -> ExternRef
|
||||
where
|
||||
T: 'static + Any,
|
||||
{
|
||||
let inner = VMExternRef::new(value);
|
||||
let store = store.weak();
|
||||
ExternRef { inner, store }
|
||||
}
|
||||
|
||||
/// Get this reference's store.
|
||||
///
|
||||
/// Returns `None` if this reference outlived its store.
|
||||
pub fn store(&self) -> Option<crate::runtime::Store> {
|
||||
crate::runtime::Store::upgrade(&self.store)
|
||||
ExternRef { inner }
|
||||
}
|
||||
|
||||
/// Get the underlying data for this `ExternRef`.
|
||||
@@ -48,39 +36,4 @@ impl ExternRef {
|
||||
pub fn ptr_eq(&self, other: &ExternRef) -> bool {
|
||||
VMExternRef::eq(&self.inner, &other.inner)
|
||||
}
|
||||
|
||||
/// Returns the host information for this `externref`, if previously created
|
||||
/// with `set_host_info`.
|
||||
pub fn host_info(&self) -> Option<Rc<RefCell<dyn Any>>> {
|
||||
let store = crate::Store::upgrade(&self.store)?;
|
||||
store.host_info(self)
|
||||
}
|
||||
|
||||
/// Set the host information for this `externref`, returning the old host
|
||||
/// information if it was previously set.
|
||||
pub fn set_host_info<T>(&self, info: T) -> Option<Rc<RefCell<dyn Any>>>
|
||||
where
|
||||
T: 'static + Any,
|
||||
{
|
||||
let store = crate::Store::upgrade(&self.store)?;
|
||||
store.set_host_info(self, Some(Rc::new(RefCell::new(info))))
|
||||
}
|
||||
|
||||
/// Remove the host information for this `externref`, returning the old host
|
||||
/// information if it was previously set.
|
||||
pub fn remove_host_info(&self) -> Option<Rc<RefCell<dyn Any>>> {
|
||||
let store = crate::Store::upgrade(&self.store)?;
|
||||
store.set_host_info(self, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for ExternRef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let ExternRef { inner, store: _ } = self;
|
||||
let store = self.store();
|
||||
f.debug_struct("ExternRef")
|
||||
.field("inner", &inner)
|
||||
.field("store", &store)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
use crate::externals::MemoryCreator;
|
||||
use crate::r#ref::ExternRef;
|
||||
use crate::trampoline::{MemoryCreatorProxy, StoreInstanceHandle};
|
||||
use crate::Module;
|
||||
use anyhow::{bail, Result};
|
||||
use std::any::Any;
|
||||
use std::cell::RefCell;
|
||||
use std::cmp;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@@ -815,7 +812,6 @@ pub(crate) struct StoreInner {
|
||||
instances: RefCell<Vec<InstanceHandle>>,
|
||||
signal_handler: RefCell<Option<Box<SignalHandler<'static>>>>,
|
||||
jit_code_ranges: RefCell<Vec<(usize, usize)>>,
|
||||
host_info: RefCell<HashMap<HostInfoKey, Rc<RefCell<dyn Any>>>>,
|
||||
externref_activations_table: Rc<VMExternRefActivationsTable>,
|
||||
stack_map_registry: Rc<StackMapRegistry>,
|
||||
}
|
||||
@@ -857,7 +853,6 @@ impl Store {
|
||||
instances: RefCell::new(Vec::new()),
|
||||
signal_handler: RefCell::new(None),
|
||||
jit_code_ranges: RefCell::new(Vec::new()),
|
||||
host_info: RefCell::new(HashMap::new()),
|
||||
externref_activations_table: Rc::new(VMExternRefActivationsTable::new()),
|
||||
stack_map_registry: Rc::new(StackMapRegistry::default()),
|
||||
}),
|
||||
@@ -980,32 +975,6 @@ impl Store {
|
||||
Some(Self { inner })
|
||||
}
|
||||
|
||||
pub(crate) fn host_info(&self, externref: &ExternRef) -> Option<Rc<RefCell<dyn Any>>> {
|
||||
debug_assert!(
|
||||
std::rc::Weak::ptr_eq(&self.weak(), &externref.store),
|
||||
"externref must be from this store"
|
||||
);
|
||||
let infos = self.inner.host_info.borrow();
|
||||
infos.get(&HostInfoKey(externref.inner.clone())).cloned()
|
||||
}
|
||||
|
||||
pub(crate) fn set_host_info(
|
||||
&self,
|
||||
externref: &ExternRef,
|
||||
info: Option<Rc<RefCell<dyn Any>>>,
|
||||
) -> Option<Rc<RefCell<dyn Any>>> {
|
||||
debug_assert!(
|
||||
std::rc::Weak::ptr_eq(&self.weak(), &externref.store),
|
||||
"externref must be from this store"
|
||||
);
|
||||
let mut infos = self.inner.host_info.borrow_mut();
|
||||
if let Some(info) = info {
|
||||
infos.insert(HostInfoKey(externref.inner.clone()), info)
|
||||
} else {
|
||||
infos.remove(&HostInfoKey(externref.inner.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn signal_handler(&self) -> std::cell::Ref<'_, Option<Box<SignalHandler<'static>>>> {
|
||||
self.inner.signal_handler.borrow()
|
||||
}
|
||||
|
||||
@@ -126,7 +126,6 @@ impl Val {
|
||||
} else {
|
||||
Val::ExternRef(Some(ExternRef {
|
||||
inner: VMExternRef::clone_from_raw(raw),
|
||||
store: store.weak(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -180,15 +179,15 @@ impl Val {
|
||||
Val::FuncRef(Some(f)) => Store::same(store, f.store()),
|
||||
Val::FuncRef(None) => true,
|
||||
|
||||
// TODO: need to implement this once we actually finalize what
|
||||
// `externref` will look like and it's actually implemented to pass it
|
||||
// to compiled wasm as well.
|
||||
Val::ExternRef(Some(e)) => e.store.ptr_eq(&store.weak()),
|
||||
Val::ExternRef(None) => true,
|
||||
|
||||
// Integers have no association with any particular store, so
|
||||
// they're always considered as "yes I came from that store",
|
||||
Val::I32(_) | Val::I64(_) | Val::F32(_) | Val::F64(_) | Val::V128(_) => true,
|
||||
// Integers, floats, vectors, and `externref`s have no association
|
||||
// with any particular store, so they're always considered as "yes I
|
||||
// came from that store",
|
||||
Val::I32(_)
|
||||
| Val::I64(_)
|
||||
| Val::F32(_)
|
||||
| Val::F64(_)
|
||||
| Val::V128(_)
|
||||
| Val::ExternRef(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user