From 45bc8c8f7a983d3dad5371b7823e34fc676bf2de Mon Sep 17 00:00:00 2001 From: Joey Hain Date: Sun, 10 Mar 2019 12:17:50 -0700 Subject: [PATCH 1/5] Move PyRef to pyobject module --- vm/src/function.rs | 118 -------------------------------------- vm/src/lib.rs | 1 - vm/src/obj/objdict.rs | 10 ++-- vm/src/obj/objfloat.rs | 5 +- vm/src/obj/objint.rs | 3 +- vm/src/obj/objlist.rs | 3 +- vm/src/obj/objmodule.rs | 3 +- vm/src/obj/objnone.rs | 4 +- vm/src/obj/objobject.rs | 4 +- vm/src/obj/objproperty.rs | 8 +-- vm/src/obj/objstr.rs | 3 +- vm/src/obj/objtuple.rs | 3 +- vm/src/obj/objtype.rs | 14 +++-- vm/src/obj/objweakref.rs | 3 +- vm/src/pyobject.rs | 108 +++++++++++++++++++++++++++++++++- 15 files changed, 135 insertions(+), 155 deletions(-) delete mode 100644 vm/src/function.rs diff --git a/vm/src/function.rs b/vm/src/function.rs deleted file mode 100644 index c978be7085..0000000000 --- a/vm/src/function.rs +++ /dev/null @@ -1,118 +0,0 @@ -use std::fmt; -use std::marker::PhantomData; -use std::ops::Deref; - -use crate::obj::objtype; -use crate::obj::objtype::PyClassRef; -use crate::pyobject::{ - IntoPyObject, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TryFromObject, TypeProtocol, -}; -use crate::vm::VirtualMachine; - -// TODO: Move PyFuncArgs, FromArgs, etc. here - -// TODO: `PyRef` probably actually belongs in the pyobject module. - -/// A reference to the payload of a built-in object. -/// -/// Note that a `PyRef` can only deref to a shared / immutable reference. -/// It is the payload type's responsibility to handle (possibly concurrent) -/// mutability with locks or concurrent data structures if required. -/// -/// A `PyRef` can be directly returned from a built-in function to handle -/// situations (such as when implementing in-place methods such as `__iadd__`) -/// where a reference to the same object must be returned. -#[derive(Clone)] -pub struct PyRef { - // invariant: this obj must always have payload of type T - obj: PyObjectRef, - _payload: PhantomData, -} - -impl PyRef -where - T: PyObjectPayload2, -{ - pub fn new(ctx: &PyContext, payload: T) -> Self { - PyRef { - obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - T::required_type(ctx), - ), - _payload: PhantomData, - } - } - - pub fn new_with_type(vm: &mut VirtualMachine, payload: T, cls: PyClassRef) -> PyResult { - let required_type = T::required_type(&vm.ctx); - if objtype::issubclass(&cls.obj, &required_type) { - Ok(PyRef { - obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - cls.obj, - ), - _payload: PhantomData, - }) - } else { - let subtype = vm.to_pystr(&cls.obj)?; - let basetype = vm.to_pystr(&required_type)?; - Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype))) - } - } - - pub fn as_object(&self) -> &PyObjectRef { - &self.obj - } - pub fn into_object(self) -> PyObjectRef { - self.obj - } -} - -impl Deref for PyRef -where - T: PyObjectPayload2, -{ - type Target = T; - - fn deref(&self) -> &T { - self.obj.payload().expect("unexpected payload for type") - } -} - -impl TryFromObject for PyRef -where - T: PyObjectPayload2, -{ - fn try_from_object(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult { - if objtype::isinstance(&obj, &T::required_type(&vm.ctx)) { - Ok(PyRef { - obj, - _payload: PhantomData, - }) - } else { - let expected_type = vm.to_pystr(&T::required_type(&vm.ctx))?; - let actual_type = vm.to_pystr(&obj.typ())?; - Err(vm.new_type_error(format!( - "Expected type {}, not {}", - expected_type, actual_type, - ))) - } - } -} - -impl IntoPyObject for PyRef { - fn into_pyobject(self, _ctx: &PyContext) -> PyResult { - Ok(self.obj) - } -} - -impl fmt::Display for PyRef { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.obj.fmt(f) - } -} diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 78696f9f3f..b32b91cb7d 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -41,7 +41,6 @@ pub mod eval; mod exceptions; pub mod format; pub mod frame; -pub mod function; pub mod import; pub mod obj; pub mod pyobject; diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index fe27b5b28f..84391cdd87 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -1,17 +1,17 @@ -use crate::function::PyRef; use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::ops::{Deref, DerefMut}; -use super::objiter; -use super::objstr; -use super::objtype; use crate::pyobject::{ PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; +use super::objiter; +use super::objstr; +use super::objtype; + pub type DictContentType = HashMap; #[derive(Default, Debug)] diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index f544adcc36..6f1790d682 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -2,10 +2,9 @@ use super::objbytes; use super::objint; use super::objstr; use super::objtype; -use crate::function::PyRef; use crate::pyobject::{ - IntoPyObject, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + IntoPyObject, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyRef, + PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::ToBigInt; diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 8e5f3f59c6..5b751faa0f 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -5,10 +5,9 @@ use num_integer::Integer; use num_traits::{Pow, Signed, ToPrimitive, Zero}; use crate::format::FormatSpec; -use crate::function::PyRef; use crate::pyobject::{ FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyResult, TryFromObject, TypeProtocol, + PyObjectPayload2, PyObjectRef, PyRef, PyResult, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index 8ad69bff31..ecabb009aa 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -8,10 +8,9 @@ use super::objsequence::{ }; use super::objstr; use super::objtype; -use crate::function::PyRef; use crate::pyobject::{ IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; use num_traits::ToPrimitive; diff --git a/vm/src/obj/objmodule.rs b/vm/src/obj/objmodule.rs index 4f7a641b99..92f55566d2 100644 --- a/vm/src/obj/objmodule.rs +++ b/vm/src/obj/objmodule.rs @@ -1,5 +1,4 @@ -use crate::function::PyRef; -use crate::pyobject::{DictProtocol, PyContext, PyObjectPayload2, PyObjectRef, PyResult}; +use crate::pyobject::{DictProtocol, PyContext, PyObjectPayload2, PyObjectRef, PyRef, PyResult}; use crate::vm::VirtualMachine; #[derive(Clone, Debug)] diff --git a/vm/src/obj/objnone.rs b/vm/src/obj/objnone.rs index 80980f5283..6cde6ef063 100644 --- a/vm/src/obj/objnone.rs +++ b/vm/src/obj/objnone.rs @@ -1,6 +1,6 @@ -use crate::function::PyRef; use crate::pyobject::{ - IntoPyObject, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + IntoPyObject, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; diff --git a/vm/src/obj/objobject.rs b/vm/src/obj/objobject.rs index 0d9d21f1ea..54fcd1b6eb 100644 --- a/vm/src/obj/objobject.rs +++ b/vm/src/obj/objobject.rs @@ -1,10 +1,9 @@ use super::objstr; use super::objtype; -use crate::function::PyRef; use crate::obj::objproperty::PropertyBuilder; use crate::pyobject::{ AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, - PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, + PyObjectPayload, PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use std::cell::RefCell; @@ -12,6 +11,7 @@ use std::collections::HashMap; #[derive(Clone, Debug)] pub struct PyInstance; + pub type PyInstanceRef = PyRef; pub fn new_instance(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index b94692177e..9aaea31cdc 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -2,15 +2,15 @@ */ -use crate::function::PyRef; +use std::marker::PhantomData; + use crate::obj::objstr::PyStringRef; use crate::obj::objtype::PyClassRef; -use crate::pyobject::IntoPyNativeFunc; use crate::pyobject::{ - OptionalArg, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, + IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, + PyObjectRef, PyRef, PyResult, }; use crate::VirtualMachine; -use std::marker::PhantomData; /// Read-only property, doesn't have __set__ or __delete__ #[derive(Debug)] diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 09ef07576d..d9c39e3371 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -6,10 +6,9 @@ use num_traits::ToPrimitive; use unicode_segmentation::UnicodeSegmentation; use crate::format::{FormatParseError, FormatPart, FormatString}; -use crate::function::PyRef; use crate::pyobject::{ IntoPyObject, OptionalArg, PyContext, PyFuncArgs, PyIterable, PyObjectPayload2, PyObjectRef, - PyResult, TypeProtocol, + PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index e583055c30..95c1b12f98 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -1,10 +1,9 @@ use std::cell::{Cell, RefCell}; use std::hash::{Hash, Hasher}; -use crate::function::PyRef; use crate::pyobject::{ IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyResult, + PyObjectPayload2, PyObjectRef, PyRef, PyResult, }; use crate::vm::{ReprGuard, VirtualMachine}; diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index be4591dfcd..44bb9bc628 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -1,19 +1,21 @@ -use super::objdict; -use super::objstr; -use crate::function::PyRef; +use std::cell::RefCell; +use std::collections::HashMap; + use crate::pyobject::{ AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; -use std::cell::RefCell; -use std::collections::HashMap; + +use super::objdict; +use super::objstr; #[derive(Clone, Debug)] pub struct PyClass { pub name: String, pub mro: Vec, } + pub type PyClassRef = PyRef; impl PyObjectPayload2 for PyClass { diff --git a/vm/src/obj/objweakref.rs b/vm/src/obj/objweakref.rs index 8a4651de73..45cb4108a8 100644 --- a/vm/src/obj/objweakref.rs +++ b/vm/src/obj/objweakref.rs @@ -1,7 +1,6 @@ -use crate::function::PyRef; use crate::obj::objtype::PyClassRef; use crate::pyobject::PyObjectPayload2; -use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyResult}; +use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyRef, PyResult}; use crate::vm::VirtualMachine; use std::rc::{Rc, Weak}; diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 23c269324a..c7c99723c6 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -2,7 +2,8 @@ use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::fmt; use std::iter; -use std::ops::RangeInclusive; +use std::marker::PhantomData; +use std::ops::{Deref, RangeInclusive}; use std::rc::Rc; use num_bigint::BigInt; @@ -42,7 +43,7 @@ use crate::obj::objslice; use crate::obj::objstr; use crate::obj::objsuper; use crate::obj::objtuple::{self, PyTuple}; -use crate::obj::objtype::{self, PyClass}; +use crate::obj::objtype::{self, PyClass, PyClassRef}; use crate::obj::objweakref; use crate::obj::objzip; use crate::vm::VirtualMachine; @@ -741,6 +742,109 @@ pub struct PyObject { pub dict: Option>, // __dict__ member } +/// A reference to a Python object. +/// +/// Note that a `PyRef` can only deref to a shared / immutable reference. +/// It is the payload type's responsibility to handle (possibly concurrent) +/// mutability with locks or concurrent data structures if required. +/// +/// A `PyRef` can be directly returned from a built-in function to handle +/// situations (such as when implementing in-place methods such as `__iadd__`) +/// where a reference to the same object must be returned. +#[derive(Clone)] +pub struct PyRef { + // invariant: this obj must always have payload of type T + obj: PyObjectRef, + _payload: PhantomData, +} + +impl PyRef +where + T: PyObjectPayload2, +{ + pub fn new(ctx: &PyContext, payload: T) -> Self { + PyRef { + obj: PyObject::new( + PyObjectPayload::AnyRustValue { + value: Box::new(payload), + }, + T::required_type(ctx), + ), + _payload: PhantomData, + } + } + + pub fn new_with_type(vm: &mut VirtualMachine, payload: T, cls: PyClassRef) -> PyResult { + let required_type = T::required_type(&vm.ctx); + if objtype::issubclass(&cls.obj, &required_type) { + Ok(PyRef { + obj: PyObject::new( + PyObjectPayload::AnyRustValue { + value: Box::new(payload), + }, + cls.obj, + ), + _payload: PhantomData, + }) + } else { + let subtype = vm.to_pystr(&cls.obj)?; + let basetype = vm.to_pystr(&required_type)?; + Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype))) + } + } + + pub fn as_object(&self) -> &PyObjectRef { + &self.obj + } + pub fn into_object(self) -> PyObjectRef { + self.obj + } +} + +impl Deref for PyRef +where + T: PyObjectPayload2, +{ + type Target = T; + + fn deref(&self) -> &T { + self.obj.payload().expect("unexpected payload for type") + } +} + +impl TryFromObject for PyRef +where + T: PyObjectPayload2, +{ + fn try_from_object(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult { + if objtype::isinstance(&obj, &T::required_type(&vm.ctx)) { + Ok(PyRef { + obj, + _payload: PhantomData, + }) + } else { + let expected_type = vm.to_pystr(&T::required_type(&vm.ctx))?; + let actual_type = vm.to_pystr(&obj.typ())?; + Err(vm.new_type_error(format!( + "Expected type {}, not {}", + expected_type, actual_type, + ))) + } + } +} + +impl IntoPyObject for PyRef { + fn into_pyobject(self, _ctx: &PyContext) -> PyResult { + Ok(self.obj) + } +} + +impl fmt::Display for PyRef { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.obj.fmt(f) + } +} + pub trait IdProtocol { fn get_id(&self) -> usize; fn is(&self, other: &PyObjectRef) -> bool; From e2e13af7ea2af733754d7aca8c1debc367d0272f Mon Sep 17 00:00:00 2001 From: Joey Hain Date: Sun, 10 Mar 2019 13:45:38 -0700 Subject: [PATCH 2/5] Remove PyObjectPayload --- vm/src/compile.rs | 6 +- vm/src/frame.rs | 20 ++-- vm/src/obj/objbytearray.rs | 7 +- vm/src/obj/objbytes.rs | 21 ++-- vm/src/obj/objcomplex.rs | 10 +- vm/src/obj/objdict.rs | 44 ++++---- vm/src/obj/objenumerate.rs | 13 +-- vm/src/obj/objfilter.rs | 14 ++- vm/src/obj/objfloat.rs | 10 +- vm/src/obj/objgenerator.rs | 7 +- vm/src/obj/objint.rs | 11 +- vm/src/obj/objlist.rs | 18 ++-- vm/src/obj/objmap.rs | 13 +-- vm/src/obj/objmemory.rs | 11 +- vm/src/obj/objobject.rs | 12 +-- vm/src/obj/objproperty.rs | 18 +--- vm/src/obj/objrange.rs | 45 +++------ vm/src/obj/objsequence.rs | 32 +++--- vm/src/obj/objset.rs | 54 ++++------ vm/src/obj/objslice.rs | 15 ++- vm/src/obj/objtuple.rs | 18 ++-- vm/src/obj/objtype.rs | 24 ++--- vm/src/obj/objzip.rs | 10 +- vm/src/pyobject.rs | 200 ++++++++----------------------------- vm/src/stdlib/re.rs | 24 +---- vm/src/stdlib/socket.rs | 21 +--- 26 files changed, 210 insertions(+), 468 deletions(-) diff --git a/vm/src/compile.rs b/vm/src/compile.rs index 21ce3a99fa..5a3fe6b1da 100644 --- a/vm/src/compile.rs +++ b/vm/src/compile.rs @@ -8,7 +8,7 @@ use crate::bytecode::{self, CallType, CodeObject, Instruction}; use crate::error::CompileError; use crate::obj::objcode; -use crate::pyobject::{PyObject, PyObjectPayload, PyObjectRef}; +use crate::pyobject::{PyObject, PyObjectRef}; use num_complex::Complex64; use rustpython_parser::{ast, parser}; @@ -50,9 +50,7 @@ pub fn compile( let code = compiler.pop_code_object(); trace!("Compilation completed: {:?}", code); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objcode::PyCode::new(code)), - }, + Box::new(objcode::PyCode::new(code)), code_type, )) } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 0d87886370..2e100f7914 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -22,8 +22,8 @@ use crate::obj::objslice::PySlice; use crate::obj::objstr; use crate::obj::objtype; use crate::pyobject::{ - DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TryFromObject, TypeProtocol, + DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, + PyResult, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -407,12 +407,8 @@ impl Frame { let stop = out[1].take(); let step = if out.len() == 3 { out[2].take() } else { None }; - let obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySlice { start, stop, step }), - }, - vm.ctx.slice_type(), - ); + let obj = + PyObject::new(Box::new(PySlice { start, stop, step }), vm.ctx.slice_type()); self.push_value(obj); Ok(None) } @@ -706,11 +702,9 @@ impl Frame { } bytecode::Instruction::LoadBuildClass => { let rustfunc = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyBuiltinFunction::new(Box::new( - builtins::builtin_build_class_, - ))), - }, + Box::new(PyBuiltinFunction::new(Box::new( + builtins::builtin_build_class_, + ))), vm.ctx.type_type(), ); self.push_value(rustfunc); diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 77b1c14ebc..d2a098edfc 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -5,8 +5,7 @@ use std::fmt::Write; use std::ops::{Deref, DerefMut}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use super::objint; @@ -174,9 +173,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { vec![] }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyByteArray::new(value)), - }, + Box::new(PyByteArray::new(value)), cls.clone(), )) } diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index 2aa0006ddf..31fe95e14c 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -5,8 +5,8 @@ use std::ops::Deref; use super::objint; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; use num_traits::ToPrimitive; @@ -95,12 +95,7 @@ fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { vec![] }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyBytes::new(value)), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyBytes::new(value)), cls.clone())) } fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -209,12 +204,10 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: obj.clone(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: obj.clone(), + }), vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index 66205559c9..afafe48ed0 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -2,8 +2,7 @@ use super::objfloat; use super::objint; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_complex::Complex64; @@ -90,12 +89,7 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let value = Complex64::new(real, imag); - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyComplex { value }), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyComplex { value }), cls.clone())) } fn complex_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index 84391cdd87..7e3cef048c 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use std::ops::{Deref, DerefMut}; use crate::pyobject::{ - PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, + PyRef, PyResult, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -251,12 +251,10 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let key_list = vm.ctx.new_list(keys); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: key_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: key_list, + }), vm.ctx.iter_type(), ); @@ -273,12 +271,10 @@ fn dict_values(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let values_list = vm.ctx.new_list(values); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: values_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: values_list, + }), vm.ctx.iter_type(), ); @@ -295,12 +291,10 @@ fn dict_items(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let items_list = vm.ctx.new_list(items); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: items_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: items_list, + }), vm.ctx.iter_type(), ); @@ -348,12 +342,10 @@ pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, dict_type: // this is not ideal let ptr = PyObjectRef::into_raw(dict_type.clone()) as *mut PyObject; unsafe { - (*ptr).payload = PyObjectPayload::AnyRustValue { - value: Box::new(objtype::PyClass { - name: String::from("dict"), - mro: vec![object_type], - }), - }; + (*ptr).payload = Box::new(objtype::PyClass { + name: String::from("dict"), + mro: vec![object_type], + }); (*ptr).dict = Some(RefCell::new(HashMap::new())); (*ptr).typ = Some(type_type.clone()); } diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index aa573ad6f0..d02fc20867 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -4,8 +4,7 @@ use std::ops::AddAssign; use super::objint; use super::objiter; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::BigInt; @@ -37,12 +36,10 @@ fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; let iterator = objiter::get_iter(vm, iterable)?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyEnumerate { - counter: RefCell::new(counter), - iterator, - }), - }, + Box::new(PyEnumerate { + counter: RefCell::new(counter), + iterator, + }), cls.clone(), )) } diff --git a/vm/src/obj/objfilter.rs b/vm/src/obj/objfilter.rs index 94703be575..009c7da90c 100644 --- a/vm/src/obj/objfilter.rs +++ b/vm/src/obj/objfilter.rs @@ -1,6 +1,6 @@ use crate::pyobject::{ - IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, - PyResult, TypeProtocol, + IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance @@ -27,12 +27,10 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let iterator = objiter::get_iter(vm, iterable)?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFilter { - predicate: function.clone(), - iterator, - }), - }, + Box::new(PyFilter { + predicate: function.clone(), + iterator, + }), cls.clone(), )) } diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 6f1790d682..efbd4fed40 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -3,8 +3,7 @@ use super::objint; use super::objstr; use super::objtype; use crate::pyobject::{ - IntoPyObject, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyRef, - PyResult, TypeProtocol, + IntoPyObject, PyContext, PyObject, PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::ToBigInt; @@ -189,12 +188,7 @@ impl PyFloatRef { let type_name = objtype::get_type_name(&arg.typ()); return Err(vm.new_type_error(format!("can't convert {} to float", type_name))); }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFloat { value }), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyFloat { value }), cls.clone())) } fn mod_(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult { diff --git a/vm/src/obj/objgenerator.rs b/vm/src/obj/objgenerator.rs index 1babd5c9dc..4e2f40a125 100644 --- a/vm/src/obj/objgenerator.rs +++ b/vm/src/obj/objgenerator.rs @@ -4,8 +4,7 @@ use crate::frame::{ExecutionResult, Frame}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -41,9 +40,7 @@ pub fn init(context: &PyContext) { pub fn new_generator(vm: &mut VirtualMachine, frame: PyObjectRef) -> PyResult { Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyGenerator { frame }), - }, + Box::new(PyGenerator { frame }), vm.ctx.generator_type.clone(), )) } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 5b751faa0f..4352e97c61 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -6,8 +6,8 @@ use num_traits::{Pow, Signed, ToPrimitive, Zero}; use crate::format::FormatSpec; use crate::pyobject::{ - FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TryFromObject, TypeProtocol, + FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, + PyRef, PyResult, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -105,12 +105,7 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Some(val) => to_int(vm, val, base)?, None => Zero::zero(), }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(val)), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyInt::new(val)), cls.clone())) } // Casting function: diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index ecabb009aa..29d8dc386a 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -9,8 +9,8 @@ use super::objsequence::{ use super::objstr; use super::objtype; use crate::pyobject::{ - IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, + PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; use num_traits::ToPrimitive; @@ -111,12 +111,10 @@ impl PyListRef { fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: self.into_object(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: self.into_object(), + }), vm.ctx.iter_type(), ) } @@ -305,9 +303,7 @@ fn list_new( }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyList::from(elements)), - }, + Box::new(PyList::from(elements)), cls.into_object(), )) } diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index 4cbb8d3ee9..eb748bc01f 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -31,12 +30,10 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyMap { - mapper: function.clone(), - iterators, - }), - }, + Box::new(PyMap { + mapper: function.clone(), + iterators, + }), cls.clone(), )) } diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index 988abbed9c..c5669532c7 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -19,11 +18,9 @@ pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]); vm.ctx.set_attr(&cls, "obj", bytes_object.clone()); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyMemoryView { - obj: bytes_object.clone(), - }), - }, + Box::new(PyMemoryView { + obj: bytes_object.clone(), + }), cls.clone(), )) } diff --git a/vm/src/obj/objobject.rs b/vm/src/obj/objobject.rs index 54fcd1b6eb..7a560399c5 100644 --- a/vm/src/obj/objobject.rs +++ b/vm/src/obj/objobject.rs @@ -3,7 +3,7 @@ use super::objtype; use crate::obj::objproperty::PropertyBuilder; use crate::pyobject::{ AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, - PyObjectPayload, PyObjectRef, PyRef, PyResult, TypeProtocol, + PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use std::cell::RefCell; @@ -25,12 +25,10 @@ pub fn create_object(type_type: PyObjectRef, object_type: PyObjectRef, _dict_typ // this is not ideal let ptr = PyObjectRef::into_raw(object_type.clone()) as *mut PyObject; unsafe { - (*ptr).payload = PyObjectPayload::AnyRustValue { - value: Box::new(objtype::PyClass { - name: String::from("object"), - mro: vec![], - }), - }; + (*ptr).payload = Box::new(objtype::PyClass { + name: String::from("object"), + mro: vec![], + }); (*ptr).dict = Some(RefCell::new(HashMap::new())); (*ptr).typ = Some(type_type.clone()); } diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index 9aaea31cdc..03c012ad50 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -7,8 +7,8 @@ use std::marker::PhantomData; use crate::obj::objstr::PyStringRef; use crate::obj::objtype::PyClassRef; use crate::pyobject::{ - IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyRef, PyResult, + IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectPayload2, PyObjectRef, PyRef, + PyResult, }; use crate::VirtualMachine; @@ -138,12 +138,7 @@ impl<'a, T> PropertyBuilder<'a, T> { deleter: None, }; - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - self.ctx.property_type(), - ) + PyObject::new(Box::new(payload), self.ctx.property_type()) } else { let payload = PyReadOnlyProperty { getter: self.getter.expect( @@ -151,12 +146,7 @@ impl<'a, T> PropertyBuilder<'a, T> { ), }; - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - self.ctx.readonly_property_type(), - ) + PyObject::new(Box::new(payload), self.ctx.readonly_property_type()) } } } diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index f249a1f486..3100d2351d 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -6,8 +6,8 @@ use num_integer::Integer; use num_traits::{One, Signed, ToPrimitive, Zero}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::VirtualMachine; @@ -229,9 +229,7 @@ fn range_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Err(vm.new_value_error("range with 0 step size".to_string())) } else { Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyRange { start, end, step }), - }, + Box::new(PyRange { start, end, step }), cls.clone(), )) } @@ -241,12 +239,10 @@ fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(range, Some(vm.ctx.range_type()))]); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: range.clone(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: range.clone(), + }), vm.ctx.iter_type(), )) } @@ -257,17 +253,10 @@ fn range_reversed(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let range = get_value(zelf).reversed(); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(range), - }, - vm.ctx.range_type(), - ), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: PyObject::new(Box::new(range), vm.ctx.range_type()), + }), vm.ctx.iter_type(), )) } @@ -330,13 +319,11 @@ fn range_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyRange { - start: new_start, - end: new_end, - step: new_step, - }), - }, + Box::new(PyRange { + start: new_start, + end: new_end, + step: new_step, + }), vm.ctx.range_type(), )) } else { diff --git a/vm/src/obj/objsequence.rs b/vm/src/obj/objsequence.rs index 5d7ddaf329..d9961b3acb 100644 --- a/vm/src/obj/objsequence.rs +++ b/vm/src/obj/objsequence.rs @@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut, Range}; use num_bigint::BigInt; use num_traits::{One, Signed, ToPrimitive, Zero}; -use crate::pyobject::{IdProtocol, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol}; +use crate::pyobject::{IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol}; use crate::vm::VirtualMachine; use super::objbool; @@ -163,29 +163,29 @@ pub fn get_item( } if subscript.payload::().is_some() { - let payload = if sequence.payload::().is_some() { - PyObjectPayload::AnyRustValue { - value: Box::new(PyList::from( + if sequence.payload::().is_some() { + Ok(PyObject::new( + Box::new(PyList::from( elements.to_vec().get_slice_items(vm, &subscript)?, )), - } + sequence.typ(), + )) } else if sequence.payload::().is_some() { - PyObjectPayload::AnyRustValue { - value: Box::new(PyTuple::from( + Ok(PyObject::new( + Box::new(PyTuple::from( elements.to_vec().get_slice_items(vm, &subscript)?, )), - } + sequence.typ(), + )) } else { panic!("sequence get_item called for non-sequence") - }; - - return Ok(PyObject::new(payload, sequence.typ())); + } + } else { + Err(vm.new_type_error(format!( + "TypeError: indexing type {:?} with index {:?} is not supported (yet?)", + sequence, subscript + ))) } - - Err(vm.new_type_error(format!( - "TypeError: indexing type {:?} with index {:?} is not supported (yet?)", - sequence, subscript - ))) } pub fn seq_equal( diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 76eca5ec2a..3f5c306a88 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -13,8 +13,8 @@ use super::objiter; use super::objstr; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2, - PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, + TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -169,11 +169,9 @@ fn set_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), cls.clone(), )) } @@ -190,11 +188,9 @@ fn set_copy(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(s, Some(vm.ctx.set_type()))]); let elements = get_elements(s); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -346,11 +342,9 @@ fn set_union(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { elements.extend(get_elements(other).clone()); Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -390,11 +384,9 @@ fn set_symmetric_difference(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResu } Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -432,11 +424,9 @@ fn set_combine_inner( } Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet { - elements: RefCell::new(elements), - }), - }, + Box::new(PySet { + elements: RefCell::new(elements), + }), vm.ctx.set_type(), )) } @@ -566,12 +556,10 @@ fn set_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let items = get_elements(zelf).values().cloned().collect(); let set_list = vm.ctx.new_list(items); let iter_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: set_list, - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: set_list, + }), vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objslice.rs b/vm/src/obj/objslice.rs index 45b92b5af6..f146bb062e 100644 --- a/vm/src/obj/objslice.rs +++ b/vm/src/obj/objslice.rs @@ -1,7 +1,6 @@ use super::objint; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::BigInt; @@ -55,13 +54,11 @@ fn slice_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } }?; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySlice { - start: start.map(|x| objint::get_value(x)), - stop: stop.map(|x| objint::get_value(x)), - step: step.map(|x| objint::get_value(x)), - }), - }, + Box::new(PySlice { + start: start.map(|x| objint::get_value(x)), + stop: stop.map(|x| objint::get_value(x)), + step: step.map(|x| objint::get_value(x)), + }), cls.clone(), )) } diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 95c1b12f98..9d120d2715 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -2,8 +2,8 @@ use std::cell::{Cell, RefCell}; use std::hash::{Hash, Hasher}; use crate::pyobject::{ - IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, + IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, + PyRef, PyResult, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -126,12 +126,10 @@ impl PyTupleRef { fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyIteratorValue { - position: Cell::new(0), - iterated_obj: self.into_object(), - }), - }, + Box::new(PyIteratorValue { + position: Cell::new(0), + iterated_obj: self.into_object(), + }), vm.ctx.iter_type(), ) } @@ -216,9 +214,7 @@ fn tuple_new( }; Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyTuple::from(elements)), - }, + Box::new(PyTuple::from(elements)), cls.into_object(), )) } diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index 44bb9bc628..a9d5e5c4fa 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -2,8 +2,8 @@ use std::cell::RefCell; use std::collections::HashMap; use crate::pyobject::{ - AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload, - PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, + PyObjectRef, PyRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -32,12 +32,10 @@ pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, _dict_type: // this is not ideal let ptr = PyObjectRef::into_raw(type_type.clone()) as *mut PyObject; unsafe { - (*ptr).payload = PyObjectPayload::AnyRustValue { - value: Box::new(PyClass { - name: String::from("type"), - mro: vec![object_type], - }), - }; + (*ptr).payload = Box::new(PyClass { + name: String::from("type"), + mro: vec![object_type], + }); (*ptr).dict = Some(RefCell::new(PyAttributes::new())); (*ptr).typ = Some(type_type); } @@ -320,12 +318,10 @@ pub fn new( let mros = bases.into_iter().map(|x| _mro(x).unwrap()).collect(); let mro = linearise_mro(mros).unwrap(); Ok(PyObject { - payload: PyObjectPayload::AnyRustValue { - value: Box::new(PyClass { - name: String::from(name), - mro, - }), - }, + payload: Box::new(PyClass { + name: String::from(name), + mro, + }), dict: Some(RefCell::new(dict)), typ: Some(typ), } diff --git a/vm/src/obj/objzip.rs b/vm/src/obj/objzip.rs index 70cded6b20..25714fd917 100644 --- a/vm/src/obj/objzip.rs +++ b/vm/src/obj/objzip.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -25,12 +24,7 @@ fn zip_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .iter() .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyZip { iterators }), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(PyZip { iterators }), cls.clone())) } fn zip_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index c7c99723c6..c213fcda77 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::fmt; @@ -213,38 +214,19 @@ impl PyContext { let exceptions = exceptions::ExceptionZoo::new(&type_type, &object_type, &dict_type); let none = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objnone::PyNone), - }, + Box::new(objnone::PyNone), create_type("NoneType", &type_type, &object_type, &dict_type), ); - let ellipsis = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(()), - }, - ellipsis_type.clone(), - ); + let ellipsis = PyObject::new(Box::new(()), ellipsis_type.clone()); let not_implemented = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(()), - }, + Box::new(()), create_type("NotImplementedType", &type_type, &object_type, &dict_type), ); - let true_value = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(BigInt::one())), - }, - bool_type.clone(), - ); - let false_value = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(BigInt::zero())), - }, - bool_type.clone(), - ); + let true_value = PyObject::new(Box::new(PyInt::new(BigInt::one())), bool_type.clone()); + let false_value = PyObject::new(Box::new(PyInt::new(BigInt::zero())), bool_type.clone()); let context = PyContext { bool_type, memoryview_type, @@ -482,55 +464,28 @@ impl PyContext { } pub fn new_int(&self, i: T) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyInt::new(i)), - }, - self.int_type(), - ) + PyObject::new(Box::new(PyInt::new(i)), self.int_type()) } pub fn new_float(&self, value: f64) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFloat::from(value)), - }, - self.float_type(), - ) + PyObject::new(Box::new(PyFloat::from(value)), self.float_type()) } pub fn new_complex(&self, value: Complex64) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyComplex::from(value)), - }, - self.complex_type(), - ) + PyObject::new(Box::new(PyComplex::from(value)), self.complex_type()) } pub fn new_str(&self, s: String) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objstr::PyString { value: s }), - }, - self.str_type(), - ) + PyObject::new(Box::new(objstr::PyString { value: s }), self.str_type()) } pub fn new_bytes(&self, data: Vec) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objbytes::PyBytes::new(data)), - }, - self.bytes_type(), - ) + PyObject::new(Box::new(objbytes::PyBytes::new(data)), self.bytes_type()) } pub fn new_bytearray(&self, data: Vec) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objbytearray::PyByteArray::new(data)), - }, + Box::new(objbytearray::PyByteArray::new(data)), self.bytearray_type(), ) } @@ -544,41 +499,21 @@ impl PyContext { } pub fn new_tuple(&self, elements: Vec) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyTuple::from(elements)), - }, - self.tuple_type(), - ) + PyObject::new(Box::new(PyTuple::from(elements)), self.tuple_type()) } pub fn new_list(&self, elements: Vec) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyList::from(elements)), - }, - self.list_type(), - ) + PyObject::new(Box::new(PyList::from(elements)), self.list_type()) } pub fn new_set(&self) -> PyObjectRef { // Initialized empty, as calling __hash__ is required for adding each object to the set // which requires a VM context - this is done in the objset code itself. - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PySet::default()), - }, - self.set_type(), - ) + PyObject::new(Box::new(PySet::default()), self.set_type()) } pub fn new_dict(&self) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyDict::default()), - }, - self.dict_type(), - ) + PyObject::new(Box::new(PyDict::default()), self.dict_type()) } pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef { @@ -591,12 +526,10 @@ impl PyContext { pub fn new_module(&self, name: &str, dict: PyObjectRef) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyModule { - name: name.to_string(), - dict, - }), - }, + Box::new(PyModule { + name: name.to_string(), + dict, + }), self.module_type.clone(), ) } @@ -606,20 +539,13 @@ impl PyContext { F: IntoPyNativeFunc, { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyBuiltinFunction::new(f.into_func())), - }, + Box::new(PyBuiltinFunction::new(f.into_func())), self.builtin_function_or_method_type(), ) } pub fn new_frame(&self, code: PyObjectRef, scope: Scope) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(Frame::new(code, scope)), - }, - self.frame_type(), - ) + PyObject::new(Box::new(Frame::new(code, scope)), self.frame_type()) } pub fn new_property(&self, f: F) -> PyObjectRef @@ -630,12 +556,7 @@ impl PyContext { } pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(objcode::PyCode::new(code)), - }, - self.code_type(), - ) + PyObject::new(Box::new(objcode::PyCode::new(code)), self.code_type()) } pub fn new_function( @@ -645,18 +566,14 @@ impl PyContext { defaults: PyObjectRef, ) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyFunction::new(code_obj, scope, defaults)), - }, + Box::new(PyFunction::new(code_obj, scope, defaults)), self.function_type(), ) } pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef { PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyMethod::new(object, function)), - }, + Box::new(PyMethod::new(object, function)), self.bound_method_type(), ) } @@ -668,11 +585,9 @@ impl PyContext { PyAttributes::new() }; PyObject { - payload: PyObjectPayload::AnyRustValue { - value: Box::new(objobject::PyInstance), - }, typ: Some(class), dict: Some(RefCell::new(dict)), + payload: Box::new(objobject::PyInstance), } .into_ref() } @@ -735,11 +650,20 @@ impl Default for PyContext { /// This is an actual python object. It consists of a `typ` which is the /// python class, and carries some rust payload optionally. This rust /// payload can be a rust float or rust int in case of float and int objects. -#[derive(Default)] pub struct PyObject { - pub payload: PyObjectPayload, pub typ: Option, pub dict: Option>, // __dict__ member + pub payload: Box, +} + +impl Default for PyObject { + fn default() -> Self { + PyObject { + typ: None, + dict: None, + payload: Box::new(()), + } + } } /// A reference to a Python object. @@ -764,12 +688,7 @@ where { pub fn new(ctx: &PyContext, payload: T) -> Self { PyRef { - obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - T::required_type(ctx), - ), + obj: PyObject::new(Box::new(payload), T::required_type(ctx)), _payload: PhantomData, } } @@ -778,12 +697,7 @@ where let required_type = T::required_type(&vm.ctx); if objtype::issubclass(&cls.obj, &required_type) { Ok(PyRef { - obj: PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(payload), - }, - cls.obj, - ), + obj: PyObject::new(Box::new(payload), cls.obj), _payload: PhantomData, }) } else { @@ -1452,12 +1366,7 @@ where T: PyObjectPayload2 + Sized, { fn into_pyobject(self, ctx: &PyContext) -> PyResult { - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(self), - }, - T::required_type(ctx), - )) + Ok(PyObject::new(Box::new(self), T::required_type(ctx))) } } @@ -1587,22 +1496,6 @@ into_py_native_func_tuple!((a, A), (b, B), (c, C)); into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D)); into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D), (e, E)); -/// Rather than determining the type of a python object, this enum is more -/// a holder for the rust payload of a python object. It is more a carrier -/// of rust data for a particular python object. Determine the python type -/// by using for example the `.typ()` method on a python object. -pub enum PyObjectPayload { - AnyRustValue { value: Box }, -} - -impl Default for PyObjectPayload { - fn default() -> Self { - PyObjectPayload::AnyRustValue { - value: Box::new(()), - } - } -} - // TODO: This is a workaround and shouldn't exist. // Each iterable type should have its own distinct iterator type. #[derive(Debug)] @@ -1617,16 +1510,8 @@ impl PyObjectPayload2 for PyIteratorValue { } } -impl fmt::Debug for PyObjectPayload { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - PyObjectPayload::AnyRustValue { value } => value.fmt(f), - } - } -} - impl PyObject { - pub fn new(payload: PyObjectPayload, typ: PyObjectRef) -> PyObjectRef { + pub fn new(payload: Box, typ: PyObjectRef) -> PyObjectRef { PyObject { payload, typ: Some(typ), @@ -1641,8 +1526,7 @@ impl PyObject { } pub fn payload(&self) -> Option<&T> { - let PyObjectPayload::AnyRustValue { ref value } = self.payload; - value.downcast_ref() + self.payload.downcast_ref() } } diff --git a/vm/src/stdlib/re.rs b/vm/src/stdlib/re.rs index 0828a13d14..bf24d7b51d 100644 --- a/vm/src/stdlib/re.rs +++ b/vm/src/stdlib/re.rs @@ -11,9 +11,7 @@ use regex::{Match, Regex}; use std::path::PathBuf; use crate::obj::objstr; -use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, -}; +use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol}; use crate::VirtualMachine; /// Create the python `re` module with all its members. @@ -118,12 +116,7 @@ fn create_match(vm: &mut VirtualMachine, match_value: &Match) -> PyResult { end: match_value.end(), }; - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(match_value), - }, - match_class.clone(), - )) + Ok(PyObject::new(Box::new(match_value), match_class.clone())) } /// Compile a regular expression into a Pattern object. @@ -141,12 +134,7 @@ fn re_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let module = import::import_module(vm, PathBuf::default(), "re").unwrap(); let pattern_class = vm.ctx.get_attr(&module, "Pattern").unwrap(); - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(regex), - }, - pattern_class.clone(), - )) + Ok(PyObject::new(Box::new(regex), pattern_class.clone())) } fn pattern_match(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -192,8 +180,7 @@ fn match_end(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { /// Retrieve inner rust regex from python object: fn get_regex<'a>(obj: &'a PyObjectRef) -> &'a Regex { // TODO: Regex shouldn't be stored in payload directly, create newtype wrapper - let PyObjectPayload::AnyRustValue { ref value } = obj.payload; - if let Some(regex) = value.downcast_ref::() { + if let Some(regex) = obj.payload.downcast_ref::() { return regex; } panic!("Inner error getting regex {:?}", obj); @@ -201,8 +188,7 @@ fn get_regex<'a>(obj: &'a PyObjectRef) -> &'a Regex { /// Retrieve inner rust match from python object: fn get_match<'a>(obj: &'a PyObjectRef) -> &'a PyMatch { - let PyObjectPayload::AnyRustValue { ref value } = obj.payload; - if let Some(value) = value.downcast_ref::() { + if let Some(value) = obj.payload.downcast_ref::() { return value; } panic!("Inner error getting match {:?}", obj); diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index d6e6283af9..b4f1c61b31 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -9,9 +9,7 @@ use crate::obj::objbytes; use crate::obj::objint; use crate::obj::objsequence::get_elements; use crate::obj::objstr; -use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, -}; +use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol}; use crate::vm::VirtualMachine; use num_traits::ToPrimitive; @@ -127,8 +125,7 @@ impl Socket { } fn get_socket<'a>(obj: &'a PyObjectRef) -> impl DerefMut + 'a { - let PyObjectPayload::AnyRustValue { ref value } = obj.payload; - if let Some(socket) = value.downcast_ref::>() { + if let Some(socket) = obj.payload.downcast_ref::>() { return socket.borrow_mut(); } panic!("Inner error getting socket {:?}", obj); @@ -151,12 +148,7 @@ fn socket_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let socket = RefCell::new(Socket::new(address_family, kind)); - Ok(PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(socket), - }, - cls.clone(), - )) + Ok(PyObject::new(Box::new(socket), cls.clone())) } fn socket_connect(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -274,12 +266,7 @@ fn socket_accept(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { con: Some(Connection::TcpStream(tcp_stream)), }); - let sock_obj = PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(socket), - }, - zelf.typ(), - ); + let sock_obj = PyObject::new(Box::new(socket), zelf.typ()); let addr_tuple = get_addr_tuple(vm, addr)?; From 5971fc3bd487a144cfd1730fd7bd2730e85dafee Mon Sep 17 00:00:00 2001 From: Joey Hain Date: Sun, 10 Mar 2019 13:48:19 -0700 Subject: [PATCH 3/5] rename PyObjectPayload2 to PyValue --- vm/src/frame.rs | 6 +++--- vm/src/obj/objbuiltinfunc.rs | 4 ++-- vm/src/obj/objbytearray.rs | 4 ++-- vm/src/obj/objbytes.rs | 5 ++--- vm/src/obj/objcode.rs | 4 ++-- vm/src/obj/objcomplex.rs | 4 ++-- vm/src/obj/objdict.rs | 6 +++--- vm/src/obj/objenumerate.rs | 4 ++-- vm/src/obj/objfilter.rs | 5 ++--- vm/src/obj/objfloat.rs | 4 ++-- vm/src/obj/objfunction.rs | 6 +++--- vm/src/obj/objgenerator.rs | 4 ++-- vm/src/obj/objint.rs | 6 +++--- vm/src/obj/objlist.rs | 6 +++--- vm/src/obj/objmap.rs | 4 ++-- vm/src/obj/objmemory.rs | 4 ++-- vm/src/obj/objmodule.rs | 4 ++-- vm/src/obj/objnone.rs | 5 ++--- vm/src/obj/objproperty.rs | 7 +++---- vm/src/obj/objrange.rs | 5 ++--- vm/src/obj/objset.rs | 5 ++--- vm/src/obj/objslice.rs | 4 ++-- vm/src/obj/objstr.rs | 6 +++--- vm/src/obj/objtuple.rs | 6 +++--- vm/src/obj/objtype.rs | 6 +++--- vm/src/obj/objweakref.rs | 4 ++-- vm/src/obj/objzip.rs | 4 ++-- vm/src/pyobject.rs | 14 +++++++------- 28 files changed, 70 insertions(+), 76 deletions(-) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 2e100f7914..8342010748 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -22,8 +22,8 @@ use crate::obj::objslice::PySlice; use crate::obj::objstr; use crate::obj::objtype; use crate::pyobject::{ - DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, - PyResult, TryFromObject, TypeProtocol, + DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, + TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -181,7 +181,7 @@ pub struct Frame { pub lasti: RefCell, // index of last instruction ran } -impl PyObjectPayload2 for Frame { +impl PyValue for Frame { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.frame_type() } diff --git a/vm/src/obj/objbuiltinfunc.rs b/vm/src/obj/objbuiltinfunc.rs index 8e3b2c3422..94a8bc59ba 100644 --- a/vm/src/obj/objbuiltinfunc.rs +++ b/vm/src/obj/objbuiltinfunc.rs @@ -1,13 +1,13 @@ use std::fmt; -use crate::pyobject::{PyContext, PyNativeFunc, PyObjectPayload2, PyObjectRef}; +use crate::pyobject::{PyContext, PyNativeFunc, PyObjectRef, PyValue}; pub struct PyBuiltinFunction { // TODO: shouldn't be public pub value: PyNativeFunc, } -impl PyObjectPayload2 for PyBuiltinFunction { +impl PyValue for PyBuiltinFunction { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.builtin_function_or_method_type() } diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index d2a098edfc..56a925b883 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -5,7 +5,7 @@ use std::fmt::Write; use std::ops::{Deref, DerefMut}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use super::objint; @@ -28,7 +28,7 @@ impl PyByteArray { } } -impl PyObjectPayload2 for PyByteArray { +impl PyValue for PyByteArray { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.bytearray_type() } diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index 31fe95e14c..b24912bafd 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -5,8 +5,7 @@ use std::ops::Deref; use super::objint; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; use num_traits::ToPrimitive; @@ -30,7 +29,7 @@ impl Deref for PyBytes { } } -impl PyObjectPayload2 for PyBytes { +impl PyValue for PyBytes { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.bytes_type() } diff --git a/vm/src/obj/objcode.rs b/vm/src/obj/objcode.rs index ccd2114aed..36aaf4eab8 100644 --- a/vm/src/obj/objcode.rs +++ b/vm/src/obj/objcode.rs @@ -4,7 +4,7 @@ use crate::bytecode; use crate::pyobject::{ - IdProtocol, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; use std::fmt; @@ -25,7 +25,7 @@ impl fmt::Debug for PyCode { } } -impl PyObjectPayload2 for PyCode { +impl PyValue for PyCode { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.code_type() } diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index afafe48ed0..76f298bdd8 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -2,7 +2,7 @@ use super::objfloat; use super::objint; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; use num_complex::Complex64; @@ -13,7 +13,7 @@ pub struct PyComplex { value: Complex64, } -impl PyObjectPayload2 for PyComplex { +impl PyValue for PyComplex { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.complex_type() } diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index 7e3cef048c..8d8a62c4f0 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use std::ops::{Deref, DerefMut}; use crate::pyobject::{ - PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, - PyRef, PyResult, TypeProtocol, + PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef, PyRef, PyResult, + PyValue, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -21,7 +21,7 @@ pub struct PyDict { } pub type PyDictRef = PyRef; -impl PyObjectPayload2 for PyDict { +impl PyValue for PyDict { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.dict_type() } diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index d02fc20867..c2c6c86189 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -4,7 +4,7 @@ use std::ops::AddAssign; use super::objint; use super::objiter; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::BigInt; @@ -16,7 +16,7 @@ pub struct PyEnumerate { iterator: PyObjectRef, } -impl PyObjectPayload2 for PyEnumerate { +impl PyValue for PyEnumerate { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.enumerate_type() } diff --git a/vm/src/obj/objfilter.rs b/vm/src/obj/objfilter.rs index 009c7da90c..f7a08e30fc 100644 --- a/vm/src/obj/objfilter.rs +++ b/vm/src/obj/objfilter.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; // Required for arg_check! to use isinstance @@ -13,7 +12,7 @@ pub struct PyFilter { iterator: PyObjectRef, } -impl PyObjectPayload2 for PyFilter { +impl PyValue for PyFilter { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.filter_type() } diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index efbd4fed40..a5416b233f 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -3,7 +3,7 @@ use super::objint; use super::objstr; use super::objtype; use crate::pyobject::{ - IntoPyObject, PyContext, PyObject, PyObjectPayload2, PyObjectRef, PyRef, PyResult, TypeProtocol, + IntoPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::ToBigInt; @@ -15,7 +15,7 @@ pub struct PyFloat { value: f64, } -impl PyObjectPayload2 for PyFloat { +impl PyValue for PyFloat { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.float_type() } diff --git a/vm/src/obj/objfunction.rs b/vm/src/obj/objfunction.rs index 2f278ca076..ec2a460737 100644 --- a/vm/src/obj/objfunction.rs +++ b/vm/src/obj/objfunction.rs @@ -1,6 +1,6 @@ use crate::frame::Scope; use crate::pyobject::{ - AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyResult, + AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -23,7 +23,7 @@ impl PyFunction { } } -impl PyObjectPayload2 for PyFunction { +impl PyValue for PyFunction { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.function_type() } @@ -42,7 +42,7 @@ impl PyMethod { } } -impl PyObjectPayload2 for PyMethod { +impl PyValue for PyMethod { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.bound_method_type() } diff --git a/vm/src/obj/objgenerator.rs b/vm/src/obj/objgenerator.rs index 4e2f40a125..3ba5037135 100644 --- a/vm/src/obj/objgenerator.rs +++ b/vm/src/obj/objgenerator.rs @@ -4,7 +4,7 @@ use crate::frame::{ExecutionResult, Frame}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -13,7 +13,7 @@ pub struct PyGenerator { frame: PyObjectRef, } -impl PyObjectPayload2 for PyGenerator { +impl PyValue for PyGenerator { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.generator_type() } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 4352e97c61..7b77301f3a 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -6,8 +6,8 @@ use num_traits::{Pow, Signed, ToPrimitive, Zero}; use crate::format::FormatSpec; use crate::pyobject::{ - FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, - PyRef, PyResult, TryFromObject, TypeProtocol, + FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectRef, PyRef, PyResult, + PyValue, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -31,7 +31,7 @@ impl PyInt { } } -impl PyObjectPayload2 for PyInt { +impl PyValue for PyInt { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.int_type() } diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index 29d8dc386a..c2a3d84975 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -9,8 +9,8 @@ use super::objsequence::{ use super::objstr; use super::objtype; use crate::pyobject::{ - IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, - PyObjectRef, PyRef, PyResult, TypeProtocol, + IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef, PyRef, + PyResult, PyValue, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; use num_traits::ToPrimitive; @@ -29,7 +29,7 @@ impl From> for PyList { } } -impl PyObjectPayload2 for PyList { +impl PyValue for PyList { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.list_type() } diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index eb748bc01f..1a84307e47 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -1,5 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -11,7 +11,7 @@ pub struct PyMap { iterators: Vec, } -impl PyObjectPayload2 for PyMap { +impl PyValue for PyMap { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.map_type() } diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index c5669532c7..13e455d0f4 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -1,5 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -8,7 +8,7 @@ pub struct PyMemoryView { obj: PyObjectRef, } -impl PyObjectPayload2 for PyMemoryView { +impl PyValue for PyMemoryView { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.memoryview_type() } diff --git a/vm/src/obj/objmodule.rs b/vm/src/obj/objmodule.rs index 92f55566d2..297ee6dc00 100644 --- a/vm/src/obj/objmodule.rs +++ b/vm/src/obj/objmodule.rs @@ -1,4 +1,4 @@ -use crate::pyobject::{DictProtocol, PyContext, PyObjectPayload2, PyObjectRef, PyRef, PyResult}; +use crate::pyobject::{DictProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue}; use crate::vm::VirtualMachine; #[derive(Clone, Debug)] @@ -8,7 +8,7 @@ pub struct PyModule { } pub type PyModuleRef = PyRef; -impl PyObjectPayload2 for PyModule { +impl PyValue for PyModule { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.module_type() } diff --git a/vm/src/obj/objnone.rs b/vm/src/obj/objnone.rs index 6cde6ef063..6e3bca6804 100644 --- a/vm/src/obj/objnone.rs +++ b/vm/src/obj/objnone.rs @@ -1,6 +1,5 @@ use crate::pyobject::{ - IntoPyObject, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyRef, PyResult, - TypeProtocol, + IntoPyObject, PyContext, PyFuncArgs, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -8,7 +7,7 @@ use crate::vm::VirtualMachine; pub struct PyNone; pub type PyNoneRef = PyRef; -impl PyObjectPayload2 for PyNone { +impl PyValue for PyNone { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.none().typ() } diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index 03c012ad50..166ca9c8ef 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -7,8 +7,7 @@ use std::marker::PhantomData; use crate::obj::objstr::PyStringRef; use crate::obj::objtype::PyClassRef; use crate::pyobject::{ - IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectPayload2, PyObjectRef, PyRef, - PyResult, + IntoPyNativeFunc, OptionalArg, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, }; use crate::VirtualMachine; @@ -18,7 +17,7 @@ pub struct PyReadOnlyProperty { getter: PyObjectRef, } -impl PyObjectPayload2 for PyReadOnlyProperty { +impl PyValue for PyReadOnlyProperty { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.readonly_property_type() } @@ -40,7 +39,7 @@ pub struct PyProperty { deleter: Option, } -impl PyObjectPayload2 for PyProperty { +impl PyValue for PyProperty { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.property_type() } diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index 3100d2351d..84448a4b00 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -6,8 +6,7 @@ use num_integer::Integer; use num_traits::{One, Signed, ToPrimitive, Zero}; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -24,7 +23,7 @@ pub struct PyRange { pub step: BigInt, } -impl PyObjectPayload2 for PyRange { +impl PyValue for PyRange { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.range_type() } diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 3f5c306a88..83b36d6765 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -13,8 +13,7 @@ use super::objiter; use super::objstr; use super::objtype; use crate::pyobject::{ - PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, PyResult, - TypeProtocol, + PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -23,7 +22,7 @@ pub struct PySet { elements: RefCell>, } -impl PyObjectPayload2 for PySet { +impl PyValue for PySet { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.set_type() } diff --git a/vm/src/obj/objslice.rs b/vm/src/obj/objslice.rs index f146bb062e..cb06f9424f 100644 --- a/vm/src/obj/objslice.rs +++ b/vm/src/obj/objslice.rs @@ -1,6 +1,6 @@ use super::objint; use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; use num_bigint::BigInt; @@ -13,7 +13,7 @@ pub struct PySlice { pub step: Option, } -impl PyObjectPayload2 for PySlice { +impl PyValue for PySlice { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.slice_type() } diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index d9c39e3371..a0e2e110d7 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -7,8 +7,8 @@ use unicode_segmentation::UnicodeSegmentation; use crate::format::{FormatParseError, FormatPart, FormatString}; use crate::pyobject::{ - IntoPyObject, OptionalArg, PyContext, PyFuncArgs, PyIterable, PyObjectPayload2, PyObjectRef, - PyRef, PyResult, TypeProtocol, + IntoPyObject, OptionalArg, PyContext, PyFuncArgs, PyIterable, PyObjectRef, PyRef, PyResult, + PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -596,7 +596,7 @@ impl PyStringRef { } } -impl PyObjectPayload2 for PyString { +impl PyValue for PyString { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.str_type() } diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 9d120d2715..5772aa3e8b 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -2,8 +2,8 @@ use std::cell::{Cell, RefCell}; use std::hash::{Hash, Hasher}; use crate::pyobject::{ - IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectPayload2, PyObjectRef, - PyRef, PyResult, + IdProtocol, OptionalArg, PyContext, PyIteratorValue, PyObject, PyObjectRef, PyRef, PyResult, + PyValue, }; use crate::vm::{ReprGuard, VirtualMachine}; @@ -30,7 +30,7 @@ impl From> for PyTuple { } } -impl PyObjectPayload2 for PyTuple { +impl PyValue for PyTuple { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.tuple_type() } diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index a9d5e5c4fa..3b893f321e 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -2,8 +2,8 @@ use std::cell::RefCell; use std::collections::HashMap; use crate::pyobject::{ - AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload2, - PyObjectRef, PyRef, PyResult, TypeProtocol, + AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectRef, + PyRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -18,7 +18,7 @@ pub struct PyClass { pub type PyClassRef = PyRef; -impl PyObjectPayload2 for PyClass { +impl PyValue for PyClass { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.type_type() } diff --git a/vm/src/obj/objweakref.rs b/vm/src/obj/objweakref.rs index 45cb4108a8..d2e2d08dc4 100644 --- a/vm/src/obj/objweakref.rs +++ b/vm/src/obj/objweakref.rs @@ -1,5 +1,5 @@ use crate::obj::objtype::PyClassRef; -use crate::pyobject::PyObjectPayload2; +use crate::pyobject::PyValue; use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyRef, PyResult}; use crate::vm::VirtualMachine; @@ -22,7 +22,7 @@ impl PyWeak { } } -impl PyObjectPayload2 for PyWeak { +impl PyValue for PyWeak { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.weakref_type() } diff --git a/vm/src/obj/objzip.rs b/vm/src/obj/objzip.rs index 25714fd917..883d95ab1b 100644 --- a/vm/src/obj/objzip.rs +++ b/vm/src/obj/objzip.rs @@ -1,5 +1,5 @@ use crate::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use crate::vm::VirtualMachine; @@ -10,7 +10,7 @@ pub struct PyZip { iterators: Vec, } -impl PyObjectPayload2 for PyZip { +impl PyValue for PyZip { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.zip_type() } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index c213fcda77..5b76ffd5b8 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -684,7 +684,7 @@ pub struct PyRef { impl PyRef where - T: PyObjectPayload2, + T: PyValue, { pub fn new(ctx: &PyContext, payload: T) -> Self { PyRef { @@ -717,7 +717,7 @@ where impl Deref for PyRef where - T: PyObjectPayload2, + T: PyValue, { type Target = T; @@ -728,7 +728,7 @@ where impl TryFromObject for PyRef where - T: PyObjectPayload2, + T: PyValue, { fn try_from_object(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult { if objtype::isinstance(&obj, &T::required_type(&vm.ctx)) { @@ -1363,7 +1363,7 @@ where // explicitly implementing `IntoPyObject`. impl IntoPyObject for T where - T: PyObjectPayload2 + Sized, + T: PyValue + Sized, { fn into_pyobject(self, ctx: &PyContext) -> PyResult { Ok(PyObject::new(Box::new(self), T::required_type(ctx))) @@ -1504,7 +1504,7 @@ pub struct PyIteratorValue { pub iterated_obj: PyObjectRef, } -impl PyObjectPayload2 for PyIteratorValue { +impl PyValue for PyIteratorValue { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.iter_type() } @@ -1525,14 +1525,14 @@ impl PyObject { Rc::new(self) } - pub fn payload(&self) -> Option<&T> { + pub fn payload(&self) -> Option<&T> { self.payload.downcast_ref() } } // The intention is for this to replace `PyObjectPayload` once everything is // converted to use `PyObjectPayload::AnyRustvalue`. -pub trait PyObjectPayload2: std::any::Any + fmt::Debug { +pub trait PyValue: Any + fmt::Debug { fn required_type(ctx: &PyContext) -> PyObjectRef; } From 4510489bba1c135fc84946bdadf721888d2ebb47 Mon Sep 17 00:00:00 2001 From: Joey Hain Date: Sun, 10 Mar 2019 15:19:04 -0700 Subject: [PATCH 4/5] Fix PyObjectPayload usage in wasm --- wasm/lib/src/browser_module.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index d8014ae520..3d9ed71354 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -4,7 +4,7 @@ use js_sys::Promise; use num_traits::cast::ToPrimitive; use rustpython_vm::obj::{objint, objstr}; use rustpython_vm::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol, }; use rustpython_vm::{import::import, VirtualMachine}; use std::path::PathBuf; @@ -157,20 +157,13 @@ pub struct PyPromise { impl PyPromise { pub fn new_obj(promise_type: PyObjectRef, value: Promise) -> PyObjectRef { - PyObject::new( - PyObjectPayload::AnyRustValue { - value: Box::new(PyPromise { value }), - }, - promise_type, - ) + PyObject::new(Box::new(PyPromise { value }), promise_type) } } pub fn get_promise_value(obj: &PyObjectRef) -> Promise { - if let PyObjectPayload::AnyRustValue { value } = &obj.payload { - if let Some(promise) = value.downcast_ref::() { - return promise.value.clone(); - } + if let Some(promise) = obj.payload.downcast_ref::() { + return promise.value.clone(); } panic!("Inner error getting promise") } From 053ceb1a30c267c88169c34ee099eb35a5a58653 Mon Sep 17 00:00:00 2001 From: Joey Hain Date: Sun, 10 Mar 2019 20:14:02 -0700 Subject: [PATCH 5/5] Move payload boxing into PyObject::new --- vm/src/compile.rs | 5 +- vm/src/frame.rs | 7 +- vm/src/obj/objbytearray.rs | 5 +- vm/src/obj/objbytes.rs | 6 +- vm/src/obj/objcomplex.rs | 2 +- vm/src/obj/objdict.rs | 12 +-- vm/src/obj/objenumerate.rs | 4 +- vm/src/obj/objfilter.rs | 4 +- vm/src/obj/objfloat.rs | 2 +- vm/src/obj/objgenerator.rs | 2 +- vm/src/obj/objint.rs | 2 +- vm/src/obj/objlist.rs | 9 +-- vm/src/obj/objmap.rs | 4 +- vm/src/obj/objmemory.rs | 4 +- vm/src/obj/objproperty.rs | 4 +- vm/src/obj/objrange.rs | 19 ++--- vm/src/obj/objsequence.rs | 8 +- vm/src/obj/objset.rs | 24 +++--- vm/src/obj/objslice.rs | 4 +- vm/src/obj/objtuple.rs | 9 +-- vm/src/obj/objzip.rs | 2 +- vm/src/pyobject.rs | 83 +++++++++++--------- vm/src/stdlib/re.rs | 23 +++++- vm/src/stdlib/socket.rs | 135 +++++++++++++++++++-------------- wasm/lib/src/browser_module.rs | 12 ++- 25 files changed, 210 insertions(+), 181 deletions(-) diff --git a/vm/src/compile.rs b/vm/src/compile.rs index 5a3fe6b1da..08045eb535 100644 --- a/vm/src/compile.rs +++ b/vm/src/compile.rs @@ -49,10 +49,7 @@ pub fn compile( let code = compiler.pop_code_object(); trace!("Compilation completed: {:?}", code); - Ok(PyObject::new( - Box::new(objcode::PyCode::new(code)), - code_type, - )) + Ok(PyObject::new(objcode::PyCode::new(code), code_type)) } pub enum Mode { diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 8342010748..48d3d6229c 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -407,8 +407,7 @@ impl Frame { let stop = out[1].take(); let step = if out.len() == 3 { out[2].take() } else { None }; - let obj = - PyObject::new(Box::new(PySlice { start, stop, step }), vm.ctx.slice_type()); + let obj = PyObject::new(PySlice { start, stop, step }, vm.ctx.slice_type()); self.push_value(obj); Ok(None) } @@ -702,9 +701,7 @@ impl Frame { } bytecode::Instruction::LoadBuildClass => { let rustfunc = PyObject::new( - Box::new(PyBuiltinFunction::new(Box::new( - builtins::builtin_build_class_, - ))), + PyBuiltinFunction::new(Box::new(builtins::builtin_build_class_)), vm.ctx.type_type(), ); self.push_value(rustfunc); diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 56a925b883..d737efaaea 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -172,10 +172,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } else { vec![] }; - Ok(PyObject::new( - Box::new(PyByteArray::new(value)), - cls.clone(), - )) + Ok(PyObject::new(PyByteArray::new(value), cls.clone())) } fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index b24912bafd..c78e919bbc 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -94,7 +94,7 @@ fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { vec![] }; - Ok(PyObject::new(Box::new(PyBytes::new(value)), cls.clone())) + Ok(PyObject::new(PyBytes::new(value), cls.clone())) } fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -203,10 +203,10 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]); let iter_obj = PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: obj.clone(), - }), + }, vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index 76f298bdd8..75cd455f82 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -89,7 +89,7 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let value = Complex64::new(real, imag); - Ok(PyObject::new(Box::new(PyComplex { value }), cls.clone())) + Ok(PyObject::new(PyComplex { value }, cls.clone())) } fn complex_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index 8d8a62c4f0..c5cc3ec992 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -251,10 +251,10 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let key_list = vm.ctx.new_list(keys); let iter_obj = PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: key_list, - }), + }, vm.ctx.iter_type(), ); @@ -271,10 +271,10 @@ fn dict_values(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let values_list = vm.ctx.new_list(values); let iter_obj = PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: values_list, - }), + }, vm.ctx.iter_type(), ); @@ -291,10 +291,10 @@ fn dict_items(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let items_list = vm.ctx.new_list(items); let iter_obj = PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: items_list, - }), + }, vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index c2c6c86189..b37f610628 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -36,10 +36,10 @@ fn enumerate_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; let iterator = objiter::get_iter(vm, iterable)?; Ok(PyObject::new( - Box::new(PyEnumerate { + PyEnumerate { counter: RefCell::new(counter), iterator, - }), + }, cls.clone(), )) } diff --git a/vm/src/obj/objfilter.rs b/vm/src/obj/objfilter.rs index f7a08e30fc..94cd15bb83 100644 --- a/vm/src/obj/objfilter.rs +++ b/vm/src/obj/objfilter.rs @@ -26,10 +26,10 @@ fn filter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let iterator = objiter::get_iter(vm, iterable)?; Ok(PyObject::new( - Box::new(PyFilter { + PyFilter { predicate: function.clone(), iterator, - }), + }, cls.clone(), )) } diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index a5416b233f..ae16b625aa 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -188,7 +188,7 @@ impl PyFloatRef { let type_name = objtype::get_type_name(&arg.typ()); return Err(vm.new_type_error(format!("can't convert {} to float", type_name))); }; - Ok(PyObject::new(Box::new(PyFloat { value }), cls.clone())) + Ok(PyObject::new(PyFloat { value }, cls.clone())) } fn mod_(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult { diff --git a/vm/src/obj/objgenerator.rs b/vm/src/obj/objgenerator.rs index 3ba5037135..8e2434c384 100644 --- a/vm/src/obj/objgenerator.rs +++ b/vm/src/obj/objgenerator.rs @@ -40,7 +40,7 @@ pub fn init(context: &PyContext) { pub fn new_generator(vm: &mut VirtualMachine, frame: PyObjectRef) -> PyResult { Ok(PyObject::new( - Box::new(PyGenerator { frame }), + PyGenerator { frame }, vm.ctx.generator_type.clone(), )) } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 7b77301f3a..a7bc9e2b29 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -105,7 +105,7 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Some(val) => to_int(vm, val, base)?, None => Zero::zero(), }; - Ok(PyObject::new(Box::new(PyInt::new(val)), cls.clone())) + Ok(PyObject::new(PyInt::new(val), cls.clone())) } // Casting function: diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index c2a3d84975..8bf7dce249 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -111,10 +111,10 @@ impl PyListRef { fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef { PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: self.into_object(), - }), + }, vm.ctx.iter_type(), ) } @@ -302,10 +302,7 @@ fn list_new( vec![] }; - Ok(PyObject::new( - Box::new(PyList::from(elements)), - cls.into_object(), - )) + Ok(PyObject::new(PyList::from(elements), cls.into_object())) } fn quicksort( diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index 1a84307e47..c183da0df8 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -30,10 +30,10 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; Ok(PyObject::new( - Box::new(PyMap { + PyMap { mapper: function.clone(), iterators, - }), + }, cls.clone(), )) } diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index 13e455d0f4..0170ee435c 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -18,9 +18,9 @@ pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]); vm.ctx.set_attr(&cls, "obj", bytes_object.clone()); Ok(PyObject::new( - Box::new(PyMemoryView { + PyMemoryView { obj: bytes_object.clone(), - }), + }, cls.clone(), )) } diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index 166ca9c8ef..59117244d2 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -137,7 +137,7 @@ impl<'a, T> PropertyBuilder<'a, T> { deleter: None, }; - PyObject::new(Box::new(payload), self.ctx.property_type()) + PyObject::new(payload, self.ctx.property_type()) } else { let payload = PyReadOnlyProperty { getter: self.getter.expect( @@ -145,7 +145,7 @@ impl<'a, T> PropertyBuilder<'a, T> { ), }; - PyObject::new(Box::new(payload), self.ctx.readonly_property_type()) + PyObject::new(payload, self.ctx.readonly_property_type()) } } } diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index 84448a4b00..1e0eee53f6 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -227,10 +227,7 @@ fn range_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { if step.is_zero() { Err(vm.new_value_error("range with 0 step size".to_string())) } else { - Ok(PyObject::new( - Box::new(PyRange { start, end, step }), - cls.clone(), - )) + Ok(PyObject::new(PyRange { start, end, step }, cls.clone())) } } @@ -238,10 +235,10 @@ fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(range, Some(vm.ctx.range_type()))]); Ok(PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: range.clone(), - }), + }, vm.ctx.iter_type(), )) } @@ -252,10 +249,10 @@ fn range_reversed(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let range = get_value(zelf).reversed(); Ok(PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), - iterated_obj: PyObject::new(Box::new(range), vm.ctx.range_type()), - }), + iterated_obj: PyObject::new(range, vm.ctx.range_type()), + }, vm.ctx.iter_type(), )) } @@ -318,11 +315,11 @@ fn range_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; Ok(PyObject::new( - Box::new(PyRange { + PyRange { start: new_start, end: new_end, step: new_step, - }), + }, vm.ctx.range_type(), )) } else { diff --git a/vm/src/obj/objsequence.rs b/vm/src/obj/objsequence.rs index d9961b3acb..120868d452 100644 --- a/vm/src/obj/objsequence.rs +++ b/vm/src/obj/objsequence.rs @@ -165,16 +165,12 @@ pub fn get_item( if subscript.payload::().is_some() { if sequence.payload::().is_some() { Ok(PyObject::new( - Box::new(PyList::from( - elements.to_vec().get_slice_items(vm, &subscript)?, - )), + PyList::from(elements.to_vec().get_slice_items(vm, &subscript)?), sequence.typ(), )) } else if sequence.payload::().is_some() { Ok(PyObject::new( - Box::new(PyTuple::from( - elements.to_vec().get_slice_items(vm, &subscript)?, - )), + PyTuple::from(elements.to_vec().get_slice_items(vm, &subscript)?), sequence.typ(), )) } else { diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 83b36d6765..bb8483a0e2 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -168,9 +168,9 @@ fn set_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { }; Ok(PyObject::new( - Box::new(PySet { + PySet { elements: RefCell::new(elements), - }), + }, cls.clone(), )) } @@ -187,9 +187,9 @@ fn set_copy(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(s, Some(vm.ctx.set_type()))]); let elements = get_elements(s); Ok(PyObject::new( - Box::new(PySet { + PySet { elements: RefCell::new(elements), - }), + }, vm.ctx.set_type(), )) } @@ -341,9 +341,9 @@ fn set_union(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { elements.extend(get_elements(other).clone()); Ok(PyObject::new( - Box::new(PySet { + PySet { elements: RefCell::new(elements), - }), + }, vm.ctx.set_type(), )) } @@ -383,9 +383,9 @@ fn set_symmetric_difference(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResu } Ok(PyObject::new( - Box::new(PySet { + PySet { elements: RefCell::new(elements), - }), + }, vm.ctx.set_type(), )) } @@ -423,9 +423,9 @@ fn set_combine_inner( } Ok(PyObject::new( - Box::new(PySet { + PySet { elements: RefCell::new(elements), - }), + }, vm.ctx.set_type(), )) } @@ -555,10 +555,10 @@ fn set_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let items = get_elements(zelf).values().cloned().collect(); let set_list = vm.ctx.new_list(items); let iter_obj = PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: set_list, - }), + }, vm.ctx.iter_type(), ); diff --git a/vm/src/obj/objslice.rs b/vm/src/obj/objslice.rs index cb06f9424f..5ab3c7a332 100644 --- a/vm/src/obj/objslice.rs +++ b/vm/src/obj/objslice.rs @@ -54,11 +54,11 @@ fn slice_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } }?; Ok(PyObject::new( - Box::new(PySlice { + PySlice { start: start.map(|x| objint::get_value(x)), stop: stop.map(|x| objint::get_value(x)), step: step.map(|x| objint::get_value(x)), - }), + }, cls.clone(), )) } diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 5772aa3e8b..2c692469fc 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -126,10 +126,10 @@ impl PyTupleRef { fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef { PyObject::new( - Box::new(PyIteratorValue { + PyIteratorValue { position: Cell::new(0), iterated_obj: self.into_object(), - }), + }, vm.ctx.iter_type(), ) } @@ -213,10 +213,7 @@ fn tuple_new( vec![] }; - Ok(PyObject::new( - Box::new(PyTuple::from(elements)), - cls.into_object(), - )) + Ok(PyObject::new(PyTuple::from(elements), cls.into_object())) } #[rustfmt::skip] // to avoid line splitting diff --git a/vm/src/obj/objzip.rs b/vm/src/obj/objzip.rs index 883d95ab1b..8831564a8f 100644 --- a/vm/src/obj/objzip.rs +++ b/vm/src/obj/objzip.rs @@ -24,7 +24,7 @@ fn zip_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { .iter() .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; - Ok(PyObject::new(Box::new(PyZip { iterators }), cls.clone())) + Ok(PyObject::new(PyZip { iterators }, cls.clone())) } fn zip_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 5b76ffd5b8..55ea50a390 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -161,6 +161,24 @@ pub fn create_type( objtype::new(type_type.clone(), name, vec![base.clone()], dict).unwrap() } +#[derive(Debug)] +pub struct PyNotImplemented; + +impl PyValue for PyNotImplemented { + fn required_type(ctx: &PyContext) -> PyObjectRef { + ctx.not_implemented().typ() + } +} + +#[derive(Debug)] +pub struct PyEllipsis; + +impl PyValue for PyEllipsis { + fn required_type(ctx: &PyContext) -> PyObjectRef { + ctx.ellipsis_type.clone() + } +} + // Basic objects: impl PyContext { pub fn new() -> Self { @@ -214,19 +232,19 @@ impl PyContext { let exceptions = exceptions::ExceptionZoo::new(&type_type, &object_type, &dict_type); let none = PyObject::new( - Box::new(objnone::PyNone), + objnone::PyNone, create_type("NoneType", &type_type, &object_type, &dict_type), ); - let ellipsis = PyObject::new(Box::new(()), ellipsis_type.clone()); + let ellipsis = PyObject::new(PyEllipsis, ellipsis_type.clone()); let not_implemented = PyObject::new( - Box::new(()), + PyNotImplemented, create_type("NotImplementedType", &type_type, &object_type, &dict_type), ); - let true_value = PyObject::new(Box::new(PyInt::new(BigInt::one())), bool_type.clone()); - let false_value = PyObject::new(Box::new(PyInt::new(BigInt::zero())), bool_type.clone()); + let true_value = PyObject::new(PyInt::new(BigInt::one()), bool_type.clone()); + let false_value = PyObject::new(PyInt::new(BigInt::zero()), bool_type.clone()); let context = PyContext { bool_type, memoryview_type, @@ -464,30 +482,27 @@ impl PyContext { } pub fn new_int(&self, i: T) -> PyObjectRef { - PyObject::new(Box::new(PyInt::new(i)), self.int_type()) + PyObject::new(PyInt::new(i), self.int_type()) } pub fn new_float(&self, value: f64) -> PyObjectRef { - PyObject::new(Box::new(PyFloat::from(value)), self.float_type()) + PyObject::new(PyFloat::from(value), self.float_type()) } pub fn new_complex(&self, value: Complex64) -> PyObjectRef { - PyObject::new(Box::new(PyComplex::from(value)), self.complex_type()) + PyObject::new(PyComplex::from(value), self.complex_type()) } pub fn new_str(&self, s: String) -> PyObjectRef { - PyObject::new(Box::new(objstr::PyString { value: s }), self.str_type()) + PyObject::new(objstr::PyString { value: s }, self.str_type()) } pub fn new_bytes(&self, data: Vec) -> PyObjectRef { - PyObject::new(Box::new(objbytes::PyBytes::new(data)), self.bytes_type()) + PyObject::new(objbytes::PyBytes::new(data), self.bytes_type()) } pub fn new_bytearray(&self, data: Vec) -> PyObjectRef { - PyObject::new( - Box::new(objbytearray::PyByteArray::new(data)), - self.bytearray_type(), - ) + PyObject::new(objbytearray::PyByteArray::new(data), self.bytearray_type()) } pub fn new_bool(&self, b: bool) -> PyObjectRef { @@ -499,21 +514,21 @@ impl PyContext { } pub fn new_tuple(&self, elements: Vec) -> PyObjectRef { - PyObject::new(Box::new(PyTuple::from(elements)), self.tuple_type()) + PyObject::new(PyTuple::from(elements), self.tuple_type()) } pub fn new_list(&self, elements: Vec) -> PyObjectRef { - PyObject::new(Box::new(PyList::from(elements)), self.list_type()) + PyObject::new(PyList::from(elements), self.list_type()) } pub fn new_set(&self) -> PyObjectRef { // Initialized empty, as calling __hash__ is required for adding each object to the set // which requires a VM context - this is done in the objset code itself. - PyObject::new(Box::new(PySet::default()), self.set_type()) + PyObject::new(PySet::default(), self.set_type()) } pub fn new_dict(&self) -> PyObjectRef { - PyObject::new(Box::new(PyDict::default()), self.dict_type()) + PyObject::new(PyDict::default(), self.dict_type()) } pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef { @@ -526,10 +541,10 @@ impl PyContext { pub fn new_module(&self, name: &str, dict: PyObjectRef) -> PyObjectRef { PyObject::new( - Box::new(PyModule { + PyModule { name: name.to_string(), dict, - }), + }, self.module_type.clone(), ) } @@ -539,13 +554,13 @@ impl PyContext { F: IntoPyNativeFunc, { PyObject::new( - Box::new(PyBuiltinFunction::new(f.into_func())), + PyBuiltinFunction::new(f.into_func()), self.builtin_function_or_method_type(), ) } pub fn new_frame(&self, code: PyObjectRef, scope: Scope) -> PyObjectRef { - PyObject::new(Box::new(Frame::new(code, scope)), self.frame_type()) + PyObject::new(Frame::new(code, scope), self.frame_type()) } pub fn new_property(&self, f: F) -> PyObjectRef @@ -556,7 +571,7 @@ impl PyContext { } pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyObjectRef { - PyObject::new(Box::new(objcode::PyCode::new(code)), self.code_type()) + PyObject::new(objcode::PyCode::new(code), self.code_type()) } pub fn new_function( @@ -566,16 +581,13 @@ impl PyContext { defaults: PyObjectRef, ) -> PyObjectRef { PyObject::new( - Box::new(PyFunction::new(code_obj, scope, defaults)), + PyFunction::new(code_obj, scope, defaults), self.function_type(), ) } pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef { - PyObject::new( - Box::new(PyMethod::new(object, function)), - self.bound_method_type(), - ) + PyObject::new(PyMethod::new(object, function), self.bound_method_type()) } pub fn new_instance(&self, class: PyObjectRef, dict: Option) -> PyObjectRef { @@ -682,13 +694,10 @@ pub struct PyRef { _payload: PhantomData, } -impl PyRef -where - T: PyValue, -{ +impl PyRef { pub fn new(ctx: &PyContext, payload: T) -> Self { PyRef { - obj: PyObject::new(Box::new(payload), T::required_type(ctx)), + obj: PyObject::new(payload, T::required_type(ctx)), _payload: PhantomData, } } @@ -697,7 +706,7 @@ where let required_type = T::required_type(&vm.ctx); if objtype::issubclass(&cls.obj, &required_type) { Ok(PyRef { - obj: PyObject::new(Box::new(payload), cls.obj), + obj: PyObject::new(payload, cls.obj), _payload: PhantomData, }) } else { @@ -1366,7 +1375,7 @@ where T: PyValue + Sized, { fn into_pyobject(self, ctx: &PyContext) -> PyResult { - Ok(PyObject::new(Box::new(self), T::required_type(ctx))) + Ok(PyObject::new(self, T::required_type(ctx))) } } @@ -1511,11 +1520,11 @@ impl PyValue for PyIteratorValue { } impl PyObject { - pub fn new(payload: Box, typ: PyObjectRef) -> PyObjectRef { + pub fn new(payload: T, typ: PyObjectRef) -> PyObjectRef { PyObject { - payload, typ: Some(typ), dict: Some(RefCell::new(PyAttributes::new())), + payload: Box::new(payload), } .into_ref() } diff --git a/vm/src/stdlib/re.rs b/vm/src/stdlib/re.rs index bf24d7b51d..d9e1a2e761 100644 --- a/vm/src/stdlib/re.rs +++ b/vm/src/stdlib/re.rs @@ -11,9 +11,18 @@ use regex::{Match, Regex}; use std::path::PathBuf; use crate::obj::objstr; -use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol}; +use crate::pyobject::{ + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, +}; use crate::VirtualMachine; +impl PyValue for Regex { + fn required_type(_ctx: &PyContext) -> PyObjectRef { + // TODO + unimplemented!() + } +} + /// Create the python `re` module with all its members. pub fn mk_module(ctx: &PyContext) -> PyObjectRef { let match_type = py_class!(ctx, "Match", ctx.object(), { @@ -95,11 +104,19 @@ fn make_regex(vm: &mut VirtualMachine, pattern: &PyObjectRef) -> PyResult } /// Inner data for a match object. +#[derive(Debug)] struct PyMatch { start: usize, end: usize, } +impl PyValue for PyMatch { + fn required_type(_ctx: &PyContext) -> PyObjectRef { + // TODO + unimplemented!() + } +} + /// Take a found regular expression and convert it to proper match object. fn create_match(vm: &mut VirtualMachine, match_value: &Match) -> PyResult { // Return match object: @@ -116,7 +133,7 @@ fn create_match(vm: &mut VirtualMachine, match_value: &Match) -> PyResult { end: match_value.end(), }; - Ok(PyObject::new(Box::new(match_value), match_class.clone())) + Ok(PyObject::new(match_value, match_class.clone())) } /// Compile a regular expression into a Pattern object. @@ -134,7 +151,7 @@ fn re_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let module = import::import_module(vm, PathBuf::default(), "re").unwrap(); let pattern_class = vm.ctx.get_attr(&module, "Pattern").unwrap(); - Ok(PyObject::new(Box::new(regex), pattern_class.clone())) + Ok(PyObject::new(regex, pattern_class.clone())) } fn pattern_match(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index b4f1c61b31..df8bd33243 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -3,18 +3,20 @@ use std::io; use std::io::Read; use std::io::Write; use std::net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket}; -use std::ops::DerefMut; +use std::ops::Deref; use crate::obj::objbytes; use crate::obj::objint; use crate::obj::objsequence::get_elements; use crate::obj::objstr; -use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol}; +use crate::pyobject::{ + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, +}; use crate::vm::VirtualMachine; use num_traits::ToPrimitive; -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] enum AddressFamily { Unix = 1, Inet = 2, @@ -32,7 +34,7 @@ impl AddressFamily { } } -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] enum SocketKind { Stream = 1, Dgram = 2, @@ -48,6 +50,7 @@ impl SocketKind { } } +#[derive(Debug)] enum Connection { TcpListener(TcpListener), TcpStream(TcpStream), @@ -108,10 +111,18 @@ impl Write for Connection { } } +#[derive(Debug)] pub struct Socket { address_family: AddressFamily, socket_kind: SocketKind, - con: Option, + con: RefCell>, +} + +impl PyValue for Socket { + fn required_type(_ctx: &PyContext) -> PyObjectRef { + // TODO + unimplemented!() + } } impl Socket { @@ -119,16 +130,13 @@ impl Socket { Socket { address_family, socket_kind, - con: None, + con: RefCell::new(None), } } } -fn get_socket<'a>(obj: &'a PyObjectRef) -> impl DerefMut + 'a { - if let Some(socket) = obj.payload.downcast_ref::>() { - return socket.borrow_mut(); - } - panic!("Inner error getting socket {:?}", obj); +fn get_socket<'a>(obj: &'a PyObjectRef) -> impl Deref + 'a { + obj.payload::().unwrap() } fn socket_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -146,9 +154,10 @@ fn socket_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { AddressFamily::from_i32(vm, objint::get_value(family_int).to_i32().unwrap())?; let kind = SocketKind::from_i32(vm, objint::get_value(kind_int).to_i32().unwrap())?; - let socket = RefCell::new(Socket::new(address_family, kind)); - - Ok(PyObject::new(Box::new(socket), cls.clone())) + Ok(PyObject::new( + Socket::new(address_family, kind), + cls.clone(), + )) } fn socket_connect(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { @@ -160,18 +169,21 @@ fn socket_connect(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let address_string = get_address_string(vm, address)?; - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); match socket.socket_kind { SocketKind::Stream => match TcpStream::connect(address_string) { Ok(stream) => { - socket.con = Some(Connection::TcpStream(stream)); + socket + .con + .borrow_mut() + .replace(Connection::TcpStream(stream)); Ok(vm.get_none()) } Err(s) => Err(vm.new_os_error(s.to_string())), }, SocketKind::Dgram => { - if let Some(Connection::UdpSocket(con)) = &socket.con { + if let Some(Connection::UdpSocket(con)) = socket.con.borrow().as_ref() { match con.connect(address_string) { Ok(_) => Ok(vm.get_none()), Err(s) => Err(vm.new_os_error(s.to_string())), @@ -192,19 +204,25 @@ fn socket_bind(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let address_string = get_address_string(vm, address)?; - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); match socket.socket_kind { SocketKind::Stream => match TcpListener::bind(address_string) { Ok(stream) => { - socket.con = Some(Connection::TcpListener(stream)); + socket + .con + .borrow_mut() + .replace(Connection::TcpListener(stream)); Ok(vm.get_none()) } Err(s) => Err(vm.new_os_error(s.to_string())), }, SocketKind::Dgram => match UdpSocket::bind(address_string) { Ok(dgram) => { - socket.con = Some(Connection::UdpSocket(dgram)); + socket + .con + .borrow_mut() + .replace(Connection::UdpSocket(dgram)); Ok(vm.get_none()) } Err(s) => Err(vm.new_os_error(s.to_string())), @@ -248,10 +266,10 @@ fn socket_listen(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { fn socket_accept(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(zelf, None)]); - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); - let ret = match socket.con { - Some(ref mut v) => v.accept(), + let ret = match socket.con.borrow_mut().as_mut() { + Some(v) => v.accept(), None => return Err(vm.new_type_error("".to_string())), }; @@ -260,13 +278,13 @@ fn socket_accept(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Err(s) => return Err(vm.new_os_error(s.to_string())), }; - let socket = RefCell::new(Socket { + let socket = Socket { address_family: socket.address_family, socket_kind: socket.socket_kind, - con: Some(Connection::TcpStream(tcp_stream)), - }); + con: RefCell::new(Some(Connection::TcpStream(tcp_stream))), + }; - let sock_obj = PyObject::new(Box::new(socket), zelf.typ()); + let sock_obj = PyObject::new(socket, zelf.typ()); let addr_tuple = get_addr_tuple(vm, addr)?; @@ -279,11 +297,11 @@ fn socket_recv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { args, required = [(zelf, None), (bufsize, Some(vm.ctx.int_type()))] ); - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); let mut buffer = vec![0u8; objint::get_value(bufsize).to_usize().unwrap()]; - match socket.con { - Some(ref mut v) => match v.read_exact(&mut buffer) { + match socket.con.borrow_mut().as_mut() { + Some(v) => match v.read_exact(&mut buffer) { Ok(_) => (), Err(s) => return Err(vm.new_os_error(s.to_string())), }, @@ -299,11 +317,11 @@ fn socket_recvfrom(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { required = [(zelf, None), (bufsize, Some(vm.ctx.int_type()))] ); - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); let mut buffer = vec![0u8; objint::get_value(bufsize).to_usize().unwrap()]; - let ret = match socket.con { - Some(ref mut v) => v.recv_from(&mut buffer), + let ret = match socket.con.borrow().as_ref() { + Some(v) => v.recv_from(&mut buffer), None => return Err(vm.new_type_error("".to_string())), }; @@ -323,10 +341,10 @@ fn socket_send(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { args, required = [(zelf, None), (bytes, Some(vm.ctx.bytes_type()))] ); - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); - match socket.con { - Some(ref mut v) => match v.write(&objbytes::get_value(&bytes)) { + match socket.con.borrow_mut().as_mut() { + Some(v) => match v.write(&objbytes::get_value(&bytes)) { Ok(_) => (), Err(s) => return Err(vm.new_os_error(s.to_string())), }, @@ -347,30 +365,29 @@ fn socket_sendto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ); let address_string = get_address_string(vm, address)?; - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); match socket.socket_kind { SocketKind::Dgram => { - match socket.con { - Some(ref mut v) => match v.send_to(&objbytes::get_value(&bytes), address_string) { + if let Some(v) = socket.con.borrow().as_ref() { + return match v.send_to(&objbytes::get_value(&bytes), address_string) { Ok(_) => Ok(vm.get_none()), Err(s) => Err(vm.new_os_error(s.to_string())), - }, - None => { - // Doing implicit bind - match UdpSocket::bind("0.0.0.0:0") { - Ok(dgram) => { - match dgram.send_to(&objbytes::get_value(&bytes), address_string) { - Ok(_) => { - socket.con = Some(Connection::UdpSocket(dgram)); - Ok(vm.get_none()) - } - Err(s) => Err(vm.new_os_error(s.to_string())), - } - } - Err(s) => Err(vm.new_os_error(s.to_string())), + }; + } + // Doing implicit bind + match UdpSocket::bind("0.0.0.0:0") { + Ok(dgram) => match dgram.send_to(&objbytes::get_value(&bytes), address_string) { + Ok(_) => { + socket + .con + .borrow_mut() + .replace(Connection::UdpSocket(dgram)); + Ok(vm.get_none()) } - } + Err(s) => Err(vm.new_os_error(s.to_string())), + }, + Err(s) => Err(vm.new_os_error(s.to_string())), } } _ => Err(vm.new_not_implemented_error("".to_string())), @@ -380,17 +397,17 @@ fn socket_sendto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { fn socket_close(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(zelf, None)]); - let mut socket = get_socket(zelf); - socket.con = None; + let socket = get_socket(zelf); + socket.con.borrow_mut().take(); Ok(vm.get_none()) } fn socket_getsockname(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(zelf, None)]); - let mut socket = get_socket(zelf); + let socket = get_socket(zelf); - let addr = match socket.con { - Some(ref mut v) => v.local_addr(), + let addr = match socket.con.borrow().as_ref() { + Some(v) => v.local_addr(), None => return Err(vm.new_type_error("".to_string())), }; diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index 3d9ed71354..f6362be0ea 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -4,7 +4,7 @@ use js_sys::Promise; use num_traits::cast::ToPrimitive; use rustpython_vm::obj::{objint, objstr}; use rustpython_vm::pyobject::{ - PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, TypeProtocol, + PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, }; use rustpython_vm::{import::import, VirtualMachine}; use std::path::PathBuf; @@ -151,13 +151,21 @@ fn browser_cancel_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) -> Ok(vm.get_none()) } +#[derive(Debug)] pub struct PyPromise { value: Promise, } +impl PyValue for PyPromise { + fn required_type(_ctx: &PyContext) -> PyObjectRef { + // TODO + unimplemented!() + } +} + impl PyPromise { pub fn new_obj(promise_type: PyObjectRef, value: Promise) -> PyObjectRef { - PyObject::new(Box::new(PyPromise { value }), promise_type) + PyObject::new(PyPromise { value }, promise_type) } }