Move MethodInst stack mangling to MethodInst::call
When a MethodInst is called as a function in the VM, it needs to push its `self_binding` member to the stack. Previously, we were downcasting (if possible) to MethodInst in the VM, but really, we are calling `MethodInst::call` anyway, so it makes more sense to do MethodInst-specific stuff in the MethodInst-specific function. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
26
src/vm.rs
26
src/vm.rs
@@ -131,6 +131,11 @@ impl Vm {
|
||||
&self.stack
|
||||
}
|
||||
|
||||
/// Get the stack, mutably.
|
||||
pub fn stack_mut(&mut self) -> &mut Vec<ObjP> {
|
||||
&mut self.stack
|
||||
}
|
||||
|
||||
/// Current stack frame.
|
||||
pub fn frame(&self) -> &Frame {
|
||||
self.frames.last().unwrap()
|
||||
@@ -281,12 +286,12 @@ impl Vm {
|
||||
}
|
||||
}
|
||||
Op::Call(argc) => {
|
||||
let argc = argc as usize;
|
||||
let index = self.stack.len() - argc - 1;
|
||||
let argc = argc;
|
||||
let index = self.stack.len() - (argc as usize) - 1;
|
||||
let fun_ptr = Ptr::clone(&self.stack[index]);
|
||||
|
||||
let arity = if let Some(arity) = fun_ptr.borrow().arity() {
|
||||
arity as usize
|
||||
arity
|
||||
} else {
|
||||
// TODO Vm::run, Op::Call - throw an exception when the value isn't
|
||||
// callable
|
||||
@@ -297,20 +302,7 @@ impl Vm {
|
||||
);
|
||||
};
|
||||
|
||||
// Methods with bound "self" parameter
|
||||
// argc may be mutated
|
||||
let mut argc = argc;
|
||||
if let Some(method) = fun_ptr.borrow().as_any().downcast_ref::<MethodInst>() {
|
||||
// shift all of the arguments over by one
|
||||
// (duplicate the last item on the stack and then shift everyone else over)
|
||||
self.stack
|
||||
.insert(self.stack.len() - argc, Ptr::clone(method.self_binding()));
|
||||
// also increment argc since we're specifying another arg
|
||||
argc += 1;
|
||||
}
|
||||
// remove mutability
|
||||
let argc = argc;
|
||||
|
||||
// Check arity
|
||||
if arity != argc {
|
||||
// TODO Vm::run, Op::Call - throw an exception when the number of arguments
|
||||
// does not match the function's arity
|
||||
|
||||
Reference in New Issue
Block a user