Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 405564c

Browse files
refactor: module instantiation
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent 489abcd commit 405564c

File tree

5 files changed

+103
-89
lines changed

5 files changed

+103
-89
lines changed

Cargo.lock

Lines changed: 44 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tinywasm/src/instance.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec};
22
use tinywasm_types::{
3-
DataAddr, ElmAddr, Export, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr,
4-
TableAddr,
3+
DataAddr, ElmAddr, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, TableAddr,
54
};
65

76
use crate::{
87
func::{FromWasmValueTuple, IntoWasmValueTuple},
9-
Error, ExportInstance, FuncHandle, Result, Store, TypedFuncHandle,
8+
Error, ExportInstance, FuncHandle, Module, Result, Store, TypedFuncHandle,
109
};
1110

1211
/// A WebAssembly Module Instance
@@ -36,6 +35,38 @@ pub(crate) struct ModuleInstanceInner {
3635
}
3736

3837
impl ModuleInstance {
38+
/// Instantiate the module in the given store
39+
pub fn instantiate(store: &mut Store, module: Module) -> Result<Self> {
40+
let idx = store.next_module_instance_idx();
41+
42+
let func_addrs = store.add_funcs(module.data.funcs.into(), idx);
43+
let table_addrs = store.add_tables(module.data.table_types.into(), idx);
44+
let mem_addrs = store.add_mems(module.data.memory_types.into(), idx);
45+
let global_addrs = store.add_globals(module.data.globals.into(), idx);
46+
let elem_addrs = store.add_elems(module.data.elements.into(), idx);
47+
let data_addrs = store.add_datas(module.data.data.into(), idx);
48+
49+
let instance = ModuleInstanceInner {
50+
store_id: store.id(),
51+
idx,
52+
53+
types: module.data.func_types,
54+
func_addrs,
55+
table_addrs,
56+
mem_addrs,
57+
global_addrs,
58+
elem_addrs,
59+
data_addrs,
60+
61+
func_start: module.data.start_func,
62+
imports: module.data.imports,
63+
exports: crate::ExportInstance(module.data.exports),
64+
};
65+
let instance = ModuleInstance::new(instance);
66+
store.add_instance(instance.clone())?;
67+
Ok(instance)
68+
}
69+
3970
/// Get the module's exports
4071
pub fn exports(&self) -> &ExportInstance {
4172
&self.0.exports
@@ -101,7 +132,7 @@ impl ModuleInstance {
101132
/// (which is not part of the spec, but used by llvm)
102133
///
103134
/// See <https://webassembly.github.io/spec/core/syntax/modules.html#start-function>
104-
pub fn start_func(&mut self, store: &Store) -> Result<Option<FuncHandle>> {
135+
pub fn start_func(&self, store: &Store) -> Result<Option<FuncHandle>> {
105136
if self.0.store_id != store.id() {
106137
return Err(Error::InvalidStore);
107138
}
@@ -135,7 +166,7 @@ impl ModuleInstance {
135166
/// Returns None if the module has no start function
136167
///
137168
/// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start>
138-
pub fn start(&mut self, store: &mut Store) -> Result<Option<()>> {
169+
pub fn start(&self, store: &mut Store) -> Result<Option<()>> {
139170
let Some(func) = self.start_func(store)? else {
140171
return Ok(None);
141172
};

crates/tinywasm/src/module.rs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use alloc::vec::Vec;
21
use tinywasm_types::TinyWasmModule;
32

4-
use crate::{instance::ModuleInstanceInner, ModuleInstance, Result, Store};
3+
use crate::{ModuleInstance, Result, Store};
54

65
#[derive(Debug)]
76
/// A WebAssembly Module
87
///
98
/// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-module>
109
pub struct Module {
11-
data: TinyWasmModule,
10+
pub(crate) data: TinyWasmModule,
1211
}
1312

1413
impl From<&TinyWasmModule> for Module {
@@ -50,46 +49,17 @@ impl Module {
5049

5150
/// Instantiate the module in the given store
5251
///
53-
// TODO: /// Runs the start function if it exists
54-
// /// If you want to run the start function yourself, use `ModuleInstance::new`
52+
/// Runs the start function if it exists
53+
/// If you want to run the start function yourself, use `ModuleInstance::instantiate`
5554
///
5655
/// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation>
5756
pub fn instantiate(
5857
self,
5958
store: &mut Store,
6059
// imports: Option<()>,
6160
) -> Result<ModuleInstance> {
62-
let idx = store.next_module_instance_idx();
63-
64-
let func_addrs = store.add_funcs(self.data.funcs.into(), idx);
65-
let table_addrs = store.add_tables(self.data.table_types.into(), idx);
66-
let mem_addrs = store.add_mems(self.data.memory_types.into(), idx);
67-
let global_addrs = store.add_globals(self.data.globals.into(), idx);
68-
let elem_addrs = store.add_elems(self.data.elements.into(), idx);
69-
let data_addrs = store.add_datas(self.data.data.into(), idx);
70-
71-
let instance = ModuleInstanceInner {
72-
store_id: store.id(),
73-
idx,
74-
75-
types: self.data.func_types,
76-
func_addrs,
77-
table_addrs,
78-
mem_addrs,
79-
global_addrs,
80-
elem_addrs,
81-
data_addrs,
82-
83-
func_start: self.data.start_func,
84-
imports: self.data.imports,
85-
exports: crate::ExportInstance(self.data.exports),
86-
};
87-
88-
let instance = ModuleInstance::new(instance);
89-
store.add_instance(instance.clone())?;
90-
91-
// TODO: Auto-run start function?
92-
// let _ = instance.start(store)?;
61+
let instance = ModuleInstance::instantiate(store, self)?;
62+
let _ = instance.start(store)?;
9363
Ok(instance)
9464
}
9565
}

0 commit comments

Comments
 (0)