Add misc code cleanups

This commit is contained in:
Mark McCaskey
2021-02-10 12:13:33 -08:00
parent 1d3ee22991
commit b140dc2c8e
7 changed files with 5 additions and 300 deletions

View File

@@ -296,8 +296,8 @@ pub use crate::ptr::{Array, Item, WasmPtr};
pub use crate::store::{Store, StoreObject}; pub use crate::store::{Store, StoreObject};
pub use crate::tunables::BaseTunables; pub use crate::tunables::BaseTunables;
pub use crate::types::{ pub use crate::types::{
ExportType, ExternRef, ExternType, FunctionType, GlobalType, HostInfo, HostRef, ImportType, ExportType, ExternType, FunctionType, GlobalType, ImportType, MemoryType, Mutability,
MemoryType, Mutability, TableType, Val, ValType, TableType, Val, ValType,
}; };
pub use crate::types::{Val as Value, ValType as Type}; pub use crate::types::{Val as Value, ValType as Type};
pub use crate::utils::is_wasm; pub use crate::utils::is_wasm;

View File

@@ -3,8 +3,8 @@ use crate::store::{Store, StoreObject};
use crate::RuntimeError; use crate::RuntimeError;
use wasmer_types::Value; use wasmer_types::Value;
pub use wasmer_types::{ pub use wasmer_types::{
ExportType, ExternRef, ExternType, FunctionType, GlobalType, HostInfo, HostRef, ImportType, ExportType, ExternType, FunctionType, GlobalType, ImportType, MemoryType, Mutability,
MemoryType, Mutability, TableType, Type as ValType, TableType, Type as ValType,
}; };
use wasmer_vm::VMFuncRef; use wasmer_vm::VMFuncRef;

View File

