Skip to content

Commit 9d19098

Browse files
chore: cleanup
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 9438505 commit 9d19098

File tree

10 files changed

+96
-223
lines changed

10 files changed

+96
-223
lines changed

Cargo.lock

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

crates/cli/src/bin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn main() -> Result<()> {
8484
TinyWasmSubcommand::Run(Run { wasm_file, engine, args, func }) => {
8585
debug!("args: {args:?}");
8686

87-
let path = cwd.join(wasm_file.clone());
87+
let path = cwd.join(&wasm_file);
8888
let module = match wasm_file.ends_with(".wat") {
8989
#[cfg(feature = "wat")]
9090
true => {

crates/parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Parser {
116116
/// Parse a [`TinyWasmModule`] from a file. Requires `std` feature.
117117
pub fn parse_module_file(&self, path: impl AsRef<crate::std::path::Path> + Clone) -> Result<TinyWasmModule> {
118118
use alloc::format;
119-
let f = crate::std::fs::File::open(path.clone())
119+
let f = crate::std::fs::File::open(&path)
120120
.map_err(|e| ParseError::Other(format!("Error opening file {:?}: {}", path.as_ref(), e)))?;
121121

122122
let mut reader = crate::std::io::BufReader::new(f);

crates/tinywasm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ log={workspace=true, optional=true}
2020
tinywasm-parser={version="0.9.0-alpha.0", path="../parser", default-features=false, optional=true}
2121
tinywasm-types={version="0.9.0-alpha.0", path="../types", default-features=false}
2222
libm={version="0.2", default-features=false}
23-
allocator-api2={version="0.4", optional=true}
2423

2524
[dev-dependencies]
2625
wasm-testsuite.workspace=true
@@ -49,9 +48,6 @@ archive=["tinywasm-types/archive"]
4948
# canonicalize all NaN values to a single representation
5049
canonicalize_nans=[]
5150

52-
# support for the allocator-api2 crate
53-
allocator-api2=["dep:allocator-api2"]
54-
5551
[[test]]
5652
name="test-wasm-1"
5753
harness=false

crates/tinywasm/src/func.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ impl FuncHandle {
7070
debug_assert!(store.stack.values.len() >= func_ty.results.len());
7171

7272
// 2. Pop m values from the stack
73-
let res = store.stack.values.pop_results(&func_ty.results);
73+
let mut res: Vec<_> = store.stack.values.pop_types(func_ty.results.iter().rev()).collect(); // pop in reverse order since the stack is LIFO
74+
res.reverse(); // reverse to get the original order
7475

7576
// The values are returned as the results of the invocation.
7677
Ok(res)

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 65 additions & 184 deletions
Large diffs are not rendered by default.

crates/tinywasm/src/interpreter/stack/call_stack.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,10 @@ impl CallFrame {
176176
block_ptr: u32,
177177
) -> Self {
178178
let locals = {
179-
let mut locals_32 = Vec::new();
180-
locals_32.reserve_exact(wasm_func_inst.locals.c32 as usize);
181-
let mut locals_64 = Vec::new();
182-
locals_64.reserve_exact(wasm_func_inst.locals.c64 as usize);
183-
let mut locals_128 = Vec::new();
184-
locals_128.reserve_exact(wasm_func_inst.locals.c128 as usize);
185-
let mut locals_ref = Vec::new();
186-
locals_ref.reserve_exact(wasm_func_inst.locals.cref as usize);
179+
let mut locals_32 = Vec::with_capacity(wasm_func_inst.locals.c32 as usize);
180+
let mut locals_64 = Vec::with_capacity(wasm_func_inst.locals.c64 as usize);
181+
let mut locals_128 = Vec::with_capacity(wasm_func_inst.locals.c128 as usize);
182+
let mut locals_ref = Vec::with_capacity(wasm_func_inst.locals.cref as usize);
187183

188184
for p in params {
189185
match p.into() {

crates/tinywasm/src/interpreter/stack/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ impl Stack {
2323

2424
/// Initialize the stack with the given call frame (used for starting execution)
2525
pub(crate) fn initialize(&mut self, callframe: CallFrame) {
26-
self.blocks.clear();
2726
self.values.clear();
27+
self.blocks.clear();
2828
self.call_stack.reset(callframe);
2929
}
3030
}

crates/tinywasm/src/interpreter/stack/value_stack.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,12 @@ impl ValueStack {
122122
T::replace_top(self, func)
123123
}
124124

125-
pub(crate) fn pop_params(&mut self, val_types: &[ValType]) -> Vec<WasmValue> {
126-
val_types.iter().map(|val_type| self.pop_wasmvalue(*val_type)).collect::<Vec<_>>()
127-
}
128-
129-
pub(crate) fn pop_results(&mut self, val_types: &[ValType]) -> Vec<WasmValue> {
130-
let mut results = val_types.iter().rev().map(|val_type| self.pop_wasmvalue(*val_type)).collect::<Vec<_>>();
131-
results.reverse();
132-
results
125+
#[inline]
126+
pub(crate) fn pop_types<'a>(
127+
&'a mut self,
128+
val_types: impl IntoIterator<Item = &'a ValType>,
129+
) -> impl core::iter::Iterator<Item = WasmValue> {
130+
val_types.into_iter().map(|val_type| self.pop_wasmvalue(*val_type))
133131
}
134132

135133
#[inline]
@@ -168,7 +166,7 @@ impl ValueStack {
168166

169167
pub(crate) fn truncate_keep(&mut self, to: StackLocation, keep: StackHeight) {
170168
#[inline(always)]
171-
fn truncate_keep<T: Copy + Default>(data: &mut Vec<T>, n: u32, end_keep: u32) {
169+
fn truncate_keep<T>(data: &mut Vec<T>, n: u32, end_keep: u32) {
172170
let len = data.len() as u32;
173171
if len <= n {
174172
return; // No need to truncate if the current size is already less than or equal to total_to_keep
@@ -205,7 +203,15 @@ impl ValueStack {
205203

206204
pub(crate) fn extend_from_wasmvalues(&mut self, values: &[WasmValue]) {
207205
for value in values {
208-
self.push_dyn(value.into());
206+
match value {
207+
WasmValue::I32(v) => self.stack_32.push(*v as u32),
208+
WasmValue::I64(v) => self.stack_64.push(*v as u64),
209+
WasmValue::F32(v) => self.stack_32.push(v.to_bits()),
210+
WasmValue::F64(v) => self.stack_64.push(v.to_bits()),
211+
WasmValue::RefExtern(v) => self.stack_ref.push(v.addr()),
212+
WasmValue::RefFunc(v) => self.stack_ref.push(v.addr()),
213+
WasmValue::V128(v) => self.stack_128.push((*v).into()),
214+
}
209215
}
210216
}
211217
}

crates/tinywasm/src/interpreter/values.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,31 @@ impl TinyWasmValue {
7070
pub fn unwrap_32(&self) -> Value32 {
7171
match self {
7272
Self::Value32(v) => *v,
73-
_ => unreachable!("Expected Value32"),
73+
_ => panic!("Expected Value32"),
7474
}
7575
}
7676

7777
/// Asserts that the value is a 64-bit value and returns it (panics if the value is the wrong size)
7878
pub fn unwrap_64(&self) -> Value64 {
7979
match self {
8080
Self::Value64(v) => *v,
81-
_ => unreachable!("Expected Value64"),
81+
_ => panic!("Expected Value64"),
8282
}
8383
}
8484

8585
/// Asserts that the value is a 128-bit value and returns it (panics if the value is the wrong size)
8686
pub fn unwrap_128(&self) -> Value128 {
8787
match self {
8888
Self::Value128(v) => *v,
89-
_ => unreachable!("Expected Value128"),
89+
_ => panic!("Expected Value128"),
9090
}
9191
}
9292

9393
/// Asserts that the value is a reference value and returns it (panics if the value is the wrong size)
9494
pub fn unwrap_ref(&self) -> ValueRef {
9595
match self {
9696
Self::ValueRef(v) => *v,
97-
_ => unreachable!("Expected ValueRef"),
97+
_ => panic!("Expected ValueRef"),
9898
}
9999
}
100100

0 commit comments

Comments
 (0)