Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the wasmtime::Ref::null constructor and use it in a few places #8492

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions crates/c-api/src/table.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
Comment on lines -81 to +83
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out I even added Val::null_ref previously lol

}

/// 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