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

Commit 9f82dd9

Browse files
chore: improve global testing
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent 34dedd5 commit 9f82dd9

File tree

8 files changed

+92
-22
lines changed

8 files changed

+92
-22
lines changed

crates/tinywasm/src/instance.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ impl ModuleInstance {
4242
let func_addrs = store.add_funcs(module.data.funcs.into(), idx);
4343
let table_addrs = store.add_tables(module.data.table_types.into(), idx);
4444
let mem_addrs = store.add_mems(module.data.memory_types.into(), idx);
45-
let global_addrs = store.add_globals(module.data.globals.into(), idx);
45+
46+
let global_addrs = store.add_globals(module.data.globals.into(), &module.data.imports, idx);
4647
let elem_addrs = store.add_elems(module.data.elements.into(), idx);
4748
let data_addrs = store.add_datas(module.data.data.into(), idx);
4849

crates/tinywasm/src/module.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ impl Module {
5959
// imports: Option<()>,
6060
) -> Result<ModuleInstance> {
6161
let instance = ModuleInstance::instantiate(store, self)?;
62-
let _ = instance.start(store)?;
62+
63+
// TODO: this currently panics if theres no start fn
64+
// let _ = instance.start(store)?;
6365
Ok(instance)
6466
}
6567
}

crates/tinywasm/src/store.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::{
55

66
use alloc::{format, rc::Rc, vec::Vec};
77
use tinywasm_types::{
8-
Addr, Data, Element, ElementKind, FuncAddr, Function, Global, GlobalType, Instruction, MemAddr, MemoryType,
8+
Addr, Data, Element, ElementKind, FuncAddr, Function, Global, GlobalType, Import, Instruction, MemAddr, MemoryType,
99
ModuleInstanceAddr, TableAddr, TableType, TypeAddr, ValType,
1010
};
1111

@@ -141,11 +141,39 @@ impl Store {
141141
}
142142

143143
/// Add globals to the store, returning their addresses in the store
144-
pub(crate) fn add_globals(&mut self, globals: Vec<Global>, idx: ModuleInstanceAddr) -> Vec<Addr> {
144+
pub(crate) fn add_globals(
145+
&mut self,
146+
globals: Vec<Global>,
147+
imports: &[Import],
148+
idx: ModuleInstanceAddr,
149+
) -> Vec<Addr> {
145150
let global_count = self.data.globals.len();
146151
let mut global_addrs = Vec::with_capacity(global_count);
152+
153+
// TODO: initialize imported globals
154+
let imported_globals = imports
155+
.iter()
156+
.filter_map(|import| match &import.kind {
157+
tinywasm_types::ImportKind::Global(t) => Some(t),
158+
_ => None,
159+
})
160+
.collect::<Vec<_>>();
161+
162+
for (i, global) in imported_globals.into_iter().enumerate() {
163+
log::debug!("imported global: {:?}", global);
164+
self.data.globals.push(Rc::new(RefCell::new(GlobalInstance::new(
165+
global.clone(),
166+
global.ty.default_value().into(),
167+
idx,
168+
))));
169+
global_addrs.push((i + global_count) as Addr);
170+
}
171+
172+
let global_count = self.data.globals.len();
173+
log::debug!("globals: {:?}", globals);
147174
for (i, global) in globals.into_iter().enumerate() {
148175
// TODO: initialize globals
176+
149177
use tinywasm_types::ConstInstruction::*;
150178
let val = match global.init {
151179
F32Const(f) => RawWasmValue::from(f),
@@ -168,6 +196,7 @@ impl Store {
168196

169197
global_addrs.push((i + global_count) as Addr);
170198
}
199+
log::debug!("global_addrs: {:?}", global_addrs);
171200
global_addrs
172201
}
173202

crates/tinywasm/tests/generated/mvp.csv

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

crates/tinywasm/tests/generated/progress-mvp.svg

Lines changed: 3 additions & 3 deletions
Loading

crates/tinywasm/tests/testsuite/run.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use crate::testsuite::util::*;
2-
use std::borrow::Cow;
2+
use std::{
3+
borrow::Cow,
4+
panic::{catch_unwind, AssertUnwindSafe},
5+
};
36

47
use super::TestSuite;
58
use eyre::{eyre, Result};
69
use log::{debug, error, info};
10+
use tinywasm::ModuleInstance;
711
use tinywasm_types::TinyWasmModule;
812
use wast::{lexer::Lexer, parser::ParseBuffer, Wast};
913

@@ -45,20 +49,33 @@ impl TestSuite {
4549
let buf = ParseBuffer::new_with_lexer(lexer).expect("failed to create parse buffer");
4650
let wast_data = wast::parser::parse::<Wast>(&buf).expect("failed to parse wat");
4751

48-
let mut last_module: Option<TinyWasmModule> = None;
52+
let mut store = tinywasm::Store::default();
53+
let mut last_module: Option<ModuleInstance> = None;
54+
4955
println!("running {} tests for group: {}", wast_data.directives.len(), group_name);
5056
for (i, directive) in wast_data.directives.into_iter().enumerate() {
5157
let span = directive.span();
5258
use wast::WastDirective::*;
53-
// let name = format!("{}-{}", group_name, i);
5459

5560
match directive {
5661
Wat(mut module) => {
5762
debug!("got wat module");
5863

59-
let result = catch_unwind_silent(move || parse_module_bytes(&module.encode().unwrap()))
60-
.map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
61-
.and_then(|res| res);
64+
// TODO: Reusing store beaks some things
65+
store = tinywasm::Store::default();
66+
let result = catch_unwind(AssertUnwindSafe(|| {
67+
let m = parse_module_bytes(&module.encode().expect("failed to encode module"))
68+
.expect("failed to parse module");
69+
tinywasm::Module::from(m)
70+
.instantiate(&mut store)
71+
.map_err(|e| {
72+
println!("failed to instantiate module: {:?}", e);
73+
e
74+
})
75+
.expect("failed to instantiate module")
76+
}))
77+
.map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
78+
.and_then(|res| Ok(res));
6279

6380
match &result {
6481
Err(_) => last_module = None,
@@ -132,7 +149,7 @@ impl TestSuite {
132149
.collect::<Result<Vec<_>>>()
133150
.expect("failed to convert args");
134151

135-
exec_fn(module, name, &args).map(|_| ())
152+
exec_fn_instance(module, &mut store, name, &args).map(|_| ())
136153
});
137154

138155
match res {
@@ -189,10 +206,11 @@ impl TestSuite {
189206
e
190207
})?;
191208

192-
let outcomes = exec_fn(last_module.as_ref(), invoke.name, &args).map_err(|e| {
193-
error!("failed to execute function: {:?}", e);
194-
e
195-
})?;
209+
let outcomes =
210+
exec_fn_instance(last_module.as_ref(), &mut store, invoke.name, &args).map_err(|e| {
211+
error!("failed to execute function: {:?}", e);
212+
e
213+
})?;
196214

197215
debug!("outcomes: {:?}", outcomes);
198216

crates/tinywasm/tests/testsuite/util.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::panic;
1+
use std::panic::{self, AssertUnwindSafe};
22

33
use eyre::{eyre, Result};
44
use tinywasm_types::{TinyWasmModule, WasmValue};
@@ -19,6 +19,20 @@ pub fn try_downcast_panic(panic: Box<dyn std::any::Any + Send>) -> String {
1919
)
2020
}
2121

22+
pub fn exec_fn_instance(
23+
instance: Option<&tinywasm::ModuleInstance>,
24+
store: &mut tinywasm::Store,
25+
name: &str,
26+
args: &[tinywasm_types::WasmValue],
27+
) -> Result<Vec<tinywasm_types::WasmValue>, tinywasm::Error> {
28+
let Some(instance) = instance else {
29+
return Err(tinywasm::Error::Other("no instance found".to_string()));
30+
};
31+
32+
let func = instance.exported_func_by_name(store, name)?;
33+
func.call(store, args)
34+
}
35+
2236
pub fn exec_fn(
2337
module: Option<&TinyWasmModule>,
2438
name: &str,
@@ -34,10 +48,10 @@ pub fn exec_fn(
3448
instance.exported_func_by_name(&store, name)?.call(&mut store, args)
3549
}
3650

37-
pub fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> std::thread::Result<R> {
51+
pub fn catch_unwind_silent<F: FnOnce() -> R, R>(f: F) -> std::thread::Result<R> {
3852
let prev_hook = panic::take_hook();
3953
panic::set_hook(Box::new(|_| {}));
40-
let result = panic::catch_unwind(f);
54+
let result = panic::catch_unwind(AssertUnwindSafe(|| f()));
4155
panic::set_hook(prev_hook);
4256
result
4357
}

crates/types/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ pub enum ValType {
245245
ExternRef,
246246
}
247247

248+
impl ValType {
249+
pub fn default_value(&self) -> WasmValue {
250+
WasmValue::default_for(*self)
251+
}
252+
}
253+
248254
/// A WebAssembly External Kind.
249255
///
250256
/// See <https://webassembly.github.io/spec/core/syntax/types.html#external-types>

0 commit comments

Comments
 (0)