Skip to content

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

Add `anyref` downcast methods to the C and C++ APIs (#12917)

Nick Fitzgerald0126a94171fd
diff --git a/crates/c-api/include/wasmtime/gc.h b/crates/c-api/include/wasmtime/gc.h
index 486ba319bf..701c296bec 100644
--- a/crates/c-api/include/wasmtime/gc.h
+++ b/crates/c-api/include/wasmtime/gc.h
@@ -535,6 +535,60 @@ WASM_API_EXTERN bool wasmtime_eqref_as_array(wasmtime_context_t *context,
const wasmtime_eqref_t *eqref,
wasmtime_arrayref_t *out);
+/**
+ * \brief Test whether an `anyref` is an `eqref`.
+ *
+ * Returns `false` for null references.
+ */
+WASM_API_EXTERN bool wasmtime_anyref_is_eqref(wasmtime_context_t *context,
+ const wasmtime_anyref_t *anyref);
+
+/**
+ * \brief Downcast an `anyref` to an `eqref`.
+ *
+ * If the given `anyref` is an `eqref`, a new root is stored in `out` and
+ * `true` is returned. Otherwise `false` is returned and `out` is set to null.
+ */
+WASM_API_EXTERN bool wasmtime_anyref_as_eqref(wasmtime_context_t *context,
+ const wasmtime_anyref_t *anyref,
+ wasmtime_eqref_t *out);
+
+/**
+ * \brief Test whether an `anyref` is a `structref`.
+ *
+ * Returns `false` for null references.
+ */
+WASM_API_EXTERN bool wasmtime_anyref_is_struct(wasmtime_context_t *context,
+ const wasmtime_anyref_t *anyref);
+
+/**
+ * \brief Downcast an `anyref` to a `structref`.
+ *
+ * If the given `anyref` is a `structref`, a new root is stored in `out` and
+ * `true` is returned. Otherwise `false` is returned and `out` is set to null.
+ */
+WASM_API_EXTERN bool wasmtime_anyref_as_struct(wasmtime_context_t *context,
+ const wasmtime_anyref_t *anyref,
+ wasmtime_structref_t *out);
+
+/**
+ * \brief Test whether an `anyref` is an `arrayref`.
+ *
+ * Returns `false` for null references.
+ */
+WASM_API_EXTERN bool wasmtime_anyref_is_array(wasmtime_context_t *context,
+ const wasmtime_anyref_t *anyref);
+
+/**
+ * \brief Downcast an `anyref` to an `arrayref`.
+ *
+ * If the given `anyref` is an `arrayref`, a new root is stored in `out` and
+ * `true` is returned. Otherwise `false` is returned and `out` is set to null.
+ */
+WASM_API_EXTERN bool wasmtime_anyref_as_array(wasmtime_context_t *context,
+ const wasmtime_anyref_t *anyref,
+ wasmtime_arrayref_t *out);
+
#ifdef __cplusplus
} // extern "C"
#endif
diff --git a/crates/c-api/include/wasmtime/gc.hh b/crates/c-api/include/wasmtime/gc.hh
index aa0eb68c9d..7ce47841f6 100644
--- a/crates/c-api/include/wasmtime/gc.hh
+++ b/crates/c-api/include/wasmtime/gc.hh
@@ -445,6 +445,35 @@ inline ArrayRef EqRef::as_array(Store::Context cx) const {
return ArrayRef(out);
}
+// AnyRef downcast method definitions (declared in val.hh)
+inline bool AnyRef::is_eqref(Store::Context cx) const {
+ return wasmtime_anyref_is_eqref(cx.capi(), &val);
+}
+inline bool AnyRef::is_struct(Store::Context cx) const {
+ return wasmtime_anyref_is_struct(cx.capi(), &val);
+}
+inline bool AnyRef::is_array(Store::Context cx) const {
+ return wasmtime_anyref_is_array(cx.capi(), &val);
+}
+inline std::optional<EqRef> AnyRef::as_eqref(Store::Context cx) const {
+ wasmtime_eqref_t out;
+ if (wasmtime_anyref_as_eqref(cx.capi(), &val, &out))
+ return EqRef(out);
+ return std::nullopt;
+}
+inline std::optional<StructRef> AnyRef::as_struct(Store::Context cx) const {
+ wasmtime_structref_t out;
+ if (wasmtime_anyref_as_struct(cx.capi(), &val, &out))
+ return StructRef(out);
+ return std::nullopt;
+}
+inline std::optional<ArrayRef> AnyRef::as_array(Store::Context cx) const {
+ wasmtime_arrayref_t out;
+ if (wasmtime_anyref_as_array(cx.capi(), &val, &out))
+ return ArrayRef(out);
+ return std::nullopt;
+}
+
} // namespace wasmtime
#endif // WASMTIME_GC_HH
diff --git a/crates/c-api/include/wasmtime/val.hh b/crates/c-api/include/wasmtime/val.hh
index 1d49cfe2c0..7f27aad84f 100644
--- a/crates/c-api/include/wasmtime/val.hh
+++ b/crates/c-api/include/wasmtime/val.hh
@@ -13,6 +13,10 @@
namespace wasmtime {
+class EqRef;
+class StructRef;
+class ArrayRef;
+
/**
* \brief Representation of a WebAssembly `externref` value.
*
@@ -181,6 +185,24 @@ public:
bool is_i31(Store::Context cx) const {
return wasmtime_anyref_is_i31(cx.ptr, &val);
}
+
+ /// \brief Returns `true` if this anyref is an eqref.
+ inline bool is_eqref(Store::Context cx) const;
+
+ /// \brief Returns `true` if this anyref is a structref.
+ inline bool is_struct(Store::Context cx) const;
+
+ /// \brief Returns `true` if this anyref is an arrayref.
+ inline bool is_array(Store::Context cx) const;
+
+ /// \brief Downcast to eqref. Returns null eqref if not an eqref.
+ inline std::optional<EqRef> as_eqref(Store::Context cx) const;
+
+ /// \brief Downcast to structref. Returns null structref if not a structref.
+ inline std::optional<StructRef> as_struct(Store::Context cx) const;
+
+ /// \brief Downcast to arrayref. Returns null arrayref if not an arrayref.
+ inline std::optional<ArrayRef> as_array(Store::Context cx) const;
};
/// \brief Container for the `v128` WebAssembly type.
diff --git a/crates/c-api/src/ref.rs b/crates/c-api/src/ref.rs
index e75870a167..9774a713c1 100644
--- a/crates/c-api/src/ref.rs
+++ b/crates/c-api/src/ref.rs
@@ -937,3 +937,93 @@ pub unsafe extern "C" fn wasmtime_eqref_as_array(
crate::initialize(out, None::<OwnedRooted<ArrayRef>>.into());
false
}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_anyref_is_eqref(
+ cx: WasmtimeStoreContextMut<'_>,
+ anyref: Option<&wasmtime_anyref_t>,
+) -> bool {
+ match anyref.and_then(|a| a.as_wasmtime()) {
+ Some(anyref) => anyref.is_eqref(&cx).expect("OwnedRooted always in scope"),
+ None => false,
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_anyref_as_eqref(
+ mut cx: WasmtimeStoreContextMut<'_>,
+ anyref: Option<&wasmtime_anyref_t>,
+ out: &mut MaybeUninit<wasmtime_eqref_t>,
+) -> bool {
+ if let Some(anyref) = anyref.and_then(|a| a.as_wasmtime()) {
+ let mut scope = RootScope::new(&mut cx);
+ let rooted = anyref.to_rooted(&mut scope);
+ if let Ok(Some(eqref)) = rooted.as_eqref(&mut scope) {
+ let owned = eqref.to_owned_rooted(&mut scope).expect("in scope");
+ crate::initialize(out, Some(owned).into());
+ return true;
+ }
+ }
+ crate::initialize(out, None::<OwnedRooted<EqRef>>.into());
+ false
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_anyref_is_struct(
+ cx: WasmtimeStoreContextMut<'_>,
+ anyref: Option<&wasmtime_anyref_t>,
+) -> bool {
+ match anyref.and_then(|a| a.as_wasmtime()) {
+ Some(anyref) => anyref.is_struct(&cx).expect("OwnedRooted always in scope"),
+ None => false,
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_anyref_as_struct(
+ mut cx: WasmtimeStoreContextMut<'_>,
+ anyref: Option<&wasmtime_anyref_t>,
+ out: &mut MaybeUninit<wasmtime_structref_t>,
+) -> bool {
+ if let Some(anyref) = anyref.and_then(|a| a.as_wasmtime()) {
+ let mut scope = RootScope::new(&mut cx);
+ let rooted = anyref.to_rooted(&mut scope);
+ if let Ok(Some(structref)) = rooted.as_struct(&scope) {
+ let owned = structref.to_owned_rooted(&mut scope).expect("in scope");
+ crate::initialize(out, Some(owned).into());
+ return true;
+ }
+ }
+ crate::initialize(out, None::<OwnedRooted<StructRef>>.into());
+ false
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_anyref_is_array(
+ cx: WasmtimeStoreContextMut<'_>,
+ anyref: Option<&wasmtime_anyref_t>,
+) -> bool {
+ match anyref.and_then(|a| a.as_wasmtime()) {
+ Some(anyref) => anyref.is_array(&cx).expect("OwnedRooted always in scope"),
+ None => false,
+ }
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_anyref_as_array(
+ mut cx: WasmtimeStoreContextMut<'_>,
+ anyref: Option<&wasmtime_anyref_t>,
+ out: &mut MaybeUninit<wasmtime_arrayref_t>,
+) -> bool {
+ if let Some(anyref) = anyref.and_then(|a| a.as_wasmtime()) {
+ let mut scope = RootScope::new(&mut cx);
+ let rooted = anyref.to_rooted(&mut scope);
+ if let Ok(Some(arrayref)) = rooted.as_array(&scope) {
+ let owned = arrayref.to_owned_rooted(&mut scope).expect("in scope");
+ crate::initialize(out, Some(owned).into());
+ return true;
+ }
+ }
+ crate::initialize(out, None::<OwnedRooted<ArrayRef>>.into());
+ false
+}
diff --git a/crates/c-api/tests/gc.cc b/crates/c-api/tests/gc.cc
index 71629c48d7..b7fbc44501 100644
--- a/crates/c-api/tests/gc.cc
+++ b/crates/c-api/tests/gc.cc
@@ -232,3 +232,82 @@ TEST(ArrayRef, UpcastAndDowncast) {
AnyRef any = arr.to_anyref();
EXPECT_FALSE(any.is_i31(cx));
}
+
+TEST(AnyRef, DowncastI31) {
+ Config config;
+ config.wasm_gc(true);
+ Engine engine(std::move(config));
+ Store store(engine);
+ auto cx = store.context();
+
+ AnyRef any = AnyRef::i31(cx, 42);
+ EXPECT_TRUE(any.is_i31(cx));
+ EXPECT_TRUE(any.is_eqref(cx));
+ EXPECT_FALSE(any.is_struct(cx));
+ EXPECT_FALSE(any.is_array(cx));
+
+ // Downcast to eqref.
+ auto opt_eq = any.as_eqref(cx);
+ EXPECT_TRUE(opt_eq);
+ EqRef eq = *opt_eq;
+ EXPECT_TRUE(eq.is_i31(cx));
+ auto val = eq.i31_get_u(cx);
+ ASSERT_TRUE(val.has_value());
+ EXPECT_EQ(*val, 42u);
+}
+
+TEST(AnyRef, DowncastStruct) {
+ Config config;
+ config.wasm_gc(true);
+ Engine engine(std::move(config));
+ Store store(engine);
+ auto cx = store.context();
+
+ auto ty = StructType::create(engine, {FieldType::const_(WASMTIME_I32)});
+ auto pre = StructRefPre::create(cx, ty);
+ auto result = StructRef::create(cx, pre, {Val(int32_t(77))});
+ ASSERT_TRUE(result);
+ StructRef s = result.ok();
+
+ AnyRef any = s.to_anyref();
+ EXPECT_TRUE(any.is_eqref(cx));
+ EXPECT_TRUE(any.is_struct(cx));
+ EXPECT_FALSE(any.is_array(cx));
+ EXPECT_FALSE(any.is_i31(cx));
+
+ // Downcast back to struct.
+ auto opt_s2 = any.as_struct(cx);
+ EXPECT_TRUE(opt_s2);
+ StructRef s2 = *opt_s2;
+ auto v = s2.field(cx, 0);
+ ASSERT_TRUE(v);
+ EXPECT_EQ(v.ok().i32(), 77);
+}
+
+TEST(AnyRef, DowncastArray) {
+ Config config;
+ config.wasm_gc(true);
+ Engine engine(std::move(config));
+ Store store(engine);
+ auto cx = store.context();
+
+ auto ty = ArrayType::create(engine, FieldType::const_(WASMTIME_I32));
+ auto pre = ArrayRefPre::create(cx, ty);
+ auto result = ArrayRef::create(cx, pre, Val(int32_t(55)), 2);
+ ASSERT_TRUE(result);
+ ArrayRef arr = result.ok();
+
+ AnyRef any = arr.to_anyref();
+ EXPECT_TRUE(any.is_eqref(cx));
+ EXPECT_FALSE(any.is_struct(cx));
+ EXPECT_TRUE(any.is_array(cx));
+ EXPECT_FALSE(any.is_i31(cx));
+
+ // Downcast back to array.
+ auto opt_arr2 = any.as_array(cx);
+ EXPECT_TRUE(opt_arr2);
+ ArrayRef arr2 = *opt_arr2;
+ auto len = arr2.len(cx);
+ ASSERT_TRUE(len);
+ EXPECT_EQ(len.ok(), 2u);
+}