Skip to content

Commit 525208e

Browse files
chore: improve documentation, tests
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 8077653 commit 525208e

File tree

18 files changed

+123
-51
lines changed

18 files changed

+123
-51
lines changed

.cargo/config.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
version-dev="workspaces version --no-git-commit --force tinywasm*"
33
dev="run -- -l debug run"
44

5-
test-mvp="test --package tinywasm --test test-mvp"
6-
test-wast="test --package tinywasm --test test-wast --"
7-
generate-charts="test --package tinywasm --test generate-charts"
5+
test-mvp="test --package tinywasm --test test-mvp --release -- --enable "
6+
test-wast="test --package tinywasm --test test-wast -- --enable "
7+
generate-charts="test --package tinywasm --test generate-charts -- --enable "

.github/workflows/test.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
rustup default stable
2121
2222
- name: Build (stable)
23-
run: cargo +stable build --verbose
23+
run: cargo +stable build --all
2424

2525
- name: Run tests (stable)
26-
run: cargo +stable test --verbose
26+
run: cargo +stable test --all
2727

2828
test-no-std:
2929
name: Test without default features on nightly Rust
@@ -37,7 +37,7 @@ jobs:
3737
rustup default nightly
3838
3939
- name: Build (nightly, no default features)
40-
run: cargo +nightly build --verbose --no-default-features
40+
run: cargo +nightly build --all --no-default-features
4141

4242
- name: Run tests (nightly, no default features)
43-
run: cargo +nightly test --verbose --no-default-features
43+
run: cargo +nightly test --all --no-default-features

Cargo.lock

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

crates/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ argh="0.1"
1919
color-eyre={version="0.6", default-features=false}
2020
log="0.4"
2121
pretty_env_logger="0.5"
22-
wast={version="69.0", optional=true}
22+
wast={version="70.0", optional=true}
2323

2424
[features]
2525
default=["wat"]

crates/parser/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ repository.workspace=true
99

1010
[dependencies]
1111
# fork of wasmparser with no_std support, see https://github.com/bytecodealliance/wasmtime/issues/3495
12-
# TODO: create dependency free parser
1312
wasmparser={version="0.100", package="wasmparser-nostd", default-features=false}
1413
log={version="0.4", optional=true}
1514
tinywasm-types={version="0.2.0-alpha.0", path="../types"}

crates/parser/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern crate alloc;
1010
#[allow(clippy::single_component_path_imports)]
1111
use log;
1212

