Rename anyref to externref across the board

This commit is contained in:
Nick Fitzgerald
2020-05-20 11:55:30 -07:00
parent 5c39b74eb8
commit f28b3738ee
25 changed files with 151 additions and 130 deletions

View File

@@ -298,7 +298,7 @@ impl Func {
/// | `f32` | `f32` |
/// | `f64` | `f64` |
/// | (not supported) | `v128` |
/// | (not supported) | `anyref` |
/// | (not supported) | `externref` |
///
/// Any of the Rust types can be returned from the closure as well, in
/// addition to some extra types

View File

@@ -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::{AnyRef, HostRef};
pub use crate::r#ref::{ExternRef, HostRef};
pub use crate::runtime::*;
pub use crate::trap::Trap;
pub use crate::types::*;

View File

@@ -38,7 +38,7 @@ pub struct OtherRef(Rc<RefCell<AnyAndHostInfo>>);
/// Represents an opaque reference to any data within WebAssembly.
#[derive(Clone)]
pub enum AnyRef {
pub enum ExternRef {
/// A reference to no data.
Null,
/// A reference to data stored internally in `wasmtime`.
@@ -47,52 +47,54 @@ pub enum AnyRef {
Other(OtherRef),
}
impl AnyRef {
/// Creates a new instance of `AnyRef` from `Box<dyn Any>`.
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,
};
AnyRef::Other(OtherRef(Rc::new(RefCell::new(info))))
ExternRef::Other(OtherRef(Rc::new(RefCell::new(info))))
}
/// Creates a `Null` reference.
pub fn null() -> Self {
AnyRef::Null
ExternRef::Null
}
/// Returns the data stored in the reference if available.
/// # Panics
/// Panics if the variant isn't `AnyRef::Other`.
/// Panics if the variant isn't `ExternRef::Other`.
pub fn data(&self) -> cell::Ref<Box<dyn Any>> {
match self {
AnyRef::Other(OtherRef(r)) => cell::Ref::map(r.borrow(), |r| &r.any),
_ => panic!("expected AnyRef::Other"),
ExternRef::Other(OtherRef(r)) => cell::Ref::map(r.borrow(), |r| &r.any),
_ => panic!("expected ExternRef::Other"),
}
}
/// Returns true if the two `AnyRef<T>`'s point to the same value (not just
/// 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: &AnyRef) -> bool {
pub fn ptr_eq(&self, other: &ExternRef) -> bool {
match (self, other) {
(AnyRef::Null, AnyRef::Null) => true,
(AnyRef::Ref(InternalRef(ref a)), AnyRef::Ref(InternalRef(ref b))) => {
(ExternRef::Null, ExternRef::Null) => true,
(ExternRef::Ref(InternalRef(ref a)), ExternRef::Ref(InternalRef(ref b))) => {
a.ptr_eq(b.as_ref())
}
(AnyRef::Other(OtherRef(ref a)), AnyRef::Other(OtherRef(ref b))) => Rc::ptr_eq(a, b),
(ExternRef::Other(OtherRef(ref a)), ExternRef::Other(OtherRef(ref b))) => {
Rc::ptr_eq(a, b)
}
_ => false,
}
}
/// Returns a mutable reference to the host information if available.
/// # Panics
/// Panics if `AnyRef` is already borrowed or `AnyRef` is `Null`.
/// Panics if `ExternRef` is already borrowed or `ExternRef` is `Null`.
pub fn host_info(&self) -> Option<cell::RefMut<Box<dyn Any>>> {
match self {
AnyRef::Null => panic!("null"),
AnyRef::Ref(r) => r.0.host_info(),
AnyRef::Other(r) => {
ExternRef::Null => panic!("null"),
ExternRef::Ref(r) => r.0.host_info(),
ExternRef::Other(r) => {
let info = cell::RefMut::map(r.0.borrow_mut(), |b| &mut b.host_info);
if info.is_none() {
return None;
@@ -102,26 +104,26 @@ impl AnyRef {
}
}
/// Sets the host information for an `AnyRef`.
/// Sets the host information for an `ExternRef`.
/// # Panics
/// Panics if `AnyRef` is already borrowed or `AnyRef` is `Null`.
/// Panics if `ExternRef` is already borrowed or `ExternRef` is `Null`.
pub fn set_host_info(&self, info: Option<Box<dyn Any>>) {
match self {
AnyRef::Null => panic!("null"),
AnyRef::Ref(r) => r.0.set_host_info(info),
AnyRef::Other(r) => {
ExternRef::Null => panic!("null"),
ExternRef::Ref(r) => r.0.set_host_info(info),
ExternRef::Other(r) => {
r.0.borrow_mut().host_info = info;
}
}
}
}
impl fmt::Debug for AnyRef {
impl fmt::Debug for ExternRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AnyRef::Null => write!(f, "null"),
AnyRef::Ref(_) => write!(f, "anyref"),
AnyRef::Other(_) => write!(f, "other ref"),
ExternRef::Null => write!(f, "null"),
ExternRef::Ref(_) => write!(f, "externref"),
ExternRef::Other(_) => write!(f, "other ref"),
}
}
}
@@ -129,7 +131,7 @@ impl fmt::Debug for AnyRef {
struct ContentBox<T> {
content: T,
host_info: Option<Box<dyn Any>>,
anyref_data: Weak<dyn InternalRefBase>,
externref_data: Weak<dyn InternalRefBase>,
}
/// Represents a piece of data located in the host environment.
@@ -138,11 +140,11 @@ 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) -> HostRef<T> {
let anyref_data: Weak<HostRef<T>> = Weak::new();
let externref_data: Weak<HostRef<T>> = Weak::new();
let content = ContentBox {
content: item,
host_info: None,
anyref_data,
externref_data,
};
HostRef(Rc::new(RefCell::new(content)))
}
@@ -168,17 +170,17 @@ impl<T: 'static> HostRef<T> {
}
/// Returns an opaque reference to the wrapped data in the form of
/// an `AnyRef`.
/// an `ExternRef`.
/// # Panics
/// Panics if `HostRef<T>` is already mutably borrowed.
pub fn anyref(&self) -> AnyRef {
let r = self.0.borrow_mut().anyref_data.upgrade();
pub fn externref(&self) -> ExternRef {
let r = self.0.borrow_mut().externref_data.upgrade();
if let Some(r) = r {
return AnyRef::Ref(InternalRef(r));
return ExternRef::Ref(InternalRef(r));
}
let anyref_data: Rc<dyn InternalRefBase> = Rc::new(self.clone());
self.0.borrow_mut().anyref_data = Rc::downgrade(&anyref_data);
AnyRef::Ref(InternalRef(anyref_data))
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))
}
}

View File

@@ -172,7 +172,7 @@ impl Config {
/// feature can be enabled through this method for appropriate wasm
/// modules.
///
/// This feature gates items such as the `anyref` type and multiple tables
/// This feature gates items such as the `externref` type and multiple tables
/// being in a module. Note that enabling the reference types feature will
/// also enable the bulk memory feature.
///

View File

@@ -62,7 +62,7 @@ pub enum ValType {
/// A 128 bit number.
V128,
/// A reference to opaque data in the Wasm instance.
AnyRef, /* = 128 */
ExternRef, /* = 128 */
/// A reference to a Wasm function.
FuncRef,
}
@@ -80,7 +80,7 @@ impl ValType {
/// Returns true if `ValType` matches either of the reference types.
pub fn is_ref(&self) -> bool {
match self {
ValType::AnyRef | ValType::FuncRef => true,
ValType::ExternRef | ValType::FuncRef => true,
_ => false,
}
}
@@ -321,7 +321,7 @@ impl GlobalType {
/// A descriptor for a table in a WebAssembly module.
///
/// Tables are contiguous chunks of a specific element, typically a `funcref` or
/// an `anyref`. The most common use for tables is a function table through
/// an `externref`. The most common use for tables is a function table through
/// which `call_indirect` can invoke other functions.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct TableType {

View File

@@ -1,4 +1,4 @@
use crate::r#ref::AnyRef;
use crate::r#ref::ExternRef;
use crate::{Func, Store, ValType};
use anyhow::{bail, Result};
use std::ptr;
@@ -25,10 +25,10 @@ pub enum Val {
/// `f64::from_bits` to create an `f64` value.
F64(u64),
/// An `anyref` value which can hold opaque data to the wasm instance itself.
/// An `externref` value which can hold opaque data to the wasm instance itself.
///
/// Note that this is a nullable value as well.
AnyRef(AnyRef),
ExternRef(ExternRef),
/// A first-class reference to a WebAssembly function.
FuncRef(Func),
@@ -62,9 +62,9 @@ macro_rules! accessors {
}
impl Val {
/// Returns a null `anyref` value.
/// Returns a null `externref` value.
pub fn null() -> Val {
Val::AnyRef(AnyRef::null())
Val::ExternRef(ExternRef::null())
}
/// Returns the corresponding [`ValType`] for this `Val`.
@@ -74,7 +74,7 @@ impl Val {
Val::I64(_) => ValType::I64,
Val::F32(_) => ValType::F32,
Val::F64(_) => ValType::F64,
Val::AnyRef(_) => ValType::AnyRef,
Val::ExternRef(_) => ValType::ExternRef,
Val::FuncRef(_) => ValType::FuncRef,
Val::V128(_) => ValType::V128,
}
@@ -115,10 +115,10 @@ impl Val {
/// Attempt to access the underlying value of this `Val`, returning
/// `None` if it is not the correct type.
///
/// This will return `Some` for both the `AnyRef` and `FuncRef` types.
pub fn anyref(&self) -> Option<AnyRef> {
/// This will return `Some` for both the `ExternRef` and `FuncRef` types.
pub fn externref(&self) -> Option<ExternRef> {
match self {
Val::AnyRef(e) => Some(e.clone()),
Val::ExternRef(e) => Some(e.clone()),
_ => None,
}
}
@@ -129,8 +129,8 @@ impl Val {
/// # Panics
///
/// Panics if `self` is not of the right type.
pub fn unwrap_anyref(&self) -> AnyRef {
self.anyref().expect("expected anyref")
pub fn unwrap_externref(&self) -> ExternRef {
self.externref().expect("expected externref")
}
pub(crate) fn comes_from_same_store(&self, store: &Store) -> bool {
@@ -138,10 +138,10 @@ impl Val {
Val::FuncRef(f) => Store::same(store, f.store()),
// TODO: need to implement this once we actually finalize what
// `anyref` will look like and it's actually implemented to pass it
// `externref` will look like and it's actually implemented to pass it
// to compiled wasm as well.
Val::AnyRef(AnyRef::Ref(_)) | Val::AnyRef(AnyRef::Other(_)) => false,
Val::AnyRef(AnyRef::Null) => true,
Val::ExternRef(ExternRef::Ref(_)) | Val::ExternRef(ExternRef::Other(_)) => false,
Val::ExternRef(ExternRef::Null) => true,
// Integers have no association with any particular store, so
// they're always considered as "yes I came from that store",
@@ -174,9 +174,9 @@ impl From<f64> for Val {
}
}
impl From<AnyRef> for Val {
fn from(val: AnyRef) -> Val {
Val::AnyRef(val)
impl From<ExternRef> for Val {
fn from(val: ExternRef) -> Val {
Val::ExternRef(val)
}
}
@@ -194,7 +194,7 @@ pub(crate) fn into_checked_anyfunc(
bail!("cross-`Store` values are not supported");
}
Ok(match val {
Val::AnyRef(AnyRef::Null) => wasmtime_runtime::VMCallerCheckedAnyfunc {
Val::ExternRef(ExternRef::Null) => wasmtime_runtime::VMCallerCheckedAnyfunc {
func_ptr: ptr::null(),
type_index: wasmtime_runtime::VMSharedSignatureIndex::default(),
vmctx: ptr::null_mut(),
@@ -216,7 +216,7 @@ pub(crate) fn from_checked_anyfunc(
store: &Store,
) -> Val {
if item.type_index == wasmtime_runtime::VMSharedSignatureIndex::default() {
return Val::AnyRef(AnyRef::Null);
return Val::ExternRef(ExternRef::Null);
}
let instance_handle = unsafe { wasmtime_runtime::InstanceHandle::from_vmctx(item.vmctx) };
let export = wasmtime_runtime::ExportFunction {