diff --git a/lib/runtime-core/src/macros.rs b/lib/runtime-core/src/macros.rs index 52bf892ce..4477f6c3e 100644 --- a/lib/runtime-core/src/macros.rs +++ b/lib/runtime-core/src/macros.rs @@ -17,49 +17,21 @@ macro_rules! func { ($func:ident, [ $( $params:ident ),* ] -> [ $( $returns:ident ),* ] ) => {{ use $crate::{ export::{Context, Export, FuncPointer}, - types::{FuncSig, Type}, + types::{FuncSig, Type, WasmExternType}, vm, }; - let func: extern fn( $( $params, )* &mut vm::Ctx) -> ($( $returns )*) = $func; - Export::Function { func: unsafe { FuncPointer::new(func as _) }, ctx: Context::Internal, signature: FuncSig::new( - &[$($crate::__export_func_convert_type!($params),)*] as &[Type], - &[$($crate::__export_func_convert_type!($returns),)*] as &[Type], + &[ $( <$params as WasmExternType>::TYPE, )* ] as &[Type], + &[ $( <$returns as WasmExternType>::TYPE, )* ] as &[Type], ).into(), } }}; } -#[macro_export] -#[doc(hidden)] -macro_rules! __export_func_convert_type { - (i32) => { - Type::I32 - }; - (u32) => { - Type::I32 - }; - (i64) => { - Type::I64 - }; - (u64) => { - Type::I64 - }; - (f32) => { - Type::F32 - }; - (f64) => { - Type::F64 - }; - ($x:ty) => { - compile_error!("Only `i32`, `u32`, `i64`, `u64`, `f32`, and `f64` are supported for argument and return types") - }; -} - /// Generate an [`ImportObject`] safely. /// /// [`ImportObject`]: struct.ImportObject.html diff --git a/lib/runtime-core/src/types.rs b/lib/runtime-core/src/types.rs index 0ed0134a4..efe537813 100644 --- a/lib/runtime-core/src/types.rs +++ b/lib/runtime-core/src/types.rs @@ -65,6 +65,31 @@ impl From for Value { } } +pub unsafe trait WasmExternType: Copy + Clone +where + Self: Sized, +{ + const TYPE: Type; +} +unsafe impl WasmExternType for i32 { + const TYPE: Type = Type::I32; +} +unsafe impl WasmExternType for u32 { + const TYPE: Type = Type::I32; +} +unsafe impl WasmExternType for i64 { + const TYPE: Type = Type::I64; +} +unsafe impl WasmExternType for u64 { + const TYPE: Type = Type::I64; +} +unsafe impl WasmExternType for f32 { + const TYPE: Type = Type::F32; +} +unsafe impl WasmExternType for f64 { + const TYPE: Type = Type::F64; +} + pub enum ValueError { BufferTooSmall, }