Skip to content

Commit

Permalink
refactor: improve call wasm (#508)
Browse files Browse the repository at this point in the history
* chore: improve call wasm

* chore: improve call wasm

* chore: improve call wasm

* clippy

* use `==` to check recursion_limit

Co-authored-by: Robin Freyler <robin.freyler@gmail.com>
  • Loading branch information
yjhmelody and Robbepop authored Oct 31, 2022
1 parent 0d087e2 commit f5e1c46
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl EngineInner {
CallOutcome::NestedCall(called_func) => {
match called_func.as_internal(ctx.as_context()) {
FuncEntityInternal::Wasm(wasm_func) => {
self.stack.call_wasm(frame, wasm_func, &self.code_map)?;
*frame = self.stack.call_wasm(frame, wasm_func, &self.code_map)?;
}
FuncEntityInternal::Host(host_func) => {
cache.reset_default_memory_bytes();
Expand Down
14 changes: 3 additions & 11 deletions crates/wasmi/src/engine/stack/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use super::{err_stack_overflow, DEFAULT_MAX_RECURSION_DEPTH};
use crate::{core::TrapCode, engine::code_map::InstructionPtr, Instance};
use alloc::vec::Vec;
use core::mem::replace;

/// A function frame of a function on the call stack.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -72,20 +71,13 @@ impl CallStack {
FuncFrame::new(ip, instance)
}

/// Pushes a Wasm function onto the [`CallStack`].
pub(crate) fn push(
&mut self,
caller: &mut FuncFrame,
ip: InstructionPtr,
instance: Instance,
) -> Result<FuncFrame, TrapCode> {
/// Pushes a Wasm caller function onto the [`CallStack`].
pub(crate) fn push(&mut self, caller: FuncFrame) -> Result<(), TrapCode> {
if self.len() == self.recursion_limit {
return Err(err_stack_overflow());
}
let frame = FuncFrame::new(ip, instance);
let caller = replace(caller, frame);
self.frames.push(caller);
Ok(frame)
Ok(())
}

/// Pops the last [`FuncFrame`] from the [`CallStack`] if any.
Expand Down
5 changes: 3 additions & 2 deletions crates/wasmi/src/engine/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ impl Stack {
/// Prepares the [`Stack`] for the given Wasm function call.
pub(crate) fn call_wasm<'engine>(
&mut self,
caller: &mut FuncFrame,
caller: &FuncFrame,
wasm_func: &WasmFuncEntity,
code_map: &'engine CodeMap,
) -> Result<FuncFrame, TrapCode> {
let ip = self.call_wasm_impl(wasm_func, code_map)?;
self.frames.push(*caller)?;
let instance = wasm_func.instance();
let frame = self.frames.push(caller, ip, instance)?;
let frame = FuncFrame::new(ip, instance);
Ok(frame)
}

Expand Down

0 comments on commit f5e1c46

Please sign in to comment.