diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2023-12-09 14:56:03 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2023-12-09 14:56:03 +0100 |
| commit | d08750034a582b7ef338432a360c62a2969f6aa0 (patch) | |
| tree | 577296351a97ed044dd2ceabc7d772c42cf21c0f | |
| parent | ea0a362c585c2a2da39ec8fbafacd79c17680665 (diff) | |
chore: improve documentation
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
| -rw-r--r-- | Cargo.lock | 6 | ||||
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | crates/cli/Cargo.toml | 3 | ||||
| -rw-r--r-- | crates/parser/Cargo.toml | 2 | ||||
| -rw-r--r-- | crates/parser/README.md | 21 | ||||
| -rw-r--r-- | crates/parser/src/lib.rs | 7 | ||||
| -rw-r--r-- | crates/tinywasm/Cargo.toml | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/error.rs | 33 | ||||
| -rw-r--r-- | crates/tinywasm/src/export.rs | 6 | ||||
| -rw-r--r-- | crates/tinywasm/src/func.rs | 10 | ||||
| -rw-r--r-- | crates/tinywasm/src/instance.rs | 25 | ||||
| -rw-r--r-- | crates/tinywasm/src/lib.rs | 32 | ||||
| -rw-r--r-- | crates/tinywasm/src/module.rs | 10 | ||||
| -rw-r--r-- | crates/tinywasm/src/runtime/mod.rs | 3 | ||||
| -rw-r--r-- | crates/tinywasm/src/runtime/stack/blocks.rs | 8 | ||||
| -rw-r--r-- | crates/tinywasm/src/runtime/stack/call_stack.rs | 22 | ||||
| -rw-r--r-- | crates/tinywasm/src/runtime/stack/value_stack.rs | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/runtime/value.rs | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/std.rs | 10 | ||||
| -rw-r--r-- | crates/tinywasm/src/store.rs | 7 | ||||
| -rw-r--r-- | crates/types/Cargo.toml | 3 | ||||
| -rw-r--r-- | crates/types/README.md | 1 | ||||
| -rw-r--r-- | crates/types/src/instructions.rs | 12 | ||||
| -rw-r--r-- | crates/types/src/lib.rs | 19 |
24 files changed, 185 insertions, 79 deletions
@@ -536,7 +536,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tinywasm" -version = "0.0.0" +version = "0.0.1" dependencies = [ "log", "tinywasm-parser", @@ -558,7 +558,7 @@ dependencies = [ [[package]] name = "tinywasm-parser" -version = "0.0.0" +version = "0.0.1" dependencies = [ "log", "tinywasm-types", @@ -567,7 +567,7 @@ dependencies = [ [[package]] name = "tinywasm-types" -version = "0.0.0" +version = "0.0.1" dependencies = [ "log", "rkyv", @@ -8,13 +8,21 @@ <br> -[](https://docs.rs/okv) [](https://crates.io/crates/okv) [](./LICENSE-APACHE) +[](https://docs.rs/tinywasm) [](https://crates.io/crates/tinywasm) [](./LICENSE-APACHE) -<br/> > [!WARNING] > This project is still in development and is not ready for use. +## Features + +- **`std`**\ + Enables the use of `std` and `std::io` for parsing from files and streams. This is enabled by default. +- **`logging`**\ + Enables logging of the parsing process using the `log` crate. This is enabled by default. +- **`parser`**\ + Enables the `tinywasm_parser` crate. This is enabled by default. + # 🎯 Goals * Interpreted Runtime (no JIT) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 1f34a67..2bd9613 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -2,9 +2,10 @@ name="tinywasm-cli" version="0.0.0" edition="2021" +private=true [[bin]] -name="tinywasm" +name="tinywasm-cli" path="src/bin.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/crates/parser/Cargo.toml b/crates/parser/Cargo.toml index 17c5557..823e69b 100644 --- a/crates/parser/Cargo.toml +++ b/crates/parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name="tinywasm-parser" -version="0.0.0" +version="0.0.1" edition="2021" [dependencies] diff --git a/crates/parser/README.md b/crates/parser/README.md new file mode 100644 index 0000000..5772bb7 --- /dev/null +++ b/crates/parser/README.md @@ -0,0 +1,21 @@ +# `tinywasm_parser` + +This crate provides a parser that can parse WebAssembly modules into a TinyWasm module. It is based on +[`wasmparser_nostd`](https://crates.io/crates/wasmparser_nostd) and used by [`tinywasm`](https://crates.io/crates/tinywasm). + +## Features + +- `std`: Enables the use of `std` and `std::io` for parsing from files and streams. +- `logging`: Enables logging of the parsing process using the `log` crate. + +## Usage + +```rust +use tinywasm_parser::{Parser, TinyWasmModule}; +let bytes = include_bytes!("./file.wasm"); + +let parser = Parser::new(); +let module: TinyWasmModule = parser.parse_module_bytes(bytes).unwrap(); +let mudule: TinyWasmModule = parser.parse_module_file("path/to/file.wasm").unwrap(); +let module: TinyWasmModule = parser.parse_module_stream(&mut stream).unwrap(); +``` diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 343350a..0719c96 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -22,9 +22,11 @@ mod module; use alloc::vec::Vec; pub use error::*; use module::ModuleReader; -use tinywasm_types::{Function, TinyWasmModule}; +use tinywasm_types::Function; use wasmparser::Validator; +pub use tinywasm_types::TinyWasmModule; + #[derive(Default)] pub struct Parser {} @@ -33,7 +35,8 @@ impl Parser { Self {} } - pub fn parse_module_bytes(&self, wasm: &[u8]) -> Result<TinyWasmModule> { + pub fn parse_module_bytes(&self, wasm: impl AsRef<[u8]>) -> Result<TinyWasmModule> { + let wasm = wasm.as_ref(); let mut validator = Validator::new(); let mut reader = ModuleReader::new(); diff --git a/crates/tinywasm/Cargo.toml b/crates/tinywasm/Cargo.toml index 421ec37..1fee641 100644 --- a/crates/tinywasm/Cargo.toml +++ b/crates/tinywasm/Cargo.toml @@ -1,12 +1,12 @@ [package] name="tinywasm" -version="0.0.0" +version="0.0.1" edition="2021" [lib] name="tinywasm" path="src/lib.rs" -crate-type=["cdylib", "rlib"] +crate-type=["lib", "cdylib", "rlib"] [dependencies] log={version="0.4", optional=true} diff --git a/crates/tinywasm/src/error.rs b/crates/tinywasm/src/error.rs index c0e8d10..926a9bc 100644 --- a/crates/tinywasm/src/error.rs +++ b/crates/tinywasm/src/error.rs @@ -1,31 +1,51 @@ -use alloc::string::{String, ToString}; +use alloc::string::String; use core::fmt::Display; #[cfg(feature = "parser")] use tinywasm_parser::ParseError; #[derive(Debug)] +/// A WebAssembly trap +/// +/// See <https://webassembly.github.io/spec/core/intro/overview.html#trap> pub enum Trap { + /// An unreachable instruction was executed Unreachable, } #[derive(Debug)] +/// A tinywasm error pub enum Error { #[cfg(feature = "parser")] + /// A parsing error occurred ParseError(ParseError), #[cfg(feature = "std")] + /// An I/O error occurred Io(crate::std::io::Error), + /// A WebAssembly feature is not supported UnsupportedFeature(String), + + /// An unknown error occurred Other(String), + /// A WebAssembly trap occurred Trap(Trap), + /// A function did not return a value FuncDidNotReturn, + + /// The stack is empty StackUnderflow, + + /// The block stack is empty BlockStackUnderflow, + + /// The call stack is empty CallStackEmpty, + + /// The store is not the one that the module instance was instantiated in InvalidStore, } @@ -53,16 +73,6 @@ impl Display for Error { impl crate::std::error::Error for Error {} -impl Error { - pub fn other<T>(message: &str) -> Result<T, Self> { - Err(Self::Other(message.to_string())) - } - - pub fn unsupported<T>(feature: &str) -> Result<T, Self> { - Err(Self::UnsupportedFeature(feature.to_string())) - } -} - #[cfg(feature = "parser")] impl From<tinywasm_parser::ParseError> for Error { fn from(value: tinywasm_parser::ParseError) -> Self { @@ -70,4 +80,5 @@ impl From<tinywasm_parser::ParseError> for Error { } } +/// A specialized [`Result`] type for tinywasm operations pub type Result<T, E = Error> = crate::std::result::Result<T, E>; diff --git a/crates/tinywasm/src/export.rs b/crates/tinywasm/src/export.rs index c2aad88..c6ae6d3 100644 --- a/crates/tinywasm/src/export.rs +++ b/crates/tinywasm/src/export.rs @@ -4,13 +4,15 @@ use tinywasm_types::{Export, ExternalKind}; use crate::{Error, Result}; #[derive(Debug)] +/// Exports of a module instance pub struct ExportInstance(pub(crate) Box<[Export]>); impl ExportInstance { - pub fn func(&self, name: &str) -> Result<&Export> { + /// Get an export by name + pub fn get(&self, name: &str, ty: ExternalKind) -> Result<&Export> { self.0 .iter() - .find(|e| e.name == name.into() && e.kind == ExternalKind::Func) + .find(|e| e.name == name.into() && e.kind == ty) .ok_or(Error::Other(format!("export {} not found", name))) } } diff --git a/crates/tinywasm/src/func.rs b/crates/tinywasm/src/func.rs index ad46fba..3428256 100644 --- a/crates/tinywasm/src/func.rs +++ b/crates/tinywasm/src/func.rs @@ -8,15 +8,19 @@ use crate::{ }; #[derive(Debug)] +/// A function handle pub struct FuncHandle { pub(crate) _module: ModuleInstance, pub(crate) addr: FuncAddr, pub(crate) ty: FuncType, + + /// The name of the function, if it has one pub name: Option<String>, } impl FuncHandle { /// Call a function - /// See https://webassembly.github.io/spec/core/exec/modules.html#invocation + /// + /// See <https://webassembly.github.io/spec/core/exec/modules.html#invocation> pub fn call(&self, store: &mut Store, params: &[WasmValue]) -> Result<Vec<WasmValue>> { let mut stack = Stack::default(); @@ -75,7 +79,10 @@ impl FuncHandle { } } +#[derive(Debug)] +/// A typed function handle pub struct TypedFuncHandle<P, R> { + /// The underlying function handle pub func: FuncHandle, pub(crate) marker: core::marker::PhantomData<(P, R)>, } @@ -91,6 +98,7 @@ pub trait FromWasmValueTuple { } impl<P: IntoWasmValueTuple, R: FromWasmValueTuple> TypedFuncHandle<P, R> { + /// Call a typed function pub fn call(&self, store: &mut Store, params: P) -> Result<R> { // Convert params into Vec<WasmValue> let wasm_values = params.into_wasm_value_tuple(); diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs index 598dba1..6b1374e 100644 --- a/crates/tinywasm/src/instance.rs +++ b/crates/tinywasm/src/instance.rs @@ -1,5 +1,5 @@ use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec}; -use tinywasm_types::{Export, FuncAddr, FuncType, ModuleInstanceAddr}; +use tinywasm_types::{Export, ExternalKind, FuncAddr, FuncType, ModuleInstanceAddr}; use crate::{ func::{FromWasmValueTuple, IntoWasmValueTuple}, @@ -10,7 +10,7 @@ use crate::{ /// /// Addrs are indices into the store's data structures. /// -/// See https://webassembly.github.io/spec/core/exec/runtime.html#module-instances +/// See <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances> #[derive(Debug, Clone)] pub struct ModuleInstance(Arc<ModuleInstanceInner>); @@ -20,7 +20,7 @@ struct ModuleInstanceInner { pub(crate) _idx: ModuleInstanceAddr, pub(crate) func_start: Option<FuncAddr>, pub(crate) types: Box<[FuncType]>, - pub exports: ExportInstance, + pub(crate) exports: ExportInstance, pub(crate) func_addrs: Vec<FuncAddr>, // pub table_addrs: Vec<TableAddr>, @@ -31,6 +31,11 @@ struct ModuleInstanceInner { } impl ModuleInstance { + /// Get the module's exports + pub fn exports(&self) -> &ExportInstance { + &self.0.exports + } + pub(crate) fn new( types: Box<[FuncType]>, func_start: Option<FuncAddr>, @@ -55,7 +60,7 @@ impl ModuleInstance { return Err(Error::InvalidStore); } - let export = self.0.exports.func(name)?; + let export = self.0.exports.get(name, ExternalKind::Func)?; let func_addr = self.0.func_addrs[export.index as usize]; let func = store.get_func(func_addr as usize)?; let ty = self.0.types[func.ty_addr() as usize].clone(); @@ -81,11 +86,13 @@ impl ModuleInstance { }) } - /// Get the start function of the module + /// Get the start function of the module + /// /// Returns None if the module has no start function /// If no start function is specified, also checks for a _start function in the exports /// (which is not part of the spec, but used by llvm) - /// https://webassembly.github.io/spec/core/syntax/modules.html#start-function + /// + /// See <https://webassembly.github.io/spec/core/syntax/modules.html#start-function> pub fn get_start_func(&mut self, store: &Store) -> Result<Option<FuncHandle>> { if self.0.store_id != store.id() { return Err(Error::InvalidStore); @@ -95,7 +102,7 @@ impl ModuleInstance { Some(func_index) => func_index, None => { // alternatively, check for a _start function in the exports - let Ok(start) = self.0.exports.func("_start") else { + let Ok(start) = self.0.exports.get("_start", ExternalKind::Func) else { return Ok(None); }; @@ -116,8 +123,10 @@ impl ModuleInstance { } /// Invoke the start function of the module + /// /// Returns None if the module has no start function - /// https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start + /// + /// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start> pub fn start(&mut self, store: &mut Store) -> Result<Option<()>> { let Some(func) = self.get_start_func(store)? else { return Ok(None); diff --git a/crates/tinywasm/src/lib.rs b/crates/tinywasm/src/lib.rs index c09afb8..87d3923 100644 --- a/crates/tinywasm/src/lib.rs +++ b/crates/tinywasm/src/lib.rs @@ -1,6 +1,16 @@ #![no_std] #![forbid(unsafe_code)] #![cfg_attr(not(feature = "std"), feature(error_in_core))] +#![doc(test( + no_crate_inject, + attr( + deny(warnings, rust_2018_idioms), + allow(dead_code, unused_assignments, unused_variables) + ) +))] +#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)] + +//! ## A tiny WebAssembly Runtime written in Rust mod std; extern crate alloc; @@ -19,25 +29,31 @@ pub(crate) mod log { mod error; pub use error::*; -pub mod store; -pub use store::Store; +mod store; +pub use store::*; -pub mod module; +mod module; pub use module::Module; -pub mod instance; +mod instance; pub use instance::ModuleInstance; -pub mod export; +mod export; pub use export::ExportInstance; -pub mod func; +mod func; pub use func::{FuncHandle, TypedFuncHandle}; +mod runtime; +pub use runtime::*; + #[cfg(feature = "parser")] -pub use tinywasm_parser as parser; +/// Re-export of `tinywasm_parser`. Requires `parser` feature. +pub mod parser { + pub use tinywasm_parser::*; +} + pub use tinywasm_types::*; -pub mod runtime; #[cfg(test)] mod tests { diff --git a/crates/tinywasm/src/module.rs b/crates/tinywasm/src/module.rs index 740b4b7..885f28c 100644 --- a/crates/tinywasm/src/module.rs +++ b/crates/tinywasm/src/module.rs @@ -3,6 +3,9 @@ use tinywasm_types::TinyWasmModule; use crate::{ModuleInstance, Result, Store}; #[derive(Debug)] +/// A WebAssembly Module +/// +/// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-module> pub struct Module { data: TinyWasmModule, } @@ -15,6 +18,7 @@ impl From<TinyWasmModule> for Module { impl Module { #[cfg(feature = "parser")] + /// Parse a module from bytes. Requires `parser` feature. pub fn parse_bytes(wasm: &[u8]) -> Result<Self> { let parser = tinywasm_parser::Parser::new(); let data = parser.parse_module_bytes(wasm)?; @@ -22,6 +26,7 @@ impl Module { } #[cfg(all(feature = "parser", feature = "std"))] + /// Parse a module from a file. Requires `parser` and `std` features. pub fn parse_file(path: impl AsRef<crate::std::path::Path> + Clone) -> Result<Self> { let parser = tinywasm_parser::Parser::new(); let data = parser.parse_module_file(path)?; @@ -29,6 +34,7 @@ impl Module { } #[cfg(all(feature = "parser", feature = "std"))] + /// Parse a module from a stream. Requires `parser` and `std` features. pub fn parse_stream(stream: impl crate::std::io::Read) -> Result<Self> { let parser = tinywasm_parser::Parser::new(); let data = parser.parse_module_stream(stream)?; @@ -36,9 +42,11 @@ impl Module { } /// Instantiate the module in the given store - /// See https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation + /// /// Runs the start function if it exists /// If you want to run the start function yourself, use `ModuleInstance::new` + /// + /// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation> pub fn instantiate( self, store: &mut Store, diff --git a/crates/tinywasm/src/runtime/mod.rs b/crates/tinywasm/src/runtime/mod.rs index f0b6d66..0f22a52 100644 --- a/crates/tinywasm/src/runtime/mod.rs +++ b/crates/tinywasm/src/runtime/mod.rs @@ -6,11 +6,12 @@ pub use stack::*; pub(crate) use value::RawWasmValue; /// A WebAssembly Runtime. -/// See https://webassembly.github.io/spec/core/exec/runtime.html /// /// Generic over `CheckTypes` to enable type checking at runtime. /// This is useful for debugging, but should be disabled if you know /// that the module is valid. +/// +/// See <https://webassembly.github.io/spec/core/exec/runtime.html> // Execution is implemented in the `executer` module #[derive(Debug, Default)] pub struct Runtime<const CHECK_TYPES: bool> {} diff --git a/crates/tinywasm/src/runtime/stack/blocks.rs b/crates/tinywasm/src/runtime/stack/blocks.rs index 6bf2237..f1a02f7 100644 --- a/crates/tinywasm/src/runtime/stack/blocks.rs +++ b/crates/tinywasm/src/runtime/stack/blocks.rs @@ -34,11 +34,11 @@ impl Blocks { #[derive(Debug)] pub(crate) struct BlockFrame { // where to resume execution when the block is broken - pub instr_ptr: usize, + pub(crate) instr_ptr: usize, // position of the stack pointer when the block was entered - pub stack_ptr: usize, - pub args: BlockArgs, - pub ty: BlockFrameType, + pub(crate) stack_ptr: usize, + pub(crate) args: BlockArgs, + pub(crate) ty: BlockFrameType, } #[derive(Debug, Copy, Clone)] diff --git a/crates/tinywasm/src/runtime/stack/call_stack.rs b/crates/tinywasm/src/runtime/stack/call_stack.rs index 3eaf886..f1055ae 100644 --- a/crates/tinywasm/src/runtime/stack/call_stack.rs +++ b/crates/tinywasm/src/runtime/stack/call_stack.rs @@ -5,10 +5,10 @@ use tinywasm_types::{ValType, WasmValue}; use super::{blocks::Blocks, BlockFrameType}; // minimum call stack size -pub const CALL_STACK_SIZE: usize = 1024; +const CALL_STACK_SIZE: usize = 1024; #[derive(Debug)] -pub struct CallStack { +pub(crate) struct CallStack { stack: Vec<CallFrame>, top: usize, } @@ -49,19 +49,19 @@ impl CallStack { } #[derive(Debug)] -pub struct CallFrame { - pub instr_ptr: usize, - pub func_ptr: usize, +pub(crate) struct CallFrame { + pub(crate) instr_ptr: usize, + pub(crate) _func_ptr: usize, - pub blocks: Blocks, - pub locals: Box<[RawWasmValue]>, - pub local_count: usize, + pub(crate) blocks: Blocks, + pub(crate) locals: Box<[RawWasmValue]>, + pub(crate) local_count: usize, } impl CallFrame { /// Break to a block at the given index (relative to the current frame) #[inline] - pub fn break_to(&mut self, block_index: u32, value_stack: &mut super::ValueStack) -> Result<()> { + pub(crate) fn break_to(&mut self, block_index: u32, value_stack: &mut super::ValueStack) -> Result<()> { let block = self .blocks .get(block_index as usize) @@ -83,14 +83,14 @@ impl CallFrame { Ok(()) } - pub fn new(func_ptr: usize, params: &[WasmValue], local_types: Vec<ValType>) -> Self { + pub(crate) fn new(func_ptr: usize, params: &[WasmValue], local_types: Vec<ValType>) -> Self { let mut locals = Vec::with_capacity(local_types.len() + params.len()); locals.extend(params.iter().map(|v| RawWasmValue::from(*v))); locals.extend(local_types.iter().map(|_| RawWasmValue::default())); Self { instr_ptr: 0, - func_ptr, + _func_ptr: func_ptr, local_count: locals.len(), locals: locals.into_boxed_slice(), blocks: Blocks::default(), diff --git a/crates/tinywasm/src/runtime/stack/value_stack.rs b/crates/tinywasm/src/runtime/stack/value_stack.rs index df1b011..3c69884 100644 --- a/crates/tinywasm/src/runtime/stack/value_stack.rs +++ b/crates/tinywasm/src/runtime/stack/value_stack.rs @@ -3,10 +3,10 @@ use alloc::vec::Vec; use tinywasm_types::BlockArgs; // minimum stack size -pub const STACK_SIZE: usize = 1024; +pub(crate) const STACK_SIZE: usize = 1024; #[derive(Debug)] -pub struct ValueStack { +pub(crate) struct ValueStack { stack: Vec<RawWasmValue>, // TODO: don't pop the stack, just keep track of the top for better performance diff --git a/crates/tinywasm/src/runtime/value.rs b/crates/tinywasm/src/runtime/value.rs index e48ed2c..2476038 100644 --- a/crates/tinywasm/src/runtime/value.rs +++ b/crates/tinywasm/src/runtime/value.rs @@ -3,13 +3,15 @@ use core::fmt::Debug; use tinywasm_types::{ValType, WasmValue}; /// A raw wasm value. +/// /// This is the internal representation of all wasm values +/// /// See [`WasmValue`] for the public representation. #[derive(Clone, Copy, Default)] pub struct RawWasmValue(u64); impl Debug for RawWasmValue { - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "RawWasmValue({})", self.0 as i64) // cast to i64 so at least negative numbers for i32 and i64 are printed correctly } } diff --git a/crates/tinywasm/src/std.rs b/crates/tinywasm/src/std.rs index ba30537..c31de8f 100644 --- a/crates/tinywasm/src/std.rs +++ b/crates/tinywasm/src/std.rs @@ -1,18 +1,18 @@ #[cfg(not(feature = "std"))] -pub use core::*; +pub(crate) use core::*; #[cfg(feature = "std")] extern crate std; #[cfg(feature = "std")] -pub use std::*; +pub(crate) use std::*; -pub mod error { +pub(crate) mod error { #[cfg(feature = "std")] extern crate std; #[cfg(feature = "std")] - pub use std::error::Error; + pub(crate) use std::error::Error; #[cfg(not(feature = "std"))] - pub use core::error::Error; + pub(crate) use core::error::Error; } diff --git a/crates/tinywasm/src/store.rs b/crates/tinywasm/src/store.rs index b12be95..10aa56a 100644 --- a/crates/tinywasm/src/store.rs +++ b/crates/tinywasm/src/store.rs @@ -16,7 +16,7 @@ static STORE_ID: AtomicUsize = AtomicUsize::new(0); /// indefinitely if you keep adding modules to it. When calling temporary /// functions, you should create a new store and then drop it when you're done (e.g. in a request handler) /// -/// See also: https://webassembly.github.io/spec/core/exec/runtime.html#store +/// See <https://webassembly.github.io/spec/core/exec/runtime.html#store> #[derive(Debug)] pub struct Store { id: usize, @@ -48,6 +48,9 @@ impl Default for Store { } #[derive(Debug)] +/// A WebAssembly Function Instance +/// +/// See <https://webassembly.github.io/spec/core/exec/runtime.html#function-instances> pub struct FunctionInstance { pub(crate) func: Function, pub(crate) _module_instance: ModuleInstanceAddr, // index into store.module_instances @@ -72,6 +75,7 @@ impl FunctionInstance { } #[derive(Debug, Default)] +/// Global state that can be manipulated by WebAssembly programs pub struct StoreData { pub(crate) funcs: Vec<FunctionInstance>, // pub tables: Vec<TableAddr>, @@ -82,6 +86,7 @@ pub struct StoreData { } impl Store { + /// Get the store's ID (unique per process) pub fn id(&self) -> usize { self.id } diff --git a/crates/types/Cargo.toml b/crates/types/Cargo.toml index 082723f..e91a4f1 100644 --- a/crates/types/Cargo.toml +++ b/crates/types/Cargo.toml @@ -1,7 +1,8 @@ [package] name="tinywasm-types" -version="0.0.0" +version="0.0.1" edition="2021" +readme="../../README.md" [dependencies] log={version="0.4", optional=true} diff --git a/crates/types/README.md b/crates/types/README.md new file mode 100644 index 0000000..93b6f4b --- /dev/null +++ b/crates/types/README.md @@ -0,0 +1 @@ +# `tinywasm_types` diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index 005da36..6db5e6b 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -18,17 +18,19 @@ type BrTableDefault = u32; type BrTableLen = usize; /// A WebAssembly Instruction -/// See https://webassembly.github.io/spec/core/binary/instructions.html +/// /// These are our own internal bytecode instructions so they may not match the spec exactly. /// Wasm Bytecode can map to multiple of these instructions. /// For example, `br_table` stores the jump lables in the following `br_label` instructions to keep this enum small. +/// +/// See <https://webassembly.github.io/spec/core/binary/instructions.html> #[derive(Debug, Clone, Copy, PartialEq)] pub enum Instruction { // Custom Instructions BrLabel(LabelAddr), // Control Instructions - // See https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions + // See <https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions> Unreachable, Nop, Block(BlockArgs), @@ -44,12 +46,12 @@ pub enum Instruction { CallIndirect(TypeAddr, TableAddr), // Parametric Instructions - // See https://webassembly.github.io/spec/core/binary/instructions.html#parametric-instructions + // See <https://webassembly.github.io/spec/core/binary/instructions.html#parametric-instructions> Drop, Select, // Variable Instructions - // See https://webassembly.github.io/spec/core/binary/instructions.html#variable-instructions + // See <https://webassembly.github.io/spec/core/binary/instructions.html#variable-instructions> LocalGet(LocalAddr), LocalSet(LocalAddr), LocalTee(LocalAddr), @@ -90,7 +92,7 @@ pub enum Instruction { F64Const(f64), // Numeric Instructions - // See https://webassembly.github.io/spec/core/binary/instructions.html#numeric-instructions + // See <https://webassembly.github.io/spec/core/binary/instructions.html#numeric-instructions> I32Eqz, I32Eq, I32Ne, diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 90c4f10..b79c72a 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -34,7 +34,8 @@ pub struct TinyWasmModule { } /// A WebAssembly value. -/// See https://webassembly.github.io/spec/core/syntax/types.html#value-types +/// +/// See <https://webassembly.github.io/spec/core/syntax/types.html#value-types> #[derive(Clone, PartialEq, Copy)] pub enum WasmValue { // Num types @@ -182,7 +183,8 @@ pub enum ValType { } /// A WebAssembly External Kind. -/// See https://webassembly.github.io/spec/core/syntax/types.html#external-types +/// +/// See <https://webassembly.github.io/spec/core/syntax/types.html#external-types> #[derive(Debug, Clone, PartialEq)] pub enum ExternalKind { Func, @@ -192,8 +194,10 @@ pub enum ExternalKind { } /// A WebAssembly Address. +/// /// These are indexes into the respective stores. -/// See https://webassembly.github.io/spec/core/exec/runtime.html#addresses +/// +/// See <https://webassembly.github.io/spec/core/exec/runtime.html#addresses> pub type Addr = u32; pub type FuncAddr = Addr; pub type TableAddr = Addr; @@ -209,7 +213,8 @@ pub type LabelAddr = Addr; pub type ModuleInstanceAddr = Addr; /// A WebAssembly Export Instance. -/// https://webassembly.github.io/spec/core/exec/runtime.html#export-instances +/// +/// See <https://webassembly.github.io/spec/core/exec/runtime.html#export-instances> #[derive(Debug)] pub struct ExportInst { pub name: String, @@ -217,7 +222,8 @@ pub struct ExportInst { } /// A WebAssembly External Value. -/// https://webassembly.github.io/spec/core/exec/runtime.html#external-values +/// +/// See <https://webassembly.github.io/spec/core/exec/runtime.html#external-values> #[derive(Debug)] pub enum ExternVal { Func(FuncAddr), @@ -227,7 +233,8 @@ pub enum ExternVal { } /// The type of a WebAssembly Function. -/// See https://webassembly.github.io/spec/core/syntax/types.html#function-types +/// +/// See <https://webassembly.github.io/spec/core/syntax/types.html#function-types> #[derive(Debug, Clone, PartialEq)] pub struct FuncType { pub params: Box<[ValType]>, |
