Skip to content

Commit 90148db

Browse files
committed
refactor initialization
1 parent af8db56 commit 90148db

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

crates/wasmi/src/engine/translator/translator2/mod.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::{
2626
module::{FuncIdx, ModuleHeader, WasmiValueType},
2727
Engine,
2828
Error,
29+
FuncType,
2930
};
3031
use wasmparser::WasmFeatures;
3132

@@ -146,23 +147,12 @@ impl FuncTranslator {
146147
.then(|| config.fuel_costs())
147148
.cloned();
148149
let FuncTranslatorAllocations {
149-
mut stack,
150+
stack,
150151
layout,
151-
mut labels,
152-
mut instrs,
153-
} = {
154-
let mut alloc = alloc;
155-
alloc.reset();
156-
alloc
157-
};
158-
let func_ty = module.get_type_of_func(func);
159-
let block_ty = BlockType::func_type(func_ty);
160-
let end_label = labels.new_label();
161-
let consume_fuel = fuel_costs
162-
.as_ref()
163-
.map(|_| instrs.push_instr(Instruction::consume_fuel(1)));
164-
stack.push_block(block_ty, end_label, consume_fuel)?;
165-
Ok(Self {
152+
labels,
153+
instrs,
154+
} = alloc.into_reset();
155+
let mut translator = Self {
166156
func,
167157
engine,
168158
module,
@@ -172,7 +162,39 @@ impl FuncTranslator {
172162
layout,
173163
labels,
174164
instrs,
175-
})
165+
};
166+
translator.init_func_body_block()?;
167+
translator.init_func_params()?;
168+
Ok(translator)
169+
}
170+
171+
/// Initializes the function body enclosing control block.
172+
fn init_func_body_block(&mut self) -> Result<(), Error> {
173+
let func_ty = self.module.get_type_of_func(self.func);
174+
let block_ty = BlockType::func_type(func_ty);
175+
let end_label = self.labels.new_label();
176+
let consume_fuel = self
177+
.fuel_costs
178+
.as_ref()
179+
.map(|_| self.instrs.push_instr(Instruction::consume_fuel(1)));
180+
self.stack.push_block(block_ty, end_label, consume_fuel)?;
181+
Ok(())
182+
}
183+
184+
/// Initializes the function's parameters.
185+
fn init_func_params(&mut self) -> Result<(), Error> {
186+
for ty in self.func_type().params() {
187+
self.stack.register_locals(1, *ty)?;
188+
self.layout.register_locals(1, *ty)?;
189+
}
190+
Ok(())
191+
}
192+
193+
/// Returns the [`FuncType`] of the function that is currently translated.
194+
fn func_type(&self) -> FuncType {
195+
let dedup_func_type = self.module.get_type_of_func(self.func);
196+
self.engine()
197+
.resolve_func_type(dedup_func_type, Clone::clone)
176198
}
177199

178200
/// Consumes `self` and returns the underlying reusable [`FuncTranslatorAllocations`].

crates/wasmi/src/engine/translator/translator2/utils.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ macro_rules! bail_unreachable {
1616
}
1717

1818
/// Implemented by types that can be reset for reuse.
19-
pub trait Reset {
19+
pub trait Reset: Sized {
2020
/// Resets `self` for reuse.
2121
fn reset(&mut self);
22+
23+
/// Returns `self` in resetted state.
24+
#[must_use]
25+
fn into_reset(self) -> Self {
26+
let mut this = self;
27+
this.reset();
28+
this
29+
}
2230
}

0 commit comments

Comments
 (0)