Skip to content

Commit

Permalink
Add the Ref::null constructor and use it in a few places (#8492)
Browse files Browse the repository at this point in the history
Just a small follow up to #8481
  • Loading branch information
fitzgen committed Apr 27, 2024
1 parent 3e87883 commit 6232904
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 39 deletions.
11 changes: 3 additions & 8 deletions crates/c-api/src/table.rs
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use anyhow::anyhow;
use std::mem::MaybeUninit;
use wasmtime::{Extern, HeapType, Ref, RootScope, Table, TableType};
use wasmtime::{Extern, Ref, RootScope, Table, TableType};

#[derive(Clone)]
#[repr(transparent)]
Expand Down Expand Up @@ -33,13 +33,8 @@ impl wasm_table_t {
}

fn option_wasm_ref_t_to_ref(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Ref {
match (r.map(|r| r.r.clone()), table_ty.element().heap_type().top()) {
(Some(r), _) => r,
(None, HeapType::Func) => Ref::Func(None),
(None, HeapType::Extern) => Ref::Extern(None),
(None, HeapType::Any) => Ref::Any(None),
(None, ty) => unreachable!("not a top type: {ty:?}"),
}
r.map(|r| r.r.clone())
.unwrap_or_else(|| Ref::null(table_ty.element().heap_type()))
}

#[no_mangle]
Expand Down
16 changes: 8 additions & 8 deletions crates/fuzzing/src/oracles/dummy.rs
@@ -1,6 +1,6 @@
//! Dummy implementations of things that a Wasm module can import.

use anyhow::bail;
use anyhow::ensure;
use wasmtime::*;

/// Create a set of dummy functions/globals/etc for the given imports.
Expand Down Expand Up @@ -45,13 +45,13 @@ pub fn dummy_value(val_ty: ValType) -> Result<Val> {
ValType::F32 => Val::F32(0),
ValType::F64 => Val::F64(0),
ValType::V128 => Val::V128(0.into()),
ValType::Ref(r) => match r.heap_type().top() {
_ if !r.is_nullable() => bail!("cannot construct a dummy value of type `{r}`"),
HeapType::Extern => Val::null_extern_ref(),
HeapType::Func => Val::null_func_ref(),
HeapType::Any => Val::null_any_ref(),
ty => unreachable!("not a top type: {ty:?}"),
},
ValType::Ref(r) => {
ensure!(
r.is_nullable(),
"cannot construct a dummy value of type `{r}`"
);
Val::null_ref(r.heap_type())
}
})
}

Expand Down
9 changes: 1 addition & 8 deletions crates/wasmtime/src/runtime/externals/table.rs
Expand Up @@ -167,14 +167,7 @@ impl Table {
}

runtime::TableElement::GcRef(None) => {
match self._ty(&store).element().heap_type().top() {
HeapType::Any => Some(Ref::Any(None)),
HeapType::Extern => Some(Ref::Extern(None)),
HeapType::Func => {
unreachable!("never have TableElement::GcRef for func tables")
}
ty => unreachable!("not a top type: {ty:?}"),
}
Some(Ref::null(self._ty(&store).element().heap_type()))
}

#[cfg_attr(not(feature = "gc"), allow(unreachable_code, unused_variables))]
Expand Down
9 changes: 1 addition & 8 deletions crates/wasmtime/src/runtime/linker.rs
Expand Up @@ -316,8 +316,6 @@ impl<T> Linker<T> {
&mut self,
module: &Module,
) -> anyhow::Result<()> {
use crate::HeapType;

for import in module.imports() {
if let Err(import_err) = self._get_by_import(&import) {
if let ExternType::Func(func_ty) = import_err.ty() {
Expand All @@ -343,12 +341,7 @@ impl<T> Linker<T> {
ValType::V128 => Val::V128(0_u128.into()),
ValType::Ref(r) => {
debug_assert!(r.is_nullable());
match r.heap_type().top() {
HeapType::Func => Val::null_func_ref(),
HeapType::Extern => Val::null_extern_ref(),
HeapType::Any => Val::null_any_ref(),
ty => unreachable!("not a top type: {ty:?}"),
}
Val::null_ref(r.heap_type())
}
};
}
Expand Down
1 change: 1 addition & 0 deletions crates/wasmtime/src/runtime/types.rs
Expand Up @@ -619,6 +619,7 @@ impl HeapType {
///
/// The returned heap type is a supertype of all types in this heap type's
/// type hierarchy.
#[inline]
pub fn top(&self) -> HeapType {
match self {
HeapType::Func | HeapType::ConcreteFunc(_) | HeapType::NoFunc => HeapType::Func,
Expand Down
21 changes: 14 additions & 7 deletions crates/wasmtime/src/runtime/values.rs
Expand Up @@ -78,13 +78,9 @@ macro_rules! accessors {

impl Val {
/// Returns the null reference for the given heap type.
pub fn null_ref(heap_type: HeapType) -> Val {
match heap_type.top() {
HeapType::Func => Val::FuncRef(None),
HeapType::Extern => Val::ExternRef(None),
HeapType::Any => Val::AnyRef(None),
_ => unreachable!(),
}
#[inline]
pub fn null_ref(heap_type: &HeapType) -> Val {
Ref::null(&heap_type).into()
}

/// Returns the null function reference value.
Expand Down Expand Up @@ -631,6 +627,17 @@ impl From<Option<Rooted<AnyRef>>> for Ref {
}

impl Ref {
/// Create a null reference to the given heap type.
#[inline]
pub fn null(heap_type: &HeapType) -> Self {
match heap_type.top() {
HeapType::Any => Ref::Any(None),
HeapType::Extern => Ref::Extern(None),
HeapType::Func => Ref::Func(None),
ty => unreachable!("not a heap type: {ty:?}"),
}
}

/// Is this a null reference?
#[inline]
pub fn is_null(&self) -> bool {
Expand Down

0 comments on commit 6232904

Please sign in to comment.