13+
// noop fallback if logging is disabled.
1314
#[cfg(not(feature = "logging"))]
1415
mod log {
1516
macro_rules! debug ( ($($tt:tt)*) => {{}} );

crates/tinywasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tinywasm-types={version="0.2.0-alpha.0", path="../types", default-features=false
1919

2020
[dev-dependencies]
2121
wasm-testsuite={path="../wasm-testsuite"}
22-
wast={version="69.0"}
22+
wast={version="70.0"}
2323
owo-colors={version="4.0"}
2424
eyre={version="0.6"}
2525
serde_json={version="1.0"}

crates/tinywasm/src/func.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct FuncHandle {
1919
}
2020

2121
impl FuncHandle {
22-
/// Call a function
22+
/// Call a function (Invocation)
2323
///
2424
/// See <https://webassembly.github.io/spec/core/exec/modules.html#invocation>
2525
pub fn call(&self, store: &mut Store, params: &[WasmValue]) -> Result<Vec<WasmValue>> {
@@ -66,8 +66,14 @@ impl FuncHandle {
6666

6767
// Once the function returns:
6868
let result_m = func_ty.results.len();
69+
70+
// 1. Assert: m values are on the top of the stack (Ensured by validation)
71+
debug_assert!(stack.values.len() >= result_m);
72+
73+
// 2. Pop m values from the stack
6974
let res = stack.values.last_n(result_m)?;
7075

76+
// The values are returned as the results of the invocation.
7177
Ok(res
7278
.iter()
7379
.zip(func_ty.results.iter())

crates/tinywasm/src/instance.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,30 @@ impl ModuleInstance {
4242
}
4343

4444
/// Instantiate the module in the given store
45+
///
46+
/// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation>
4547
pub fn instantiate(store: &mut Store, module: Module, imports: Option<Imports>) -> Result<Self> {
48+
// This doesn't completely follow the steps in the spec, but the end result is the same
49+
// Constant expressions are evaluated directly where they are used, so we
50+
// don't need to create a auxiliary frame etc.
51+
4652
let idx = store.next_module_instance_idx();
4753
let imports = imports.unwrap_or_default();
54+
55+
// TODO: doesn't link other modules yet
4856
let linked_imports = imports.link(store, &module)?;
57+
let global_addrs = store.add_globals(module.data.globals.into(), &module.data.imports, &linked_imports, idx)?;
4958

59+
// TODO: imported functions missing
5060
let func_addrs = store.add_funcs(module.data.funcs.into(), idx);
61+
5162
let table_addrs = store.add_tables(module.data.table_types.into(), idx);
5263
let mem_addrs = store.add_mems(module.data.memory_types.into(), idx)?;
53-
let global_addrs = store.add_globals(module.data.globals.into(), &module.data.imports, &linked_imports, idx)?;
64+
65+
// TODO: active/declared elems need to be initialized
5466
let elem_addrs = store.add_elems(module.data.elements.into(), idx)?;
67+
68+
// TODO: active data segments need to be initialized
5569
let data_addrs = store.add_datas(module.data.data.into(), idx);
5670

5771
let instance = ModuleInstanceInner {
@@ -70,8 +84,10 @@ impl ModuleInstance {
7084
imports: module.data.imports,
7185
exports: crate::ExportInstance(module.data.exports),
7286
};
87+
7388
let instance = ModuleInstance::new(instance);
7489
store.add_instance(instance.clone())?;
90+
7591
Ok(instance)
7692
}
7793

@@ -176,13 +192,18 @@ impl ModuleInstance {
176192
}
177193
};
178194

179-
let func_addr = self.0.func_addrs[func_index as usize];
180-
let func = store.get_func(func_addr as usize)?;
195+
let func_addr = self
196+
.0
197+
.func_addrs
198+
.get(func_index as usize)
199+
.expect("No func addr for start func, this is a bug");
200+
201+
let func = store.get_func(*func_addr as usize)?;
181202
let ty = self.0.types[func.ty_addr() as usize].clone();
182203

183204
Ok(Some(FuncHandle {
184205
module: self.clone(),
185-
addr: func_addr,
206+
addr: *func_addr,
186207
ty,
187208
name: None,
188209
}))

crates/tinywasm/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//! // Get a typed handle to the exported "add" function
4444
//! // Alternatively, you can use `instance.get_func` to get an untyped handle
4545
//! // that takes and returns WasmValue types
46-
//! let func = instance.get_typed_func::<(i32, i32), (i32,)>(&mut store, "add")?;
46+
//! let func = instance.typed_func::<(i32, i32), (i32,)>(&mut store, "add")?;
4747
//! let res = func.call(&mut store, (1, 2))?;
4848
//!
4949
//! assert_eq!(res, (3,));
@@ -60,7 +60,7 @@
6060
//! a custom allocator. This removes support for parsing from files and streams,
6161
//! but otherwise the API is the same.
6262
//!
63-
//! Additionally, if you want proper error types, you must use a `nightly` compiler.
63+
//! Additionally, if you want proper error types, you must use a `nightly` compiler to have the error trait in core.
6464
6565
mod std;
6666
extern crate alloc;
@@ -70,6 +70,7 @@ extern crate alloc;
7070
#[allow(clippy::single_component_path_imports)]
7171
use log;
7272

73+
// noop fallback if logging is disabled.
7374
#[cfg(not(feature = "logging"))]
7475
pub(crate) mod log {
7576
macro_rules! debug ( ($($tt:tt)*) => {{}} );

0 commit comments

Comments
 (0)