Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add `Global::get_numeric` taking `AsContext` (#14979)

smarcd9535b22e8c7a
diff --git a/crates/wasmtime/src/runtime/externals/global.rs b/crates/wasmtime/src/runtime/externals/global.rs
index 59ad8be326..91e887f3a0 100644
--- a/crates/wasmtime/src/runtime/externals/global.rs
+++ b/crates/wasmtime/src/runtime/externals/global.rs
@@ -149,15 +149,43 @@ impl Global {
self._get(&mut store)
}
+ /// Returns the current [`Val`] of this global if it has a numeric type
+ /// (`i32`, `i64`, `f32`, `f64`, or `v128`), or `None` if it holds a
+ /// reference.
+ ///
+ /// Unlike [`Global::get`] this only needs shared access to the store since
+ /// reading a numeric value never has to clone a GC reference. That makes
+ /// it usable where only an [`AsContext`] is available, for example from
+ /// epoch or call hooks.
+ ///
+ /// # Panics
+ ///
+ /// Panics if `store` does not own this global.
+ pub fn get_numeric(&self, store: impl AsContext) -> Option<Val> {
+ self._get_numeric(store.as_context().0)
+ }
+
+ fn _get_numeric(&self, store: &StoreOpaque) -> Option<Val> {
+ unsafe {
+ let definition = self.definition(store).as_ref();
+ match self._ty(store).content() {
+ ValType::I32 => Some(Val::from(definition.get_i32())),
+ ValType::I64 => Some(Val::from(definition.get_i64())),
+ ValType::F32 => Some(Val::F32(definition.get_f32_bits())),
+ ValType::F64 => Some(Val::F64(definition.get_f64_bits())),
+ ValType::V128 => Some(Val::V128(definition.get_u128().into())),
+ ValType::Ref(_) => None,
+ }
+ }
+ }
+
pub(crate) fn _get(&self, store: &mut AutoAssertNoGc<'_>) -> Val {
unsafe {
let definition = self.definition(store).as_ref();
match self._ty(&store).content() {
- ValType::I32 => Val::from(definition.get_i32()),
- ValType::I64 => Val::from(definition.get_i64()),
- ValType::F32 => Val::F32(definition.get_f32_bits()),
- ValType::F64 => Val::F64(definition.get_f64_bits()),
- ValType::V128 => Val::V128(definition.get_u128().into()),
+ ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => self
+ ._get_numeric(store)
+ .expect("numeric globals always have a numeric value"),
ValType::Ref(ref_ty) => {
let reference: Ref = match ref_ty.heap_type() {
HeapType::Func | HeapType::ConcreteFunc(_) => Ref::Func(
diff --git a/tests/all/globals.rs b/tests/all/globals.rs
index e6694f6d4e..a5992fa664 100644
--- a/tests/all/globals.rs
+++ b/tests/all/globals.rs
@@ -103,6 +103,41 @@ fn v128() -> wasmtime::Result<()> {
Ok(())
}
+#[test]
+fn get_numeric() -> 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 new = |store: &mut Store<()>, ty: ValType, val: Val| {
+ Global::new(&mut *store, GlobalType::new(ty, Mutability::Var), val)
+ };
+ let g = new(&mut store, ValType::I32, 1i32.into())?;
+ assert_eq!(g.get_numeric(&store).and_then(|v| v.i32()), Some(1));
+ g.set(&mut store, 2i32.into())?;
+ assert_eq!(g.get_numeric(&store).and_then(|v| v.i32()), Some(2));
+ let g = new(&mut store, ValType::I64, 3i64.into())?;
+ assert_eq!(g.get_numeric(&store).and_then(|v| v.i64()), Some(3));
+ let g = new(&mut store, ValType::F32, 4f32.into())?;
+ assert_eq!(g.get_numeric(&store).and_then(|v| v.f32()), Some(4.0));
+ let g = new(&mut store, ValType::F64, 5f64.into())?;
+ assert_eq!(g.get_numeric(&store).and_then(|v| v.f64()), Some(5.0));
+ let g = new(&mut store, ValType::V128, 6u128.into())?;
+ assert_eq!(
+ g.get_numeric(&store).and_then(|v| v.v128()),
+ Some(V128::from(6))
+ );
+
+ // Reference-typed globals are not numeric.
+ let g = new(&mut store, ValType::I31REF, Val::null_any_ref())?;
+ assert!(g.get_numeric(&store).is_none());
+ let g = new(&mut store, ValType::FUNCREF, Val::null_func_ref())?;
+ assert!(g.get_numeric(&store).is_none());
+ Ok(())
+}
+
#[test]
fn i31ref_global_new() -> Result<()> {
let mut config = Config::new();