Skip to content

Commit

Permalink
chore: refactor push_frame
Browse files Browse the repository at this point in the history
  • Loading branch information
skanehira committed Nov 23, 2023
1 parent b86d071 commit c7405c4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/execution/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ pub fn get_else_or_end_address(insts: &[Instruction], pc: isize) -> Result<usize

pub fn push_frame(stack: &mut Vec<Value>, call_stack: &mut Vec<Frame>, func: &InternalFuncInst) {
let arity = func.func_type.results.len();
let len = stack.len();
let mut locals = stack.split_off(len - func.func_type.params.len());
let bottom = stack.len() - func.func_type.params.len();
let mut locals = stack.split_off(bottom);

for local in func.code.locals.iter() {
match local {
Expand Down
24 changes: 1 addition & 23 deletions src/execution/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::op::*;
use super::store::{Exports, Store};
use super::value::{ExternalVal, Frame, Label, StackAccess, Value};
use crate::binary::instruction::*;
use crate::binary::types::ValueType;
use crate::execution::error::Error;
use crate::execution::value::LabelKind;
use crate::{load, store, Importer};
Expand Down Expand Up @@ -132,31 +131,10 @@ impl Runtime {
}

fn invoke_internal(&mut self, func: InternalFuncInst) -> Result<Option<Value>> {
let bottom = self.stack.len() - func.func_type.params.len();
let mut locals: Vec<_> = self.stack.split_off(bottom).into_iter().collect();

for local in func.code.locals.iter() {
match local {
ValueType::I32 => locals.push(Value::I32(0)),
ValueType::I64 => locals.push(Value::I64(0)),
ValueType::F32 => locals.push(Value::F32(0.0)),
ValueType::F64 => locals.push(Value::F64(0.0)),
}
}

let arity = func.func_type.results.len();

let frame = Frame {
pc: -1,
sp: self.stack.len(),
insts: func.code.body,
arity,
locals,
labels: vec![],
};
self.call_stack.push(frame);
push_frame(&mut self.stack, &mut self.call_stack, &func);

trace!("call stack: {:?}", &self.call_stack.last());
self.execute()?;

let result = if arity > 0 {
Expand Down

0 comments on commit c7405c4

Please sign in to comment.