Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e71f86

Browse files
chore: refactor module instances, fix named module tests
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 4c1386b commit 3e71f86

File tree

6 files changed

+53
-39
lines changed

6 files changed

+53
-39
lines changed

crates/tinywasm/src/instance.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{boxed::Box, format, string::ToString, sync::Arc, vec::Vec};
1+
use alloc::{boxed::Box, format, string::ToString, sync::Arc};
22
use tinywasm_types::{
33
DataAddr, ElemAddr, Export, ExternVal, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr,
44
ModuleInstanceAddr, TableAddr,
@@ -11,11 +11,11 @@ use crate::{
1111

1212
/// A WebAssembly Module Instance
1313
///
14-
/// Addrs are indices into the store's data structures.
14+
/// Backed by an Arc, so cloning is cheap
1515
///
1616
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
1717
#[derive(Debug, Clone)]
18-
pub struct ModuleInstance(pub(crate) Arc<ModuleInstanceInner>);
18+
pub struct ModuleInstance(Arc<ModuleInstanceInner>);
1919

2020
#[allow(dead_code)]
2121
#[derive(Debug)]
@@ -25,19 +25,24 @@ pub(crate) struct ModuleInstanceInner {
2525

2626
pub(crate) types: Box<[FuncType]>,
2727

28-
pub(crate) func_addrs: Vec<FuncAddr>,
29-
pub(crate) table_addrs: Vec<TableAddr>,
30-
pub(crate) mem_addrs: Vec<MemAddr>,
31-
pub(crate) global_addrs: Vec<GlobalAddr>,
32-
pub(crate) elem_addrs: Vec<ElemAddr>,
33-
pub(crate) data_addrs: Vec<DataAddr>,
28+
pub(crate) func_addrs: Box<[FuncAddr]>,
29+
pub(crate) table_addrs: Box<[TableAddr]>,
30+
pub(crate) mem_addrs: Box<[MemAddr]>,
31+
pub(crate) global_addrs: Box<[GlobalAddr]>,
32+
pub(crate) elem_addrs: Box<[ElemAddr]>,
33+
pub(crate) data_addrs: Box<[DataAddr]>,
3434

3535
pub(crate) func_start: Option<FuncAddr>,
3636
pub(crate) imports: Box<[Import]>,
3737
pub(crate) exports: Box<[Export]>,
3838
}
3939

4040
impl ModuleInstance {
41+
// drop the module instance reference and swap it with another one
42+
pub(crate) fn swap(&mut self, other: Self) {
43+
self.0 = other.0;
44+
}
45+
4146
/// Get the module instance's address
4247
pub fn id(&self) -> ModuleInstanceAddr {
4348
self.0.idx
@@ -57,6 +62,7 @@ impl ModuleInstance {
5762
let mut addrs = imports.link(store, &module, idx)?;
5863
let data = module.data;
5964

65+
// TODO: check if the compiler correctly optimizes this to prevent wasted allocations
6066
addrs.globals.extend(store.init_globals(data.globals.into(), idx)?);
6167
addrs.funcs.extend(store.init_funcs(data.funcs.into(), idx)?);
6268
addrs.tables.extend(store.init_tables(data.table_types.into(), idx)?);
@@ -70,12 +76,12 @@ impl ModuleInstance {
7076
idx,
7177

7278
types: data.func_types,
73-
func_addrs: addrs.funcs,
74-
table_addrs: addrs.tables,
75-
mem_addrs: addrs.mems,
76-
global_addrs: addrs.globals,
79+
func_addrs: addrs.funcs.into_boxed_slice(),
80+
table_addrs: addrs.tables.into_boxed_slice(),
81+
mem_addrs: addrs.mems.into_boxed_slice(),
82+
global_addrs: addrs.globals.into_boxed_slice(),
7783
elem_addrs,
78-
data_addrs,
84+
data_addrs: data_addrs,
7985
func_start: data.start_func,
8086
imports: data.imports,
8187
exports: data.exports,

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ impl DefaultRuntime {
3030

3131
let mut current_module = module;
3232

33-
// TODO: we might be able to index into the instructions directly
34-
// since the instruction pointer should always be in bounds
3533
while let Some(instr) = instrs.get(cf.instr_ptr) {
3634
match exec_one(&mut cf, instr, instrs, stack, store, &current_module)? {
3735
// Continue execution at the new top of the call stack
@@ -42,7 +40,7 @@ impl DefaultRuntime {
4240
instrs = &wasm_func.instructions;
4341

4442
if cf.module != current_module.id() {
45-
current_module = store.get_module_instance(cf.module).unwrap().clone()
43+
current_module.swap(store.get_module_instance(cf.module).unwrap().clone());
4644
}
4745

4846
continue;
@@ -168,6 +166,8 @@ fn exec_one(
168166
let table_idx = module.resolve_table_addr(*table_addr);
169167
let table = store.get_table(table_idx as usize)?;
170168

169+
// TODO: currently, the type resolution is subtlely broken for imported functions
170+
171171
let call_ty = module.func_ty(*type_addr);
172172

173173
let func_idx = stack.values.pop_t::<u32>()?;

crates/tinywasm/src/store.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::{
33
sync::atomic::{AtomicUsize, Ordering},
44
};
55

6-
use alloc::{format, rc::Rc, string::ToString, vec, vec::Vec};
6+
use alloc::{boxed::Box, format, rc::Rc, string::ToString, vec, vec::Vec};
77
use tinywasm_types::*;
88

99
use crate::{
@@ -180,7 +180,7 @@ impl Store {
180180
table_addrs: &[TableAddr],
181181
elems: Vec<Element>,
182182
idx: ModuleInstanceAddr,
183-
) -> Result<Vec<Addr>> {
183+
) -> Result<Box<[Addr]>> {
184184
let elem_count = self.data.elems.len();
185185
let mut elem_addrs = Vec::with_capacity(elem_count);
186186
for (i, elem) in elems.into_iter().enumerate() {
@@ -232,7 +232,8 @@ impl Store {
232232
elem_addrs.push((i + elem_count) as Addr);
233233
}
234234

235-
Ok(elem_addrs)
235+
// this should be optimized out by the compiler
236+
Ok(elem_addrs.into_boxed_slice())
236237
}
237238

238239
/// Add data to the store, returning their addresses in the store
@@ -241,7 +242,7 @@ impl Store {
241242
mem_addrs: &[MemAddr],
242243
datas: Vec<Data>,
243244
idx: ModuleInstanceAddr,
244-
) -> Result<Vec<Addr>> {
245+
) -> Result<Box<[Addr]>> {
245246
let data_count = self.data.datas.len();
246247
let mut data_addrs = Vec::with_capacity(data_count);
247248
for (i, data) in datas.into_iter().enumerate() {
@@ -276,7 +277,9 @@ impl Store {
276277
self.data.datas.push(DataInstance::new(data.data.to_vec(), idx));
277278
data_addrs.push((i + data_count) as Addr);
278279
}
279-
Ok(data_addrs)
280+
281+
// this should be optimized out by the compiler
282+
Ok(data_addrs.into_boxed_slice())
280283
}
281284

282285
pub(crate) fn add_global(&mut self, ty: GlobalType, value: RawWasmValue, idx: ModuleInstanceAddr) -> Result<Addr> {

0 commit comments

Comments
 (0)