From ffe5d2ad6122ccfdd75c5b8a38ea5d771c10bddf Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Wed, 18 Jun 2025 16:47:53 -0400 Subject: [PATCH] panic on wrong number of args to functions --- src/value.rs | 15 ++++++++++++++- src/vm.rs | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/value.rs b/src/value.rs index 190f343..b260fa7 100644 --- a/src/value.rs +++ b/src/value.rs @@ -45,6 +45,13 @@ impl LFn { } } + pub fn accepts(&self, arity: u8) -> bool { + match self { + LFn::Defined { chunks, .. } => chunks.iter().any(|(a, _)| *a == arity), + LFn::Declared { .. } => unreachable!(), + } + } + pub fn name(&self) -> &'static str { match self { LFn::Declared { name } | LFn::Defined { name, .. } => name, @@ -52,7 +59,6 @@ impl LFn { } pub fn chunk(&self, arity: u8) -> &Chunk { - // println!("Getting chunk of {arity} from {:?}", self); match self { LFn::Declared { .. } => unreachable!(), LFn::Defined { chunks, .. } => &chunks.iter().find(|(a, _)| *a == arity).unwrap().1, @@ -268,4 +274,11 @@ impl Value { Partial(..) => "fn", } } + + pub fn as_fn(&self) -> &LFn { + match self { + Value::Fn(inner) => inner.as_ref(), + _ => unreachable!(), + } + } } diff --git a/src/vm.rs b/src/vm.rs index 8555a78..503e63b 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -790,6 +790,12 @@ impl Vm { match val { Value::Fn(_) => { + if !val.as_fn().accepts(arity) { + return self.panic_with(format!( + "wrong number of arguments to {} passing {arity} args", + val.show() + )); + } let mut frame = CallFrame { function: val, arity,