@@ -792,17 +792,6 @@ pub struct VMCallerCheckedAnyfunc {
// If more elements are added here, remember to add offset_of tests below! // If more elements are added here, remember to add offset_of tests below!
} }
impl VMCallerCheckedAnyfunc {
/// Check if this anyfunc is a null funcref.
// TODO: we probably want to clean this up before shipping, maybe actually add
// an extra layer of indirection
pub fn is_null(&self) -> bool {
self.func_ptr == std::ptr::null()
&& self.type_index == VMSharedSignatureIndex::default()
&& self.vmctx.is_null()
}
}
#[cfg(test)] #[cfg(test)]
mod test_vmcaller_checked_anyfunc { mod test_vmcaller_checked_anyfunc {
use super::VMCallerCheckedAnyfunc; use super::VMCallerCheckedAnyfunc;

View File

@@ -56,7 +56,6 @@ mod indexes;
mod initializers; mod initializers;
mod memory_view; mod memory_view;
mod native; mod native;
mod r#ref;
mod types; mod types;
mod units; mod units;
mod values; mod values;
@@ -78,7 +77,6 @@ pub use crate::initializers::{
}; };
pub use crate::memory_view::{Atomically, MemoryView}; pub use crate::memory_view::{Atomically, MemoryView};
pub use crate::native::{NativeWasmType, ValueType}; pub use crate::native::{NativeWasmType, ValueType};
pub use crate::r#ref::{ExternRef, HostInfo, HostRef};
pub use crate::units::{ pub use crate::units::{
Bytes, PageCountOutOfRange, Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE, Bytes, PageCountOutOfRange, Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
}; };

View File

@@ -1,267 +0,0 @@
#![allow(missing_docs)]
use crate::lib::std::any::Any;
use crate::lib::std::boxed::Box;
use crate::lib::std::cell::{self, RefCell};
use crate::lib::std::fmt;
use crate::lib::std::hash;
use crate::lib::std::rc::{Rc, Weak};
pub trait HostInfo {
fn finalize(&mut self) {}
}
trait InternalRefBase: Any {
fn as_any(&self) -> &dyn Any;
fn host_info(&self) -> Option<cell::RefMut<Box<dyn HostInfo>>>;
fn set_host_info(&self, info: Option<Box<dyn HostInfo>>);
fn ptr_eq(&self, other: &dyn InternalRefBase) -> bool;
}
#[derive(Clone)]
pub struct InternalRef(Rc<dyn InternalRefBase>);
impl InternalRef {
pub fn is_ref<T: 'static>(&self) -> bool {
let r = self.0.as_any();
Any::is::<HostRef<T>>(r)
}
pub fn get_ref<T: 'static>(&self) -> HostRef<T> {
let r = self.0.as_any();
r.downcast_ref::<HostRef<T>>()
.expect("reference is not T type")
.clone()
}
}
struct AnyAndHostInfo {
any: Box<dyn Any>,
host_info: Option<Box<dyn HostInfo>>,
}
impl Drop for AnyAndHostInfo {
fn drop(&mut self) {
if let Some(info) = &mut self.host_info {
info.finalize();
}
}
}
#[derive(Clone)]
pub struct OtherRef(Rc<RefCell<AnyAndHostInfo>>);
/// Represents an opaque reference to any data within WebAssembly.
#[derive(Clone)]
pub enum ExternRef {
/// A reference to no data.
Null,
/// A reference to data stored internally.
Ref(InternalRef),
/// A reference to data located outside.
Other(OtherRef),
}
impl hash::Hash for ExternRef {
fn hash<H: hash::Hasher>(&self, _state: &mut H) {}
}
impl PartialEq for ExternRef {
fn eq(&self, other: &Self) -> bool {
// The `ExternRef`s are the same if they point to the same value
self.ptr_eq(other)
}
}
impl Eq for ExternRef {}
impl ExternRef {
/// Creates a new instance of `ExternRef` from `Box<dyn Any>`.
pub fn new(data: Box<dyn Any>) -> Self {
let info = AnyAndHostInfo {
any: data,
host_info: None,
};
Self::Other(OtherRef(Rc::new(RefCell::new(info))))
}
/// Creates a `Null` reference.
pub fn null() -> Self {
Self::Null
}
/// Returns the data stored in the reference if available.
///
/// # Panics
///
/// Panics if the variant isn't `ExternRef::Other`.
pub fn data(&self) -> cell::Ref<Box<dyn Any>> {
match self {
Self::Other(OtherRef(r)) => cell::Ref::map(r.borrow(), |r| &r.any),
_ => panic!("expected ExternRef::Other"),
}
}
/// Returns true if the two `ExternRef<T>`'s point to the same value (not just
/// values that compare as equal).
pub fn ptr_eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Null, Self::Null) => true,
(Self::Ref(InternalRef(ref a)), Self::Ref(InternalRef(ref b))) => a.ptr_eq(b.as_ref()),
(Self::Other(OtherRef(ref a)), Self::Other(OtherRef(ref b))) => Rc::ptr_eq(a, b),
_ => false,
}
}
/// Returns a mutable reference to the host information if available.
///
/// # Panics
///
/// Panics if `ExternRef` is already borrowed or `ExternRef` is `Null`.
pub fn host_info(&self) -> Option<cell::RefMut<Box<dyn HostInfo>>> {
match self {
Self::Null => panic!("null"),
Self::Ref(r) => r.0.host_info(),
Self::Other(r) => {
let info = cell::RefMut::map(r.0.borrow_mut(), |b| &mut b.host_info);
if info.is_none() {
return None;
}
Some(cell::RefMut::map(info, |info| info.as_mut().unwrap()))
}
}
}
/// Sets the host information for an `ExternRef`.
///
/// # Panics
///
/// Panics if `ExternRef` is already borrowed or `ExternRef` is `Null`.
pub fn set_host_info(&self, info: Option<Box<dyn HostInfo>>) {
match self {
Self::Null => panic!("null"),
Self::Ref(r) => r.0.set_host_info(info),
Self::Other(r) => {
r.0.borrow_mut().host_info = info;
}
}
}
}
impl fmt::Debug for ExternRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Null => write!(f, "null"),
Self::Ref(_) => write!(f, "externref"),
Self::Other(_) => write!(f, "other ref"),
}
}
}
struct ContentBox<T> {
content: T,
host_info: Option<Box<dyn HostInfo>>,
externref_data: Weak<dyn InternalRefBase>,
}
impl<T> Drop for ContentBox<T> {
fn drop(&mut self) {
if let Some(info) = &mut self.host_info {
info.finalize();
}
}
}
/// Represents a piece of data located in the host environment.
pub struct HostRef<T>(Rc<RefCell<ContentBox<T>>>);
impl<T: 'static> HostRef<T> {
/// Creates a new `HostRef<T>` from `T`.
pub fn new(item: T) -> Self {
let externref_data: Weak<Self> = Weak::new();
let content = ContentBox {
content: item,
host_info: None,
externref_data,
};
Self(Rc::new(RefCell::new(content)))
}
/// Immutably borrows the wrapped data.
///
/// # Panics
///
/// Panics if the value is currently mutably borrowed.
pub fn borrow(&self) -> cell::Ref<T> {
cell::Ref::map(self.0.borrow(), |b| &b.content)
}
/// Mutably borrows the wrapped data.
///
/// # Panics
///
/// Panics if the `HostRef<T>` is already borrowed.
pub fn borrow_mut(&self) -> cell::RefMut<T> {
cell::RefMut::map(self.0.borrow_mut(), |b| &mut b.content)
}
/// Returns true if the two `HostRef<T>`'s point to the same value (not just
/// values that compare as equal).
pub fn ptr_eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
/// Returns an opaque reference to the wrapped data in the form of
/// an `ExternRef`.
///
/// # Panics
///
/// Panics if `HostRef<T>` is already mutably borrowed.
pub fn externref(&self) -> ExternRef {
let r = self.0.borrow_mut().externref_data.upgrade();
if let Some(r) = r {
return ExternRef::Ref(InternalRef(r));
}
let externref_data: Rc<dyn InternalRefBase> = Rc::new(self.clone());
self.0.borrow_mut().externref_data = Rc::downgrade(&externref_data);
ExternRef::Ref(InternalRef(externref_data))
}
}
impl<T: 'static> InternalRefBase for HostRef<T> {
fn ptr_eq(&self, other: &dyn InternalRefBase) -> bool {
if let Some(other) = other.as_any().downcast_ref() {
self.ptr_eq(other)
} else {
false
}
}
fn as_any(&self) -> &dyn Any {
self
}
fn host_info(&self) -> Option<cell::RefMut<Box<dyn HostInfo>>> {
let info = cell::RefMut::map(self.0.borrow_mut(), |b| &mut b.host_info);
if info.is_none() {
return None;
}
Some(cell::RefMut::map(info, |info| info.as_mut().unwrap()))
}
fn set_host_info(&self, info: Option<Box<dyn HostInfo>>) {
self.0.borrow_mut().host_info = info;
}
}
impl<T> Clone for HostRef<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T: fmt::Debug> fmt::Debug for HostRef<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Ref(")?;
self.0.borrow().content.fmt(f)?;
write!(f, ")")
}
}

