Move to VTableBuilder for IntObj VTable, add __splat__ impl
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -11,11 +11,29 @@ pub struct IntObj {
|
|||||||
vtable: VTable,
|
vtable: VTable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntObj {
|
thread_local! {
|
||||||
pub fn new(value: Int) -> Gc<Self> {
|
static VTABLE: VTable = VTableBuilder::default()
|
||||||
thread_local! {
|
.with_builtin("__splat__", |machine, _| {
|
||||||
static VTABLE: VTable = vtable! {
|
let rhs_ptr: ObjPtr = machine.stack_pop()?;
|
||||||
"__bool__" => builtin_fn!("__bool__", |machine, _| {
|
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user