Skip to content

Commit 80cf746

Browse files
feat: remove lifetimes from runtime objects
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent d89729d commit 80cf746

36 files changed

Lines changed: 984 additions & 1009 deletions

‎CHANGELOG.md‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- `engine::FuelPolicy` and `engine::Config::fuel_policy` for fuel accounting behavior
2222
- New `canonicalize_nans` feature flag to enable canonicalizing NaN values in the `f32`, `f64`, and `v128` types
2323
- Public API rework for runtime object access:
24-
- export lookups: `func`, `func_typed`, `memory`, `memory_mut`
25-
- table/global access: `table`, `table_mut`, `global`, `global_mut`, `global_get`, `global_set`
26-
- generic export access: `extern_item`, `extern_item_mut`
24+
- export lookups: `func_untyped`/`func`, `memory`, `table`, `global`
25+
- table/global value access: `global_get`, `global_set`
26+
- generic export access: `extern_item` and `ExternItem`
2727
- export iteration: `ModuleInstance::exports`
2828
- module descriptors: `Module::imports`, `Module::exports`
29-
- raw memory data access: `MemoryRef::data`/`data_size`, `MemoryRefMut::data`/`data_mut`/`data_size`
29+
- handle-based runtime objects with explicit store access: `Memory`, `Table`, `Global`, `Function`
3030

3131
### Changed
3232

@@ -50,10 +50,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5050
- Increased MSRV to 1.90
5151
- `Error::ParseError` was renamed to `Error::Parser`, and `Error::Twasm` was added
5252
- `ModuleInstance` export lookup APIs were renamed:
53-
- `exported_func_untyped` -> `func`
54-
- `exported_func` -> `func_typed`
53+
- `exported_func_untyped` -> `func_untyped`
54+
- `exported_func` -> `func`
5555
- `exported_memory` -> `memory`
56-
- `exported_memory_mut` -> `memory_mut`
56+
- `ModuleInstance` mutable export lookup variants were removed:
57+
- `memory_mut`, `table_mut`, `global_mut`
58+
- `extern_item_mut`
59+
- `FuncHandle` / `FuncHandleTyped` were renamed to `Function` / `FunctionTyped`
60+
- `HostFunction::new` / `HostFunction::func` / `HostFunction::typed` now require `&mut Store`
5761
- `Imports::link_module` now takes a `ModuleInstance` instead of a raw module instance id
5862
- `func_typed` now validates the exact wasm signature at lookup time and fails immediately on mismatches
5963

‎crates/cli/src/bin.rs‎

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

111111
if let Some(func) = func {
112-
let func = instance.func(&store, &func)?;
112+
let func = instance.func_untyped(&store, &func)?;
113113
let res = func.call(&mut store, args)?;
114114
info!("{res:?}");
115115
}

‎crates/parser/src/conversion.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
8686
}),
8787
wasmparser::TypeRef::Memory(ty) => ImportKind::Memory(convert_module_memory(ty)),
8888
wasmparser::TypeRef::Global(ty) => {
89-
ImportKind::Global(GlobalType { mutable: ty.mutable, ty: convert_valtype(&ty.content_type) })
89+
ImportKind::Global(GlobalType::new(convert_valtype(&ty.content_type), ty.mutable))
9090
}
9191
wasmparser::TypeRef::Tag(ty) => {
9292
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {ty:?}")));
@@ -140,7 +140,7 @@ pub(crate) fn convert_module_globals(
140140
let global = global?;
141141
let ty = convert_valtype(&global.ty.content_type);
142142
let ops = global.init_expr.get_operators_reader();
143-
Ok(Global { init: process_const_operators(ops)?, ty: GlobalType { mutable: global.ty.mutable, ty } })
143+
Ok(Global { init: process_const_operators(ops)?, ty: GlobalType::new(ty, global.ty.mutable) })
144144
})
145145
.collect::<Result<Vec<_>>>()
146146
}

