Skip to content

Commit 388e37e

Browse files
committed
fix: pop arity+1 instead of arity when calling native function
1 parent 532b69c commit 388e37e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/vm/src/vm.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl Vm {
201201
Object::NativeFun(native_fun) => {
202202
let args = self.stack.iter().rev().take(arg_count.0 as usize).cloned();
203203
let result = native_fun.0(args.collect());
204-
self.stack.pop_n(arg_count.0 as usize);
204+
self.stack.pop_n(arg_count.0 as usize + 1);
205205
self.push(result)?;
206206
Ok(())
207207
}
@@ -935,4 +935,17 @@ mod tests {
935935
Vm::new().run_source(source, &mut output).unwrap();
936936
assert_eq!(String::from_utf8(output).unwrap().lines().collect_vec(), vec!["A", "A"]);
937937
}
938+
939+
#[test]
940+
fn call_native_fn() {
941+
let mut vm = Vm::new();
942+
vm.define_native("increment", |args| Value::Number(args[0].clone().unwrap_number() + 1.0));
943+
let source = r#"
944+
print increment(123);
945+
"#;
946+
947+
let mut output = Vec::new();
948+
vm.run_source(source, &mut output).unwrap();
949+
assert_eq!(String::from_utf8(output).unwrap().lines().collect_vec(), vec!["124"]);
950+
}
938951
}

0 commit comments

Comments
 (0)