Move more objects to obj folder

This commit is contained in:
Windel Bouwman
2018-09-01 10:00:55 +02:00
parent 5cdd0ab7b8
commit 40cf7f4579
10 changed files with 19 additions and 19 deletions

View File

@@ -1,7 +1,10 @@
pub mod objbytes;
pub mod objdict;
pub mod objfloat;
pub mod objfunction;
pub mod objint;
pub mod objlist;
pub mod objobject;
pub mod objsequence;
pub mod objstr;
pub mod objtype;

24
vm/src/obj/objfunction.rs Normal file
View File

@@ -0,0 +1,24 @@
use super::super::pyobject::{AttributeProtocol, PyContext, PyFuncArgs, PyResult};
use super::super::vm::VirtualMachine;
pub fn init(context: &PyContext) {
let ref function_type = context.function_type;
function_type.set_attr("__get__", context.new_rustfunc(bind_method));
let ref member_descriptor_type = context.member_descriptor_type;
member_descriptor_type.set_attr("__get__", context.new_rustfunc(member_get));
}
fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.new_bound_method(args.args[0].clone(), args.args[1].clone()))
}
fn member_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
match args.shift().get_attr("function") {
Some(function) => vm.invoke(function, args),
None => {
let attribute_error = vm.context().exceptions.attribute_error.clone();
Err(vm.new_exception(attribute_error, String::from("Attribute Error")))
}
}
}

View File

@@ -1,8 +1,8 @@
use super::super::objsequence::PySliceableSequence;
use super::super::pyobject::{
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objsequence::PySliceableSequence;
use super::objstr;
use super::objtype;

52
vm/src/obj/objobject.rs Normal file
View File

@@ -0,0 +1,52 @@
use super::super::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objdict;
use super::objtype;
pub fn new_instance(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
// more or less __new__ operator
let type_ref = args.shift();
let dict = vm.new_dict();
let obj = PyObject::new(PyObjectKind::Instance { dict: dict }, type_ref.clone());
Ok(obj)
}
pub fn call(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
let instance = args.shift();
let function = objtype::get_attribute(vm, instance, &String::from("__call__"))?;
vm.invoke(function, args)
}
pub fn create_object(type_type: PyObjectRef, object_type: PyObjectRef, dict_type: PyObjectRef) {
(*object_type.borrow_mut()).kind = PyObjectKind::Class {
name: String::from("object"),
dict: objdict::new(dict_type),
mro: vec![],
};
(*object_type.borrow_mut()).typ = Some(type_type.clone());
}
fn obj_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]);
let type_name = objtype::get_type_name(&obj.typ());
let address = obj.get_id();
Ok(vm.new_str(format!("<{} object at 0x{:x}>", type_name, address)))
}
pub fn init(context: &PyContext) {
let ref object = context.object;
object.set_attr("__new__", context.new_rustfunc(new_instance));
object.set_attr("__dict__", context.new_member_descriptor(object_dict));
object.set_attr("__str__", context.new_rustfunc(obj_str));
}
fn object_dict(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match args.args[0].borrow().kind {
PyObjectKind::Class { ref dict, .. } => Ok(dict.clone()),
PyObjectKind::Instance { ref dict, .. } => Ok(dict.clone()),
_ => Err(vm.new_type_error("TypeError: no dictionary.".to_string())),
}
}

108
vm/src/obj/objsequence.rs Normal file
View File

@@ -0,0 +1,108 @@
use super::super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult};
use super::super::vm::VirtualMachine;
use std::marker::Sized;
pub trait PySliceableSequence {
fn do_slice(&self, start: usize, stop: usize) -> Self;
fn do_stepped_slice(&self, start: usize, stop: usize, step: usize) -> Self;
fn len(&self) -> usize;
fn get_pos(&self, p: i32) -> usize {
if p < 0 {
self.len() - ((-p) as usize)
} else if p as usize > self.len() {
// This is for the slicing case where the end element is greater than the length of the
// sequence
self.len()
} else {
p as usize
}
}
fn get_slice_items(&self, slice: &PyObjectRef) -> Self
where
Self: Sized,
{
// TODO: we could potentially avoid this copy and use slice
match &(slice.borrow()).kind {
PyObjectKind::Slice { start, stop, step } => {
let start = match start {
&Some(start) => self.get_pos(start),
&None => 0,
};
let stop = match stop {
&Some(stop) => self.get_pos(stop),
&None => self.len() as usize,
};
match step {
&None | &Some(1) => self.do_slice(start, stop),
&Some(num) => {
if num < 0 {
unimplemented!("negative step indexing not yet supported")
};
self.do_stepped_slice(start, stop, num as usize)
}
}
}
kind => panic!("get_slice_items called with non-slice: {:?}", kind),
}
}
}
impl PySliceableSequence for Vec<PyObjectRef> {
fn do_slice(&self, start: usize, stop: usize) -> Self {
self[start..stop].to_vec()
}
fn do_stepped_slice(&self, start: usize, stop: usize, step: usize) -> Self {
self[start..stop].iter().step_by(step).cloned().collect()
}
fn len(&self) -> usize {
self.len()
}
}
pub fn get_item(
vm: &mut VirtualMachine,
sequence: &PyObjectRef,
elements: &Vec<PyObjectRef>,
subscript: PyObjectRef,
) -> PyResult {
match &(subscript.borrow()).kind {
PyObjectKind::Integer { value } => {
let pos_index = elements.get_pos(*value);
if pos_index < elements.len() {
let obj = elements[pos_index].clone();
Ok(obj)
} else {
let value_error = vm.context().exceptions.value_error.clone();
Err(vm.new_exception(value_error, "Index out of bounds!".to_string()))
}
}
PyObjectKind::Slice {
start: _,
stop: _,
step: _,
} => Ok(PyObject::new(
match &(sequence.borrow()).kind {
PyObjectKind::Tuple { elements: _ } => PyObjectKind::Tuple {
elements: elements.get_slice_items(&subscript),
},
PyObjectKind::List { elements: _ } => PyObjectKind::List {
elements: elements.get_slice_items(&subscript),
},
ref kind => panic!("sequence get_item called for non-sequence: {:?}", kind),
},
vm.get_type(),
)),
_ => Err(vm.new_type_error(format!(
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",
sequence, subscript
))),
}
}
pub fn get_elements(obj: PyObjectRef) -> Vec<PyObjectRef> {
if let PyObjectKind::Tuple { elements } = &obj.borrow().kind {
elements.to_vec()
} else {
panic!("Cannot extract list elements from non-list");
}
}

View File

@@ -1,9 +1,9 @@
use super::super::objsequence::PySliceableSequence;
use super::super::pyobject::{
AttributeProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objint;
use super::objsequence::PySliceableSequence;
use super::objtype;
pub fn init(context: &PyContext) {