Skip to content

Commit 7f7b39c

Browse files
authored
EqRef from_i31 implementation (#11884)
* feat(gc/eqref): from_i31 implementation Copy of AnyRef's from_i31, exact same thing. * test: case for EqRef's from_i31 implementation
1 parent 33224b2 commit 7f7b39c

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

crates/wasmtime/src/runtime/gc/enabled/eqref.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Working with GC `eqref`s.
22
33
use crate::{
4-
AnyRef, ArrayRef, ArrayType, AsContext, GcRefImpl, GcRootIndex, HeapType, I31, OwnedRooted,
5-
RefType, Rooted, StructRef, StructType, ValRaw, ValType, WasmTy,
4+
AnyRef, ArrayRef, ArrayType, AsContext, AsContextMut, GcRefImpl, GcRootIndex, HeapType, I31,
5+
OwnedRooted, RefType, Rooted, StructRef, StructType, ValRaw, ValType, WasmTy,
66
prelude::*,
77
runtime::vm::VMGcRef,
88
store::{AutoAssertNoGc, StoreOpaque},
@@ -248,6 +248,33 @@ impl EqRef {
248248
}
249249
}
250250

251+
/// Construct an `eqref` from an `i31`.
252+
///
253+
/// # Example
254+
///
255+
/// ```
256+
/// # use wasmtime::*;
257+
/// # fn _foo() -> Result<()> {
258+
/// let mut store = Store::<()>::default();
259+
///
260+
/// // Create an `i31`.
261+
/// let i31 = I31::wrapping_u32(999);
262+
///
263+
/// // Convert it into an `eqref`.
264+
/// let eqref = EqRef::from_i31(&mut store, i31);
265+
/// # Ok(())
266+
/// # }
267+
/// ```
268+
pub fn from_i31(mut store: impl AsContextMut, value: I31) -> Rooted<Self> {
269+
let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
270+
Self::_from_i31(&mut store, value)
271+
}
272+
273+
pub(crate) fn _from_i31(store: &mut AutoAssertNoGc<'_>, value: I31) -> Rooted<Self> {
274+
let gc_ref = VMGcRef::from_i31(value.runtime_i31());
275+
Rooted::new(store, gc_ref)
276+
}
277+
251278
/// Is this `eqref` an `i31`?
252279
///
253280
/// # Errors

tests/all/eqref.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use wasmtime::*;
2+
3+
#[test]
4+
fn eqref_from_i31() -> Result<()> {
5+
let mut config = Config::new();
6+
config.wasm_function_references(true);
7+
config.wasm_gc(true);
8+
9+
let engine = Engine::new(&config)?;
10+
let mut store = Store::new(&engine, ());
11+
12+
let i31 = I31::wrapping_u32(31);
13+
14+
// without EqRef::from_i31
15+
let any_ref = AnyRef::from_i31(&mut store, i31);
16+
let eq_ref1 = any_ref.unwrap_eqref(&mut store)?;
17+
18+
// with EqRef::from_i31
19+
let eq_ref2 = EqRef::from_i31(&mut store, i31);
20+
21+
// reference to same i31
22+
assert_eq!(Rooted::ref_eq(&store, &eq_ref1, &eq_ref2)?, true);
23+
24+
Ok(())
25+
}

tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod custom_code_memory;
1313
mod debug;
1414
mod defaults;
1515
mod epoch_interruption;
16+
mod eqref;
1617
mod exceptions;
1718
mod exnrefs;
1819
mod externals;

0 commit comments

Comments
 (0)