Skip to content

Commit 77cf2a6

Browse files
chore: cleanup
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 468a600 commit 77cf2a6

19 files changed

Lines changed: 343 additions & 439 deletions

File tree

crates/parser/src/visit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct LoweringCtx {
3030

3131
#[derive(Default)]
3232
struct FunctionDataBuilder {
33-
v128_constants: Vec<i128>,
33+
v128_constants: Vec<[u8; 16]>,
3434
branch_table_targets: Vec<u32>,
3535
}
3636

@@ -481,12 +481,12 @@ impl<R: WasmModuleResources> wasmparser::VisitSimdOperator<'_> for FunctionBuild
481481

482482
fn visit_i8x16_shuffle(&mut self, lanes: [u8; 16]) -> Self::Output {
483483
self.instructions.push(Instruction::I8x16Shuffle(self.data.v128_constants.len() as u32));
484-
self.data.v128_constants.push(i128::from_le_bytes(lanes));
484+
self.data.v128_constants.push(lanes);
485485
}
486486

487487
fn visit_v128_const(&mut self, value: wasmparser::V128) -> Self::Output {
488488
self.instructions.push(Instruction::V128Const(self.data.v128_constants.len() as u32));
489-
self.data.v128_constants.push(value.i128());
489+
self.data.v128_constants.push(*value.bytes());
490490
}
491491
}
492492

crates/tinywasm/src/func.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::interpreter::stack::CallFrame;
1+
use crate::interpreter::stack::{CallFrame, ValueStack};
22
use crate::reference::StoreItem;
33
use crate::{Error, FunctionInstance, InterpreterRuntime, Result, Store, unlikely};
44
use alloc::rc::Rc;
@@ -22,16 +22,16 @@ impl Function {
2222
};
2323

2424
// Reset stack, push args, allocate locals, create entry frame.
25-
store.stack.clear();
26-
store.stack.values.extend_from_wasmvalues(params)?;
27-
let locals_base = store.stack.values.enter_locals(&wasm_func.func.params, &wasm_func.func.locals)?;
25+
store.call_stack.clear();
26+
store.value_stack.clear();
27+
store.value_stack.extend_from_wasmvalues(params)?;
28+
let locals_base = store.value_stack.enter_locals(&wasm_func.func.params, &wasm_func.func.locals)?;
2829
let stack_offset = wasm_func.func.locals;
2930
let callframe = CallFrame::new(self.addr, wasm_func.owner, locals_base, stack_offset);
3031

3132
// Execute until completion and then collect result values from the stack.
3233
InterpreterRuntime::exec(store, callframe)?;
33-
34-
collect_call_results(store, &self.ty)
34+
collect_call_results(&mut store.value_stack, &self.ty)
3535
}
3636

3737
/// Call a function and return a resumable execution handle.
@@ -53,9 +53,10 @@ impl Function {
5353
Ok(FuncExecution { store, state: FuncExecutionState::Completed { result: Some(result) } })
5454
}
5555
FunctionInstance::Wasm(wasm_func) => {
56-
store.stack.clear();
57-
store.stack.values.extend_from_wasmvalues(params)?;
58-
let locals_base = store.stack.values.enter_locals(&wasm_func.func.params, &wasm_func.func.locals)?;
56+
store.call_stack.clear();
57+
store.value_stack.clear();
58+
store.value_stack.extend_from_wasmvalues(params)?;
59+
let locals_base = store.value_stack.enter_locals(&wasm_func.func.params, &wasm_func.func.locals)?;
5960
let stack_offset = wasm_func.func.locals;
6061
let callframe = CallFrame::new(self.addr, wasm_func.owner, locals_base, stack_offset);
6162

@@ -312,7 +313,7 @@ impl<'store> FuncExecution<'store> {
312313
match InterpreterRuntime::exec_with_fuel(self.store, exec_state.callframe, fuel)? {
313314
crate::interpreter::ExecState::Completed => {
314315
let result_ty = self.store.state.get_func(*root_func_addr).ty().clone();
315-
let result = collect_call_results(self.store, &result_ty)?;
316+
let result = collect_call_results(&mut self.store.value_stack, &result_ty)?;
316317
self.state = FuncExecutionState::Completed { result: None };
317318
Ok(ExecProgress::Completed(result))
318319
}
@@ -349,7 +350,7 @@ impl<'store> FuncExecution<'store> {
349350
match InterpreterRuntime::exec_with_time_budget(self.store, exec_state.callframe, time_budget)? {
350351
crate::interpreter::ExecState::Completed => {
351352
let result_ty = self.store.state.get_func(*root_func_addr).ty().clone();
352-
let result = collect_call_results(self.store, &result_ty)?;
353+
let result = collect_call_results(&mut self.store.value_stack, &result_ty)?;
353354
self.state = FuncExecutionState::Completed { result: None };
354355
Ok(ExecProgress::Completed(result))
355356
}
@@ -377,9 +378,9 @@ fn validate_call_params(func_ty: &FuncType, params: &[WasmValue]) -> Result<()>
377378
Ok(())
378379
}
379380

380-
fn collect_call_results(store: &mut Store, func_ty: &FuncType) -> Result<Vec<WasmValue>> {
381-
debug_assert!(store.stack.values.len() >= func_ty.results().len()); // m values are on the top of the stack (Ensured by validation)
382-
let mut res: Vec<_> = store.stack.values.pop_types(func_ty.results().iter().rev()).collect(); // pop in reverse order since the stack is LIFO
381+
fn collect_call_results(value_stack: &mut ValueStack, func_ty: &FuncType) -> Result<Vec<WasmValue>> {
382+
debug_assert!(value_stack.len() >= func_ty.results().len()); // m values are on the top of the stack (Ensured by validation)
383+
let mut res: Vec<_> = value_stack.pop_types(func_ty.results().iter().rev()).collect(); // pop in reverse order since the stack is LIFO
383384
res.reverse(); // reverse to get the original order
384385
Ok(res)
385386
}

0 commit comments

Comments
 (0)