‎crates/parser/src/module.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ impl ModuleReader {
158158
validator.end(offset)?;
159159
self.end_reached = true;
160160
}
161-
Payload::CustomSection(_reader) => {
161+
Payload::CustomSection(reader) => {
162162
debug!("Found custom section");
163-
debug!("Skipping custom section: {:?}", _reader.name());
163+
debug!("Skipping custom section: {:?}", reader.name());
164164
}
165165
Payload::UnknownSection { .. } => return Err(ParseError::UnsupportedSection("Unknown section".into())),
166166
section => return Err(ParseError::UnsupportedSection(format!("Unsupported section: {section:?}"))),

‎crates/tinywasm/benches/argon2id.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn argon2id_from_twasm(twasm: &[u8]) -> Result<TinyWasmModule> {
2424
fn argon2id_run(module: TinyWasmModule) -> Result<()> {
2525
let mut store = Store::default();
2626
let instance = ModuleInstance::instantiate(&mut store, module.into(), None)?;
27-
let argon2 = instance.func_typed::<(i32, i32, i32), i32>(&store, "argon2id")?;
27+
let argon2 = instance.func::<(i32, i32, i32), i32>(&store, "argon2id")?;
2828
argon2.call(&mut store, (1000, 2, 1))?;
2929
Ok(())
3030
}

‎crates/tinywasm/benches/fibonacci.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn fibonacci_from_twasm(twasm: &[u8]) -> Result<TinyWasmModule> {
2323
fn fibonacci_run(module: TinyWasmModule, recursive: bool, n: i32) -> Result<()> {
2424
let mut store = Store::default();
2525
let instance = ModuleInstance::instantiate(&mut store, module.into(), None)?;
26-
let argon2 = instance.func_typed::<i32, i32>(
26+
let argon2 = instance.func::<i32, i32>(
2727
&store,
2828
match recursive {
2929
true => "fibonacci_recursive",

‎crates/tinywasm/benches/tinywasm.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use criterion::{Criterion, criterion_group, criterion_main};
22
use eyre::Result;
3-
use tinywasm::{Extern, FuncContext, Imports, ModuleInstance, Store, types};
3+
use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store, types};
44
use types::TinyWasmModule;
55

66
const WASM: &[u8] = include_bytes!("../../../examples/rust/out/tinywasm.wasm");
@@ -24,9 +24,9 @@ fn tinywasm_from_twasm(twasm: &[u8]) -> Result<TinyWasmModule> {
2424
fn tinywasm_run(module: TinyWasmModule) -> Result<()> {
2525
let mut store = Store::default();
2626
let mut imports = Imports::default();
27-
imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(()))).expect("define");
27+
imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _: i32| Ok(())));
2828
let instance = ModuleInstance::instantiate(&mut store, module.into(), Some(imports)).expect("instantiate");
29-
let hello = instance.func_typed::<(), ()>(&store, "hello").expect("func_typed");
29+
let hello = instance.func::<(), ()>(&store, "hello").expect("func_typed");
3030
hello.call(&mut store, ()).expect("call");
3131
Ok(())
3232
}

‎crates/tinywasm/benches/tinywasm_modes.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
22
use eyre::Result;
33
use tinywasm::engine::{Config, FuelPolicy};
44
use tinywasm::types::TinyWasmModule;
5-
use tinywasm::{Engine, ExecProgress, Extern, FuncContext, FuncHandleTyped, Imports, ModuleInstance, Store};
5+
use tinywasm::{Engine, ExecProgress, FuncContext, FunctionTyped, HostFunction, Imports, ModuleInstance, Store};
66

77
const WASM: &[u8] = include_bytes!("../../../examples/rust/out/tinywasm.wasm");
88
const FUEL_PER_ROUND: u32 = 512;
@@ -14,26 +14,26 @@ fn tinywasm_parse() -> Result<TinyWasmModule> {
1414
Ok(parser.parse_module_bytes(WASM)?)
1515
}
1616

17-
fn setup_typed_func(module: TinyWasmModule, engine: Option<Engine>) -> Result<(Store, FuncHandleTyped<(), ()>)> {
17+
fn setup_typed_func(module: TinyWasmModule, engine: Option<Engine>) -> Result<(Store, FunctionTyped<(), ()>)> {
1818
let mut store = match engine {
1919
Some(engine) => Store::new(engine),
2020
None => Store::default(),
2121
};
2222

2323
let mut imports = Imports::default();
24-
imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(())))?;
24+
imports.define("env", "printi32", HostFunction::from(&mut store, |_: FuncContext<'_>, _: i32| Ok(())));
2525

2626
let instance = ModuleInstance::instantiate(&mut store, module.into(), Some(imports))?;
27-
let func = instance.func_typed::<(), ()>(&store, "hello")?;
27+
let func = instance.func::<(), ()>(&store, "hello")?;
2828
Ok((store, func))
2929
}
3030

31-
fn run_call(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Result<()> {
31+
fn run_call(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> {
3232
func.call(store, ())?;
3333
Ok(())
3434
}
3535

36-
fn run_resume_with_fuel(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Result<()> {
36+
fn run_resume_with_fuel(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> {
3737
let mut execution = func.call_resumable(store, ())?;
3838
loop {
3939
match execution.resume_with_fuel(FUEL_PER_ROUND)? {
@@ -43,7 +43,7 @@ fn run_resume_with_fuel(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Re
4343
}
4444
}
4545

46-
fn run_resume_with_time_budget(store: &mut Store, func: &FuncHandleTyped<(), ()>) -> Result<()> {
46+
fn run_resume_with_time_budget(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> {
4747
let mut execution = func.call_resumable(store, ())?;
4848
loop {
4949
match execution.resume_with_time_budget(TIME_BUDGET_PER_ROUND)? {

0 commit comments

Comments
 (0)