diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 7625802e1..a0b396482 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -12,7 +12,9 @@ pub mod eval; mod frame; mod import; mod objbool; +mod objclass; mod objdict; +mod objfunction; mod objint; mod objlist; mod objsequence; diff --git a/vm/src/objclass.rs b/vm/src/objclass.rs new file mode 100644 index 000000000..28f48874c --- /dev/null +++ b/vm/src/objclass.rs @@ -0,0 +1,29 @@ +use super::pyobject::AttributeProtocol; +use super::pyobject::{PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol}; +use super::vm::VirtualMachine; + +pub fn get_attribute( + vm: &mut VirtualMachine, + cls: PyObjectRef, + obj: PyObjectRef, + name: &String, +) -> PyResult { + if obj.has_attr(name) { + Ok(obj.get_attr(name)) + } else if cls.has_attr(name) { + let attr = cls.get_attr(name); + let attr_class = attr.typ(); + if attr_class.has_attr(&String::from("__get__")) { + vm.invoke( + attr_class.get_attr(&String::from("__get__")), + PyFuncArgs { + args: vec![attr, cls], + }, + ) + } else { + Ok(attr) + } + } else { + Err(vm.new_exception(String::from("TypeError goes here!"))) + } +} diff --git a/vm/src/objfunction.rs b/vm/src/objfunction.rs new file mode 100644 index 000000000..61d1746c4 --- /dev/null +++ b/vm/src/objfunction.rs @@ -0,0 +1,28 @@ +use super::pyobject::{PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol}; +use super::vm::VirtualMachine; +use std::collections::HashMap; + +pub fn create_type(type_type: PyObjectRef) -> PyObjectRef { + let mut dict = HashMap::new(); + dict.insert( + "__get__".to_string(), + PyObject::new( + PyObjectKind::RustFunction { + function: bind_method, + }, + type_type.clone(), + ), + ); + let typ = PyObject::new( + PyObjectKind::Class { + name: "function".to_string(), + dict: PyObject::new(PyObjectKind::Dict { elements: dict }, type_type.clone()), + }, + type_type.clone(), + ); + typ +} + +fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + unimplemented!("not yet!"); +}