Skip to content

Commit 0ae393b

Browse files
committed
Make GcStore::expose_gc_ref_to_wasm return the raw GC ref
Every caller of `expose_gc_ref_to_wasm` except for one does this weird dance where they get the raw representation of the GC ref before giving up ownership of it to the `expose_gc_ref_to_wasm` call, and then do something with the raw GC ref (like return it to Wasm or whatever). This small refactoring boxes up that dance inside `expose_gc_ref_to_wasm` so that callers don't have to do it themselves anymore. Makes things that much more concise and harder to mess up.
1 parent 8357599 commit 0ae393b

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ impl AnyRef {
330330

331331
pub(crate) unsafe fn _to_raw(&self, store: &mut AutoAssertNoGc<'_>) -> Result<u32> {
332332
let gc_ref = self.inner.try_clone_gc_ref(store)?;
333-
let raw = gc_ref.as_raw_u32();
334-
store.gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
333+
let raw = store.gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
335334
Ok(raw)
336335
}
337336

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ impl ExternRef {
503503

504504
pub(crate) fn _to_raw(&self, store: &mut AutoAssertNoGc) -> Result<u32> {
505505
let gc_ref = self.inner.try_clone_gc_ref(store)?;
506-
let raw = gc_ref.as_raw_u32();
507-
store.unwrap_gc_store_mut().expose_gc_ref_to_wasm(gc_ref);
506+
let raw = store.unwrap_gc_store_mut().expose_gc_ref_to_wasm(gc_ref);
508507
Ok(raw)
509508
}
510509
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,7 @@ impl<T: GcRef> Rooted<T> {
968968
val_raw: impl Fn(u32) -> ValRaw,
969969
) -> Result<()> {
970970
let gc_ref = self.inner.try_clone_gc_ref(store)?;
971-
let raw = gc_ref.as_raw_u32();
972-
debug_assert_ne!(raw, 0);
973-
store.gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
971+
let raw = store.gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
974972
ptr.write(val_raw(raw));
975973
Ok(())
976974
}
@@ -1758,9 +1756,7 @@ where
17581756
val_raw: impl Fn(u32) -> ValRaw,
17591757
) -> Result<()> {
17601758
let gc_ref = self.try_clone_gc_ref(store)?;
1761-
let raw = gc_ref.as_raw_u32();
1762-
debug_assert_ne!(raw, 0);
1763-
store.gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
1759+
let raw = store.gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
17641760
ptr.write(val_raw(raw));
17651761
Ok(())
17661762
}

crates/wasmtime/src/runtime/vm/gc.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,18 @@ impl GcStore {
134134
}
135135

136136
/// Hook to call whenever a GC reference is about to be exposed to Wasm.
137-
pub fn expose_gc_ref_to_wasm(&mut self, gc_ref: VMGcRef) {
137+
///
138+
/// Returns the raw representation of this GC ref, ready to be passed to
139+
/// Wasm.
140+
#[must_use]
141+
pub fn expose_gc_ref_to_wasm(&mut self, gc_ref: VMGcRef) -> u32 {
142+
let raw = gc_ref.as_raw_u32();
143+
debug_assert_ne!(raw, 0);
138144
if !gc_ref.is_i31() {
139145
log::trace!("exposing GC ref to Wasm: {gc_ref:p}");
140146
self.gc_heap.expose_gc_ref_to_wasm(gc_ref);
141147
}
148+
raw
142149
}
143150

144151
/// Allocate a new `externref`.

crates/wasmtime/src/runtime/vm/libcalls.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,13 @@ unsafe fn gc(store: &mut dyn VMStore, _instance: &mut Instance, gc_ref: u32) ->
460460
// GC.
461461
let gc_store = store.store_opaque_mut().unwrap_gc_store_mut();
462462
let gc_ref = gc_store.clone_gc_ref(gc_ref);
463-
gc_store.expose_gc_ref_to_wasm(gc_ref);
463+
let _ = gc_store.expose_gc_ref_to_wasm(gc_ref);
464464
}
465465

466466
match store.maybe_async_gc(gc_ref)? {
467467
None => Ok(0),
468468
Some(r) => {
469-
let raw = r.as_raw_u32();
470-
store
469+
let raw = store
471470
.store_opaque_mut()
472471
.unwrap_gc_store_mut()
473472
.expose_gc_ref_to_wasm(r);
@@ -529,9 +528,7 @@ unsafe fn gc_alloc_raw(
529528
}
530529
};
531530

532-
let raw = gc_ref.as_raw_u32();
533-
534-
store
531+
let raw = store
535532
.store_opaque_mut()
536533
.unwrap_gc_store_mut()
537534
.expose_gc_ref_to_wasm(gc_ref);
@@ -669,8 +666,7 @@ unsafe fn array_new_data(
669666
.copy_from_slice(array_layout.base_size, data);
670667

671668
// Return the array to Wasm!
672-
let raw = array_ref.as_gc_ref().as_raw_u32();
673-
store
669+
let raw = store
674670
.store_opaque_mut()
675671
.unwrap_gc_store_mut()
676672
.expose_gc_ref_to_wasm(array_ref.into());
@@ -839,8 +835,7 @@ unsafe fn array_new_elem(
839835

840836
let mut store = AutoAssertNoGc::new(store);
841837
let gc_ref = array.try_clone_gc_ref(&mut store)?;
842-
let raw = gc_ref.as_raw_u32();
843-
store.unwrap_gc_store_mut().expose_gc_ref_to_wasm(gc_ref);
838+
let raw = store.unwrap_gc_store_mut().expose_gc_ref_to_wasm(gc_ref);
844839
Ok(raw)
845840
})
846841
}

0 commit comments

Comments
 (0)