From f26d077b5a8141253f807ea14551852da6842dd1 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 28 Nov 2023 20:16:08 +0100 Subject: chore: add wasm spec testsuite Signed-off-by: Henry Gressmann --- .gitmodules | 3 + Cargo.toml | 4 +- crates/cli/Cargo.toml | 2 +- crates/core/Cargo.toml | 16 ---- crates/core/src/error.rs | 28 ------- crates/core/src/instructions.rs | 106 ------------------------ crates/core/src/lib.rs | 33 -------- crates/core/src/module.rs | 155 ----------------------------------- crates/core/src/std.rs | 18 ---- crates/tinywasm/Cargo.toml | 16 ++++ crates/tinywasm/src/error.rs | 28 +++++++ crates/tinywasm/src/instructions.rs | 106 ++++++++++++++++++++++++ crates/tinywasm/src/lib.rs | 33 ++++++++ crates/tinywasm/src/module.rs | 155 +++++++++++++++++++++++++++++++++++ crates/tinywasm/src/std.rs | 18 ++++ crates/tinywasm/tests/spec/testsuite | 1 + 16 files changed, 363 insertions(+), 359 deletions(-) create mode 100644 .gitmodules delete mode 100644 crates/core/Cargo.toml delete mode 100644 crates/core/src/error.rs delete mode 100644 crates/core/src/instructions.rs delete mode 100644 crates/core/src/lib.rs delete mode 100644 crates/core/src/module.rs delete mode 100644 crates/core/src/std.rs create mode 100644 crates/tinywasm/Cargo.toml create mode 100644 crates/tinywasm/src/error.rs create mode 100644 crates/tinywasm/src/instructions.rs create mode 100644 crates/tinywasm/src/lib.rs create mode 100644 crates/tinywasm/src/module.rs create mode 100644 crates/tinywasm/src/std.rs create mode 160000 crates/tinywasm/tests/spec/testsuite diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a7949f3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "crates/tinywasm/tests/spec/testsuite"] + path = crates/tinywasm/tests/spec/testsuite + url = https://github.com/WebAssembly/testsuite.git diff --git a/Cargo.toml b/Cargo.toml index 42abe2e..eb4aa02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ [workspace] -members=["crates/core", "crates/cli"] -resolver="2" +members=["crates/tinywasm", "crates/cli"] default-members=["crates/cli"] +resolver="2" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index cdd0266..4a9974b 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -10,6 +10,6 @@ path="bin.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tinywasm={path="../core"} +tinywasm={path="../tinywasm"} argh="0.1" color-eyre="0.6" diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml deleted file mode 100644 index 2f71aa9..0000000 --- a/crates/core/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name="tinywasm" -version="0.0.0" -edition="2021" - -[lib] -path="src/lib.rs" - -[dependencies] -thiserror={version="1.0", package="thiserror-core", default-features=false} -wasmparser={version="0.100", package="wasmparser-nostd", default-features=false} -tracing={version="0.1.38", default-features=false} # logging - -[features] -default=["std"] -std=["thiserror/std"] diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs deleted file mode 100644 index b9c93c4..0000000 --- a/crates/core/src/error.rs +++ /dev/null @@ -1,28 +0,0 @@ -use alloc::string::{String, ToString}; -use thiserror::Error; - -#[derive(Debug, Error)] -pub enum Error { - #[error("error parsing module")] - ParseError { message: String, offset: usize }, - - #[error("unknown error: {0}")] - Other(String), -} - -impl Error { - pub fn other(message: &str) -> Result { - Err(Self::Other(message.to_string())) - } -} - -impl From for Error { - fn from(value: wasmparser::BinaryReaderError) -> Self { - Self::ParseError { - message: value.message().to_string(), - offset: value.offset(), - } - } -} - -pub type Result = crate::std::result::Result; diff --git a/crates/core/src/instructions.rs b/crates/core/src/instructions.rs deleted file mode 100644 index e3e6a08..0000000 --- a/crates/core/src/instructions.rs +++ /dev/null @@ -1,106 +0,0 @@ -// https://webassembly.github.io/spec/core/binary/instructions.html - -// Controll Instructions -pub mod control { - pub const WASM_UNREACHABLE: u8 = 0x00; - pub const WASM_NOP: u8 = 0x01; // do nothing - - // stuctured instructions - pub const WASM_BLOCK: u8 = 0x02; - pub const WASM_LOOP: u8 = 0x03; - pub const WASM_IF: u8 = 0x04; - - pub const WASM_ELSE: u8 = 0x05; - pub const WASM_END: u8 = 0x0B; - pub const WASM_BR: u8 = 0x0C; - pub const WASM_BR_IF: u8 = 0x0D; - pub const WASM_BR_TABLE: u8 = 0x0E; - pub const WASM_RETURN: u8 = 0x0F; - pub const WASM_CALL: u8 = 0x10; - pub const WASM_CALL_INDIRECT: u8 = 0x11; - pub const WASM_DROP: u8 = 0x1A; -} - -// Reference Instructions -pub mod reference { - pub const WASM_REF_NULL: u8 = 0xD0; - pub const WASM_REF_IS_NULL: u8 = 0xD1; - pub const WASM_REF_FUNC: u8 = 0xD2; -} - -// Parametric Instructions -pub mod parametric { - pub const WASM_DROP: u8 = 0x1A; - pub const WASM_SELECT: u8 = 0x1B; - pub const WASM_SELECT_T: u8 = 0x1C; -} - -// Variable Instructions -pub mod variable { - pub const WASM_LOCAL_GET: u8 = 0x20; - pub const WASM_LOCAL_SET: u8 = 0x21; - pub const WASM_LOCAL_TEE: u8 = 0x22; - pub const WASM_GLOBAL_GET: u8 = 0x23; - pub const WASM_GLOBAL_SET: u8 = 0x24; -} - -// Table Instructions -pub mod table { - pub const WASM_TABLE_GET: u8 = 0x25; - pub const WASM_TABLE_SET: u8 = 0x26; - pub const WASM_TABLE_INIT: u8 = 0xFC; - pub const WASM_ELEM_DROP: u8 = 0xFC; - pub const WASM_TABLE_COPY: u8 = 0xFC; - pub const WASM_TABLE_GROW: u8 = 0xFC; - pub const WASM_TABLE_SIZE: u8 = 0xFC; - pub const WASM_TABLE_FILL: u8 = 0xFC; -} - -// Memory Instructions -pub mod memory { - pub const WASM_I32_LOAD: u8 = 0x28; - pub const WASM_I64_LOAD: u8 = 0x29; - pub const WASM_F32_LOAD: u8 = 0x2A; - pub const WASM_F64_LOAD: u8 = 0x2B; - pub const WASM_I32_LOAD8_S: u8 = 0x2C; - pub const WASM_I32_LOAD8_U: u8 = 0x2D; - pub const WASM_I32_LOAD16_S: u8 = 0x2E; - pub const WASM_I32_LOAD16_U: u8 = 0x2F; - pub const WASM_I64_LOAD8_S: u8 = 0x30; - pub const WASM_I64_LOAD8_U: u8 = 0x31; - pub const WASM_I64_LOAD16_S: u8 = 0x32; - pub const WASM_I64_LOAD16_U: u8 = 0x33; - pub const WASM_I64_LOAD32_S: u8 = 0x34; - pub const WASM_I64_LOAD32_U: u8 = 0x35; - pub const WASM_I32_STORE: u8 = 0x36; - pub const WASM_I64_STORE: u8 = 0x37; - pub const WASM_F32_STORE: u8 = 0x38; - pub const WASM_F64_STORE: u8 = 0x39; - pub const WASM_I32_STORE8: u8 = 0x3A; - pub const WASM_I32_STORE16: u8 = 0x3B; - pub const WASM_I64_STORE8: u8 = 0x3C; - pub const WASM_I64_STORE16: u8 = 0x3D; - pub const WASM_I64_STORE32: u8 = 0x3E; - pub const WASM_MEMORY_SIZE: u8 = 0x3F; - pub const WASM_MEMORY_GROW: u8 = 0x40; - pub const WASM_MEMORY_INIT: u8 = 0xFC; - pub const WASM_DATA_DROP: u8 = 0xFC; - pub const WASM_MEMORY_COPY: u8 = 0xFC; - pub const WASM_MEMORY_FILL: u8 = 0xFC; -} - -// Numeric Instructions -pub mod numeric { - // Constants - pub const WASM_I32_CONST: u8 = 0x41; - pub const WASM_I64_CONST: u8 = 0x42; - pub const WASM_F32_CONST: u8 = 0x43; - pub const WASM_F64_CONST: u8 = 0x44; - - // Operations - pub const START_NUMERIC: u8 = 0x45; - pub const END_NUMERIC: u8 = 0xC4; - pub const WASM_SATURATING_TRUNC: u8 = 0xFC; -} - -pub const WASM_VEC: u8 = 0xFD; diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs deleted file mode 100644 index 3d572a2..0000000 --- a/crates/core/src/lib.rs +++ /dev/null @@ -1,33 +0,0 @@ -#![no_std] -#![forbid(unsafe_code)] -#![cfg_attr(not(feature = "std"), feature(error_in_core))] - -mod std; -extern crate alloc; - -mod error; -pub mod instructions; -pub mod module; -pub use error::*; -pub use module::Module; - -pub struct Store {} - -pub struct Instance {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{error::Result, Module}; - use std::dbg; - - #[test] - fn it_works() -> Result<()> { - let wasm = include_bytes!("../../../examples/wasm/helloworld.wasm"); - let module = Module::new(wasm)?; - - dbg!(module); - - Ok(()) - } -} diff --git a/crates/core/src/module.rs b/crates/core/src/module.rs deleted file mode 100644 index 1ed42c4..0000000 --- a/crates/core/src/module.rs +++ /dev/null @@ -1,155 +0,0 @@ -use crate::error::{Error, Result}; -use alloc::{format, vec::Vec}; -use tracing::error; -use wasmparser::*; - -pub struct Module<'a> { - reader: ModuleReader<'a>, -} - -impl<'a> core::fmt::Debug for Module<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Module") - .field("version", &self.reader.version) - .field("type_section", &self.reader.type_section) - .field("function_section", &self.reader.function_section) - .field("table_section", &self.reader.table_section) - .field("memory_section", &self.reader.memory_section) - .field("global_section", &self.reader.global_section) - .field("element_section", &self.reader.element_section) - .field("data_section", &self.reader.data_section) - .field("code_section", &self.reader.code_section) - .field("import_section", &self.reader.import_section) - .field("export_section", &self.reader.export_section) - .finish() - } -} - -#[derive(Default)] -pub struct ModuleReader<'a> { - pub version: Option, - pub type_section: Option>, - pub function_section: Option>, - pub table_section: Option>, - pub memory_section: Option>, - pub global_section: Option>, - pub element_section: Option>, - pub data_section: Option>, - pub code_section: Option>, - pub import_section: Option>, - pub export_section: Option>, -} - -#[derive(Debug)] -pub struct CodeSection<'a> { - pub(crate) functions: Vec>, -} - -impl<'a> CodeSection<'a> { - fn new() -> Self { - Self { - functions: Vec::new(), - } - } -} - -impl<'a> Module<'a> { - pub fn new(wasm: &'a [u8]) -> Result { - let mut validator = Validator::new(); - let mut reader = ModuleReader::new(); - - for payload in wasmparser::Parser::new(0).parse_all(wasm) { - reader.process_payload(payload?, &mut validator)?; - } - - Ok(Self { reader }) - } -} - -impl<'a> ModuleReader<'a> { - pub fn new() -> Self { - Self::default() - } - - pub fn process_payload( - &mut self, - payload: Payload<'a>, - validator: &mut Validator, - ) -> Result<()> { - use wasmparser::Payload::*; - match payload { - Version { - num, - encoding, - range, - } => { - validator.version(num, encoding, &range)?; - self.version = Some(num); - match encoding { - wasmparser::Encoding::Module => {} - wasmparser::Encoding::Component => return Error::other("Component"), - } - } - TypeSection(reader) => { - validator.type_section(&reader)?; - self.type_section = Some(reader); - } - FunctionSection(reader) => { - validator.function_section(&reader)?; - self.function_section = Some(reader); - } - TableSection(reader) => { - validator.table_section(&reader)?; - self.table_section = Some(reader); - } - MemorySection(reader) => { - validator.memory_section(&reader)?; - self.memory_section = Some(reader); - } - GlobalSection(reader) => { - validator.global_section(&reader)?; - self.global_section = Some(reader); - } - ElementSection(reader) => { - validator.element_section(&reader)?; - self.element_section = Some(reader); - } - DataSection(reader) => { - validator.data_section(&reader)?; - self.data_section = Some(reader); - } - CodeSectionStart { count, range, .. } => { - validator.code_section_start(count, &range)?; - - self.code_section = Some(CodeSection::new()); - } - CodeSectionEntry(function) => { - validator.code_section_entry(&function)?; - - if let Some(code_section) = &mut self.code_section { - code_section.functions.push(function); - } else { - return Error::other("Empty code section"); - } - } - ImportSection(reader) => { - validator.import_section(&reader)?; - self.import_section = Some(reader); - } - ExportSection(reader) => { - validator.export_section(&reader)?; - self.export_section = Some(reader); - } - - End(offset) => { - validator.end(offset)?; - return Ok(()); - } - x => Error::other(&format!("Unknown payload: {:?}", x))?, - }; - - error!("Missing end"); - - Ok(()) - } -} diff --git a/crates/core/src/std.rs b/crates/core/src/std.rs deleted file mode 100644 index 6c112f1..0000000 --- a/crates/core/src/std.rs +++ /dev/null @@ -1,18 +0,0 @@ -pub use core::*; - -#[cfg(feature = "std")] -extern crate std; - -#[cfg(feature = "std")] -pub use std::*; - -pub mod error { - #[cfg(feature = "std")] - extern crate std; - - #[cfg(feature = "std")] - pub use std::error::Error; - - #[cfg(not(feature = "std"))] - pub use core::error::Error; -} diff --git a/crates/tinywasm/Cargo.toml b/crates/tinywasm/Cargo.toml new file mode 100644 index 0000000..2f71aa9 --- /dev/null +++ b/crates/tinywasm/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name="tinywasm" +version="0.0.0" +edition="2021" + +[lib] +path="src/lib.rs" + +[dependencies] +thiserror={version="1.0", package="thiserror-core", default-features=false} +wasmparser={version="0.100", package="wasmparser-nostd", default-features=false} +tracing={version="0.1.38", default-features=false} # logging + +[features] +default=["std"] +std=["thiserror/std"] diff --git a/crates/tinywasm/src/error.rs b/crates/tinywasm/src/error.rs new file mode 100644 index 0000000..b9c93c4 --- /dev/null +++ b/crates/tinywasm/src/error.rs @@ -0,0 +1,28 @@ +use alloc::string::{String, ToString}; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error { + #[error("error parsing module")] + ParseError { message: String, offset: usize }, + + #[error("unknown error: {0}")] + Other(String), +} + +impl Error { + pub fn other(message: &str) -> Result { + Err(Self::Other(message.to_string())) + } +} + +impl From for Error { + fn from(value: wasmparser::BinaryReaderError) -> Self { + Self::ParseError { + message: value.message().to_string(), + offset: value.offset(), + } + } +} + +pub type Result = crate::std::result::Result; diff --git a/crates/tinywasm/src/instructions.rs b/crates/tinywasm/src/instructions.rs new file mode 100644 index 0000000..e3e6a08 --- /dev/null +++ b/crates/tinywasm/src/instructions.rs @@ -0,0 +1,106 @@ +// https://webassembly.github.io/spec/core/binary/instructions.html + +// Controll Instructions +pub mod control { + pub const WASM_UNREACHABLE: u8 = 0x00; + pub const WASM_NOP: u8 = 0x01; // do nothing + + // stuctured instructions + pub const WASM_BLOCK: u8 = 0x02; + pub const WASM_LOOP: u8 = 0x03; + pub const WASM_IF: u8 = 0x04; + + pub const WASM_ELSE: u8 = 0x05; + pub const WASM_END: u8 = 0x0B; + pub const WASM_BR: u8 = 0x0C; + pub const WASM_BR_IF: u8 = 0x0D; + pub const WASM_BR_TABLE: u8 = 0x0E; + pub const WASM_RETURN: u8 = 0x0F; + pub const WASM_CALL: u8 = 0x10; + pub const WASM_CALL_INDIRECT: u8 = 0x11; + pub const WASM_DROP: u8 = 0x1A; +} + +// Reference Instructions +pub mod reference { + pub const WASM_REF_NULL: u8 = 0xD0; + pub const WASM_REF_IS_NULL: u8 = 0xD1; + pub const WASM_REF_FUNC: u8 = 0xD2; +} + +// Parametric Instructions +pub mod parametric { + pub const WASM_DROP: u8 = 0x1A; + pub const WASM_SELECT: u8 = 0x1B; + pub const WASM_SELECT_T: u8 = 0x1C; +} + +// Variable Instructions +pub mod variable { + pub const WASM_LOCAL_GET: u8 = 0x20; + pub const WASM_LOCAL_SET: u8 = 0x21; + pub const WASM_LOCAL_TEE: u8 = 0x22; + pub const WASM_GLOBAL_GET: u8 = 0x23; + pub const WASM_GLOBAL_SET: u8 = 0x24; +} + +// Table Instructions +pub mod table { + pub const WASM_TABLE_GET: u8 = 0x25; + pub const WASM_TABLE_SET: u8 = 0x26; + pub const WASM_TABLE_INIT: u8 = 0xFC; + pub const WASM_ELEM_DROP: u8 = 0xFC; + pub const WASM_TABLE_COPY: u8 = 0xFC; + pub const WASM_TABLE_GROW: u8 = 0xFC; + pub const WASM_TABLE_SIZE: u8 = 0xFC; + pub const WASM_TABLE_FILL: u8 = 0xFC; +} + +// Memory Instructions +pub mod memory { + pub const WASM_I32_LOAD: u8 = 0x28; + pub const WASM_I64_LOAD: u8 = 0x29; + pub const WASM_F32_LOAD: u8 = 0x2A; + pub const WASM_F64_LOAD: u8 = 0x2B; + pub const WASM_I32_LOAD8_S: u8 = 0x2C; + pub const WASM_I32_LOAD8_U: u8 = 0x2D; + pub const WASM_I32_LOAD16_S: u8 = 0x2E; + pub const WASM_I32_LOAD16_U: u8 = 0x2F; + pub const WASM_I64_LOAD8_S: u8 = 0x30; + pub const WASM_I64_LOAD8_U: u8 = 0x31; + pub const WASM_I64_LOAD16_S: u8 = 0x32; + pub const WASM_I64_LOAD16_U: u8 = 0x33; + pub const WASM_I64_LOAD32_S: u8 = 0x34; + pub const WASM_I64_LOAD32_U: u8 = 0x35; + pub const WASM_I32_STORE: u8 = 0x36; + pub const WASM_I64_STORE: u8 = 0x37; + pub const WASM_F32_STORE: u8 = 0x38; + pub const WASM_F64_STORE: u8 = 0x39; + pub const WASM_I32_STORE8: u8 = 0x3A; + pub const WASM_I32_STORE16: u8 = 0x3B; + pub const WASM_I64_STORE8: u8 = 0x3C; + pub const WASM_I64_STORE16: u8 = 0x3D; + pub const WASM_I64_STORE32: u8 = 0x3E; + pub const WASM_MEMORY_SIZE: u8 = 0x3F; + pub const WASM_MEMORY_GROW: u8 = 0x40; + pub const WASM_MEMORY_INIT: u8 = 0xFC; + pub const WASM_DATA_DROP: u8 = 0xFC; + pub const WASM_MEMORY_COPY: u8 = 0xFC; + pub const WASM_MEMORY_FILL: u8 = 0xFC; +} + +// Numeric Instructions +pub mod numeric { + // Constants + pub const WASM_I32_CONST: u8 = 0x41; + pub const WASM_I64_CONST: u8 = 0x42; + pub const WASM_F32_CONST: u8 = 0x43; + pub const WASM_F64_CONST: u8 = 0x44; + + // Operations + pub const START_NUMERIC: u8 = 0x45; + pub const END_NUMERIC: u8 = 0xC4; + pub const WASM_SATURATING_TRUNC: u8 = 0xFC; +} + +pub const WASM_VEC: u8 = 0xFD; diff --git a/crates/tinywasm/src/lib.rs b/crates/tinywasm/src/lib.rs new file mode 100644 index 0000000..3d572a2 --- /dev/null +++ b/crates/tinywasm/src/lib.rs @@ -0,0 +1,33 @@ +#![no_std] +#![forbid(unsafe_code)] +#![cfg_attr(not(feature = "std"), feature(error_in_core))] + +mod std; +extern crate alloc; + +mod error; +pub mod instructions; +pub mod module; +pub use error::*; +pub use module::Module; + +pub struct Store {} + +pub struct Instance {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{error::Result, Module}; + use std::dbg; + + #[test] + fn it_works() -> Result<()> { + let wasm = include_bytes!("../../../examples/wasm/helloworld.wasm"); + let module = Module::new(wasm)?; + + dbg!(module); + + Ok(()) + } +} diff --git a/crates/tinywasm/src/module.rs b/crates/tinywasm/src/module.rs new file mode 100644 index 0000000..1ed42c4 --- /dev/null +++ b/crates/tinywasm/src/module.rs @@ -0,0 +1,155 @@ +use crate::error::{Error, Result}; +use alloc::{format, vec::Vec}; +use tracing::error; +use wasmparser::*; + +pub struct Module<'a> { + reader: ModuleReader<'a>, +} + +impl<'a> core::fmt::Debug for Module<'a> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("Module") + .field("version", &self.reader.version) + .field("type_section", &self.reader.type_section) + .field("function_section", &self.reader.function_section) + .field("table_section", &self.reader.table_section) + .field("memory_section", &self.reader.memory_section) + .field("global_section", &self.reader.global_section) + .field("element_section", &self.reader.element_section) + .field("data_section", &self.reader.data_section) + .field("code_section", &self.reader.code_section) + .field("import_section", &self.reader.import_section) + .field("export_section", &self.reader.export_section) + .finish() + } +} + +#[derive(Default)] +pub struct ModuleReader<'a> { + pub version: Option, + pub type_section: Option>, + pub function_section: Option>, + pub table_section: Option>, + pub memory_section: Option>, + pub global_section: Option>, + pub element_section: Option>, + pub data_section: Option>, + pub code_section: Option>, + pub import_section: Option>, + pub export_section: Option>, +} + +#[derive(Debug)] +pub struct CodeSection<'a> { + pub(crate) functions: Vec>, +} + +impl<'a> CodeSection<'a> { + fn new() -> Self { + Self { + functions: Vec::new(), + } + } +} + +impl<'a> Module<'a> { + pub fn new(wasm: &'a [u8]) -> Result { + let mut validator = Validator::new(); + let mut reader = ModuleReader::new(); + + for payload in wasmparser::Parser::new(0).parse_all(wasm) { + reader.process_payload(payload?, &mut validator)?; + } + + Ok(Self { reader }) + } +} + +impl<'a> ModuleReader<'a> { + pub fn new() -> Self { + Self::default() + } + + pub fn process_payload( + &mut self, + payload: Payload<'a>, + validator: &mut Validator, + ) -> Result<()> { + use wasmparser::Payload::*; + match payload { + Version { + num, + encoding, + range, + } => { + validator.version(num, encoding, &range)?; + self.version = Some(num); + match encoding { + wasmparser::Encoding::Module => {} + wasmparser::Encoding::Component => return Error::other("Component"), + } + } + TypeSection(reader) => { + validator.type_section(&reader)?; + self.type_section = Some(reader); + } + FunctionSection(reader) => { + validator.function_section(&reader)?; + self.function_section = Some(reader); + } + TableSection(reader) => { + validator.table_section(&reader)?; + self.table_section = Some(reader); + } + MemorySection(reader) => { + validator.memory_section(&reader)?; + self.memory_section = Some(reader); + } + GlobalSection(reader) => { + validator.global_section(&reader)?; + self.global_section = Some(reader); + } + ElementSection(reader) => { + validator.element_section(&reader)?; + self.element_section = Some(reader); + } + DataSection(reader) => { + validator.data_section(&reader)?; + self.data_section = Some(reader); + } + CodeSectionStart { count, range, .. } => { + validator.code_section_start(count, &range)?; + + self.code_section = Some(CodeSection::new()); + } + CodeSectionEntry(function) => { + validator.code_section_entry(&function)?; + + if let Some(code_section) = &mut self.code_section { + code_section.functions.push(function); + } else { + return Error::other("Empty code section"); + } + } + ImportSection(reader) => { + validator.import_section(&reader)?; + self.import_section = Some(reader); + } + ExportSection(reader) => { + validator.export_section(&reader)?; + self.export_section = Some(reader); + } + + End(offset) => { + validator.end(offset)?; + return Ok(()); + } + x => Error::other(&format!("Unknown payload: {:?}", x))?, + }; + + error!("Missing end"); + + Ok(()) + } +} diff --git a/crates/tinywasm/src/std.rs b/crates/tinywasm/src/std.rs new file mode 100644 index 0000000..6c112f1 --- /dev/null +++ b/crates/tinywasm/src/std.rs @@ -0,0 +1,18 @@ +pub use core::*; + +#[cfg(feature = "std")] +extern crate std; + +#[cfg(feature = "std")] +pub use std::*; + +pub mod error { + #[cfg(feature = "std")] + extern crate std; + + #[cfg(feature = "std")] + pub use std::error::Error; + + #[cfg(not(feature = "std"))] + pub use core::error::Error; +} diff --git a/crates/tinywasm/tests/spec/testsuite b/crates/tinywasm/tests/spec/testsuite new file mode 160000 index 0000000..dc27dad --- /dev/null +++ b/crates/tinywasm/tests/spec/testsuite @@ -0,0 +1 @@ +Subproject commit dc27dad3e34e466bdbfea32fe3c73f5e31f88560 -- cgit v1.3.1