A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
EqRef from_i31 implementation (#11884)
siaeyy7f7b39cb9fca
diff --git a/crates/wasmtime/src/runtime/gc/enabled/eqref.rs b/crates/wasmtime/src/runtime/gc/enabled/eqref.rsindex de88a26536..0c6be3e56d 100644--- a/crates/wasmtime/src/runtime/gc/enabled/eqref.rs+++ b/crates/wasmtime/src/runtime/gc/enabled/eqref.rs@@ -1,8 +1,8 @@//! Working with GC `eqref`s.use crate::{- AnyRef, ArrayRef, ArrayType, AsContext, GcRefImpl, GcRootIndex, HeapType, I31, OwnedRooted,- RefType, Rooted, StructRef, StructType, ValRaw, ValType, WasmTy,+ AnyRef, ArrayRef, ArrayType, AsContext, AsContextMut, GcRefImpl, GcRootIndex, HeapType, I31,+ OwnedRooted, RefType, Rooted, StructRef, StructType, ValRaw, ValType, WasmTy,prelude::*,runtime::vm::VMGcRef,store::{AutoAssertNoGc, StoreOpaque},@@ -248,6 +248,33 @@ impl EqRef {}}+ /// Construct an `eqref` from an `i31`.+ ///+ /// # Example+ ///+ /// ```+ /// # use wasmtime::*;+ /// # fn _foo() -> Result<()> {+ /// let mut store = Store::<()>::default();+ ///+ /// // Create an `i31`.+ /// let i31 = I31::wrapping_u32(999);+ ///+ /// // Convert it into an `eqref`.+ /// let eqref = EqRef::from_i31(&mut store, i31);+ /// # Ok(())+ /// # }+ /// ```+ pub fn from_i31(mut store: impl AsContextMut, value: I31) -> Rooted<Self> {+ let mut store = AutoAssertNoGc::new(store.as_context_mut().0);+ Self::_from_i31(&mut store, value)+ }++ pub(crate) fn _from_i31(store: &mut AutoAssertNoGc<'_>, value: I31) -> Rooted<Self> {+ let gc_ref = VMGcRef::from_i31(value.runtime_i31());+ Rooted::new(store, gc_ref)+ }+/// Is this `eqref` an `i31`?////// # Errorsdiff --git a/tests/all/eqref.rs b/tests/all/eqref.rsnew file mode 100644index 0000000000..4c5bb573a5--- /dev/null+++ b/tests/all/eqref.rs@@ -0,0 +1,25 @@+use wasmtime::*;++#[test]+fn eqref_from_i31() -> Result<()> {+ let mut config = Config::new();+ config.wasm_function_references(true);+ config.wasm_gc(true);++ let engine = Engine::new(&config)?;+ let mut store = Store::new(&engine, ());++ let i31 = I31::wrapping_u32(31);++ // without EqRef::from_i31+ let any_ref = AnyRef::from_i31(&mut store, i31);+ let eq_ref1 = any_ref.unwrap_eqref(&mut store)?;++ // with EqRef::from_i31+ let eq_ref2 = EqRef::from_i31(&mut store, i31);++ // reference to same i31+ assert_eq!(Rooted::ref_eq(&store, &eq_ref1, &eq_ref2)?, true);++ Ok(())+}diff --git a/tests/all/main.rs b/tests/all/main.rsindex 0c6375b971..3e2803baa7 100644--- a/tests/all/main.rs+++ b/tests/all/main.rs@@ -13,6 +13,7 @@ mod custom_code_memory;mod debug;mod defaults;mod epoch_interruption;+mod eqref;mod exceptions;mod exnrefs;mod externals;