diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index b1427c0..4f79a4a 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -11,19 +11,13 @@ use crate::{module::CodeSection, Result}; pub(crate) fn convert_module_elements<'a, T: IntoIterator>>>( elements: T, ) -> Result> { - let elements = elements - .into_iter() - .map(|element| convert_module_element(element?)) - .collect::>>()?; + let elements = elements.into_iter().map(|element| convert_module_element(element?)).collect::>>()?; Ok(elements) } pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result { let kind = match element.kind { - wasmparser::ElementKind::Active { - table_index, - offset_expr, - } => tinywasm_types::ElementKind::Active { + wasmparser::ElementKind::Active { table_index, offset_expr } => tinywasm_types::ElementKind::Active { table: table_index, offset: process_const_operators(offset_expr.get_operators_reader())?, }, @@ -32,38 +26,24 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result }; let items = match element.items { - wasmparser::ElementItems::Functions(funcs) => funcs - .into_iter() - .map(|func| Ok(ElementItem::Func(func?))) - .collect::>>()? - .into_boxed_slice(), + wasmparser::ElementItems::Functions(funcs) => { + funcs.into_iter().map(|func| Ok(ElementItem::Func(func?))).collect::>>()?.into_boxed_slice() + } wasmparser::ElementItems::Expressions(exprs) => exprs .into_iter() - .map(|expr| { - Ok(ElementItem::Expr(process_const_operators( - expr?.get_operators_reader(), - )?)) - }) + .map(|expr| Ok(ElementItem::Expr(process_const_operators(expr?.get_operators_reader())?))) .collect::>>()? .into_boxed_slice(), }; - Ok(tinywasm_types::Element { - kind, - items, - ty: convert_valtype(&element.ty), - range: element.range, - }) + Ok(tinywasm_types::Element { kind, items, ty: convert_valtype(&element.ty), range: element.range }) } pub(crate) fn convert_module_data_sections<'a, T: IntoIterator>>>( data_sections: T, ) -> Result> { - let data_sections = data_sections - .into_iter() - .map(|data| convert_module_data(data?)) - .collect::>>()?; + let data_sections = data_sections.into_iter().map(|data| convert_module_data(data?)).collect::>>()?; Ok(data_sections) } @@ -72,15 +52,9 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result { + wasmparser::DataKind::Active { memory_index, offset_expr } => { let offset = process_const_operators(offset_expr.get_operators_reader())?; - tinywasm_types::DataKind::Active { - mem: memory_index, - offset, - } + tinywasm_types::DataKind::Active { mem: memory_index, offset } } wasmparser::DataKind::Passive => tinywasm_types::DataKind::Passive, }, @@ -90,10 +64,7 @@ pub(crate) fn convert_module_data(data: wasmparser::Data<'_>) -> Result>>>( imports: T, ) -> Result> { - let imports = imports - .into_iter() - .map(|import| convert_module_import(import?)) - .collect::>>()?; + let imports = imports.into_iter().map(|import| convert_module_import(import?)).collect::>>()?; Ok(imports) } @@ -105,15 +76,11 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result ImportKind::Func(ty), wasmparser::TypeRef::Table(ty) => ImportKind::Table(convert_module_table(ty)?), wasmparser::TypeRef::Memory(ty) => ImportKind::Mem(convert_module_memory(ty)?), - wasmparser::TypeRef::Global(ty) => ImportKind::Global(GlobalType { - mutable: ty.mutable, - ty: convert_valtype(&ty.content_type), - }), + wasmparser::TypeRef::Global(ty) => { + ImportKind::Global(GlobalType { mutable: ty.mutable, ty: convert_valtype(&ty.content_type) }) + } wasmparser::TypeRef::Tag(ty) => { - return Err(crate::ParseError::UnsupportedOperator(format!( - "Unsupported import kind: {:?}", - ty - ))) + return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {:?}", ty))) } }, }) @@ -122,10 +89,8 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result>>( memory_types: T, ) -> Result> { - let memory_type = memory_types - .into_iter() - .map(|memory| convert_module_memory(memory?)) - .collect::>>()?; + let memory_type = + memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::>>()?; Ok(memory_type) } @@ -144,21 +109,14 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result>>( table_types: T, ) -> Result> { - let table_type = table_types - .into_iter() - .map(|table| convert_module_table(table?)) - .collect::>>()?; + let table_type = table_types.into_iter().map(|table| convert_module_table(table?)).collect::>>()?; Ok(table_type) } pub(crate) fn convert_module_table(table: wasmparser::TableType) -> Result { let ty = convert_valtype(&table.element_type); - Ok(TableType { - element_type: ty, - size_initial: table.initial, - size_max: table.maximum, - }) + Ok(TableType { element_type: ty, size_initial: table.initial, size_max: table.maximum }) } pub(crate) fn convert_module_globals<'a, T: IntoIterator>>>( @@ -171,13 +129,7 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator>>()?; Ok(globals) @@ -190,18 +142,11 @@ pub(crate) fn convert_module_export(export: wasmparser::Export) -> Result ExternalKind::Memory, wasmparser::ExternalKind::Global => ExternalKind::Global, wasmparser::ExternalKind::Tag => { - return Err(crate::ParseError::UnsupportedOperator(format!( - "Unsupported export kind: {:?}", - export.kind - ))) + return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported export kind: {:?}", export.kind))) } }; - Ok(Export { - index: export.index, - name: Box::from(export.name), - kind, - }) + Ok(Export { index: export.index, name: Box::from(export.name), kind }) } pub(crate) fn convert_module_code( @@ -224,27 +169,16 @@ pub(crate) fn convert_module_code( let body_reader = func.get_operators_reader()?; let body = process_operators(body_reader.original_position(), body_reader.into_iter(), validator)?; - Ok(CodeSection { - locals: locals.into_boxed_slice(), - body, - }) + Ok(CodeSection { locals: locals.into_boxed_slice(), body }) } pub(crate) fn convert_module_type(ty: wasmparser::Type) -> Result { let wasmparser::Type::Func(ty) = ty; - let params = ty - .params() - .iter() - .map(|p| Ok(convert_valtype(p))) - .collect::>>()? - .into_boxed_slice(); - - let results = ty - .results() - .iter() - .map(|p| Ok(convert_valtype(p))) - .collect::>>()? - .into_boxed_slice(); + let params = + ty.params().iter().map(|p| Ok(convert_valtype(p))).collect::>>()?.into_boxed_slice(); + + let results = + ty.results().iter().map(|p| Ok(convert_valtype(p))).collect::>>()?.into_boxed_slice(); Ok(FuncType { params, results }) } @@ -270,19 +204,14 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType { I64 => ValType::I64, F32 => ValType::F32, F64 => ValType::F64, - V128 => ValType::V128, + V128 => unimplemented!("128-bit values are not supported yet"), FuncRef => ValType::FuncRef, ExternRef => ValType::ExternRef, } } pub(crate) fn convert_memarg(memarg: wasmparser::MemArg) -> MemArg { - MemArg { - offset: memarg.offset, - align: memarg.align, - align_max: memarg.max_align, - mem_addr: memarg.memory, - } + MemArg { offset: memarg.offset, align: memarg.align, align_max: memarg.max_align, mem_addr: memarg.memory } } pub(crate) fn process_const_operators(ops: OperatorsReader) -> Result { @@ -306,10 +235,7 @@ pub fn process_const_operator(op: wasmparser::Operator) -> Result Ok(ConstInstruction::F32Const(f32::from_bits(value.bits()))), // TODO: check if this is correct wasmparser::Operator::F64Const { value } => Ok(ConstInstruction::F64Const(f64::from_bits(value.bits()))), // TODO: check if this is correct wasmparser::Operator::GlobalGet { global_index } => Ok(ConstInstruction::GlobalGet(global_index)), - op => Err(crate::ParseError::UnsupportedOperator(format!( - "Unsupported const instruction: {:?}", - op - ))), + op => Err(crate::ParseError::UnsupportedOperator(format!("Unsupported const instruction: {:?}", op))), } } @@ -332,9 +258,7 @@ pub fn process_operators<'a>( let res = match op { BrTable { targets } => { let def = targets.default(); - let targets = targets - .targets() - .collect::, wasmparser::BinaryReaderError>>()?; + let targets = targets.targets().collect::, wasmparser::BinaryReaderError>>()?; instructions.push(Instruction::BrTable(def, targets.len())); instructions.extend(targets.into_iter().map(Instruction::BrLabel)); continue; @@ -406,11 +330,7 @@ pub fn process_operators<'a>( BrIf { relative_depth } => Instruction::BrIf(relative_depth), Return => Instruction::Return, Call { function_index } => Instruction::Call(function_index), - CallIndirect { - type_index, - table_index, - .. - } => Instruction::CallIndirect(type_index, table_index), + CallIndirect { type_index, table_index, .. } => Instruction::CallIndirect(type_index, table_index), Drop => Instruction::Drop, Select => Instruction::Select(None), TypedSelect { ty } => Instruction::Select(Some(convert_valtype(&ty))), @@ -590,19 +510,13 @@ pub fn process_operators<'a>( TableGet { table } => Instruction::TableGet(table), TableSet { table } => Instruction::TableSet(table), TableInit { table, elem_index } => Instruction::TableInit(table, elem_index), - TableCopy { src_table, dst_table } => Instruction::TableCopy { - from: src_table, - to: dst_table, - }, + TableCopy { src_table, dst_table } => Instruction::TableCopy { from: src_table, to: dst_table }, TableGrow { table } => Instruction::TableGrow(table), TableSize { table } => Instruction::TableSize(table), TableFill { table } => Instruction::TableFill(table), op => { log::error!("Unsupported instruction: {:?}", op); - return Err(crate::ParseError::UnsupportedOperator(format!( - "Unsupported instruction: {:?}", - op - ))); + return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported instruction: {:?}", op))); } }; @@ -610,10 +524,7 @@ pub fn process_operators<'a>( } if !labels_ptrs.is_empty() { - panic!( - "last_label_pointer should be None after processing all instructions: {:?}", - labels_ptrs - ); + panic!("last_label_pointer should be None after processing all instructions: {:?}", labels_ptrs); } validator.finish(offset)?; diff --git a/crates/tinywasm/src/runtime/executor/mod.rs b/crates/tinywasm/src/runtime/executor/mod.rs index 1072577..5d6892a 100644 --- a/crates/tinywasm/src/runtime/executor/mod.rs +++ b/crates/tinywasm/src/runtime/executor/mod.rs @@ -111,7 +111,7 @@ fn exec_one( Nop => { /* do nothing */ } Unreachable => return Ok(ExecResult::Trap(crate::Trap::Unreachable)), // we don't need to include the call frame here because it's already on the stack Drop => stack.values.pop().map(|_| ())?, - Select(_) => { + Select(t) => { // due to validation, we know that the type of the values on the stack let cond: i32 = stack.values.pop()?.into(); let val2 = stack.values.pop()?; @@ -143,8 +143,7 @@ fn exec_one( debug!("params: {:?}", func_ty.params); debug!("stack: {:?}", stack.values); - let params = stack.values.pop_n(func_ty.params.len())?; - + let params = stack.values.pop_n_rev(func_ty.params.len())?; let call_frame = CallFrame::new_raw(func_idx as usize, ¶ms, func.locals.to_vec()); // push the call frame @@ -163,10 +162,11 @@ fn exec_one( let call_ty = module.func_ty(*type_addr); let func_idx = stack.values.pop_t::()?; - let func_addr = table.borrow().get(func_idx as usize)?; + let actual_func_addr = table.borrow().get(func_idx as usize)?; + let resolved_func_addr = module.resolve_func_addr(actual_func_addr); // prepare the call frame - let func_inst = store.get_func(func_addr as usize)?; + let func_inst = store.get_func(resolved_func_addr as usize)?; let func = match &func_inst.func { crate::Function::Wasm(ref f) => f, crate::Function::Host(host_func) => { @@ -185,9 +185,8 @@ fn exec_one( ); } - let params = stack.values.pop_n(func_ty.params.len())?; - - let call_frame = CallFrame::new_raw(func_addr as usize, ¶ms, func.locals.to_vec()); + let params = stack.values.pop_n_rev(func_ty.params.len())?; + let call_frame = CallFrame::new_raw(resolved_func_addr as usize, ¶ms, func.locals.to_vec()); // push the call frame cf.instr_ptr += 1; // skip the call instruction @@ -218,7 +217,7 @@ fn exec_one( cf.instr_ptr += *end_offset } } else { - log::info!("entering then"); + log::trace!("entering then"); cf.enter_label( LabelFrame { instr_ptr: cf.instr_ptr, diff --git a/crates/tinywasm/src/runtime/stack/call_stack.rs b/crates/tinywasm/src/runtime/stack/call_stack.rs index 28ea621..dc9c64d 100644 --- a/crates/tinywasm/src/runtime/stack/call_stack.rs +++ b/crates/tinywasm/src/runtime/stack/call_stack.rs @@ -16,10 +16,7 @@ pub(crate) struct CallStack { impl Default for CallStack { fn default() -> Self { - Self { - stack: Vec::with_capacity(CALL_STACK_SIZE), - top: 0, - } + Self { stack: Vec::with_capacity(CALL_STACK_SIZE), top: 0 } } } @@ -42,7 +39,7 @@ impl CallStack { pub(crate) fn push(&mut self, call_frame: CallFrame) -> Result<()> { assert!(self.top <= self.stack.len(), "stack is too small"); - log::info!("stack size: {}", self.stack.len()); + log::debug!("stack size: {}", self.stack.len()); if self.stack.len() >= CALL_STACK_MAX_SIZE { return Err(Trap::CallStackOverflow.into()); } @@ -96,8 +93,7 @@ impl CallFrame { self.instr_ptr = break_to.end_instr_ptr; // we also want to trim the label stack, including the block - self.labels - .truncate(self.labels.len() - (break_to_relative as usize + 1)); + self.labels.truncate(self.labels.len() - (break_to_relative as usize + 1)); } } @@ -119,11 +115,7 @@ impl CallFrame { } pub(crate) fn new(func_ptr: usize, params: &[WasmValue], local_types: Vec) -> Self { - CallFrame::new_raw( - func_ptr, - ¶ms.iter().map(|v| RawWasmValue::from(*v)).collect::>(), - local_types, - ) + CallFrame::new_raw(func_ptr, ¶ms.iter().map(|v| RawWasmValue::from(*v)).collect::>(), local_types) } #[inline] diff --git a/crates/tinywasm/src/runtime/stack/value_stack.rs b/crates/tinywasm/src/runtime/stack/value_stack.rs index 1021289..18fbe07 100644 --- a/crates/tinywasm/src/runtime/stack/value_stack.rs +++ b/crates/tinywasm/src/runtime/stack/value_stack.rs @@ -115,6 +115,16 @@ impl ValueStack { Ok(res) } + #[inline] + pub(crate) fn pop_n_rev(&mut self, n: usize) -> Result> { + if self.top < n { + return Err(Error::StackUnderflow); + } + self.top -= n; + let res = self.stack.drain(self.top..).collect::>(); + Ok(res) + } + #[inline] pub(crate) fn pop_n_const(&mut self) -> Result<[RawWasmValue; N]> { if self.top < N { diff --git a/crates/tinywasm/src/runtime/value.rs b/crates/tinywasm/src/runtime/value.rs index 9b0b074..c01617e 100644 --- a/crates/tinywasm/src/runtime/value.rs +++ b/crates/tinywasm/src/runtime/value.rs @@ -27,9 +27,14 @@ impl RawWasmValue { ValType::I64 => WasmValue::I64(self.0 as i64), ValType::F32 => WasmValue::F32(f32::from_bits(self.0 as u32)), ValType::F64 => WasmValue::F64(f64::from_bits(self.0)), - ValType::ExternRef => WasmValue::RefExtern(self.0 as u32), - ValType::FuncRef => WasmValue::RefFunc(self.0 as u32), - ValType::V128 => todo!("v128"), + ValType::ExternRef => WasmValue::RefExtern(match self.0 { + 0 => None, + _ => Some(self.0 as u32), + }), + ValType::FuncRef => WasmValue::RefFunc(match self.0 { + 0 => None, + _ => Some(self.0 as u32), + }), } } } @@ -41,9 +46,8 @@ impl From for RawWasmValue { WasmValue::I64(i) => Self(i as u64), WasmValue::F32(i) => Self(i.to_bits() as u64), WasmValue::F64(i) => Self(i.to_bits()), - WasmValue::RefNull(v) => v.default_value().into(), - WasmValue::RefExtern(v) => Self(v as u64), - WasmValue::RefFunc(v) => Self(v as u64), + WasmValue::RefExtern(v) => Self(v.unwrap_or(0) as u64), + WasmValue::RefFunc(v) => Self(v.unwrap_or(0) as u64), } } } diff --git a/crates/tinywasm/tests/generated/mvp.csv b/crates/tinywasm/tests/generated/mvp.csv index 824bf55..d5389e1 100644 --- a/crates/tinywasm/tests/generated/mvp.csv +++ b/crates/tinywasm/tests/generated/mvp.csv @@ -2,4 +2,4 @@ 0.0.5,11135,9093,[{"name":"address.wast","passed":1,"failed":259},{"name":"align.wast","passed":108,"failed":48},{"name":"binary-leb128.wast","passed":78,"failed":13},{"name":"binary.wast","passed":107,"failed":5},{"name":"block.wast","passed":170,"failed":53},{"name":"br.wast","passed":20,"failed":77},{"name":"br_if.wast","passed":29,"failed":89},{"name":"br_table.wast","passed":24,"failed":150},{"name":"call.wast","passed":18,"failed":73},{"name":"call_indirect.wast","passed":34,"failed":136},{"name":"comments.wast","passed":5,"failed":3},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":25,"failed":594},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":22,"failed":39},{"name":"elem.wast","passed":27,"failed":72},{"name":"endianness.wast","passed":1,"failed":68},{"name":"exports.wast","passed":90,"failed":6},{"name":"f32.wast","passed":1018,"failed":1496},{"name":"f32_bitwise.wast","passed":4,"failed":360},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":1018,"failed":1496},{"name":"f64_bitwise.wast","passed":4,"failed":360},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":1,"failed":7},{"name":"float_exprs.wast","passed":275,"failed":625},{"name":"float_literals.wast","passed":112,"failed":51},{"name":"float_memory.wast","passed":0,"failed":90},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":81,"failed":91},{"name":"func_ptrs.wast","passed":7,"failed":29},{"name":"global.wast","passed":50,"failed":60},{"name":"i32.wast","passed":85,"failed":375},{"name":"i64.wast","passed":31,"failed":385},{"name":"if.wast","passed":116,"failed":125},{"name":"imports.wast","passed":23,"failed":160},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":25,"failed":26},{"name":"labels.wast","passed":13,"failed":16},{"name":"left-to-right.wast","passed":0,"failed":96},{"name":"linking.wast","passed":5,"failed":127},{"name":"load.wast","passed":59,"failed":38},{"name":"local_get.wast","passed":18,"failed":18},{"name":"local_set.wast","passed":38,"failed":15},{"name":"local_tee.wast","passed":41,"failed":56},{"name":"loop.wast","passed":42,"failed":78},{"name":"memory.wast","passed":30,"failed":49},{"name":"memory_grow.wast","passed":11,"failed":85},{"name":"memory_redundancy.wast","passed":1,"failed":7},{"name":"memory_size.wast","passed":6,"failed":36},{"name":"memory_trap.wast","passed":1,"failed":181},{"name":"names.wast","passed":484,"failed":2},{"name":"nop.wast","passed":4,"failed":84},{"name":"return.wast","passed":20,"failed":64},{"name":"select.wast","passed":28,"failed":120},{"name":"skip-stack-guard-page.wast","passed":1,"failed":10},{"name":"stack.wast","passed":2,"failed":5},{"name":"start.wast","passed":4,"failed":16},{"name":"store.wast","passed":59,"failed":9},{"name":"switch.wast","passed":2,"failed":26},{"name":"token.wast","passed":39,"failed":19},{"name":"traps.wast","passed":4,"failed":32},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":0,"failed":64},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unwind.wast","passed":9,"failed":41},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] 0.1.0,17630,2598,[{"name":"address.wast","passed":5,"failed":255},{"name":"align.wast","passed":108,"failed":48},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":110,"failed":2},{"name":"block.wast","passed":193,"failed":30},{"name":"br.wast","passed":84,"failed":13},{"name":"br_if.wast","passed":90,"failed":28},{"name":"br_table.wast","passed":25,"failed":149},{"name":"call.wast","passed":29,"failed":62},{"name":"call_indirect.wast","passed":36,"failed":134},{"name":"comments.wast","passed":7,"failed":1},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":371,"failed":248},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":47,"failed":14},{"name":"elem.wast","passed":50,"failed":49},{"name":"endianness.wast","passed":1,"failed":68},{"name":"exports.wast","passed":92,"failed":4},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":2,"failed":6},{"name":"float_exprs.wast","passed":761,"failed":139},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":6,"failed":84},{"name":"float_misc.wast","passed":437,"failed":4},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":124,"failed":48},{"name":"func_ptrs.wast","passed":10,"failed":26},{"name":"global.wast","passed":51,"failed":59},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":120,"failed":121},{"name":"imports.wast","passed":74,"failed":109},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":14,"failed":15},{"name":"left-to-right.wast","passed":1,"failed":95},{"name":"linking.wast","passed":21,"failed":111},{"name":"load.wast","passed":60,"failed":37},{"name":"local_get.wast","passed":32,"failed":4},{"name":"local_set.wast","passed":50,"failed":3},{"name":"local_tee.wast","passed":68,"failed":29},{"name":"loop.wast","passed":93,"failed":27},{"name":"memory.wast","passed":34,"failed":45},{"name":"memory_grow.wast","passed":12,"failed":84},{"name":"memory_redundancy.wast","passed":1,"failed":7},{"name":"memory_size.wast","passed":6,"failed":36},{"name":"memory_trap.wast","passed":2,"failed":180},{"name":"names.wast","passed":485,"failed":1},{"name":"nop.wast","passed":46,"failed":42},{"name":"return.wast","passed":73,"failed":11},{"name":"select.wast","passed":86,"failed":62},{"name":"skip-stack-guard-page.wast","passed":1,"failed":10},{"name":"stack.wast","passed":2,"failed":5},{"name":"start.wast","passed":9,"failed":11},{"name":"store.wast","passed":59,"failed":9},{"name":"switch.wast","passed":2,"failed":26},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":22,"failed":14},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":50,"failed":14},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unwind.wast","passed":35,"failed":15},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] 0.2.0,19344,884,[{"name":"address.wast","passed":181,"failed":79},{"name":"align.wast","passed":156,"failed":0},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":112,"failed":0},{"name":"block.wast","passed":220,"failed":3},{"name":"br.wast","passed":97,"failed":0},{"name":"br_if.wast","passed":118,"failed":0},{"name":"br_table.wast","passed":171,"failed":3},{"name":"call.wast","passed":73,"failed":18},{"name":"call_indirect.wast","passed":50,"failed":120},{"name":"comments.wast","passed":7,"failed":1},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":439,"failed":180},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":47,"failed":14},{"name":"elem.wast","passed":56,"failed":43},{"name":"endianness.wast","passed":29,"failed":40},{"name":"exports.wast","passed":92,"failed":4},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":6,"failed":2},{"name":"float_exprs.wast","passed":890,"failed":10},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":78,"failed":12},{"name":"float_misc.wast","passed":437,"failed":4},{"name":"forward.wast","passed":5,"failed":0},{"name":"func.wast","passed":168,"failed":4},{"name":"func_ptrs.wast","passed":10,"failed":26},{"name":"global.wast","passed":103,"failed":7},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":231,"failed":10},{"name":"imports.wast","passed":80,"failed":103},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":26,"failed":3},{"name":"left-to-right.wast","passed":92,"failed":4},{"name":"linking.wast","passed":29,"failed":103},{"name":"load.wast","passed":93,"failed":4},{"name":"local_get.wast","passed":36,"failed":0},{"name":"local_set.wast","passed":53,"failed":0},{"name":"local_tee.wast","passed":93,"failed":4},{"name":"loop.wast","passed":116,"failed":4},{"name":"memory.wast","passed":78,"failed":1},{"name":"memory_grow.wast","passed":91,"failed":5},{"name":"memory_redundancy.wast","passed":8,"failed":0},{"name":"memory_size.wast","passed":35,"failed":7},{"name":"memory_trap.wast","passed":180,"failed":2},{"name":"names.wast","passed":485,"failed":1},{"name":"nop.wast","passed":78,"failed":10},{"name":"return.wast","passed":84,"failed":0},{"name":"select.wast","passed":114,"failed":34},{"name":"skip-stack-guard-page.wast","passed":1,"failed":10},{"name":"stack.wast","passed":7,"failed":0},{"name":"start.wast","passed":11,"failed":9},{"name":"store.wast","passed":68,"failed":0},{"name":"switch.wast","passed":28,"failed":0},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":36,"failed":0},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":64,"failed":0},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unwind.wast","passed":50,"failed":0},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] -0.3.0-alpha.0,19831,397,[{"name":"address.wast","passed":260,"failed":0},{"name":"align.wast","passed":156,"failed":0},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":112,"failed":0},{"name":"block.wast","passed":220,"failed":3},{"name":"br.wast","passed":97,"failed":0},{"name":"br_if.wast","passed":118,"failed":0},{"name":"br_table.wast","passed":174,"failed":0},{"name":"call.wast","passed":76,"failed":15},{"name":"call_indirect.wast","passed":155,"failed":15},{"name":"comments.wast","passed":7,"failed":1},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":619,"failed":0},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":61,"failed":0},{"name":"elem.wast","passed":69,"failed":30},{"name":"endianness.wast","passed":29,"failed":40},{"name":"exports.wast","passed":92,"failed":4},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":7,"failed":1},{"name":"float_exprs.wast","passed":900,"failed":0},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":90,"failed":0},{"name":"float_misc.wast","passed":437,"failed":4},{"name":"forward.wast","passed":5,"failed":0},{"name":"func.wast","passed":170,"failed":2},{"name":"func_ptrs.wast","passed":20,"failed":16},{"name":"global.wast","passed":106,"failed":4},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":232,"failed":9},{"name":"imports.wast","passed":70,"failed":113},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":26,"failed":3},{"name":"left-to-right.wast","passed":96,"failed":0},{"name":"linking.wast","passed":18,"failed":114},{"name":"load.wast","passed":97,"failed":0},{"name":"local_get.wast","passed":36,"failed":0},{"name":"local_set.wast","passed":53,"failed":0},{"name":"local_tee.wast","passed":97,"failed":0},{"name":"loop.wast","passed":116,"failed":4},{"name":"memory.wast","passed":79,"failed":0},{"name":"memory_grow.wast","passed":92,"failed":4},{"name":"memory_redundancy.wast","passed":8,"failed":0},{"name":"memory_size.wast","passed":42,"failed":0},{"name":"memory_trap.wast","passed":182,"failed":0},{"name":"names.wast","passed":486,"failed":0},{"name":"nop.wast","passed":78,"failed":10},{"name":"return.wast","passed":84,"failed":0},{"name":"select.wast","passed":143,"failed":5},{"name":"skip-stack-guard-page.wast","passed":11,"failed":0},{"name":"stack.wast","passed":7,"failed":0},{"name":"start.wast","passed":20,"failed":0},{"name":"store.wast","passed":68,"failed":0},{"name":"switch.wast","passed":28,"failed":0},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":36,"failed":0},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":64,"failed":0},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unwind.wast","passed":50,"failed":0},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] +0.3.0-alpha.0,19969,259,[{"name":"address.wast","passed":260,"failed":0},{"name":"align.wast","passed":156,"failed":0},{"name":"binary-leb128.wast","passed":91,"failed":0},{"name":"binary.wast","passed":112,"failed":0},{"name":"block.wast","passed":223,"failed":0},{"name":"br.wast","passed":97,"failed":0},{"name":"br_if.wast","passed":118,"failed":0},{"name":"br_table.wast","passed":174,"failed":0},{"name":"call.wast","passed":91,"failed":0},{"name":"call_indirect.wast","passed":169,"failed":1},{"name":"comments.wast","passed":7,"failed":1},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":619,"failed":0},{"name":"custom.wast","passed":11,"failed":0},{"name":"data.wast","passed":61,"failed":0},{"name":"elem.wast","passed":79,"failed":20},{"name":"endianness.wast","passed":69,"failed":0},{"name":"exports.wast","passed":92,"failed":4},{"name":"f32.wast","passed":2514,"failed":0},{"name":"f32_bitwise.wast","passed":364,"failed":0},{"name":"f32_cmp.wast","passed":2407,"failed":0},{"name":"f64.wast","passed":2514,"failed":0},{"name":"f64_bitwise.wast","passed":364,"failed":0},{"name":"f64_cmp.wast","passed":2407,"failed":0},{"name":"fac.wast","passed":7,"failed":1},{"name":"float_exprs.wast","passed":900,"failed":0},{"name":"float_literals.wast","passed":163,"failed":0},{"name":"float_memory.wast","passed":90,"failed":0},{"name":"float_misc.wast","passed":437,"failed":4},{"name":"forward.wast","passed":5,"failed":0},{"name":"func.wast","passed":172,"failed":0},{"name":"func_ptrs.wast","passed":36,"failed":0},{"name":"global.wast","passed":110,"failed":0},{"name":"i32.wast","passed":460,"failed":0},{"name":"i64.wast","passed":416,"failed":0},{"name":"if.wast","passed":241,"failed":0},{"name":"imports.wast","passed":74,"failed":109},{"name":"inline-module.wast","passed":1,"failed":0},{"name":"int_exprs.wast","passed":108,"failed":0},{"name":"int_literals.wast","passed":51,"failed":0},{"name":"labels.wast","passed":26,"failed":3},{"name":"left-to-right.wast","passed":96,"failed":0},{"name":"linking.wast","passed":18,"failed":114},{"name":"load.wast","passed":97,"failed":0},{"name":"local_get.wast","passed":36,"failed":0},{"name":"local_set.wast","passed":53,"failed":0},{"name":"local_tee.wast","passed":97,"failed":0},{"name":"loop.wast","passed":119,"failed":1},{"name":"memory.wast","passed":79,"failed":0},{"name":"memory_grow.wast","passed":95,"failed":1},{"name":"memory_redundancy.wast","passed":8,"failed":0},{"name":"memory_size.wast","passed":42,"failed":0},{"name":"memory_trap.wast","passed":182,"failed":0},{"name":"names.wast","passed":486,"failed":0},{"name":"nop.wast","passed":88,"failed":0},{"name":"return.wast","passed":84,"failed":0},{"name":"select.wast","passed":148,"failed":0},{"name":"skip-stack-guard-page.wast","passed":11,"failed":0},{"name":"stack.wast","passed":7,"failed":0},{"name":"start.wast","passed":20,"failed":0},{"name":"store.wast","passed":68,"failed":0},{"name":"switch.wast","passed":28,"failed":0},{"name":"token.wast","passed":58,"failed":0},{"name":"traps.wast","passed":36,"failed":0},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":64,"failed":0},{"name":"unreached-invalid.wast","passed":118,"failed":0},{"name":"unwind.wast","passed":50,"failed":0},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":176,"failed":0}] diff --git a/crates/tinywasm/tests/generated/progress-mvp.svg b/crates/tinywasm/tests/generated/progress-mvp.svg index 1f8316a..a631dce 100644 --- a/crates/tinywasm/tests/generated/progress-mvp.svg +++ b/crates/tinywasm/tests/generated/progress-mvp.svg @@ -53,12 +53,12 @@ v0.2.0 (19344) -v0.3.0-alpha.0 (19831) +v0.3.0-alpha.0 (19969) - - - + + + diff --git a/crates/tinywasm/tests/testsuite/util.rs b/crates/tinywasm/tests/testsuite/util.rs index 17bca76..bb134d2 100644 --- a/crates/tinywasm/tests/testsuite/util.rs +++ b/crates/tinywasm/tests/testsuite/util.rs @@ -4,19 +4,11 @@ use eyre::{eyre, Result}; use tinywasm_types::{TinyWasmModule, WasmValue}; pub fn try_downcast_panic(panic: Box) -> String { - let info = panic - .downcast_ref::() - .or(None) - .map(|p| p.to_string()) - .clone(); + let info = panic.downcast_ref::().or(None).map(|p| p.to_string()).clone(); let info_string = panic.downcast_ref::().cloned(); let info_str = panic.downcast::<&str>().ok().map(|s| *s); - info.unwrap_or( - info_str - .unwrap_or(&info_string.unwrap_or("unknown panic".to_owned())) - .to_string(), - ) + info.unwrap_or(info_str.unwrap_or(&info_string.unwrap_or("unknown panic".to_owned())).to_string()) } pub fn exec_fn_instance( @@ -73,12 +65,12 @@ pub fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result WasmValue::F64(f64::from_bits(f.bits)), I32(i) => WasmValue::I32(i), I64(i) => WasmValue::I64(i), - RefExtern(v) => WasmValue::RefExtern(v), - RefNull(t) => WasmValue::RefNull(match t { - wast::core::HeapType::Func => tinywasm_types::ValType::FuncRef, - wast::core::HeapType::Extern => tinywasm_types::ValType::ExternRef, + RefExtern(v) => WasmValue::RefExtern(Some(v)), + RefNull(t) => match t { + wast::core::HeapType::Func => WasmValue::RefFunc(None), + wast::core::HeapType::Extern => WasmValue::RefExtern(None), _ => return Err(eyre!("unsupported arg type: refnull: {:?}", t)), - }), + }, v => return Err(eyre!("unsupported arg type: {:?}", v)), }) } @@ -94,14 +86,18 @@ pub fn wastret2tinywasmvalue(arg: wast::WastRet) -> Result nanpattern2tinywasmvalue(f)?, I32(i) => WasmValue::I32(i), I64(i) => WasmValue::I64(i), - RefNull(t) => WasmValue::RefNull(match t { - Some(wast::core::HeapType::Func) => tinywasm_types::ValType::FuncRef, - Some(wast::core::HeapType::Extern) => tinywasm_types::ValType::ExternRef, + RefNull(t) => match t { + Some(wast::core::HeapType::Func) => WasmValue::RefFunc(None), + Some(wast::core::HeapType::Extern) => WasmValue::RefExtern(None), _ => return Err(eyre!("unsupported arg type: refnull: {:?}", t)), - }), + }, RefExtern(v) => match v { - Some(v) => WasmValue::RefExtern(v), - None => WasmValue::RefNull(tinywasm_types::ValType::ExternRef), + Some(v) => WasmValue::RefExtern(Some(v)), + _ => return Err(eyre!("unsupported arg type: refextern: {:?}", v)), + }, + RefFunc(v) => match v { + Some(wast::token::Index::Num(n, _)) => WasmValue::RefFunc(Some(n)), + _ => return Err(eyre!("unsupported arg type: reffunc: {:?}", v)), }, a => return Err(eyre!("unsupported arg type {:?}", a)), }) diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 965c97e..29aa5d7 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -86,9 +86,8 @@ pub enum WasmValue { // Vec types // V128(i128), // RefHost(FuncAddr), - RefExtern(ExternAddr), - RefNull(ValType), - RefFunc(FuncAddr), + RefExtern(Option), + RefFunc(Option), } impl WasmValue { @@ -98,7 +97,8 @@ impl WasmValue { Self::I64(i) => ConstInstruction::I64Const(*i), Self::F32(i) => ConstInstruction::F32Const(*i), Self::F64(i) => ConstInstruction::F64Const(*i), - Self::RefNull(ty) => ConstInstruction::RefNull(*ty), + Self::RefExtern(None) => ConstInstruction::RefNull(ValType::ExternRef), + Self::RefFunc(None) => ConstInstruction::RefNull(ValType::FuncRef), // Self::RefExtern(addr) => ConstInstruction::RefExtern(*addr), _ => unimplemented!("no const_instr for {:?}", self), } @@ -111,9 +111,8 @@ impl WasmValue { ValType::I64 => Self::I64(0), ValType::F32 => Self::F32(0.0), ValType::F64 => Self::F64(0.0), - ValType::V128 => unimplemented!("V128 is not yet supported"), - ValType::FuncRef => Self::RefFunc(0), - ValType::ExternRef => Self::RefExtern(0), + ValType::FuncRef => Self::RefFunc(None), + ValType::ExternRef => Self::RefExtern(None), } } @@ -121,8 +120,8 @@ impl WasmValue { match (self, other) { (Self::I32(a), Self::I32(b)) => a == b, (Self::I64(a), Self::I64(b)) => a == b, - (Self::RefNull(ty), Self::RefNull(ty2)) => ty == ty2, (Self::RefExtern(addr), Self::RefExtern(addr2)) => addr == addr2, + (Self::RefFunc(addr), Self::RefFunc(addr2)) => addr == addr2, (Self::F32(a), Self::F32(b)) => { if a.is_nan() && b.is_nan() { true // Both are NaN, treat them as equal @@ -217,9 +216,8 @@ impl Debug for WasmValue { WasmValue::I64(i) => write!(f, "i64({})", i), WasmValue::F32(i) => write!(f, "f32({})", i), WasmValue::F64(i) => write!(f, "f64({})", i), - WasmValue::RefNull(ty) => write!(f, "ref.null({:?})", ty), - WasmValue::RefExtern(addr) => write!(f, "ref.extern({})", addr), - WasmValue::RefFunc(addr) => write!(f, "ref.func({})", addr), + WasmValue::RefExtern(addr) => write!(f, "ref.extern({:?})", addr), + WasmValue::RefFunc(addr) => write!(f, "ref.func({:?})", addr), // WasmValue::V128(i) => write!(f, "v128({})", i), } } @@ -233,10 +231,8 @@ impl WasmValue { Self::I64(_) => ValType::I64, Self::F32(_) => ValType::F32, Self::F64(_) => ValType::F64, - Self::RefNull(ty) => *ty, Self::RefExtern(_) => ValType::ExternRef, Self::RefFunc(_) => ValType::FuncRef, - // Self::V128(_) => ValType::V128, } } } @@ -252,8 +248,6 @@ pub enum ValType { F32, /// A 64-bit float. F64, - /// A 128-bit vector. - V128, /// A reference to a function. FuncRef, /// A reference to an external value.