Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 1cc10fe

Browse files
committed
feat: function and extern reference types for use in typed function signatures
1 parent 76a65cc commit 1cc10fe

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

crates/tinywasm/src/func.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::interpreter::stack::{CallFrame, Stack};
22
use crate::{log, unlikely, Function};
33
use crate::{Error, FuncContext, Result, Store};
44
use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec};
5-
use tinywasm_types::{FuncType, ModuleInstanceAddr, ValType, WasmValue};
5+
use tinywasm_types::{FuncType, ModuleInstanceAddr, ValType, WasmValue, WasmFuncRef, WasmExternRef};
66

77
#[derive(Debug)]
88
/// A function handle
@@ -219,6 +219,18 @@ impl ToValType for f64 {
219219
}
220220
}
221221

222+
impl ToValType for WasmExternRef {
223+
fn to_val_type() -> ValType {
224+
ValType::RefExtern
225+
}
226+
}
227+
228+
impl ToValType for WasmFuncRef {
229+
fn to_val_type() -> ValType {
230+
ValType::RefFunc
231+
}
232+
}
233+
222234
macro_rules! impl_val_types_from_tuple {
223235
($($t:ident),+) => {
224236
impl<$($t),+> ValTypesFromTuple for ($($t,)+)
@@ -251,11 +263,15 @@ impl_from_wasm_value_tuple_single!(i32);
251263
impl_from_wasm_value_tuple_single!(i64);
252264
impl_from_wasm_value_tuple_single!(f32);
253265
impl_from_wasm_value_tuple_single!(f64);
266+
impl_from_wasm_value_tuple_single!(WasmFuncRef);
267+
impl_from_wasm_value_tuple_single!(WasmExternRef);
254268

255269
impl_into_wasm_value_tuple_single!(i32);
256270
impl_into_wasm_value_tuple_single!(i64);
257271
impl_into_wasm_value_tuple_single!(f32);
258272
impl_into_wasm_value_tuple_single!(f64);
273+
impl_into_wasm_value_tuple_single!(WasmFuncRef);
274+
impl_into_wasm_value_tuple_single!(WasmExternRef);
259275

260276
impl_val_types_from_tuple!(T1);
261277
impl_val_types_from_tuple!(T1, T2);

crates/types/src/value.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,32 @@ impl WasmValue {
180180
}
181181
}
182182

183+
/// a wrapper for `funcref` value for use in typed wrappers
184+
#[derive(Clone, Copy, PartialEq, Debug)]
185+
pub struct WasmFuncRef(FuncAddr);
186+
187+
/// a wrapper for `externref` value for use in typed wrappers
188+
#[derive(Clone, Copy, PartialEq, Debug)]
189+
pub struct WasmExternRef(ExternAddr);
190+
191+
macro_rules! impl_newtype_from_into {
192+
($wrapper:ty, $underlying:ty) => {
193+
impl From<$underlying> for $wrapper {
194+
fn from(value: $underlying) -> Self {
195+
Self(value)
196+
}
197+
}
198+
impl From<$wrapper> for $underlying {
199+
fn from(value: $wrapper) -> Self {
200+
value.0
201+
}
202+
}
203+
};
204+
}
205+
206+
impl_newtype_from_into!(WasmFuncRef, FuncAddr);
207+
impl_newtype_from_into!(WasmExternRef, ExternAddr);
208+
183209
/// Type of a WebAssembly value.
184210
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
185211
#[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
@@ -219,7 +245,7 @@ macro_rules! impl_conversion_for_wasmvalue {
219245
impl From<$t> for WasmValue {
220246
#[inline]
221247
fn from(i: $t) -> Self {
222-
Self::$variant(i)
248+
Self::$variant(i.into())
223249
}
224250
}
225251

@@ -230,7 +256,7 @@ macro_rules! impl_conversion_for_wasmvalue {
230256
#[inline]
231257
fn try_from(value: WasmValue) -> Result<Self, Self::Error> {
232258
if let WasmValue::$variant(i) = value {
233-
Ok(i)
259+
Ok(i.into())
234260
} else {
235261
cold();
236262
Err(())
@@ -241,4 +267,4 @@ macro_rules! impl_conversion_for_wasmvalue {
241267
}
242268
}
243269

244-
impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, u128 => V128 }
270+
impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, u128 => V128, WasmFuncRef=>RefFunc, WasmExternRef=>RefExtern }

0 commit comments

Comments
 (0)