From 1cc10fe33a06830d941275da769a01e5c233ff9e Mon Sep 17 00:00:00 2001 From: Nikita Vertikov Date: Tue, 31 Dec 2024 01:02:15 +0600 Subject: [PATCH 1/2] feat: function and extern reference types for use in typed function signatures --- crates/tinywasm/src/func.rs | 18 +++++++++++++++++- crates/types/src/value.rs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/crates/tinywasm/src/func.rs b/crates/tinywasm/src/func.rs index 7035109..5cf9033 100644 --- a/crates/tinywasm/src/func.rs +++ b/crates/tinywasm/src/func.rs @@ -2,7 +2,7 @@ use crate::interpreter::stack::{CallFrame, Stack}; use crate::{log, unlikely, Function}; use crate::{Error, FuncContext, Result, Store}; use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec}; -use tinywasm_types::{FuncType, ModuleInstanceAddr, ValType, WasmValue}; +use tinywasm_types::{FuncType, ModuleInstanceAddr, ValType, WasmValue, WasmFuncRef, WasmExternRef}; #[derive(Debug)] /// A function handle @@ -219,6 +219,18 @@ impl ToValType for f64 { } } +impl ToValType for WasmExternRef { + fn to_val_type() -> ValType { + ValType::RefExtern + } +} + +impl ToValType for WasmFuncRef { + fn to_val_type() -> ValType { + ValType::RefFunc + } +} + macro_rules! impl_val_types_from_tuple { ($($t:ident),+) => { impl<$($t),+> ValTypesFromTuple for ($($t,)+) @@ -251,11 +263,15 @@ impl_from_wasm_value_tuple_single!(i32); impl_from_wasm_value_tuple_single!(i64); impl_from_wasm_value_tuple_single!(f32); impl_from_wasm_value_tuple_single!(f64); +impl_from_wasm_value_tuple_single!(WasmFuncRef); +impl_from_wasm_value_tuple_single!(WasmExternRef); impl_into_wasm_value_tuple_single!(i32); impl_into_wasm_value_tuple_single!(i64); impl_into_wasm_value_tuple_single!(f32); impl_into_wasm_value_tuple_single!(f64); +impl_into_wasm_value_tuple_single!(WasmFuncRef); +impl_into_wasm_value_tuple_single!(WasmExternRef); impl_val_types_from_tuple!(T1); impl_val_types_from_tuple!(T1, T2); diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index 8bbcaf2..086c1aa 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -180,6 +180,32 @@ impl WasmValue { } } +/// a wrapper for `funcref` value for use in typed wrappers +#[derive(Clone, Copy, PartialEq, Debug)] +pub struct WasmFuncRef(FuncAddr); + +/// a wrapper for `externref` value for use in typed wrappers +#[derive(Clone, Copy, PartialEq, Debug)] +pub struct WasmExternRef(ExternAddr); + +macro_rules! impl_newtype_from_into { + ($wrapper:ty, $underlying:ty) => { + impl From<$underlying> for $wrapper { + fn from(value: $underlying) -> Self { + Self(value) + } + } + impl From<$wrapper> for $underlying { + fn from(value: $wrapper) -> Self { + value.0 + } + } + }; +} + +impl_newtype_from_into!(WasmFuncRef, FuncAddr); +impl_newtype_from_into!(WasmExternRef, ExternAddr); + /// Type of a WebAssembly value. #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] @@ -219,7 +245,7 @@ macro_rules! impl_conversion_for_wasmvalue { impl From<$t> for WasmValue { #[inline] fn from(i: $t) -> Self { - Self::$variant(i) + Self::$variant(i.into()) } } @@ -230,7 +256,7 @@ macro_rules! impl_conversion_for_wasmvalue { #[inline] fn try_from(value: WasmValue) -> Result { if let WasmValue::$variant(i) = value { - Ok(i) + Ok(i.into()) } else { cold(); Err(()) @@ -241,4 +267,4 @@ macro_rules! impl_conversion_for_wasmvalue { } } -impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, u128 => V128 } +impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, u128 => V128, WasmFuncRef=>RefFunc, WasmExternRef=>RefExtern } From 3f06ac8608fc4fb9222b69f71d2b53d5a83de969 Mon Sep 17 00:00:00 2001 From: Nikita Vertikov Date: Tue, 31 Dec 2024 01:19:14 +0600 Subject: [PATCH 2/2] Doc: add example for using func reference as callbacks --- examples/funcref_callbacks.rs | 152 ++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 examples/funcref_callbacks.rs diff --git a/examples/funcref_callbacks.rs b/examples/funcref_callbacks.rs new file mode 100644 index 0000000..bea93ad --- /dev/null +++ b/examples/funcref_callbacks.rs @@ -0,0 +1,152 @@ +use eyre::Result; +use tinywasm::{types::WasmFuncRef, Extern, FuncContext, Imports, Module, Store}; +use wat; + +fn main() -> Result<()> { + by_func_ref_passed()?; + by_func_ref_returned()?; + Ok(()) +} + +/// example of passing wasm functions (as funcref) to imported host function +/// and imported host function calling them +fn by_func_ref_passed() -> Result<()> { + // a module with: + // imported function "host.call_this" that accepts a callback + // exported wasm function "tell_host_to_call" that calls "host.call_this" with wasm functions $add and $sub + // wasm functions $add and $sub and imported function $mul used as callbacks + // (just to show that imported functions can be referenced too) + // exported wasm function "call_binop_by_ref" is a proxy used by host to call func-references of type (i32, i32)->i32 + const WASM: &str = r#" + (module + (import "host" "call_this" (func $host_callback_caller (param funcref))) + (import "host" "mul" (func $host_mul (param $x i32) (param $y i32) (result i32))) + + (func $tell_host_to_call (export "tell_host_to_call") + (call $host_callback_caller (ref.func $add)) + (call $host_callback_caller (ref.func $sub)) + (call $host_callback_caller (ref.func $host_mul)) + ) + + (type $binop (func (param i32 i32) (result i32))) + + (table 3 funcref) + (elem (i32.const 0) $add $sub $host_mul) ;; function can only be taken reference of if it's added to a table + (func $add (param $x i32) (param $y i32) (result i32) + local.get $x + local.get $y + i32.add + ) + (func $sub (param $x i32) (param $y i32) (result i32) + local.get $x + local.get $y + i32.sub + ) + + (table $callback_register 1 funcref) + (func (export "call_binop_by_ref") (param funcref i32 i32) (result i32) + (table.set $callback_register (i32.const 0) (local.get 0)) + (call_indirect $callback_register (type $binop) (local.get 1)(local.get 2)(i32.const 0)) + ) + ) + "#; + + let wasm = wat::parse_str(WASM).expect("failed to parse wat"); + let module = Module::parse_bytes(&wasm)?; + let mut store = Store::default(); + let mut imports = Imports::new(); + // import host function that takes callbacks and calls them + imports.define( + "host", + "call_this", + Extern::typed_func(|mut ctx: FuncContext<'_>, fn_ref: WasmFuncRef| -> tinywasm::Result<()> { + let proxy_caller = + ctx.module().exported_func::<(WasmFuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?; + // call callback we got as argument using call_binop_by_ref + let res = proxy_caller.call(ctx.store_mut(), (fn_ref, 5, 3))?; + println!("(funcref {fn_ref:?})(5,3) results in {res}"); + + Ok(()) + }), + )?; + // import host.mul function (one of the functions whose references are taken) + imports.define( + "host", + "mul", + Extern::typed_func(|_, args: (i32, i32)| -> tinywasm::Result { Ok(args.0 * args.1) }), + )?; + let instance = module.instantiate(&mut store, Some(imports))?; + let caller = instance.exported_func::<(), ()>(&mut store, "tell_host_to_call")?; + // call the tell_host_to_call + caller.call(&mut store, ())?; + // interesting detail is that neither $add $sub $mul were exported, + // but with a little help from proxy "call_binop_by_ref" references to them host was able to call them + Ok(()) +} + +/// example of returning wasm function as callback to host function +/// and host function calling it +fn by_func_ref_returned() -> Result<()> { + // a module with: + // an exported function "what_should_host_call" that returns 3 funcrefs + // wasm functions $add and $sub and imported function $mul used as callbacks + // (just to show that imported functions can be referenced too) + // exported wasm function "call_binop_by_ref" is a proxy used by host to call func-references of type (i32, i32)->i32 + const WASM: &str = r#" + (module + (import "host" "mul" (func $host_mul (param $x i32) (param $y i32) (result i32))) + (type $binop (func (param i32 i32) (result i32))) + (table 3 funcref) + (elem (i32.const 0) $add $sub $host_mul) + (func $add (param $x i32) (param $y i32) (result i32) + local.get $x + local.get $y + i32.add + ) + (func $sub (param $x i32) (param $y i32) (result i32) + local.get $x + local.get $y + i32.sub + ) + (func $ref_to_funcs (export "what_should_host_call") (result funcref funcref funcref) + (ref.func $add) + (ref.func $sub) + (ref.func $host_mul) + ) + + (table $callback_register 1 funcref) + (func $call (export "call_binop_by_ref") (param funcref i32 i32) (result i32) + (table.set $callback_register (i32.const 0) (local.get 0)) + (call_indirect $callback_register (type $binop) (local.get 1)(local.get 2)(i32.const 0)) + ) + ) + "#; + + let wasm = wat::parse_str(WASM).expect("failed to parse wat"); + let module = Module::parse_bytes(&wasm)?; + let mut store = Store::default(); + let mut imports = Imports::new(); + // import host.mul function (one of the possible operations) + imports.define( + "host", + "mul", + Extern::typed_func(|_, args: (i32, i32)| -> tinywasm::Result { Ok(args.0 * args.1) }), + )?; + + let instance = module.instantiate(&mut store, Some(imports))?; + { + // ask module what should we call + let funcrefs = { + let address_getter = instance + .exported_func::<(), (WasmFuncRef, WasmFuncRef, WasmFuncRef)>(&mut store, "what_should_host_call")?; + address_getter.call(&mut store, ())? + }; + let proxy_caller = instance.exported_func::<(WasmFuncRef, i32, i32), i32>(&mut store, "call_binop_by_ref")?; + for (idx, func_ref) in [funcrefs.0, funcrefs.1, funcrefs.2].iter().enumerate() { + // call those funcrefs via "call_binop_by_ref" + let res = proxy_caller.call(&mut store, (*func_ref, 5, 3))?; + println!("at idx: {idx} funcref {func_ref:?} results in {res}"); + } + } + Ok(()) +}