1+ #![ no_std]
2+ #![ forbid( unsafe_code) ]
3+ #![ doc( test(
4+ no_crate_inject,
5+ attr(
6+ deny( warnings, rust_2018_idioms) ,
7+ allow( dead_code, unused_assignments, unused_variables)
8+ )
9+ ) ) ]
10+ #![ warn( missing_debug_implementations, rust_2018_idioms, unreachable_pub) ]
11+
12+ //! Types used by [`tinywasm`](https://docs.rs/tinywasm) and [`tinywasm_parser`](https://docs.rs/tinywasm_parser).
13+
114extern crate alloc;
215
316// log for logging (optional).
@@ -15,15 +28,29 @@ extern crate alloc;
1528mod instructions;
1629use core:: fmt:: Debug ;
1730
31+ use alloc:: boxed:: Box ;
1832pub use instructions:: * ;
1933
34+ /// A TinyWasm WebAssembly Module
35+ ///
36+ /// This is the internal representation of a WebAssembly module in TinyWasm.
37+ /// TinyWasmModules are validated before being created, so they are guaranteed to be valid (as long as they were created by TinyWasm).
38+ /// This means you should not trust a TinyWasmModule created by a third party to be valid.
2039#[ derive( Debug ) ]
2140pub struct TinyWasmModule {
41+ /// The version of the WebAssembly module.
2242 pub version : Option < u16 > ,
43+
44+ /// The start function of the WebAssembly module.
2345 pub start_func : Option < FuncAddr > ,
2446
47+ /// The functions of the WebAssembly module.
2548 pub funcs : Box < [ Function ] > ,
49+
50+ /// The types of the WebAssembly module.
2651 pub types : Box < [ FuncType ] > ,
52+
53+ /// The exports of the WebAssembly module.
2754 pub exports : Box < [ Export ] > ,
2855 // pub tables: Option<TableType>,
2956 // pub memories: Option<MemoryType>,
@@ -39,15 +66,20 @@ pub struct TinyWasmModule {
3966#[ derive( Clone , PartialEq , Copy ) ]
4067pub enum WasmValue {
4168 // Num types
69+ /// A 32-bit integer.
4270 I32 ( i32 ) ,
71+ /// A 64-bit integer.
4372 I64 ( i64 ) ,
73+ /// A 32-bit float.
4474 F32 ( f32 ) ,
75+ /// A 64-bit float.
4576 F64 ( f64 ) ,
4677 // Vec types
4778 // V128(i128),
4879}
4980
5081impl WasmValue {
82+ /// Get the default value for a given type.
5183 pub fn default_for ( ty : ValType ) -> Self {
5284 match ty {
5385 ValType :: I32 => Self :: I32 ( 0 ) ,
@@ -135,19 +167,8 @@ impl TryFrom<WasmValue> for f64 {
135167 }
136168}
137169
138- // impl TryFrom<WasmValue> for i128 {
139- // type Error = ();
140-
141- // fn try_from(value: WasmValue) -> Result<Self, Self::Error> {
142- // match value {
143- // WasmValue::V128(i) => Ok(i),
144- // _ => Err(()),
145- // }
146- // }
147- // }
148-
149170impl Debug for WasmValue {
150- fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std :: fmt:: Result {
171+ fn fmt ( & self , f : & mut alloc :: fmt:: Formatter < ' _ > ) -> alloc :: fmt:: Result {
151172 match self {
152173 WasmValue :: I32 ( i) => write ! ( f, "i32({})" , i) ,
153174 WasmValue :: I64 ( i) => write ! ( f, "i64({})" , i) ,
@@ -159,6 +180,7 @@ impl Debug for WasmValue {
159180}
160181
161182impl WasmValue {
183+ /// Get the type of a [`WasmValue`]
162184 pub fn val_type ( & self ) -> ValType {
163185 match self {
164186 Self :: I32 ( _) => ValType :: I32 ,
@@ -173,12 +195,19 @@ impl WasmValue {
173195/// Type of a WebAssembly value.
174196#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
175197pub enum ValType {
198+ /// A 32-bit integer.
176199 I32 ,
200+ /// A 64-bit integer.
177201 I64 ,
202+ /// A 32-bit float.
178203 F32 ,
204+ /// A 64-bit float.
179205 F64 ,
206+ /// A 128-bit vector.
180207 V128 ,
208+ /// A reference to a function.
181209 FuncRef ,
210+ /// A reference to an external value.
182211 ExternRef ,
183212}
184213
@@ -187,9 +216,13 @@ pub enum ValType {
187216/// See <https://webassembly.github.io/spec/core/syntax/types.html#external-types>
188217#[ derive( Debug , Clone , PartialEq ) ]
189218pub enum ExternalKind {
219+ /// A WebAssembly Function.
190220 Func ,
221+ /// A WebAssembly Table.
191222 Table ,
223+ /// A WebAssembly Memory.
192224 Memory ,
225+ /// A WebAssembly Global.
193226 Global ,
194227}
195228
@@ -212,15 +245,6 @@ pub type LocalAddr = Addr;
212245pub type LabelAddr = Addr ;
213246pub type ModuleInstanceAddr = Addr ;
214247
215- /// A WebAssembly Export Instance.
216- ///
217- /// See <https://webassembly.github.io/spec/core/exec/runtime.html#export-instances>
218- #[ derive( Debug ) ]
219- pub struct ExportInst {
220- pub name : String ,
221- pub value : ExternVal ,
222- }
223-
224248/// A WebAssembly External Value.
225249///
226250/// See <https://webassembly.github.io/spec/core/exec/runtime.html#external-values>
0 commit comments