Add internal error handling to VM, plus function arity
* VM is able to handle basic runtime errors although there is no way to catch this in executing code currently * If a function is called with the wrong number of arguments (arity) it will invoke a runtime error. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,45 +1,46 @@
|
||||
use crate::{obj::{prelude::*, reserved::*}, vm::signal::*};
|
||||
use crate::{obj::{prelude::*, reserved::*}, vm::{error::*, signal::*}};
|
||||
use maplit::btreemap;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub static PRINTLN_BUILTIN_FUN: Lazy<NativeFunRef> = Lazy::new(|| NativeFun::new_obj(|_, vm, args| {
|
||||
pub static PRINTLN_BUILTIN_FUN: Lazy<NativeFunRef> = Lazy::new(|| NativeFun::new_obj(1, |_, vm, args| {
|
||||
// TODO : use __get_attr__ when it gets added
|
||||
let to_string = {
|
||||
let obj_ref = &args[0];
|
||||
read_obj!(let obj = obj_ref);
|
||||
obj.get_attr(STR_MEMBER_NAME.sym)
|
||||
.or_else(|| obj.get_attr(REPR_MEMBER_NAME.sym))
|
||||
.expect("no __str__ or __repr__ member")
|
||||
.ok_or(Error::MissingAttr { attr: STR_MEMBER_NAME.sym })?
|
||||
};
|
||||
let return_value = vm.call(to_string, vec![]);
|
||||
let return_value = vm.call(to_string, vec![])?;
|
||||
{
|
||||
read_obj!(let str_obj = return_value);
|
||||
let str_obj: &Str = str_obj.as_any().downcast_ref().unwrap();
|
||||
let str_obj: &Str = str_obj.as_any().downcast_ref()
|
||||
.ok_or_else(|| Error::ValueError { error: "expected str value".to_string(), value: return_value.clone() })?;
|
||||
println!("{}", str_obj.value());
|
||||
}
|
||||
|
||||
vm.push(NIL_NAME.sym_ref());
|
||||
Signal::Return
|
||||
Ok(Signal::Return)
|
||||
}));
|
||||
|
||||
pub static PRINT_BUILTIN_FUN: Lazy<NativeFunRef> = Lazy::new(|| NativeFun::new_obj(|_, vm, args| {
|
||||
pub static PRINT_BUILTIN_FUN: Lazy<NativeFunRef> = Lazy::new(|| NativeFun::new_obj(1, |_, vm, args| {
|
||||
// TODO : use __get_attr__ when it gets added
|
||||
let to_string = {
|
||||
let obj_ref = &args[0];
|
||||
read_obj!(let obj = obj_ref);
|
||||
obj.get_attr(STR_MEMBER_NAME.sym)
|
||||
.or_else(|| obj.get_attr(REPR_MEMBER_NAME.sym))
|
||||
.expect("no __str__ or __repr__ member")
|
||||
.ok_or(Error::MissingAttr { attr: STR_MEMBER_NAME.sym })?
|
||||
};
|
||||
let return_value = vm.call(to_string, vec![]);
|
||||
let return_value = vm.call(to_string, vec![])?;
|
||||
{
|
||||
read_obj!(let str_obj = return_value);
|
||||
let str_obj: &Str = str_obj.as_any().downcast_ref().unwrap();
|
||||
let str_obj: &Str = str_obj.as_any().downcast_ref()
|
||||
.ok_or_else(|| Error::ValueError { error: "expected str value".to_string(), value: return_value.clone() })?;
|
||||
print!("{}", str_obj.value());
|
||||
}
|
||||
|
||||
Signal::Return
|
||||
vm.push(NIL_NAME.sym_ref());
|
||||
Ok(Signal::Return)
|
||||
}));
|
||||
|
||||
pub static BUILTIN_OBJS: Lazy<BTreeMap<Sym, ObjRef>> = Lazy::new(|| btreemap! {
|
||||
|
||||
Reference in New Issue
Block a user