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

Commit e771c6d

Browse files
chore: clippy fixes and more tests
Signed-off-by: Henry Gressmann <[email protected]>
1 parent b2b3946 commit e771c6d

File tree

7 files changed

+75
-32
lines changed

7 files changed

+75
-32
lines changed

crates/parser/src/conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(crate) fn convert_module_table(table: wasmparser::Table<'_>) -> Result<Table
137137
None => None,
138138
};
139139

140-
Ok(TableType { element_type: convert_reftype(&table.ty.element_type), size_initial: size_initial, size_max })
140+
Ok(TableType { element_type: convert_reftype(&table.ty.element_type), size_initial, size_max })
141141
}
142142

143143
pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Global<'a>>>>(

crates/parser/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ impl FunctionBuilder {
110110

111111
#[inline]
112112
fn visit(&mut self, op: Instruction) -> Result<()> {
113-
Ok(self.instructions.push(op))
113+
self.instructions.push(op);
114+
Ok(())
114115
}
115116
}
116117

crates/tinywasm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
no_crate_inject,
44
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_assignments, unused_variables))
55
))]
6+
#![allow(unexpected_cfgs, clippy::reserve_after_initialization)]
67
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
78
#![cfg_attr(nightly, feature(error_in_core))]
89
#![cfg_attr(not(feature = "unsafe"), deny(unsafe_code))]

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
138138

