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,