From b6a6cefd5bd8e2e488b03024c18de3cd576d3046 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Sun, 2 Aug 2026 18:10:24 +0200 Subject: feat: typed function references (#49) Signed-off-by: Henry --------- Signed-off-by: Henry --- crates/parser/src/conversion.rs | 78 ++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 33 deletions(-) (limited to 'crates/parser/src/conversion.rs') diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 586cac5..db8d2aa 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -1,5 +1,3 @@ -use alloc::sync::Arc; - use crate::{Result, module::FunctionCode, visit::process_operators_and_validate}; use alloc::{boxed::Box, format, string::ToString, vec::Vec}; use tinywasm_types::*; @@ -26,7 +24,7 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result .collect::>>()? .into_boxed_slice(); - Ok(tinywasm_types::Element { kind, items, ty: WasmType::RefFunc, range: element.range }) + Ok(tinywasm_types::Element { kind, items, ty: RefType::FUNCREF, range: element.range }) } wasmparser::ElementItems::Expressions(ty, exprs) => { @@ -36,7 +34,7 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result .collect::>>()? .into_boxed_slice(); - Ok(tinywasm_types::Element { kind, items, ty: convert_reftype(ty)?, range: element.range }) + Ok(tinywasm_types::Element { kind, items, ty: convert_ref_type(ty)?, range: element.range }) } } } @@ -59,7 +57,7 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result ImportKind::Function(ty), wasmparser::TypeRef::Table(ty) => { - let element_type = convert_reftype(ty.element_type)?; + let element_type = convert_ref_type(ty.element_type)?; ImportKind::Table(if ty.table64 { TableType::new64(element_type, ty.initial, ty.maximum) } else { @@ -165,8 +163,9 @@ pub(crate) fn convert_module_code( )) } -pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result> { +pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result { let mut types = ty.types(); + // TODO(wasm3): Preserve recursive groups and non-function composite types instead of flattening singleton funcs. if types.len() != 1 { return Err(crate::ParseError::UnsupportedOperator( "Expected exactly one type in the type section".to_string(), @@ -182,18 +181,11 @@ pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result>>()?; let results = ty.results().iter().map(convert_valtype).collect::>>()?; - Ok(FuncType::new(¶ms, &results).into()) + Ok(FuncType::new(¶ms, &results)) } -pub(crate) fn convert_reftype(reftype: wasmparser::RefType) -> Result { - match reftype { - _ if reftype.is_func_ref() => Ok(WasmType::RefFunc), - _ if reftype.is_extern_ref() => Ok(WasmType::RefExtern), - _ => Err(crate::ParseError::UnsupportedOperator(format!( - "Unsupported reference type: {reftype:?}, {:?}", - reftype.heap_type() - ))), - } +pub(crate) fn convert_ref_type(ty: wasmparser::RefType) -> Result { + convert_heap_type(ty.heap_type(), ty.is_nullable()) } pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> Result { @@ -203,7 +195,7 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> Result wasmparser::ValType::F32 => Ok(WasmType::F32), wasmparser::ValType::F64 => Ok(WasmType::F64), wasmparser::ValType::V128 => Ok(WasmType::V128), - wasmparser::ValType::Ref(r) => convert_reftype(*r), + wasmparser::ValType::Ref(r) => Ok(WasmType::Ref(convert_ref_type(*r)?)), } } @@ -221,16 +213,13 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result match convert_heaptype(hty)? { - WasmType::RefFunc => ConstInstruction::RefFunc(None), - WasmType::RefExtern => ConstInstruction::RefExtern(None), - other => { - return Err(crate::ParseError::UnsupportedOperator(format!( - "Unsupported ref.null heap type lowered to {other:?}" - ))); - } - }, - wasmparser::Operator::RefFunc { function_index } => ConstInstruction::RefFunc(Some(function_index)), + wasmparser::Operator::RefNull { hty } => { + convert_heap_type(hty, false)?; + ConstInstruction::Ref(RefValue::Null) + } + wasmparser::Operator::RefFunc { function_index } => { + ConstInstruction::Ref(RefValue::Func(FuncRef::new(function_index))) + } wasmparser::Operator::I32Const { value } => ConstInstruction::I32Const(value), wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(value), wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())), @@ -259,14 +248,37 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result Result { +pub(crate) fn convert_heap_type(heap: wasmparser::HeapType, nullable: bool) -> Result { match heap { - wasmparser::HeapType::Abstract { shared: false, ty: wasmparser::AbstractHeapType::Func } => { - Ok(WasmType::RefFunc) + wasmparser::HeapType::Abstract { shared: false, ty } => Ok(RefType::new_abstract( + nullable, + match ty { + wasmparser::AbstractHeapType::Any => AbstractHeapType::Any, + wasmparser::AbstractHeapType::Eq => AbstractHeapType::Eq, + wasmparser::AbstractHeapType::I31 => AbstractHeapType::I31, + wasmparser::AbstractHeapType::Struct => AbstractHeapType::Struct, + wasmparser::AbstractHeapType::Array => AbstractHeapType::Array, + wasmparser::AbstractHeapType::None => AbstractHeapType::None, + wasmparser::AbstractHeapType::Func => AbstractHeapType::Func, + wasmparser::AbstractHeapType::NoFunc => AbstractHeapType::NoFunc, + wasmparser::AbstractHeapType::Exn => AbstractHeapType::Exn, + wasmparser::AbstractHeapType::NoExn => AbstractHeapType::NoExn, + wasmparser::AbstractHeapType::Extern => AbstractHeapType::Extern, + wasmparser::AbstractHeapType::NoExtern => AbstractHeapType::NoExtern, + wasmparser::AbstractHeapType::Cont | wasmparser::AbstractHeapType::NoCont => { + return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported heap type: {heap:?}"))); + } + }, + )), + wasmparser::HeapType::Concrete(index) => { + let index = index.as_module_index().ok_or_else(|| { + crate::ParseError::UnsupportedOperator(format!("Unsupported non-module heap type index: {index:?}")) + })?; + RefType::new_concrete(nullable, index) + .ok_or_else(|| crate::ParseError::Other(format!("heap type index is too large: {index}"))) } - wasmparser::HeapType::Abstract { shared: false, ty: wasmparser::AbstractHeapType::Extern } => { - Ok(WasmType::RefExtern) + wasmparser::HeapType::Abstract { shared: true, .. } | wasmparser::HeapType::Exact(_) => { + Err(crate::ParseError::UnsupportedOperator(format!("Unsupported heap type: {heap:?}"))) } - _ => Err(crate::ParseError::UnsupportedOperator(format!("Unsupported heap type: {heap:?}"))), } } -- cgit v1.3.1