diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/parser/src/conversion.rs | 49 | ||||
| -rw-r--r-- | crates/parser/src/lib.rs | 3 | ||||
| -rw-r--r-- | crates/parser/src/module.rs | 18 | ||||
| -rw-r--r-- | crates/tinywasm/tests/mvp.csv | 2 | ||||
| -rw-r--r-- | crates/tinywasm/tests/mvp.rs | 1 | ||||
| -rw-r--r-- | crates/tinywasm/tests/progress-mvp.svg | 4 | ||||
| -rw-r--r-- | crates/tinywasm/tests/testsuite/run.rs | 22 | ||||
| -rw-r--r-- | crates/tinywasm/tests/testsuite/util.rs | 5 | ||||
| -rw-r--r-- | crates/types/src/instructions.rs | 9 | ||||
| -rw-r--r-- | crates/types/src/lib.rs | 9 |
10 files changed, 101 insertions, 21 deletions
diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 269bb66..9f4930a 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -1,10 +1,43 @@ use alloc::{boxed::Box, format, string::ToString, vec::Vec}; use log::info; -use tinywasm_types::{BlockArgs, Export, ExternalKind, FuncType, Instruction, MemArg, ValType}; +use tinywasm_types::{ + BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, ValType, +}; use wasmparser::{FuncValidator, ValidatorResources}; use crate::{module::CodeSection, Result}; +pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Global<'a>>>>( + globals: T, +) -> Result<Vec<Global>> { + let globals = globals + .into_iter() + .map(|global| { + let global = global?; + let ty = convert_valtype(&global.ty.content_type); + + let ops = global + .init_expr + .get_operators_reader() + .into_iter() + .collect::<wasmparser::Result<Vec<_>>>()?; + + // In practice, the len can never be something other than 2, + // but we'll keep this here since it's part of the spec + // Invalid modules will be rejected by the validator anyway (there are also tests for this in the testsuite) + assert!(ops.len() >= 2); + assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End)); + + Ok(Global { + ty, + init: process_const_operator(ops[ops.len() - 2].clone())?, + mutable: global.ty.mutable, + }) + }) + .collect::<Result<Vec<_>>>()?; + Ok(globals) +} + pub(crate) fn convert_module_export(export: wasmparser::Export) -> Result<Export> { let kind = match export.kind { wasmparser::ExternalKind::Func => ExternalKind::Func, @@ -105,6 +138,20 @@ pub(crate) fn convert_memarg(memarg: wasmparser::MemArg) -> MemArg { } } +pub fn process_const_operator(op: wasmparser::Operator) -> Result<ConstInstruction> { + match op { + wasmparser::Operator::I32Const { value } => Ok(ConstInstruction::I32Const(value)), + wasmparser::Operator::I64Const { value } => Ok(ConstInstruction::I64Const(value)), + wasmparser::Operator::F32Const { value } => 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 instruction: {:?}", + op + ))), + } +} + pub fn process_operators<'a>( mut offset: usize, ops: impl Iterator<Item = Result<wasmparser::Operator<'a>, wasmparser::BinaryReaderError>>, diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 0719c96..bcaef76 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -114,12 +114,15 @@ impl TryFrom<ModuleReader> for TinyWasmModule { }) .collect::<Vec<_>>(); + let globals = reader.global_section; + Ok(TinyWasmModule { version: reader.version, start_func: reader.start_func, types: reader.type_section.into_boxed_slice(), funcs: funcs.into_boxed_slice(), exports: reader.export_section.into_boxed_slice(), + globals: globals.into_boxed_slice(), }) } } diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index 69bb894..6f823de 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -1,7 +1,7 @@ use crate::log::debug; use alloc::{boxed::Box, format, vec::Vec}; use core::fmt::Debug; -use tinywasm_types::{Export, FuncType, Instruction, ValType}; +use tinywasm_types::{Export, FuncType, Global, Instruction, ValType}; use wasmparser::{Payload, Validator}; use crate::{conversion, ParseError, Result}; @@ -21,10 +21,10 @@ pub struct ModuleReader { pub function_section: Vec<u32>, pub export_section: Vec<Export>, pub code_section: Vec<CodeSection>, + pub global_section: Vec<Global>, // pub table_section: Option<TableSectionReader<'a>>, // pub memory_section: Option<MemorySectionReader<'a>>, - // pub global_section: Option<GlobalSectionReader<'a>>, // pub element_section: Option<ElementSectionReader<'a>>, // pub data_section: Option<DataSectionReader<'a>>, // pub import_section: Option<ImportSectionReader<'a>>, @@ -39,9 +39,9 @@ impl Debug for ModuleReader { .field("function_section", &self.function_section) .field("code_section", &self.code_section) .field("export_section", &self.export_section) + .field("global_section", &self.global_section) // .field("table_section", &self.table_section) // .field("memory_section", &self.memory_section) - // .field("global_section", &self.global_section) // .field("element_section", &self.element_section) // .field("data_section", &self.data_section) // .field("import_section", &self.import_section) @@ -84,6 +84,11 @@ impl ModuleReader { validator.function_section(&reader)?; self.function_section = reader.into_iter().map(|f| Ok(f?)).collect::<Result<Vec<_>>>()?; } + GlobalSection(reader) => { + debug!("Found global section"); + validator.global_section(&reader)?; + self.global_section = conversion::convert_module_globals(reader)?; + } TableSection(_reader) => { return Err(ParseError::UnsupportedSection("Table section".into())); // debug!("Found table section"); @@ -96,12 +101,7 @@ impl ModuleReader { // validator.memory_section(&reader)?; // self.memory_section = Some(reader); } - GlobalSection(_reader) => { - return Err(ParseError::UnsupportedSection("Global section".into())); - // debug!("Found global section"); - // validator.global_section(&reader)?; - // self.global_section = Some(reader); - } + ElementSection(_reader) => { return Err(ParseError::UnsupportedSection("Element section".into())); // debug!("Found element section"); diff --git a/crates/tinywasm/tests/mvp.csv b/crates/tinywasm/tests/mvp.csv index 96860cf..80ffc8e 100644 --- a/crates/tinywasm/tests/mvp.csv +++ b/crates/tinywasm/tests/mvp.csv @@ -1,3 +1,3 @@ 0.0.3,9258,7567,[{"name":"address.wast","passed":0,"failed":54},{"name":"align.wast","passed":0,"failed":109},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":171},{"name":"br.wast","passed":0,"failed":21},{"name":"br_if.wast","passed":0,"failed":30},{"name":"br_table.wast","passed":0,"failed":25},{"name":"call.wast","passed":0,"failed":22},{"name":"call_indirect.wast","passed":0,"failed":56},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":93},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":76},{"name":"endianness.wast","passed":0,"failed":1},{"name":"exports.wast","passed":21,"failed":73},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":0,"failed":2},{"name":"float_exprs.wast","passed":269,"failed":591},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":6},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":4,"failed":75},{"name":"func_ptrs.wast","passed":0,"failed":16},{"name":"global.wast","passed":4,"failed":49},{"name":"i32.wast","passed":0,"failed":96},{"name":"i64.wast","passed":0,"failed":42},{"name":"if.wast","passed":0,"failed":118},{"name":"imports.wast","passed":1,"failed":156},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":1,"failed":28},{"name":"left-to-right.wast","passed":0,"failed":1},{"name":"linking.wast","passed":1,"failed":66},{"name":"load.wast","passed":0,"failed":60},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":42},{"name":"loop.wast","passed":0,"failed":43},{"name":"memory.wast","passed":0,"failed":34},{"name":"memory_grow.wast","passed":0,"failed":19},{"name":"memory_redundancy.wast","passed":0,"failed":1},{"name":"memory_size.wast","passed":0,"failed":6},{"name":"memory_trap.wast","passed":0,"failed":172},{"name":"names.wast","passed":484,"failed":1},{"name":"nop.wast","passed":0,"failed":5},{"name":"return.wast","passed":0,"failed":21},{"name":"select.wast","passed":0,"failed":32},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":2},{"name":"start.wast","passed":0,"failed":10},{"name":"store.wast","passed":0,"failed":59},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":59},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"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":0,"failed":176}] 0.0.4,9258,10909,[{"name":"address.wast","passed":0,"failed":54},{"name":"align.wast","passed":0,"failed":109},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":171},{"name":"br.wast","passed":0,"failed":21},{"name":"br_if.wast","passed":0,"failed":30},{"name":"br_table.wast","passed":0,"failed":25},{"name":"call.wast","passed":0,"failed":22},{"name":"call_indirect.wast","passed":0,"failed":56},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":93},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":76},{"name":"endianness.wast","passed":0,"failed":1},{"name":"exports.wast","passed":21,"failed":73},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":0,"failed":2},{"name":"float_exprs.wast","passed":269,"failed":591},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":6},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":4,"failed":75},{"name":"func_ptrs.wast","passed":0,"failed":16},{"name":"global.wast","passed":4,"failed":49},{"name":"i32.wast","passed":0,"failed":96},{"name":"i64.wast","passed":0,"failed":42},{"name":"if.wast","passed":0,"failed":118},{"name":"imports.wast","passed":1,"failed":156},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":1,"failed":28},{"name":"left-to-right.wast","passed":0,"failed":1},{"name":"linking.wast","passed":1,"failed":66},{"name":"load.wast","passed":0,"failed":60},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":42},{"name":"loop.wast","passed":0,"failed":43},{"name":"memory.wast","passed":0,"failed":34},{"name":"memory_grow.wast","passed":0,"failed":19},{"name":"memory_redundancy.wast","passed":0,"failed":1},{"name":"memory_size.wast","passed":0,"failed":6},{"name":"memory_trap.wast","passed":0,"failed":172},{"name":"names.wast","passed":484,"failed":1},{"name":"nop.wast","passed":0,"failed":5},{"name":"return.wast","passed":0,"failed":21},{"name":"select.wast","passed":0,"failed":32},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":2},{"name":"start.wast","passed":0,"failed":10},{"name":"store.wast","passed":0,"failed":59},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":59},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"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":0,"failed":176}] -0.0.5-alpha.0,9277,10909,[{"name":"address.wast","passed":0,"failed":260},{"name":"align.wast","passed":0,"failed":156},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":223},{"name":"br.wast","passed":0,"failed":97},{"name":"br_if.wast","passed":0,"failed":118},{"name":"br_table.wast","passed":0,"failed":174},{"name":"call.wast","passed":0,"failed":91},{"name":"call_indirect.wast","passed":0,"failed":170},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":619},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":99},{"name":"endianness.wast","passed":0,"failed":69},{"name":"exports.wast","passed":21,"failed":75},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":1,"failed":7},{"name":"float_exprs.wast","passed":273,"failed":617},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":66},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":9,"failed":163},{"name":"func_ptrs.wast","passed":0,"failed":35},{"name":"global.wast","passed":4,"failed":106},{"name":"i32.wast","passed":0,"failed":460},{"name":"i64.wast","passed":0,"failed":416},{"name":"if.wast","passed":0,"failed":241},{"name":"imports.wast","passed":1,"failed":182},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":10,"failed":19},{"name":"left-to-right.wast","passed":0,"failed":96},{"name":"linking.wast","passed":1,"failed":131},{"name":"load.wast","passed":0,"failed":97},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":97},{"name":"loop.wast","passed":0,"failed":120},{"name":"memory.wast","passed":0,"failed":79},{"name":"memory_grow.wast","passed":0,"failed":96},{"name":"memory_redundancy.wast","passed":0,"failed":5},{"name":"memory_size.wast","passed":0,"failed":42},{"name":"memory_trap.wast","passed":0,"failed":182},{"name":"names.wast","passed":484,"failed":2},{"name":"nop.wast","passed":0,"failed":88},{"name":"return.wast","passed":0,"failed":84},{"name":"select.wast","passed":0,"failed":148},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":7},{"name":"start.wast","passed":0,"failed":16},{"name":"store.wast","passed":0,"failed":68},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":64},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"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":0,"failed":176}] +0.0.5-alpha.0,9869,10317,[{"name":"address.wast","passed":1,"failed":259},{"name":"align.wast","passed":46,"failed":110},{"name":"binary-leb128.wast","passed":74,"failed":17},{"name":"binary.wast","passed":105,"failed":7},{"name":"block.wast","passed":15,"failed":208},{"name":"br.wast","passed":0,"failed":97},{"name":"br_if.wast","passed":0,"failed":118},{"name":"br_table.wast","passed":0,"failed":174},{"name":"call.wast","passed":0,"failed":91},{"name":"call_indirect.wast","passed":11,"failed":159},{"name":"comments.wast","passed":5,"failed":3},{"name":"const.wast","passed":778,"failed":0},{"name":"conversions.wast","passed":0,"failed":619},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":99},{"name":"endianness.wast","passed":0,"failed":69},{"name":"exports.wast","passed":33,"failed":63},{"name":"f32.wast","passed":1007,"failed":1507},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1007,"failed":1507},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":1,"failed":7},{"name":"float_exprs.wast","passed":273,"failed":617},{"name":"float_literals.wast","passed":112,"failed":51},{"name":"float_memory.wast","passed":0,"failed":66},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":32,"failed":140},{"name":"func_ptrs.wast","passed":0,"failed":35},{"name":"global.wast","passed":10,"failed":100},{"name":"i32.wast","passed":2,"failed":458},{"name":"i64.wast","passed":2,"failed":414},{"name":"if.wast","passed":24,"failed":217},{"name":"imports.wast","passed":17,"failed":166},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":25,"failed":26},{"name":"labels.wast","passed":10,"failed":19},{"name":"left-to-right.wast","passed":0,"failed":96},{"name":"linking.wast","passed":3,"failed":129},{"name":"load.wast","passed":13,"failed":84},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":97},{"name":"loop.wast","passed":15,"failed":105},{"name":"memory.wast","passed":6,"failed":73},{"name":"memory_grow.wast","passed":0,"failed":96},{"name":"memory_redundancy.wast","passed":0,"failed":5},{"name":"memory_size.wast","passed":0,"failed":42},{"name":"memory_trap.wast","passed":0,"failed":182},{"name":"names.wast","passed":484,"failed":2},{"name":"nop.wast","passed":0,"failed":88},{"name":"return.wast","passed":0,"failed":84},{"name":"select.wast","passed":0,"failed":148},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":1,"failed":6},{"name":"start.wast","passed":1,"failed":15},{"name":"store.wast","passed":7,"failed":61},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":39,"failed":19},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":3,"failed":0},{"name":"unreachable.wast","passed":0,"failed":64},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"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/mvp.rs b/crates/tinywasm/tests/mvp.rs index 6e6a104..e02436f 100644 --- a/crates/tinywasm/tests/mvp.rs +++ b/crates/tinywasm/tests/mvp.rs @@ -26,6 +26,7 @@ fn generate_charts() -> Result<()> { #[ignore] fn test_mvp() -> Result<()> { let mut test_suite = TestSuite::new(); + test_suite.run(wasm_testsuite::MVP_TESTS)?; test_suite.save_csv("./tests/mvp.csv", env!("CARGO_PKG_VERSION"))?; diff --git a/crates/tinywasm/tests/progress-mvp.svg b/crates/tinywasm/tests/progress-mvp.svg index 63f33ec..e23bdb4 100644 --- a/crates/tinywasm/tests/progress-mvp.svg +++ b/crates/tinywasm/tests/progress-mvp.svg @@ -45,10 +45,10 @@ v0.0.4 (9258) </text> <polyline fill="none" opacity="1" stroke="#000000" stroke-width="1" points="534,345 534,350 "/> <text x="837" y="355" dy="0.76em" text-anchor="middle" font-family="Victor Mono" font-size="12.096774193548388" opacity="1" fill="#000000"> -v0.0.5-alpha.0 (9277) +v0.0.5-alpha.0 (9869) </text> <polyline fill="none" opacity="1" stroke="#000000" stroke-width="1" points="837,345 837,350 "/> <rect x="85" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/> <rect x="388" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/> -<rect x="691" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/> +<rect x="691" y="203" width="293" height="141" opacity="0.5" fill="#0000FF" stroke="none"/> </svg> diff --git a/crates/tinywasm/tests/testsuite/run.rs b/crates/tinywasm/tests/testsuite/run.rs index ba39f43..2c3008d 100644 --- a/crates/tinywasm/tests/testsuite/run.rs +++ b/crates/tinywasm/tests/testsuite/run.rs @@ -29,8 +29,8 @@ impl TestSuite { match directive { // TODO: needs to support more binary sections - Wat(QuoteWat::Wat(wast::Wat::Module(module))) => { - let result = catch_unwind_silent(|| parse_module(module)) + Wat(mut module) => { + let result = catch_unwind_silent(move || parse_module_bytes(&module.encode().unwrap())) .map_err(|e| eyre!("failed to parse module: {:?}", e)) .and_then(|res| res); @@ -44,19 +44,27 @@ impl TestSuite { test_group.add_result(&format!("{}-parse", name), span, result.map(|_| ())); } - // these all pass already :) AssertMalformed { span, - module: QuoteWat::Wat(wast::Wat::Module(module)), + mut module, message: _, } => { - let res = catch_unwind_silent(|| parse_module(module).map(|_| ())); + let Ok(module) = module.encode() else { + println!("malformed module: {:?}", module); + test_group.add_result(&format!("{}-malformed", name), span, Ok(())); + continue; + }; + + let res = catch_unwind_silent(|| parse_module_bytes(&module)) + .map_err(|e| eyre!("failed to parse module: {:?}", e)) + .and_then(|res| res); + test_group.add_result( &format!("{}-malformed", name), span, match res { - Ok(Ok(_)) => Err(eyre!("expected module to be malformed")), - Err(_) | Ok(Err(_)) => Ok(()), + Ok(_) => Err(eyre!("expected module to be malformed")), + Err(_) => Ok(()), }, ); } diff --git a/crates/tinywasm/tests/testsuite/util.rs b/crates/tinywasm/tests/testsuite/util.rs index f153f8a..882537a 100644 --- a/crates/tinywasm/tests/testsuite/util.rs +++ b/crates/tinywasm/tests/testsuite/util.rs @@ -16,6 +16,11 @@ pub fn parse_module(mut module: wast::core::Module) -> Result<TinyWasmModule> { Ok(parser.parse_module_bytes(module.encode().expect("failed to encode module"))?) } +pub fn parse_module_bytes(bytes: &[u8]) -> Result<TinyWasmModule> { + let parser = tinywasm_parser::Parser::new(); + Ok(parser.parse_module_bytes(bytes)?) +} + pub fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<tinywasm_types::WasmValue> { let wast::WastArg::Core(arg) = arg else { return Err(eyre!("unsupported arg type")); diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index d8aff7b..3f7b2ef 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -19,6 +19,15 @@ type BrTableLen = usize; type EndOffset = usize; type ElseOffset = usize; +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum ConstInstruction { + I32Const(i32), + I64Const(i64), + F32Const(f32), + F64Const(f64), + GlobalGet(GlobalAddr), +} + /// A WebAssembly Instruction /// /// These are our own internal bytecode instructions so they may not match the spec exactly. diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index b7bf623..ddc8ce4 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -52,9 +52,9 @@ pub struct TinyWasmModule { /// The exports of the WebAssembly module. pub exports: Box<[Export]>, + pub globals: Box<[Global]>, // pub tables: Option<TableType>, // pub memories: Option<MemoryType>, - // pub globals: Option<GlobalType>, // pub elements: Option<ElementSectionReader<'a>>, // pub imports: Option<ImportSectionReader<'a>>, // pub data_segments: Option<DataSectionReader<'a>>, @@ -293,3 +293,10 @@ pub struct Export { /// The index of the exported item. pub index: u32, } + +#[derive(Debug, Clone)] +pub struct Global { + pub mutable: bool, + pub ty: ValType, + pub init: ConstInstruction, +} |
