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

Commit d8d439e

Browse files
feat: add more examples, work on new public api
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent 67f0fd6 commit d8d439e

File tree

23 files changed

+138
-45
lines changed

23 files changed

+138
-45
lines changed

.cargo/config.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[alias]
22
version-dev="workspaces version --no-git-commit --force tinywasm*"
33
dev="run -- -l debug run"
4-
54
test-mvp="test --package tinywasm --test test-mvp --release -- --enable "
65
test-2="test --package tinywasm --test test-two --release -- --enable "
76
test-wast="test --package tinywasm --test test-wast -- --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
run: rustup update stable
2121

2222
- name: Build (stable)
23-
run: cargo +stable build --workspace --exclude wasm-testsuite
23+
run: cargo +stable build --workspace
2424

2525
- name: Run tests (stable)
26-
run: cargo +stable test --workspace --exclude wasm-testsuite
26+
run: cargo +stable test --workspace
2727

2828
- name: Run MVP testsuite
2929
run: cargo +stable test-mvp
@@ -41,10 +41,10 @@ jobs:
4141
run: rustup update nightly
4242

4343
- name: Build (nightly, no default features)
44-
run: cargo +nightly build --workspace --exclude wasm-testsuite --exclude rust-wasm-examples --no-default-features
44+
run: cargo +nightly build --workspace --no-default-features
4545

4646
- name: Run tests (nightly, no default features)
47-
run: cargo +nightly test --workspace --exclude wasm-testsuite --exclude rust-wasm-examples --no-default-features
47+
run: cargo +nightly test --workspace --no-default-features
4848

4949
- name: Run MVP testsuite (nightly)
5050
run: cargo +nightly test-mvp

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/target
22
notes.md
33
examples/rust/out/*
4+
examples/rust/target
5+
examples/rust/Cargo.lock
46
examples/wast/*

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"search.exclude": {
33
"**/wasm-testsuite/data": true
4-
}
4+
},
5+
"rust-analyzer.linkedProjects": [
6+
"./Cargo.toml",
7+
"./examples/rust/Cargo.toml"
8+
]
59
}

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members=["crates/*", "examples/rust"]
2+
members=["crates/*"]
33
resolver="2"
44

55
[profile.wasm]
@@ -21,6 +21,10 @@ name="tinywasm-root"
2121
publish=false
2222
edition="2021"
2323

24+
[[example]]
25+
name="wasm-rust"
26+
test=false
27+
2428
[dev-dependencies]
2529
color-eyre="0.6"
2630
tinywasm={path="crates/tinywasm"}

crates/cli/src/bin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn run(module: Module, func: Option<String>, args: Vec<WasmValue>) -> Result<()>
112112
let instance = module.instantiate(&mut store, None)?;
113113

114114
if let Some(func) = func {
115-
let func = instance.exported_func_by_name(&store, &func)?;
115+
let func = instance.exported_func_untyped(&store, &func)?;
116116
let res = func.call(&mut store, &args)?;
117117
info!("{res:?}");
118118
}

crates/tinywasm/src/export.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/tinywasm/src/imports.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ impl FuncContext<'_> {
7777
pub fn module(&self) -> &crate::ModuleInstance {
7878
self.module
7979
}
80+
81+
/// Get a reference to an exported memory
82+
pub fn memory(&mut self, name: &str) -> Result<crate::MemoryRef> {
83+
self.module.exported_memory(self.store, name)
84+
}
8085
}
8186

