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

Commit b2b3946

Browse files
chore: simplify code
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 868aa00 commit b2b3946

File tree

9 files changed

+176
-350
lines changed

9 files changed

+176
-350
lines changed

crates/parser/src/conversion.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
8383
size_initial: ty.initial.try_into().map_err(|_| {
8484
crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", ty.initial))
8585
})?,
86-
size_max: if let Some(max) = ty.maximum {
87-
Some(max.try_into().map_err(|_| {
86+
size_max: match ty.maximum {
87+
Some(max) => Some(max.try_into().map_err(|_| {
8888
crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {}", max))
89-
})?)
90-
} else {
91-
None
89+
})?),
90+
None => None,
9291
},
9392
}),
9493
wasmparser::TypeRef::Memory(ty) => ImportKind::Memory(convert_module_memory(ty)?),
@@ -105,10 +104,7 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
105104
pub(crate) fn convert_module_memories<T: IntoIterator<Item = wasmparser::Result<wasmparser::MemoryType>>>(
106105
memory_types: T,
107106
) -> Result<Vec<MemoryType>> {
108-
let memory_type =
109-
memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::<Result<Vec<_>>>()?;
110-
111-
Ok(memory_type)
107+
memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::<Result<Vec<_>>>()
112108
}
113109

114110
pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<MemoryType> {
@@ -125,26 +121,23 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<Me
125121
pub(crate) fn convert_module_tables<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Table<'a>>>>(
126122
table_types: T,
127123
) -> Result<Vec<TableType>> {
128-
let table_type = table_types.into_iter().map(|table| convert_module_table(table?)).collect::<Result<Vec<_>>>()?;
129-
Ok(table_type)
124+
table_types.into_iter().map(|table| convert_module_table(table?)).collect::<Result<Vec<_>>>()
130125
}
131126

132127
pub(crate) fn convert_module_table(table: wasmparser::Table<'_>) -> Result<TableType> {
133-
let ty = convert_reftype(&table.ty.element_type);
134-
135128
let size_initial = table.ty.initial.try_into().map_err(|_| {
136129
crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", table.ty.initial))
137130
})?;
138-
let size_max = if let Some(max) = table.ty.maximum {
139-
Some(
131+
132+
let size_max = match table.ty.maximum {
133+
Some(max) => Some(
140134
max.try_into()
141135
.map_err(|_| crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {}", max)))?,
142-
)
143-
} else {
144-
None
136+
),
137+
None => None,
145138
};
146139

147-
Ok(TableType { element_type: ty, size_initial: size_initial, size_max })
140+
Ok(TableType { element_type: convert_reftype(&table.ty.element_type), size_initial: size_initial, size_max })
148141
}
149142

150143
pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Global<'a>>>>(
@@ -208,12 +201,8 @@ pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result<FuncType>
208201
));
209202
}
210203
let ty = types.next().unwrap().unwrap_func();
211-
212-
let params =
213-
ty.params().iter().map(|p| Ok(convert_valtype(p))).collect::<Result<Vec<ValType>>>()?.into_boxed_slice();
214-
215-
let results =
216-
ty.results().iter().map(|p| Ok(convert_valtype(p))).collect::<Result<Vec<ValType>>>()?.into_boxed_slice();
204+
let params = ty.params().iter().map(convert_valtype).collect::<Vec<ValType>>().into_boxed_slice();
205+
let results = ty.results().iter().map(convert_valtype).collect::<Vec<ValType>>().into_boxed_slice();
217206

218207
Ok(FuncType { params, results })
219208
}
@@ -235,14 +224,13 @@ pub(crate) fn convert_reftype(reftype: &wasmparser::RefType) -> ValType {
235224
}
236225

237226
pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType {
238-
use wasmparser::ValType::*;
239227
match valtype {
240-
I32 => ValType::I32,
241-
I64 => ValType::I64,
242-
F32 => ValType::F32,
243-
F64 => ValType::F64,
244-
Ref(r) => convert_reftype(r),
245-
V128 => unimplemented!("128-bit values are not supported yet"),
228+
wasmparser::ValType::I32 => ValType::I32,
229+
wasmparser::ValType::I64 => ValType::I64,
230+
wasmparser::ValType::F32 => ValType::F32,
231+
wasmparser::ValType::F64 => ValType::F64,
232+
wasmparser::ValType::Ref(r) => convert_reftype(r),
233+
wasmparser::ValType::V128 => unimplemented!("128-bit values are not supported yet"),
246234
}
247235
}
248236

crates/parser/src/visit.rs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{conversion::convert_blocktype, Result};
33
use crate::conversion::{convert_heaptype, convert_memarg, convert_valtype};
44
use alloc::string::ToString;
55
use alloc::{boxed::Box, format, vec::Vec};
6-
use tinywasm_types::{BlockArgsPacked, Instruction};
6+
use tinywasm_types::Instruction;
77
use wasmparser::{FuncValidator, FunctionBody, VisitOperator, WasmModuleResources};
88