139139
CallIndirect(type_addr, table_addr) => {
140140
let table = store.get_table(module.resolve_table_addr(*table_addr))?;
141-
let table_idx = stack.values.pop_t::<u32>()?;
141+
let table_idx: u32 = stack.values.pop()?.into();
142142

143143
// verify that the table is of the right type, this should be validated by the parser already
144144
let func_ref = {
@@ -188,7 +188,7 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
188188

189189
If(args, else_offset, end_offset) => {
190190
// truthy value is on the top of the stack, so enter the then block
191-
if stack.values.pop_t::<i32>()? != 0 {
191+
if i32::from(stack.values.pop()?) != 0 {
192192
cf.enter_block(
193193
BlockFrame::new(
194194
cf.instr_ptr,
@@ -259,8 +259,8 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
259259
return Err(Error::Other(format!("br_table out of bounds: {} >= {}", end, cf.instructions().len())));
260260
}
261261

262-
let idx = stack.values.pop_t::<i32>()? as usize;
263-
match cf.instructions()[start..end].get(idx) {
262+
let idx: i32 = stack.values.pop()?.into();
263+
match cf.instructions()[start..end].get(idx as usize) {
264264
None => break_to!(cf, stack, default),
265265
Some(BrLabel(to)) => break_to!(cf, stack, to),
266266
_ => return Err(Error::Other("br_table with invalid label".to_string())),
@@ -269,7 +269,7 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
269269

270270
Br(v) => break_to!(cf, stack, v),
271271
BrIf(v) => {
272-
if stack.values.pop_t::<i32>()? != 0 {
272+
if i32::from(stack.values.pop()?) != 0 {
273273
break_to!(cf, stack, v);
274274
}
275275
}
@@ -329,8 +329,9 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
329329
let mem = store.get_mem(module.resolve_mem_addr(*addr))?;
330330
let mut mem = mem.borrow_mut();
331331
let prev_size = mem.page_count() as i32;
332+
let pages_delta: i32 = stack.values.pop()?.into();
332333

333-
match mem.grow(stack.values.pop_t::<i32>()?) {
334+
match mem.grow(pages_delta) {
334335
Some(_) => stack.values.push(prev_size.into()),
335336
None => stack.values.push((-1).into()),
336337
}
@@ -366,9 +367,9 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
366367
}
367368

368369
MemoryInit(data_index, mem_index) => {
369-
let size = stack.values.pop_t::<i32>()? as usize;
370-
let offset = stack.values.pop_t::<i32>()? as usize;
371-
let dst = stack.values.pop_t::<i32>()? as usize;
370+
let size = i32::from(stack.values.pop()?) as usize;
371+
let offset = i32::from(stack.values.pop()?) as usize;
372+
let dst = i32::from(stack.values.pop()?) as usize;
372373

373374
let data = match &store.get_data(module.resolve_data_addr(*data_index))?.data {
374375
Some(data) => data,
@@ -561,16 +562,16 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
561562
TableGet(table_index) => {
562563
let table_idx = module.resolve_table_addr(*table_index);
563564
let table = store.get_table(table_idx)?;
564-
let idx = stack.values.pop_t::<u32>()?;
565+
let idx: u32 = stack.values.pop()?.into();
565566
let v = table.borrow().get_wasm_val(idx)?;
566567
stack.values.push(v.into());
567568
}
568569

569570
TableSet(table_index) => {
570571
let table_idx = module.resolve_table_addr(*table_index);
571572
let table = store.get_table(table_idx)?;
572-
let val = stack.values.pop_t::<u32>()?;
573-
let idx = stack.values.pop_t::<u32>()?;
573+
let val = stack.values.pop()?.into();
574+
let idx = stack.values.pop()?.into();
574575
table.borrow_mut().set(idx, val)?;
575576
}
576577

@@ -632,7 +633,7 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
632633
stack.values.push((local + *val).into());
633634
}
634635
I32StoreLocal { local, const_i32: consti32, offset, mem_addr } => {
635-
let (mem_addr, offset) = (*mem_addr as u32, *offset as u32);
636+
let (mem_addr, offset) = (*mem_addr as u32, *offset);
636637
let mem = store.get_mem(module.resolve_mem_addr(mem_addr))?;
637638
let val = consti32.to_le_bytes();
638639
let addr: u64 = cf.get_local(*local).into();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl CallStack {
2020
let mut stack = Vec::new();
2121
stack.reserve_exact(CALL_STACK_SIZE);
2222

23-
let mut stack = Self { stack: stack };
23+
let mut stack = Self { stack };
2424
stack.push(initial_frame).unwrap();
2525
stack
2626
}

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,6 @@ impl ValueStack {
8181
}
8282
}
8383

84-
#[inline]
85-
pub(crate) fn pop_t<T: From<RawWasmValue>>(&mut self) -> Result<T> {
86-
match self.stack.pop() {
87-
Some(v) => Ok(v.into()),
88-
None => {
89-
cold(); // 20+ performance improvement most of the time
90-
Err(Error::ValueStackUnderflow)
91-
}
92-
}
93-
}
94-
9584
#[inline]
9685
pub(crate) fn pop(&mut self) -> Result<RawWasmValue> {
9786
match self.stack.pop() {
@@ -144,11 +133,38 @@ mod tests {
144133
stack.push(2.into());
145134
stack.push(3.into());
146135
assert_eq!(stack.len(), 3);
147-
assert_eq!(stack.pop_t::<i32>().unwrap(), 3);
136+
assert_eq!(i32::from(stack.pop().unwrap()), 3);
148137
assert_eq!(stack.len(), 2);
149-
assert_eq!(stack.pop_t::<i32>().unwrap(), 2);
138+
assert_eq!(i32::from(stack.pop().unwrap()), 2);
150139
assert_eq!(stack.len(), 1);
151-
assert_eq!(stack.pop_t::<i32>().unwrap(), 1);
140+
assert_eq!(i32::from(stack.pop().unwrap()), 1);
152141
assert_eq!(stack.len(), 0);
153142
}
143+
144+
#[test]
145+
fn test_truncate_keep() {
146+
macro_rules! test_macro {
147+
($( $n:expr, $end_keep:expr, $expected:expr ),*) => {
148+
$(
149+
let mut stack = ValueStack::default();
150+
stack.push(1.into());
151+
stack.push(2.into());
152+
stack.push(3.into());
153+
stack.push(4.into());
154+
stack.push(5.into());
155+
stack.truncate_keep($n, $end_keep);
156+
assert_eq!(stack.len(), $expected);
157+
)*
158+
};
159+
}
160+
161+
test_macro! {
162+
0, 0, 0,
163+
1, 0, 1,
164+
0, 1, 1,
165+
1, 1, 2,
166+
2, 1, 3,
167+
2, 2, 4
168+
}
169+
}
154170
}

crates/tinywasm/src/runtime/value.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,36 @@ impl_from_raw_wasm_value!(i64, |x| x as u64, |x: [u8; 8]| i64::from_ne_bytes(x[0
8383
impl_from_raw_wasm_value!(u8, |x| x as u64, |x: [u8; 8]| u8::from_ne_bytes(x[0..1].try_into().unwrap()));
8484
impl_from_raw_wasm_value!(u16, |x| x as u64, |x: [u8; 8]| u16::from_ne_bytes(x[0..2].try_into().unwrap()));
8585
impl_from_raw_wasm_value!(u32, |x| x as u64, |x: [u8; 8]| u32::from_ne_bytes(x[0..4].try_into().unwrap()));
86-
impl_from_raw_wasm_value!(u64, |x| x as u64, |x: [u8; 8]| u64::from_ne_bytes(x[0..8].try_into().unwrap()));
86+
impl_from_raw_wasm_value!(u64, |x| x, |x: [u8; 8]| u64::from_ne_bytes(x[0..8].try_into().unwrap()));
8787
impl_from_raw_wasm_value!(i8, |x| x as u64, |x: [u8; 8]| i8::from_ne_bytes(x[0..1].try_into().unwrap()));
8888
impl_from_raw_wasm_value!(i16, |x| x as u64, |x: [u8; 8]| i16::from_ne_bytes(x[0..2].try_into().unwrap()));
8989
impl_from_raw_wasm_value!(f32, |x| f32::to_bits(x) as u64, |x: [u8; 8]| f32::from_bits(u32::from_ne_bytes(
9090
x[0..4].try_into().unwrap()
9191
)));
92-
impl_from_raw_wasm_value!(f64, |x| f64::to_bits(x) as u64, |x: [u8; 8]| f64::from_bits(u64::from_ne_bytes(
92+
impl_from_raw_wasm_value!(f64, f64::to_bits, |x: [u8; 8]| f64::from_bits(u64::from_ne_bytes(
9393
x[0..8].try_into().unwrap()
9494
)));
95+
96+
#[cfg(test)]
97+
mod tests {
98+
use super::*;
99+
100+
#[test]
101+
fn test_raw_wasm_value() {
102+
macro_rules! test_macro {
103+
($( $ty:ty => $val:expr ),*) => {
104+
$(
105+
let raw: RawWasmValue = $val.into();
106+
let val: $ty = raw.into();
107+
assert_eq!(val, $val);
108+
)*
109+
};
110+
}
111+
112+
test_macro! {
113+
i32 => 0, i64 => 0, u8 => 0, u16 => 0, u32 => 0, u64 => 0, i8 => 0, i16 => 0, f32 => 0.0, f64 => 0.0,
114+
i32 => i32::MIN, i64 => i64::MIN, u8 => u8::MIN, u16 => u16::MIN, u32 => u32::MIN, u64 => u64::MIN, i8 => i8::MIN, i16 => i16::MIN, f32 => f32::MIN, f64 => f64::MIN,
115+
i32 => i32::MAX, i64 => i64::MAX, u8 => u8::MAX, u16 => u16::MAX, u32 => u32::MAX, u64 => u64::MAX, i8 => i8::MAX, i16 => i16::MAX, f32 => f32::MAX, f64 => f64::MAX
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)