8287
impl Debug for HostFunction {
@@ -276,7 +281,7 @@ impl Imports {
276281

277282
if let Some(addr) = self.modules.get(&name.module) {
278283
let instance = store.get_module_instance(*addr)?;
279-
return Some(ResolvedExtern::Store(instance.export(&import.name)?));
284+
return Some(ResolvedExtern::Store(instance.export_addr(&import.name)?));
280285
}
281286

282287
None
@@ -398,7 +403,7 @@ impl Imports {
398403
Self::compare_table_types(import, &table.borrow().kind, ty)?;
399404
imports.tables.push(table_addr);
400405
}
401-
(ExternVal::Mem(memory_addr), ImportKind::Memory(ty)) => {
406+
(ExternVal::Memory(memory_addr), ImportKind::Memory(ty)) => {
402407
let mem = store.get_mem(memory_addr as usize)?;
403408
let (size, kind) = {
404409
let mem = mem.borrow();

crates/tinywasm/src/instance.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use alloc::{boxed::Box, format, string::ToString, sync::Arc};
2-
use tinywasm_types::{
3-
DataAddr, ElemAddr, Export, ExternVal, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr,
4-
ModuleInstanceAddr, TableAddr,
5-
};
2+
use tinywasm_types::*;
63

74
use crate::{
85
func::{FromWasmValueTuple, IntoWasmValueTuple},
9-
log, Error, FuncHandle, FuncHandleTyped, Imports, Module, Result, Store,
6+
log, Error, FuncHandle, FuncHandleTyped, Imports, MemoryRef, Module, Result, Store,
107
};
118

129
/// An instanciated WebAssembly module
@@ -106,7 +103,7 @@ impl ModuleInstance {
106103
}
107104

108105
/// Get a export by name
109-
pub fn export(&self, name: &str) -> Option<ExternVal> {
106+
pub fn export_addr(&self, name: &str) -> Option<ExternVal> {
110107
let exports = self.0.exports.iter().find(|e| e.name == name.into())?;
111108
let kind = exports.kind.clone();
112109
let addr = match kind {
@@ -162,12 +159,12 @@ impl ModuleInstance {
162159
}
163160

164161
/// Get an exported function by name
165-
pub fn exported_func_by_name(&self, store: &Store, name: &str) -> Result<FuncHandle> {
162+
pub fn exported_func_untyped(&self, store: &Store, name: &str) -> Result<FuncHandle> {
166163
if self.0.store_id != store.id() {
167164
return Err(Error::InvalidStore);
168165
}
169166

170-
let export = self.export(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?;
167+
let export = self.export_addr(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?;
171168
let ExternVal::Func(func_addr) = export else {
172169
return Err(Error::Other(format!("Export is not a function: {}", name)));
173170
};
@@ -179,15 +176,32 @@ impl ModuleInstance {
179176
}
180177

181178
/// Get a typed exported function by name
182-
pub fn typed_func<P, R>(&self, store: &Store, name: &str) -> Result<FuncHandleTyped<P, R>>
179+
pub fn exported_func<P, R>(&self, store: &Store, name: &str) -> Result<FuncHandleTyped<P, R>>
183180
where
184181
P: IntoWasmValueTuple,
185182
R: FromWasmValueTuple,
186183
{
187-
let func = self.exported_func_by_name(store, name)?;
184+
let func = self.exported_func_untyped(store, name)?;
188185
Ok(FuncHandleTyped { func, marker: core::marker::PhantomData })
189186
}
190187

188+
/// Get an exported memory by name
189+
pub fn exported_memory(&self, store: &mut Store, name: &str) -> Result<MemoryRef> {
190+
let export = self.export_addr(name).ok_or_else(|| Error::Other(format!("Export not found: {}", name)))?;
191+
let ExternVal::Memory(mem_addr) = export else {
192+
return Err(Error::Other(format!("Export is not a memory: {}", name)));
193+
};
194+
let mem = self.memory(store, mem_addr)?;
195+
Ok(mem)
196+
}
197+
198+
/// Get a memory by address
199+
pub fn memory(&self, store: &Store, addr: MemAddr) -> Result<MemoryRef> {
200+
let addr = self.resolve_mem_addr(addr);
201+
let mem = store.get_mem(addr as usize)?;
202+
Ok(MemoryRef { instance: mem.clone() })
203+
}
204+
191205
/// Get the start function of the module
192206
///
193207
/// Returns None if the module has no start function
@@ -204,7 +218,7 @@ impl ModuleInstance {
204218
Some(func_index) => func_index,
205219
None => {
206220
// alternatively, check for a _start function in the exports
207-
let Some(ExternVal::Func(func_addr)) = self.export("_start") else {
221+
let Some(ExternVal::Func(func_addr)) = self.export_addr("_start") else {
208222
return Ok(None);
209223
};
210224

0 commit comments

Comments
 (0)