Interpreter - Initialize support in instances

* Doesn't work at this point
This commit is contained in:
Emmanuel BENOîT 2023-01-11 07:50:02 +01:00
parent 783a2e3dd0
commit 3af544a78e

View file

@ -45,16 +45,26 @@ impl Display for Class {
impl Callable for ClassRef { impl Callable for ClassRef {
fn arity(&self) -> usize { fn arity(&self) -> usize {
if let Some(init) = self.borrow().methods.get("init") {
init.arity()
} else {
0 0
} }
}
fn call( fn call(&self, itpr_state: &mut InterpreterState, arguments: Vec<Value>) -> SloxResult<Value> {
&self, let inst_value = Value::from(Instance::new(self.clone()));
_itpr_state: &mut InterpreterState, if let Some(init) = self.borrow().methods.get("init") {
_arguments: Vec<Value>, inst_value.with_instance(
) -> SloxResult<Value> { |instance| {
let instance = Instance::new(self.clone()); let bound_init = instance.bind_method(init, &inst_value);
Ok(Value::from(instance)) bound_init.call(itpr_state, arguments)
},
|| panic!("Instance is not an instance, wtf"),
)
} else {
Ok(inst_value)
}
} }
} }