99
struct ValidateThenVisit<'a, T, U>(T, &'a mut U);
@@ -65,16 +65,14 @@ macro_rules! define_primitive_operands {
6565
($($name:ident, $instr:expr, $ty:ty),*) => {
6666
$(
6767
fn $name(&mut self, arg: $ty) -> Self::Output {
68-
self.instructions.push($instr(arg));
69-
Ok(())
68+
Ok(self.instructions.push($instr(arg)))
7069
}
7170
)*
7271
};
7372
($($name:ident, $instr:expr, $ty:ty, $ty2:ty),*) => {
7473
$(
7574
fn $name(&mut self, arg: $ty, arg2: $ty) -> Self::Output {
76-
self.instructions.push($instr(arg, arg2));
77-
Ok(())
75+
Ok(self.instructions.push($instr(arg, arg2)))
7876
}
7977
)*
8078
};
@@ -112,8 +110,7 @@ impl FunctionBuilder {
112110

113111
#[inline]
114112
fn visit(&mut self, op: Instruction) -> Result<()> {
115-
self.instructions.push(op);
116-
Ok(())
113+
Ok(self.instructions.push(op))
117114
}
118115
}
119116

@@ -162,7 +159,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
162159
visit_i64_load16_u, I64Load16U,
163160
visit_i64_load32_s, I64Load32S,
164161
visit_i64_load32_u, I64Load32U,
165-
// visit_i32_store, I32Store,
162+
// visit_i32_store, I32Store, custom implementation
166163
visit_i64_store, I64Store,
167164
visit_f32_store, F32Store,
168165
visit_f64_store, F64Store,
@@ -325,15 +322,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
325322
let arg = convert_memarg(memarg);
326323
let i32store = Instruction::I32Store { offset: arg.offset, mem_addr: arg.mem_addr };
327324

328-
if self.instructions.len() < 3 {
329-
return self.visit(i32store);
330-
}
331-
332-
#[cold]
333-
fn cold() {}
334-
335-
if arg.mem_addr > 0xFF || arg.offset > 0xFFFF_FFFF {
336-
cold();
325+
if self.instructions.len() < 3 || arg.mem_addr > 0xFF || arg.offset > 0xFFFF_FFFF {
337326
return self.visit(i32store);
338327
}
339328

@@ -353,31 +342,22 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
353342
}
354343

355344
fn visit_local_get(&mut self, idx: u32) -> Self::Output {
356-
if let Some(instruction) = self.instructions.last_mut() {
357-
match instruction {
358-
Instruction::LocalGet(a) => *instruction = Instruction::LocalGet2(*a, idx),
359-
Instruction::LocalGet2(a, b) => *instruction = Instruction::LocalGet3(*a, *b, idx),
360-
Instruction::LocalTee(a) => *instruction = Instruction::LocalTeeGet(*a, idx),
361-
_ => return self.visit(Instruction::LocalGet(idx)),
362-
};
363-
Ok(())
364-
} else {
365-
self.visit(Instruction::LocalGet(idx))
366-
}
345+
let Some(instruction) = self.instructions.last_mut() else {
346+
return self.visit(Instruction::LocalGet(idx));
347+
};
348+
349+
match instruction {
350+
Instruction::LocalGet(a) => *instruction = Instruction::LocalGet2(*a, idx),
351+
Instruction::LocalGet2(a, b) => *instruction = Instruction::LocalGet3(*a, *b, idx),
352+
Instruction::LocalTee(a) => *instruction = Instruction::LocalTeeGet(*a, idx),
353+
_ => return self.visit(Instruction::LocalGet(idx)),
354+
};
355+
356+
Ok(())
367357
}
368358

369359
fn visit_local_set(&mut self, idx: u32) -> Self::Output {
370360
self.visit(Instruction::LocalSet(idx))
371-
// if let Some(instruction) = self.instructions.last_mut() {
372-
// match instruction {
373-
// // Needs more testing, seems to make performance worse
374-
// // Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx),
375-
// _ => return self.visit(Instruction::LocalSet(idx)),
376-
// };
377-
// // Ok(())
378-
// } else {
379-
// self.visit(Instruction::LocalSet(idx))
380-
// }
381361
}
382362

383363
fn visit_local_tee(&mut self, idx: u32) -> Self::Output {
@@ -426,7 +406,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
426406

427407
fn visit_if(&mut self, ty: wasmparser::BlockType) -> Self::Output {
428408
self.label_ptrs.push(self.instructions.len());
429-
self.visit(Instruction::If(BlockArgsPacked::new(convert_blocktype(ty)), 0, 0))
409+
self.visit(Instruction::If(convert_blocktype(ty).into(), 0, 0))
430410
}
431411

432412
fn visit_else(&mut self) -> Self::Output {

crates/tinywasm/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum Function {
1515
/// A host function
1616
Host(Rc<HostFunction>),
1717

18-
/// A function defined in WebAssembly
18+
/// A pointer to a WebAssembly function
1919
Wasm(Rc<WasmFunction>),
2020
}
2121

0 commit comments

Comments
 (0)