Move to VTableBuilder for IntObj VTable, add __splat__ impl

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-02-04 18:43:55 -08:00
parent 9e5b1b3176
commit 3b034052e5

View File

@@ -11,11 +11,29 @@ pub struct IntObj {
vtable: VTable, vtable: VTable,
} }
impl IntObj {
pub fn new(value: Int) -> Gc<Self> {
thread_local! { thread_local! {
static VTABLE: VTable = vtable! { static VTABLE: VTable = VTableBuilder::default()
"__bool__" => builtin_fn!("__bool__", |machine, _| { .with_builtin("__splat__", |machine, _| {
let rhs_ptr: ObjPtr = machine.stack_pop()?;
let lhs_ptr: ObjPtr = machine.stack_pop()?;
let lhs = lhs_ptr.as_any().downcast_ref::<IntObj>();
let rhs = rhs_ptr.as_any().downcast_ref::<IntObj>();
match (lhs, rhs) {
(Some(lhs), Some(rhs)) => {
let result = IntObj::new(lhs.value() * rhs.value());
machine.stack_push(result)?;
Ok(BuiltinExit::Return)
}
(Some(_lhs), None) => {
Err(RuntimeError::WrongValue("int value (rhs)".to_string()))
},
(None, Some(_rhs)) => {
Err(RuntimeError::WrongValue("int value (lhs)".to_string()))
}
(None, None) => { panic!("called an int builtin with no int values at all"); }
}
})
.with_builtin("__bool__", |machine, _| {
let obj_ptr: ObjPtr = machine.stack_pop()?; let obj_ptr: ObjPtr = machine.stack_pop()?;
let obj: &(dyn Obj + 'static) = &*obj_ptr; let obj: &(dyn Obj + 'static) = &*obj_ptr;
if let Some(int) = obj.as_any().downcast_ref::<IntObj>() { if let Some(int) = obj.as_any().downcast_ref::<IntObj>() {
@@ -25,8 +43,8 @@ impl IntObj {
} else { } else {
Err(RuntimeError::WrongValue("int".to_string())) Err(RuntimeError::WrongValue("int".to_string()))
} }
}), })
"__str__" => builtin_fn!("__str__", |machine, _| { .with_builtin("__str__", |machine, _| {
let obj_ptr: ObjPtr = machine.stack_pop()?; let obj_ptr: ObjPtr = machine.stack_pop()?;
let obj: &(dyn Obj + 'static) = &*obj_ptr; let obj: &(dyn Obj + 'static) = &*obj_ptr;
if let Some(int) = obj.as_any().downcast_ref::<IntObj>() { if let Some(int) = obj.as_any().downcast_ref::<IntObj>() {
@@ -36,9 +54,12 @@ impl IntObj {
} else { } else {
Err(RuntimeError::WrongValue("int".to_string())) Err(RuntimeError::WrongValue("int".to_string()))
} }
}), })
}; .finish();
}; }
impl IntObj {
pub fn new(value: Int) -> Gc<Self> {
// TODO : intern int value // TODO : intern int value
Gc::new(IntObj { Gc::new(IntObj {
value, value,