@@ -2,9 +2,9 @@ use crate::validation::{FuncValidator, FuncValidatorAllocations, ValidatorResour
22#[ cfg( feature = "validate" ) ]
33use crate :: visit:: process_operators_and_validate;
44use crate :: { Result , module:: FunctionCode , visit:: process_operators} ;
5- use alloc:: { boxed:: Box , format, string :: ToString , vec:: Vec } ;
5+ use alloc:: { boxed:: Box , format, vec:: Vec } ;
66use tinywasm_types:: * ;
7- use wasmparser:: { CompositeInnerType , OperatorsReader , OperatorsReaderAllocations } ;
7+ use wasmparser:: { CompositeInnerType , OperatorsReader , OperatorsReaderAllocations , UnpackedIndex } ;
88
99pub ( crate ) fn convert_module_element ( element : wasmparser:: Element < ' _ > ) -> Result < tinywasm_types:: Element > {
1010 let kind = match element. kind {
@@ -208,39 +208,98 @@ pub(crate) fn convert_module_code(
208208 ) )
209209}
210210
211- pub ( crate ) fn convert_module_type ( ty : wasmparser:: RecGroup ) -> Result < FuncType > {
212- let mut types = ty. types ( ) ;
213- // TODO(wasm3): Preserve recursive groups and non-function composite types instead of flattening singleton funcs.
214- if types. len ( ) != 1 {
215- return Err ( crate :: ParseError :: UnsupportedOperator (
216- "Expected exactly one type in the type section" . to_string ( ) ,
217- ) ) ;
211+ pub ( crate ) fn convert_rec_group ( ty : wasmparser:: RecGroup , group_start : u32 , types : & mut Vec < SubType > ) -> Result < u32 > {
212+ let group_len = u32:: try_from ( ty. types ( ) . len ( ) )
213+ . map_err ( |_| crate :: ParseError :: Other ( "recursive type group is too large" . into ( ) ) ) ?;
214+ types. reserve ( group_len as usize ) ;
215+ for ty in ty. into_types ( ) {
216+ let composite = & ty. composite_type ;
217+ if composite. shared {
218+ return Err ( crate :: ParseError :: UnsupportedOperator ( "shared composite types are unsupported" . into ( ) ) ) ;
219+ }
220+ if composite. descriptor_idx . is_some ( ) || composite. describes_idx . is_some ( ) {
221+ return Err ( crate :: ParseError :: UnsupportedOperator ( "descriptor types are unsupported" . into ( ) ) ) ;
222+ }
223+
224+ let supertype =
225+ ty. supertype_idx . map ( |idx| convert_type_index ( idx. unpack ( ) , group_start, group_len) ) . transpose ( ) ?;
226+ let composite = match & composite. inner {
227+ CompositeInnerType :: Func ( ty) => {
228+ let params = ty
229+ . params ( )
230+ . iter ( )
231+ . map ( |ty| convert_valtype_in_group ( ty, group_start, group_len) )
232+ . collect :: < Result < Vec < _ > > > ( ) ?;
233+ let results = ty
234+ . results ( )
235+ . iter ( )
236+ . map ( |ty| convert_valtype_in_group ( ty, group_start, group_len) )
237+ . collect :: < Result < Vec < _ > > > ( ) ?;
238+ CompositeType :: Func ( FuncType :: new ( & params, & results) )
239+ }
240+ CompositeInnerType :: Struct ( ty) => CompositeType :: Struct ( StructType {
241+ fields : ty
242+ . fields
243+ . iter ( )
244+ . map ( |field| convert_field_type ( field, group_start, group_len) )
245+ . collect :: < Result < _ > > ( ) ?,
246+ } ) ,
247+ CompositeInnerType :: Array ( ty) => {
248+ CompositeType :: Array ( ArrayType { field : convert_field_type ( & ty. 0 , group_start, group_len) ? } )
249+ }
250+ CompositeInnerType :: Cont ( _) => {
251+ return Err ( crate :: ParseError :: UnsupportedOperator ( "continuation types are unsupported" . into ( ) ) ) ;
252+ }
253+ } ;
254+ types. push ( SubType { is_final : ty. is_final , supertype, composite } ) ;
255+ }
256+ Ok ( group_len)
257+ }
258+
259+ fn convert_type_index ( index : UnpackedIndex , group_start : u32 , group_len : u32 ) -> Result < TypeAddr > {
260+ match index {
261+ UnpackedIndex :: Module ( index) => Ok ( index) ,
262+ UnpackedIndex :: RecGroup ( index) if index < group_len => {
263+ group_start. checked_add ( index) . ok_or_else ( || crate :: ParseError :: Other ( "type index is too large" . into ( ) ) )
264+ }
265+ UnpackedIndex :: RecGroup ( index) => {
266+ Err ( crate :: ParseError :: Other ( format ! ( "recursive group type index out of bounds: {index}" ) ) )
267+ }
268+ _ => Err ( crate :: ParseError :: UnsupportedOperator ( format ! ( "unsupported canonical type index: {index}" ) ) ) ,
218269 }
270+ }
219271
220- let ty = types. next ( ) . unwrap ( ) ;
221- let CompositeInnerType :: Func ( ty) = & ty. composite_type . inner else {
222- return Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
223- "Unsupported non-function type in type section: {}" ,
224- ty. composite_type
225- ) ) ) ;
272+ fn convert_field_type ( field : & wasmparser:: FieldType , group_start : u32 , group_len : u32 ) -> Result < FieldType > {
273+ let storage = match & field. element_type {
274+ wasmparser:: StorageType :: I8 => StorageType :: I8 ,
275+ wasmparser:: StorageType :: I16 => StorageType :: I16 ,
276+ wasmparser:: StorageType :: Val ( ty) => StorageType :: Value ( convert_valtype_in_group ( ty, group_start, group_len) ?) ,
226277 } ;
227- let params = ty. params ( ) . iter ( ) . map ( convert_valtype) . collect :: < Result < Vec < _ > > > ( ) ?;
228- let results = ty. results ( ) . iter ( ) . map ( convert_valtype) . collect :: < Result < Vec < _ > > > ( ) ?;
229- Ok ( FuncType :: new ( & params, & results) )
278+ Ok ( FieldType { storage, mutable : field. mutable } )
230279}
231280
232281pub ( crate ) fn convert_ref_type ( ty : wasmparser:: RefType ) -> Result < RefType > {
233282 convert_heap_type ( ty. heap_type ( ) , ty. is_nullable ( ) )
234283}
235284
236285pub ( crate ) fn convert_valtype ( valtype : & wasmparser:: ValType ) -> Result < WasmType > {
286+ convert_valtype_with_group ( valtype, None )
287+ }
288+
289+ fn convert_valtype_in_group ( valtype : & wasmparser:: ValType , group_start : u32 , group_len : u32 ) -> Result < WasmType > {
290+ convert_valtype_with_group ( valtype, Some ( ( group_start, group_len) ) )
291+ }
292+
293+ fn convert_valtype_with_group ( valtype : & wasmparser:: ValType , group : Option < ( u32 , u32 ) > ) -> Result < WasmType > {
237294 match valtype {
238295 wasmparser:: ValType :: I32 => Ok ( WasmType :: I32 ) ,
239296 wasmparser:: ValType :: I64 => Ok ( WasmType :: I64 ) ,
240297 wasmparser:: ValType :: F32 => Ok ( WasmType :: F32 ) ,
241298 wasmparser:: ValType :: F64 => Ok ( WasmType :: F64 ) ,
242299 wasmparser:: ValType :: V128 => Ok ( WasmType :: V128 ) ,
243- wasmparser:: ValType :: Ref ( r) => Ok ( WasmType :: Ref ( convert_ref_type ( * r) ?) ) ,
300+ wasmparser:: ValType :: Ref ( r) => {
301+ Ok ( WasmType :: Ref ( convert_heap_type_with_group ( r. heap_type ( ) , r. is_nullable ( ) , group) ?) )
302+ }
244303 }
245304}
246305
@@ -294,6 +353,14 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
294353}
295354
296355pub ( crate ) fn convert_heap_type ( heap : wasmparser:: HeapType , nullable : bool ) -> Result < RefType > {
356+ convert_heap_type_with_group ( heap, nullable, None )
357+ }
358+
359+ fn convert_heap_type_with_group (
360+ heap : wasmparser:: HeapType ,
361+ nullable : bool ,
362+ group : Option < ( u32 , u32 ) > ,
363+ ) -> Result < RefType > {
297364 match heap {
298365 wasmparser:: HeapType :: Abstract { shared : false , ty } => Ok ( RefType :: new_abstract (
299366 nullable,
@@ -316,9 +383,22 @@ pub(crate) fn convert_heap_type(heap: wasmparser::HeapType, nullable: bool) -> R
316383 } ,
317384 ) ) ,
318385 wasmparser:: HeapType :: Concrete ( index) => {
319- let index = index. as_module_index ( ) . ok_or_else ( || {
320- crate :: ParseError :: UnsupportedOperator ( format ! ( "Unsupported non-module heap type index: {index:?}" ) )
321- } ) ?;
386+ let index = match index {
387+ UnpackedIndex :: Module ( index) => index,
388+ index @ UnpackedIndex :: RecGroup ( _) => {
389+ let ( group_start, group_len) = group. ok_or_else ( || {
390+ crate :: ParseError :: UnsupportedOperator ( format ! (
391+ "recursive-group heap type outside a type group: {index}"
392+ ) )
393+ } ) ?;
394+ convert_type_index ( index, group_start, group_len) ?
395+ }
396+ index => {
397+ return Err ( crate :: ParseError :: UnsupportedOperator ( format ! (
398+ "unsupported canonical heap type index: {index}"
399+ ) ) ) ;
400+ }
401+ } ;
322402 RefType :: new_concrete ( nullable, index)
323403 . ok_or_else ( || crate :: ParseError :: Other ( format ! ( "heap type index is too large: {index}" ) ) )
324404 }
0 commit comments