diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/tinywasm/src/instance.rs | 41 | ||||
| -rw-r--r-- | crates/tinywasm/src/module.rs | 42 | ||||
| -rw-r--r-- | crates/tinywasm/src/runtime/executor/mod.rs | 17 | ||||
| -rw-r--r-- | crates/tinywasm/tests/generated/progress-mvp.svg | 4 |
4 files changed, 59 insertions, 45 deletions
diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs index 86a9296..a584571 100644 --- a/crates/tinywasm/src/instance.rs +++ b/crates/tinywasm/src/instance.rs @@ -1,12 +1,11 @@ use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec}; use tinywasm_types::{ - DataAddr, ElmAddr, Export, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, - TableAddr, + DataAddr, ElmAddr, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, TableAddr, }; use crate::{ func::{FromWasmValueTuple, IntoWasmValueTuple}, - Error, ExportInstance, FuncHandle, Result, Store, TypedFuncHandle, + Error, ExportInstance, FuncHandle, Module, Result, Store, TypedFuncHandle, }; /// A WebAssembly Module Instance @@ -36,6 +35,38 @@ pub(crate) struct ModuleInstanceInner { } impl ModuleInstance { + /// Instantiate the module in the given store + pub fn instantiate(store: &mut Store, module: Module) -> Result<Self> { + let idx = store.next_module_instance_idx(); + + let func_addrs = store.add_funcs(module.data.funcs.into(), idx); + let table_addrs = store.add_tables(module.data.table_types.into(), idx); + let mem_addrs = store.add_mems(module.data.memory_types.into(), idx); + let global_addrs = store.add_globals(module.data.globals.into(), idx); + let elem_addrs = store.add_elems(module.data.elements.into(), idx); + let data_addrs = store.add_datas(module.data.data.into(), idx); + + let instance = ModuleInstanceInner { + store_id: store.id(), + idx, + + types: module.data.func_types, + func_addrs, + table_addrs, + mem_addrs, + global_addrs, + elem_addrs, + data_addrs, + + func_start: module.data.start_func, + imports: module.data.imports, + exports: crate::ExportInstance(module.data.exports), + }; + let instance = ModuleInstance::new(instance); + store.add_instance(instance.clone())?; + Ok(instance) + } + /// Get the module's exports pub fn exports(&self) -> &ExportInstance { &self.0.exports @@ -101,7 +132,7 @@ impl ModuleInstance { /// (which is not part of the spec, but used by llvm) /// /// See <https://webassembly.github.io/spec/core/syntax/modules.html#start-function> - pub fn start_func(&mut self, store: &Store) -> Result<Option<FuncHandle>> { + pub fn start_func(&self, store: &Store) -> Result<Option<FuncHandle>> { if self.0.store_id != store.id() { return Err(Error::InvalidStore); } @@ -135,7 +166,7 @@ impl ModuleInstance { /// Returns None if the module has no start function /// /// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start> - pub fn start(&mut self, store: &mut Store) -> Result<Option<()>> { + pub fn start(&self, store: &mut Store) -> Result<Option<()>> { let Some(func) = self.start_func(store)? else { return Ok(None); }; diff --git a/crates/tinywasm/src/module.rs b/crates/tinywasm/src/module.rs index e26db90..51d9f33 100644 --- a/crates/tinywasm/src/module.rs +++ b/crates/tinywasm/src/module.rs @@ -1,14 +1,13 @@ -use alloc::vec::Vec; use tinywasm_types::TinyWasmModule; -use crate::{instance::ModuleInstanceInner, ModuleInstance, Result, Store}; +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, + pub(crate) data: TinyWasmModule, } impl From<&TinyWasmModule> for Module { @@ -50,8 +49,8 @@ impl Module { /// Instantiate the module in the given store /// - // TODO: /// Runs the start function if it exists - // /// If you want to run the start function yourself, use `ModuleInstance::new` + /// Runs the start function if it exists + /// If you want to run the start function yourself, use `ModuleInstance::instantiate` /// /// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation> pub fn instantiate( @@ -59,37 +58,8 @@ impl Module { store: &mut Store, // imports: Option<()>, ) -> Result<ModuleInstance> { - let idx = store.next_module_instance_idx(); - - let func_addrs = store.add_funcs(self.data.funcs.into(), idx); - let table_addrs = store.add_tables(self.data.table_types.into(), idx); - let mem_addrs = store.add_mems(self.data.memory_types.into(), idx); - let global_addrs = store.add_globals(self.data.globals.into(), idx); - let elem_addrs = store.add_elems(self.data.elements.into(), idx); - let data_addrs = store.add_datas(self.data.data.into(), idx); - - let instance = ModuleInstanceInner { - store_id: store.id(), - idx, - - types: self.data.func_types, - func_addrs, - table_addrs, - mem_addrs, - global_addrs, - elem_addrs, - data_addrs, - - func_start: self.data.start_func, - imports: self.data.imports, - exports: crate::ExportInstance(self.data.exports), - }; - - let instance = ModuleInstance::new(instance); - store.add_instance(instance.clone())?; - - // TODO: Auto-run start function? - // let _ = instance.start(store)?; + let instance = ModuleInstance::instantiate(store, self)?; + let _ = instance.start(store)?; Ok(instance) } } diff --git a/crates/tinywasm/src/runtime/executor/mod.rs b/crates/tinywasm/src/runtime/executor/mod.rs index ca404fa..8ae703e 100644 --- a/crates/tinywasm/src/runtime/executor/mod.rs +++ b/crates/tinywasm/src/runtime/executor/mod.rs @@ -5,11 +5,11 @@ use crate::{ get_label_args, log::debug, runtime::{BlockType, LabelFrame}, - CallFrame, Error, ModuleInstance, Result, Store, + CallFrame, Error, ModuleInstance, RawWasmValue, Result, Store, }; use alloc::vec::Vec; use log::info; -use tinywasm_types::Instruction; +use tinywasm_types::{ConstInstruction, Instruction}; mod macros; mod traits; @@ -75,6 +75,19 @@ enum ExecResult { Trap(crate::Trap), } +/// Execute a const instruction +pub(crate) fn exec_const(instr: ConstInstruction) -> RawWasmValue { + match instr { + ConstInstruction::F32Const(val) => val.into(), + ConstInstruction::F64Const(val) => val.into(), + ConstInstruction::I32Const(val) => val.into(), + ConstInstruction::I64Const(val) => val.into(), + ConstInstruction::GlobalGet(_) => unimplemented!("global get"), + ConstInstruction::RefFunc(_) => unimplemented!("ref func"), + ConstInstruction::RefNull(_) => unimplemented!("ref null"), + } +} + /// Run a single step of the interpreter /// A seperate function is used so later, we can more easily implement /// a step-by-step debugger (using generators once they're stable?) diff --git a/crates/tinywasm/tests/generated/progress-mvp.svg b/crates/tinywasm/tests/generated/progress-mvp.svg index ec72fb3..0feea61 100644 --- a/crates/tinywasm/tests/generated/progress-mvp.svg +++ b/crates/tinywasm/tests/generated/progress-mvp.svg @@ -56,9 +56,9 @@ v0.1.0 (17630) v0.2.0-alpha.0 (17747) </text> <polyline fill="none" opacity="1" stroke="#000000" stroke-width="1" points="898,345 898,350 "/> -<rect x="448" y="185" width="172" height="159" opacity="0.5" fill="#0000FF" stroke="none"/> <rect x="266" y="212" width="172" height="132" opacity="0.5" fill="#0000FF" stroke="none"/> -<rect x="85" y="212" width="171" height="132" opacity="0.5" fill="#0000FF" stroke="none"/> <rect x="630" y="92" width="172" height="252" opacity="0.5" fill="#0000FF" stroke="none"/> <rect x="812" y="90" width="172" height="254" opacity="0.5" fill="#0000FF" stroke="none"/> +<rect x="85" y="212" width="171" height="132" opacity="0.5" fill="#0000FF" stroke="none"/> +<rect x="448" y="185" width="172" height="159" opacity="0.5" fill="#0000FF" stroke="none"/> </svg> |