View File

@@ -1,9 +1,8 @@
use crate::extern_ref::VMExternRef;
use crate::lib::std::convert::TryFrom; use crate::lib::std::convert::TryFrom;
use crate::lib::std::fmt; use crate::lib::std::fmt;
use crate::lib::std::ptr; use crate::lib::std::ptr;
use crate::lib::std::string::{String, ToString}; use crate::lib::std::string::{String, ToString};
//use crate::r#ref::ExternRef;
use crate::extern_ref::VMExternRef;
use crate::types::Type; use crate::types::Type;
/// Possible runtime values that a WebAssembly module can either consume or /// Possible runtime values that a WebAssembly module can either consume or
@@ -125,8 +124,6 @@ where
Type::F32 => Self::F32(ptr::read(p as *const f32)), Type::F32 => Self::F32(ptr::read(p as *const f32)),
Type::F64 => Self::F64(ptr::read(p as *const f64)), Type::F64 => Self::F64(ptr::read(p as *const f64)),
Type::V128 => Self::V128(ptr::read(p as *const u128)), Type::V128 => Self::V128(ptr::read(p as *const u128)),
// TOOD: handle non-null funcrefs; can we even do that though?
// storage of funcrefs is not the same as what we store in globals and tables
Type::FuncRef => { Type::FuncRef => {
// a bit hairy, maybe clean this up? issue is that `Option` doesn't live on // a bit hairy, maybe clean this up? issue is that `Option` doesn't live on
// the funcref itself, but in Value. // the funcref itself, but in Value.

View File

@@ -36,18 +36,6 @@ cranelift::spec::skip_stack_guard_page on aarch64
## Reference types spectests ## Reference types spectests
# Used to disable tests during development; these ignores should not ship # Used to disable tests during development; these ignores should not ship
#cranelift::spec::ref_func
#cranelift::spec::ref_is_null
#cranelift::spec::ref_null
#cranelift::spec::table_copy
#cranelift::spec::table_fill
#cranelift::spec::table_get
#cranelift::spec::table_grow
#cranelift::spec::table_init
#cranelift::spec::table_set
#cranelift::spec::table_size
#cranelift::spec::table_sub
singlepass::spec::ref_func singlepass::spec::ref_func
singlepass::spec::ref_is_null singlepass::spec::ref_is_null
singlepass::spec::ref_null singlepass::spec::ref_null