From f8a5cc4b233c7c69ee15428db4ee37c318658e2e Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 23 Aug 2020 20:26:37 +0900 Subject: [PATCH] Remove type clone functions from vm.ctx which misleads to clone types when we don't need. --- derive/src/pyclass.rs | 4 +- examples/mini_repl.rs | 6 +- vm/src/builtins.rs | 52 +++---- vm/src/frame.rs | 2 +- vm/src/obj/objbuiltinfunc.rs | 4 +- vm/src/obj/objbytearray.rs | 4 +- vm/src/obj/objbytes.rs | 4 +- vm/src/obj/objclassmethod.rs | 2 +- vm/src/obj/objcode.rs | 2 +- vm/src/obj/objcomplex.rs | 2 +- vm/src/obj/objdict.rs | 8 +- vm/src/obj/objellipsis.rs | 6 +- vm/src/obj/objenumerate.rs | 2 +- vm/src/obj/objfilter.rs | 2 +- vm/src/obj/objfloat.rs | 2 +- vm/src/obj/objfunction.rs | 4 +- vm/src/obj/objgenerator.rs | 2 +- vm/src/obj/objgetset.rs | 2 +- vm/src/obj/objint.rs | 4 +- vm/src/obj/objiter.rs | 2 +- vm/src/obj/objlist.rs | 6 +- vm/src/obj/objmap.rs | 2 +- vm/src/obj/objmemory.rs | 2 +- vm/src/obj/objmodule.rs | 2 +- vm/src/obj/objnamespace.rs | 2 +- vm/src/obj/objobject.rs | 4 +- vm/src/obj/objproperty.rs | 2 +- vm/src/obj/objrange.rs | 6 +- vm/src/obj/objset.rs | 6 +- vm/src/obj/objslice.rs | 2 +- vm/src/obj/objstaticmethod.rs | 2 +- vm/src/obj/objstr.rs | 10 +- vm/src/obj/objsuper.rs | 2 +- vm/src/obj/objtraceback.rs | 2 +- vm/src/obj/objtuple.rs | 4 +- vm/src/obj/objtype.rs | 8 +- vm/src/obj/objweakproxy.rs | 2 +- vm/src/obj/objweakref.rs | 2 +- vm/src/obj/objzip.rs | 2 +- vm/src/pyobject.rs | 282 ++++++---------------------------- vm/src/stdlib/ast.rs | 2 +- vm/src/stdlib/io.rs | 2 +- vm/src/stdlib/pystruct.rs | 2 +- vm/src/stdlib/weakref.rs | 10 +- vm/src/vm.rs | 16 +- wasm/lib/src/js_module.rs | 2 +- wasm/lib/src/wasm_builtins.rs | 2 +- 47 files changed, 151 insertions(+), 351 deletions(-) diff --git a/derive/src/pyclass.rs b/derive/src/pyclass.rs index bab55ffb0..b26520f60 100644 --- a/derive/src/pyclass.rs +++ b/derive/src/pyclass.rs @@ -248,7 +248,7 @@ pub(crate) fn impl_pystruct_sequence( ctx: &::rustpython_vm::pyobject::PyContext, ) -> ::rustpython_vm::obj::objtype::PyClassRef { use crate::pyobject::PyClassDef; - Self::make_class_with_base(ctx, Self::NAME, &ctx.tuple_type()) + Self::make_class_with_base(ctx, Self::NAME, &ctx.types.tuple_type) } } }; @@ -472,7 +472,7 @@ impl ToTokens for GetSetNursery { #name, ::rustpython_vm::pyobject::PyObject::new( ::rustpython_vm::obj::objgetset::PyGetSet::#constructor(#name.into(), &Self::#getter #setter), - ctx.getset_type(), None) + ctx.types.getset_type.clone(), None) ); } })); diff --git a/examples/mini_repl.rs b/examples/mini_repl.rs index 2ad627f6c..3a19e75f0 100644 --- a/examples/mini_repl.rs +++ b/examples/mini_repl.rs @@ -41,7 +41,7 @@ macro_rules! add_python_function { // inserts the first function found in the module into the provided scope. $scope.globals.set_item( def.obj_name.as_str(), - $vm.context().new_pyfunction( + $vm.ctx.new_pyfunction( vm::obj::objcode::PyCode::new(*def.clone()).into_ref(&$vm), $scope.clone(), None, @@ -66,9 +66,7 @@ fn main() -> vm::pyobject::PyResult<()> { let scope: vm::scope::Scope = vm.new_scope_with_builtins(); // typing `quit()` is too long, let's make `on(False)` work instead. - scope - .globals - .set_item("on", vm.context().new_function(on), &vm)?; + scope.globals.set_item("on", vm.ctx.new_function(on), &vm)?; // let's include a fibonacci function, but let's be lazy and write it in Python add_python_function!( diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index c4c90c94f..664dfea0d 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -784,7 +784,7 @@ mod decl { let mut metaclass = if let Some(metaclass) = kwargs.pop_kwarg("metaclass") { PyClassRef::try_from_object(vm, metaclass)? } else { - vm.get_type() + vm.ctx.types.type_type.clone() }; for base in bases.clone() { @@ -865,31 +865,31 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) { //set __name__ fixes: https://github.com/RustPython/RustPython/issues/146 "__name__" => ctx.new_str(String::from("__main__")), - "bool" => ctx.bool_type(), - "bytearray" => ctx.bytearray_type(), - "bytes" => ctx.bytes_type(), - "classmethod" => ctx.classmethod_type(), - "complex" => ctx.complex_type(), - "dict" => ctx.dict_type(), - "enumerate" => ctx.enumerate_type(), - "float" => ctx.float_type(), - "frozenset" => ctx.frozenset_type(), - "filter" => ctx.filter_type(), - "int" => ctx.int_type(), - "list" => ctx.list_type(), - "map" => ctx.map_type(), - "memoryview" => ctx.memoryview_type(), - "object" => ctx.object(), - "property" => ctx.property_type(), - "range" => ctx.range_type(), - "set" => ctx.set_type(), - "slice" => ctx.slice_type(), - "staticmethod" => ctx.staticmethod_type(), - "str" => ctx.str_type(), - "super" => ctx.super_type(), - "tuple" => ctx.tuple_type(), - "type" => ctx.type_type(), - "zip" => ctx.zip_type(), + "bool" => ctx.types.bool_type.clone(), + "bytearray" => ctx.types.bytearray_type.clone(), + "bytes" => ctx.types.bytes_type.clone(), + "classmethod" => ctx.types.classmethod_type.clone(), + "complex" => ctx.types.complex_type.clone(), + "dict" => ctx.types.dict_type.clone(), + "enumerate" => ctx.types.enumerate_type.clone(), + "float" => ctx.types.float_type.clone(), + "frozenset" => ctx.types.frozenset_type.clone(), + "filter" => ctx.types.filter_type.clone(), + "int" => ctx.types.int_type.clone(), + "list" => ctx.types.list_type.clone(), + "map" => ctx.types.map_type.clone(), + "memoryview" => ctx.types.memoryview_type.clone(), + "object" => ctx.types.object_type.clone(), + "property" => ctx.types.property_type.clone(), + "range" => ctx.types.range_type.clone(), + "set" => ctx.types.set_type.clone(), + "slice" => ctx.types.slice_type.clone(), + "staticmethod" => ctx.types.staticmethod_type.clone(), + "str" => ctx.types.str_type.clone(), + "super" => ctx.types.super_type.clone(), + "tuple" => ctx.types.tuple_type.clone(), + "type" => ctx.types.type_type.clone(), + "zip" => ctx.types.zip_type.clone(), // Constants "NotImplemented" => ctx.not_implemented(), diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 0fcc8fd95..23279e9d3 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -100,7 +100,7 @@ pub struct Frame { impl PyValue for Frame { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.frame_type() + vm.ctx.types.frame_type.clone() } } diff --git a/vm/src/obj/objbuiltinfunc.rs b/vm/src/obj/objbuiltinfunc.rs index 8bb4cf457..a9b6df70b 100644 --- a/vm/src/obj/objbuiltinfunc.rs +++ b/vm/src/obj/objbuiltinfunc.rs @@ -18,7 +18,7 @@ pub struct PyBuiltinFunction { impl PyValue for PyBuiltinFunction { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.builtin_function_or_method_type() + vm.ctx.types.builtin_function_or_method_type.clone() } } @@ -77,7 +77,7 @@ pub struct PyBuiltinMethod { impl PyValue for PyBuiltinMethod { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.method_descriptor_type() + vm.ctx.types.method_descriptor_type.clone() } } diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 0d6cd8f2e..2426290e4 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -77,7 +77,7 @@ impl From> for PyByteArray { impl PyValue for PyByteArray { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.bytearray_type() + vm.ctx.types.bytearray_type.clone() } } @@ -615,7 +615,7 @@ pub struct PyByteArrayIterator { impl PyValue for PyByteArrayIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.bytearray_iterator_type() + vm.ctx.types.bytearray_iterator_type.clone() } } diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index 5b44da6b2..31c5e3516 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -72,7 +72,7 @@ impl Deref for PyBytes { impl PyValue for PyBytes { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.bytes_type() + vm.ctx.types.bytes_type.clone() } } @@ -497,7 +497,7 @@ pub struct PyBytesIterator { impl PyValue for PyBytesIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.bytes_iterator_type() + vm.ctx.types.bytes_iterator_type.clone() } } diff --git a/vm/src/obj/objclassmethod.rs b/vm/src/obj/objclassmethod.rs index edffe5987..d69b85a96 100644 --- a/vm/src/obj/objclassmethod.rs +++ b/vm/src/obj/objclassmethod.rs @@ -41,7 +41,7 @@ impl From for PyClassMethod { impl PyValue for PyClassMethod { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.classmethod_type() + vm.ctx.types.classmethod_type.clone() } } diff --git a/vm/src/obj/objcode.rs b/vm/src/obj/objcode.rs index 14772e07f..d7abaa38b 100644 --- a/vm/src/obj/objcode.rs +++ b/vm/src/obj/objcode.rs @@ -38,7 +38,7 @@ impl fmt::Debug for PyCode { impl PyValue for PyCode { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.code_type() + vm.ctx.types.code_type.clone() } } diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index 93e7b5963..9fe146048 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -24,7 +24,7 @@ type PyComplexRef = PyRef; impl PyValue for PyComplex { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.complex_type() + vm.ctx.types.complex_type.clone() } } diff --git a/vm/src/obj/objdict.rs b/vm/src/obj/objdict.rs index 5e034bddb..5c44f32b7 100644 --- a/vm/src/obj/objdict.rs +++ b/vm/src/obj/objdict.rs @@ -34,7 +34,7 @@ impl fmt::Debug for PyDict { impl PyValue for PyDict { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.dict_type() + vm.ctx.types.dict_type.clone() } } @@ -448,7 +448,7 @@ impl PyDictRef { // and prevent the creation of the KeyError exception. // Also note, that we prevent the creation of a full PyString object // if we lookup local names (which happens all of the time). - if self.lease_class().is(&vm.ctx.dict_type()) { + if self.lease_class().is(&vm.ctx.types.dict_type) { // We can take the short path here! match self.inner_getitem_option(key, vm) { Err(exc) => { @@ -486,7 +486,7 @@ where } fn set_item(&self, key: T, value: PyObjectRef, vm: &VirtualMachine) -> PyResult { - if self.lease_class().is(&vm.ctx.dict_type()) { + if self.lease_class().is(&vm.ctx.types.dict_type) { self.inner_setitem_fast(key, value, vm) .map(|_| vm.ctx.none()) } else { @@ -496,7 +496,7 @@ where } fn del_item(&self, key: T, vm: &VirtualMachine) -> PyResult { - if self.lease_class().is(&vm.ctx.dict_type()) { + if self.lease_class().is(&vm.ctx.types.dict_type) { self.entries.delete(vm, key).map(|_| vm.ctx.none()) } else { // Fall back to slow path if we are in a dict subclass: diff --git a/vm/src/obj/objellipsis.rs b/vm/src/obj/objellipsis.rs index 7c97b9248..57164bc32 100644 --- a/vm/src/obj/objellipsis.rs +++ b/vm/src/obj/objellipsis.rs @@ -3,7 +3,7 @@ use crate::pyobject::{PyClassImpl, PyContext, PyResult, PyValue}; use crate::vm::VirtualMachine; pub(crate) fn init(context: &PyContext) { - PyEllipsis::extend_class(context, &context.ellipsis_type()); + PyEllipsis::extend_class(context, &context.types.ellipsis_type); } #[pyclass(module = false, name = "EllipsisType")] @@ -12,7 +12,7 @@ pub struct PyEllipsis; impl PyValue for PyEllipsis { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.ellipsis_type() + vm.ctx.types.ellipsis_type.clone() } } @@ -20,7 +20,7 @@ impl PyValue for PyEllipsis { impl PyEllipsis { #[pyslot] fn tp_new(cls: PyClassRef, vm: &VirtualMachine) -> PyResult { - if issubclass(&cls, &vm.ctx.ellipsis_type()) { + if issubclass(&cls, &vm.ctx.types.ellipsis_type) { Ok(vm.ctx.ellipsis()) } else { Err(vm.new_type_error(format!( diff --git a/vm/src/obj/objenumerate.rs b/vm/src/obj/objenumerate.rs index d699ce8f4..0160dbf81 100644 --- a/vm/src/obj/objenumerate.rs +++ b/vm/src/obj/objenumerate.rs @@ -21,7 +21,7 @@ type PyEnumerateRef = PyRef; impl PyValue for PyEnumerate { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.enumerate_type() + vm.ctx.types.enumerate_type.clone() } } diff --git a/vm/src/obj/objfilter.rs b/vm/src/obj/objfilter.rs index f8cf51e47..a1c97e74a 100644 --- a/vm/src/obj/objfilter.rs +++ b/vm/src/obj/objfilter.rs @@ -19,7 +19,7 @@ pub struct PyFilter { impl PyValue for PyFilter { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.filter_type() + vm.ctx.types.filter_type.clone() } } diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index b61eb6930..4e5b227a5 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -33,7 +33,7 @@ impl PyFloat { impl PyValue for PyFloat { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.float_type() + vm.ctx.types.float_type.clone() } } diff --git a/vm/src/obj/objfunction.rs b/vm/src/obj/objfunction.rs index bdf845cdb..34b94b166 100644 --- a/vm/src/obj/objfunction.rs +++ b/vm/src/obj/objfunction.rs @@ -254,7 +254,7 @@ impl PyFunction { impl PyValue for PyFunction { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.function_type() + vm.ctx.types.function_type.clone() } } @@ -344,7 +344,7 @@ impl PyBoundMethod { impl PyValue for PyBoundMethod { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.bound_method_type() + vm.ctx.types.bound_method_type.clone() } } diff --git a/vm/src/obj/objgenerator.rs b/vm/src/obj/objgenerator.rs index 1578a9ad1..824c57210 100644 --- a/vm/src/obj/objgenerator.rs +++ b/vm/src/obj/objgenerator.rs @@ -20,7 +20,7 @@ pub struct PyGenerator { impl PyValue for PyGenerator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.generator_type() + vm.ctx.types.generator_type.clone() } } diff --git a/vm/src/obj/objgetset.rs b/vm/src/obj/objgetset.rs index af307d1ee..a3986efcb 100644 --- a/vm/src/obj/objgetset.rs +++ b/vm/src/obj/objgetset.rs @@ -174,7 +174,7 @@ impl std::fmt::Debug for PyGetSet { impl PyValue for PyGetSet { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.getset_type() + vm.ctx.types.getset_type.clone() } } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 9c2030025..8d6c15327 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -73,7 +73,7 @@ where impl PyValue for PyInt { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.int_type() + vm.ctx.types.int_type.clone() } fn into_simple_object(self, vm: &VirtualMachine) -> PyObjectRef { @@ -239,7 +239,7 @@ impl PyInt { where T: Into + ToPrimitive, { - if cls.is(&vm.ctx.int_type()) { + if cls.is(&vm.ctx.types.int_type) { Ok(vm.ctx.new_int(value).downcast().unwrap()) } else { PyInt::from(value).into_ref_with_type(vm, cls) diff --git a/vm/src/obj/objiter.rs b/vm/src/obj/objiter.rs index 2f13b6a2c..7f7f588bf 100644 --- a/vm/src/obj/objiter.rs +++ b/vm/src/obj/objiter.rs @@ -159,7 +159,7 @@ pub struct PySequenceIterator { impl PyValue for PySequenceIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.iter_type() + vm.ctx.types.iter_type.clone() } } diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index e6b5d1119..8bcdfc596 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -56,7 +56,7 @@ impl FromIterator for PyList { impl PyValue for PyList { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.list_type() + vm.ctx.types.list_type.clone() } } @@ -834,7 +834,7 @@ pub struct PyListIterator { impl PyValue for PyListIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.list_iterator_type() + vm.ctx.types.list_iterator_type.clone() } } @@ -873,7 +873,7 @@ pub struct PyListReverseIterator { impl PyValue for PyListReverseIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.list_reverseiterator_type() + vm.ctx.types.list_reverseiterator_type.clone() } } diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index 4df54f4d6..4f50df2b4 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -18,7 +18,7 @@ type PyMapRef = PyRef; impl PyValue for PyMap { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.map_type() + vm.ctx.types.map_type.clone() } } diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index b58b6b717..62403ff93 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -71,7 +71,7 @@ impl PyMemoryView { impl PyValue for PyMemoryView { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.memoryview_type() + vm.ctx.types.memoryview_type.clone() } } diff --git a/vm/src/obj/objmodule.rs b/vm/src/obj/objmodule.rs index 0a1c7b32b..5646ac624 100644 --- a/vm/src/obj/objmodule.rs +++ b/vm/src/obj/objmodule.rs @@ -14,7 +14,7 @@ pub type PyModuleRef = PyRef; impl PyValue for PyModule { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.module_type() + vm.ctx.types.module_type.clone() } } diff --git a/vm/src/obj/objnamespace.rs b/vm/src/obj/objnamespace.rs index 3850f5a77..7dc698692 100644 --- a/vm/src/obj/objnamespace.rs +++ b/vm/src/obj/objnamespace.rs @@ -12,7 +12,7 @@ pub struct PyNamespace; impl PyValue for PyNamespace { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.namespace_type() + vm.ctx.types.namespace_type.clone() } } diff --git a/vm/src/obj/objobject.rs b/vm/src/obj/objobject.rs index f1adbb149..a3cdbd431 100644 --- a/vm/src/obj/objobject.rs +++ b/vm/src/obj/objobject.rs @@ -19,7 +19,7 @@ pub struct PyBaseObject; impl PyValue for PyBaseObject { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.object() + vm.ctx.types.object_type.clone() } } @@ -29,7 +29,7 @@ impl PyBaseObject { fn tp_new(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult { // more or less __new__ operator let cls = PyClassRef::try_from_object(vm, args.shift())?; - let dict = if cls.is(&vm.ctx.object()) { + let dict = if cls.is(&vm.ctx.types.object_type) { None } else { Some(vm.ctx.new_dict()) diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index 0f5f12dc8..f319399a2 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -54,7 +54,7 @@ pub struct PyProperty { impl PyValue for PyProperty { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.property_type() + vm.ctx.types.property_type.clone() } } diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index 3e4ae757b..d8fe9a131 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -35,7 +35,7 @@ pub struct PyRange { impl PyValue for PyRange { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.range_type() + vm.ctx.types.range_type.clone() } } @@ -309,7 +309,7 @@ impl PyRange { .map(|x| x.as_object().clone()) .collect(); let range_paramters_tuple = PyTuple::from(range_paramters); - (vm.ctx.range_type(), range_paramters_tuple) + (vm.ctx.types.range_type.clone(), range_paramters_tuple) } #[pymethod(name = "index")] @@ -409,7 +409,7 @@ pub struct PyRangeIterator { impl PyValue for PyRangeIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.range_iterator_type() + vm.ctx.types.range_iterator_type.clone() } } diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index 9dfb0c60c..8c5a53ade 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -55,13 +55,13 @@ impl fmt::Debug for PyFrozenSet { impl PyValue for PySet { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.set_type() + vm.ctx.types.set_type.clone() } } impl PyValue for PyFrozenSet { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.frozenset_type() + vm.ctx.types.frozenset_type.clone() } } @@ -877,7 +877,7 @@ impl PySetIterator { impl PyValue for PySetIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.set_iterator_type() + vm.ctx.types.set_iterator_type.clone() } } diff --git a/vm/src/obj/objslice.rs b/vm/src/obj/objslice.rs index 78516cfa5..c74b91007 100644 --- a/vm/src/obj/objslice.rs +++ b/vm/src/obj/objslice.rs @@ -19,7 +19,7 @@ pub struct PySlice { impl PyValue for PySlice { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.slice_type() + vm.ctx.types.slice_type.clone() } } diff --git a/vm/src/obj/objstaticmethod.rs b/vm/src/obj/objstaticmethod.rs index ec031286a..b8fbedf25 100644 --- a/vm/src/obj/objstaticmethod.rs +++ b/vm/src/obj/objstaticmethod.rs @@ -13,7 +13,7 @@ pub type PyStaticMethodRef = PyRef; impl PyValue for PyStaticMethod { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.staticmethod_type() + vm.ctx.types.staticmethod_type.clone() } } diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 0f78dcab4..ec2dea1c4 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -111,7 +111,7 @@ pub struct PyStringIterator { impl PyValue for PyStringIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.str_iterator_type() + vm.ctx.types.str_iterator_type.clone() } } @@ -144,7 +144,7 @@ pub struct PyStringReverseIterator { impl PyValue for PyStringReverseIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.str_reverseiterator_type() + vm.ctx.types.str_reverseiterator_type.clone() } } @@ -997,7 +997,7 @@ impl PyString { none_str: OptionalArg, vm: &VirtualMachine, ) -> PyResult { - let new_dict = vm.context().new_dict(); + let new_dict = vm.ctx.new_dict(); if let OptionalArg::Present(to_str) = to_str { match dict_or_str.downcast::() { Ok(from_str) => { @@ -1109,7 +1109,7 @@ pub(crate) fn encode_string( impl PyValue for PyString { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.str_type() + vm.ctx.types.str_type.clone() } } @@ -1304,7 +1304,7 @@ mod tests { fn str_maketrans_and_translate() { let vm: VirtualMachine = Default::default(); - let table = vm.context().new_dict(); + let table = vm.ctx.new_dict(); table.set_item("a", vm.ctx.new_str("🎅"), &vm).unwrap(); table.set_item("b", vm.get_none(), &vm).unwrap(); table.set_item("c", vm.ctx.new_str("xda"), &vm).unwrap(); diff --git a/vm/src/obj/objsuper.rs b/vm/src/obj/objsuper.rs index 06171900e..01b07ef46 100644 --- a/vm/src/obj/objsuper.rs +++ b/vm/src/obj/objsuper.rs @@ -30,7 +30,7 @@ pub struct PySuper { impl PyValue for PySuper { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.super_type() + vm.ctx.types.super_type.clone() } } diff --git a/vm/src/obj/objtraceback.rs b/vm/src/obj/objtraceback.rs index 18c062a4c..d376545f8 100644 --- a/vm/src/obj/objtraceback.rs +++ b/vm/src/obj/objtraceback.rs @@ -16,7 +16,7 @@ pub type PyTracebackRef = PyRef; impl PyValue for PyTraceback { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.traceback_type() + vm.ctx.types.traceback_type.clone() } } diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index cfaec4e2e..b74d2e3f1 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -46,7 +46,7 @@ impl<'a> BorrowValue<'a> for PyTuple { impl PyValue for PyTuple { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.tuple_type() + vm.ctx.types.tuple_type.clone() } } @@ -264,7 +264,7 @@ pub struct PyTupleIterator { impl PyValue for PyTupleIterator { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.tuple_iterator_type() + vm.ctx.types.tuple_iterator_type.clone() } } diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index ade7ffcc9..30e39491f 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -52,7 +52,7 @@ pub type PyClassRef = PyRef; impl PyValue for PyClass { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.type_type() + vm.ctx.types.type_type.clone() } } @@ -306,7 +306,7 @@ impl PyClassRef { let bases: Vec = bases.iter(vm)?.collect::, _>>()?; let (metatype, base, bases) = if bases.is_empty() { - let base = vm.ctx.object(); + let base = vm.ctx.types.object_type.clone(); (metatype, base.clone(), vec![base]) } else { // TODO @@ -340,13 +340,13 @@ impl PyClassRef { let mut attributes = dict.to_attributes(); if let Some(f) = attributes.get_mut("__new__") { - if f.class().is(&vm.ctx.function_type()) { + if f.class().is(&vm.ctx.types.function_type) { *f = PyStaticMethod::from(f.clone()).into_ref(vm).into_object(); } } if let Some(f) = attributes.get_mut("__init_subclass__") { - if f.class().is(&vm.ctx.function_type()) { + if f.class().is(&vm.ctx.types.function_type) { *f = PyClassMethod::from(f.clone()).into_ref(vm).into_object(); } } diff --git a/vm/src/obj/objweakproxy.rs b/vm/src/obj/objweakproxy.rs index d0c382f9a..672157674 100644 --- a/vm/src/obj/objweakproxy.rs +++ b/vm/src/obj/objweakproxy.rs @@ -12,7 +12,7 @@ pub struct PyWeakProxy { impl PyValue for PyWeakProxy { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.weakproxy_type() + vm.ctx.types.weakproxy_type.clone() } } diff --git a/vm/src/obj/objweakref.rs b/vm/src/obj/objweakref.rs index ab3740fca..be7019732 100644 --- a/vm/src/obj/objweakref.rs +++ b/vm/src/obj/objweakref.rs @@ -33,7 +33,7 @@ impl PyWeak { impl PyValue for PyWeak { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.weakref_type() + vm.ctx.types.weakref_type.clone() } } diff --git a/vm/src/obj/objzip.rs b/vm/src/obj/objzip.rs index 6ff877ea7..d9f0f0976 100644 --- a/vm/src/obj/objzip.rs +++ b/vm/src/obj/objzip.rs @@ -14,7 +14,7 @@ pub struct PyZip { impl PyValue for PyZip { fn class(vm: &VirtualMachine) -> PyClassRef { - vm.ctx.zip_type() + vm.ctx.types.zip_type.clone() } } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index f78e18250..d304e686d 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -173,194 +173,6 @@ impl PyContext { context } - pub fn bytearray_type(&self) -> PyClassRef { - self.types.bytearray_type.clone() - } - - pub fn bytearray_iterator_type(&self) -> PyClassRef { - self.types.bytearray_iterator_type.clone() - } - - pub fn bytes_type(&self) -> PyClassRef { - self.types.bytes_type.clone() - } - - pub fn bytes_iterator_type(&self) -> PyClassRef { - self.types.bytes_iterator_type.clone() - } - - pub fn code_type(&self) -> PyClassRef { - self.types.code_type.clone() - } - - pub fn complex_type(&self) -> PyClassRef { - self.types.complex_type.clone() - } - - pub fn dict_type(&self) -> PyClassRef { - self.types.dict_type.clone() - } - - pub fn float_type(&self) -> PyClassRef { - self.types.float_type.clone() - } - - pub fn frame_type(&self) -> PyClassRef { - self.types.frame_type.clone() - } - - pub fn int_type(&self) -> PyClassRef { - self.types.int_type.clone() - } - - pub fn list_type(&self) -> PyClassRef { - self.types.list_type.clone() - } - - pub fn list_iterator_type(&self) -> PyClassRef { - self.types.list_iterator_type.clone() - } - - pub fn list_reverseiterator_type(&self) -> PyClassRef { - self.types.list_reverseiterator_type.clone() - } - - pub fn str_iterator_type(&self) -> PyClassRef { - self.types.str_iterator_type.clone() - } - - pub fn str_reverseiterator_type(&self) -> PyClassRef { - self.types.str_reverseiterator_type.clone() - } - - pub fn module_type(&self) -> PyClassRef { - self.types.module_type.clone() - } - - pub fn namespace_type(&self) -> PyClassRef { - self.types.namespace_type.clone() - } - - pub fn set_type(&self) -> PyClassRef { - self.types.set_type.clone() - } - - pub fn set_iterator_type(&self) -> PyClassRef { - self.types.set_iterator_type.clone() - } - - pub fn range_type(&self) -> PyClassRef { - self.types.range_type.clone() - } - - pub fn range_iterator_type(&self) -> PyClassRef { - self.types.range_iterator_type.clone() - } - - pub fn slice_type(&self) -> PyClassRef { - self.types.slice_type.clone() - } - - pub fn frozenset_type(&self) -> PyClassRef { - self.types.frozenset_type.clone() - } - - pub fn bool_type(&self) -> PyClassRef { - self.types.bool_type.clone() - } - - pub fn memoryview_type(&self) -> PyClassRef { - self.types.memoryview_type.clone() - } - - pub fn tuple_type(&self) -> PyClassRef { - self.types.tuple_type.clone() - } - - pub fn tuple_iterator_type(&self) -> PyClassRef { - self.types.tuple_iterator_type.clone() - } - - pub fn iter_type(&self) -> PyClassRef { - self.types.iter_type.clone() - } - - pub fn enumerate_type(&self) -> PyClassRef { - self.types.enumerate_type.clone() - } - - pub fn filter_type(&self) -> PyClassRef { - self.types.filter_type.clone() - } - - pub fn map_type(&self) -> PyClassRef { - self.types.map_type.clone() - } - - pub fn zip_type(&self) -> PyClassRef { - self.types.zip_type.clone() - } - - pub fn str_type(&self) -> PyClassRef { - self.types.str_type.clone() - } - - pub fn super_type(&self) -> PyClassRef { - self.types.super_type.clone() - } - - pub fn function_type(&self) -> PyClassRef { - self.types.function_type.clone() - } - - pub fn builtin_function_or_method_type(&self) -> PyClassRef { - self.types.builtin_function_or_method_type.clone() - } - - pub fn method_descriptor_type(&self) -> PyClassRef { - self.types.method_descriptor_type.clone() - } - - pub fn property_type(&self) -> PyClassRef { - self.types.property_type.clone() - } - - pub fn getset_type(&self) -> PyClassRef { - self.types.getset_type.clone() - } - - pub fn classmethod_type(&self) -> PyClassRef { - self.types.classmethod_type.clone() - } - - pub fn staticmethod_type(&self) -> PyClassRef { - self.types.staticmethod_type.clone() - } - - pub fn generator_type(&self) -> PyClassRef { - self.types.generator_type.clone() - } - - pub fn bound_method_type(&self) -> PyClassRef { - self.types.bound_method_type.clone() - } - - pub fn weakref_type(&self) -> PyClassRef { - self.types.weakref_type.clone() - } - - pub fn weakproxy_type(&self) -> PyClassRef { - self.types.weakproxy_type.clone() - } - - pub fn traceback_type(&self) -> PyClassRef { - self.types.traceback_type.clone() - } - - pub fn type_type(&self) -> PyClassRef { - self.types.type_type.clone() - } - pub fn none(&self) -> PyObjectRef { self.none.clone().into_object() } @@ -369,18 +181,10 @@ impl PyContext { self.ellipsis.clone().into_object() } - pub fn ellipsis_type(&self) -> PyClassRef { - self.types.ellipsis_type.clone() - } - pub fn not_implemented(&self) -> PyObjectRef { self.not_implemented.clone().into_object() } - pub fn object(&self) -> PyClassRef { - self.types.object_type.clone() - } - #[inline] pub fn new_int + ToPrimitive>(&self, i: T) -> PyObjectRef { if let Some(i) = i.to_i32() { @@ -389,7 +193,7 @@ impl PyContext { return self.int_cache_pool[inner_idx].clone(); } } - PyObject::new(PyInt::from(i), self.int_type(), None) + PyObject::new(PyInt::from(i), self.types.int_type.clone(), None) } #[inline] @@ -400,32 +204,40 @@ impl PyContext { return self.int_cache_pool[inner_idx].clone(); } } - PyObject::new(PyInt::from(i.clone()), self.int_type(), None) + PyObject::new(PyInt::from(i.clone()), self.types.int_type.clone(), None) } pub fn new_float(&self, value: f64) -> PyObjectRef { - PyObject::new(PyFloat::from(value), self.float_type(), None) + PyObject::new(PyFloat::from(value), self.types.float_type.clone(), None) } pub fn new_complex(&self, value: Complex64) -> PyObjectRef { - PyObject::new(PyComplex::from(value), self.complex_type(), None) + PyObject::new( + PyComplex::from(value), + self.types.complex_type.clone(), + None, + ) } pub fn new_str(&self, s: S) -> PyObjectRef where objstr::PyString: std::convert::From, { - PyObject::new(objstr::PyString::from(s), self.str_type(), None) + PyObject::new(objstr::PyString::from(s), self.types.str_type.clone(), None) } pub fn new_bytes(&self, data: Vec) -> PyObjectRef { - PyObject::new(objbytes::PyBytes::from(data), self.bytes_type(), None) + PyObject::new( + objbytes::PyBytes::from(data), + self.types.bytes_type.clone(), + None, + ) } pub fn new_bytearray(&self, data: Vec) -> PyObjectRef { PyObject::new( objbytearray::PyByteArray::from(data), - self.bytearray_type(), + self.types.bytearray_type.clone(), None, ) } @@ -444,32 +256,36 @@ impl PyContext { if elements.is_empty() { self.empty_tuple.clone().into_object() } else { - PyObject::new(PyTuple::from(elements), self.tuple_type(), None) + PyObject::new(PyTuple::from(elements), self.types.tuple_type.clone(), None) } } pub fn new_list(&self, elements: Vec) -> PyObjectRef { - PyObject::new(PyList::from(elements), self.list_type(), None) + PyObject::new(PyList::from(elements), self.types.list_type.clone(), None) } 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(PySet::default(), self.set_type(), None) + PyObject::new(PySet::default(), self.types.set_type.clone(), None) } pub fn new_dict(&self) -> PyDictRef { - PyObject::new(PyDict::default(), self.dict_type(), None) + PyObject::new(PyDict::default(), self.types.dict_type.clone(), None) .downcast() .unwrap() } pub fn new_class(&self, name: &str, base: &PyClassRef, flags: PyTpFlags) -> PyClassRef { - create_type_with_flags(name, &self.type_type(), base, flags) + create_type_with_flags(name, &self.types.type_type, base, flags) } pub fn new_namespace(&self) -> PyObjectRef { - PyObject::new(PyNamespace, self.namespace_type(), Some(self.new_dict())) + PyObject::new( + PyNamespace, + self.types.namespace_type.clone(), + Some(self.new_dict()), + ) } pub fn new_function(&self, f: F) -> PyObjectRef @@ -478,7 +294,7 @@ impl PyContext { { PyObject::new( PyBuiltinFunction::from(f.into_func()), - self.builtin_function_or_method_type(), + self.types.builtin_function_or_method_type.clone(), None, ) } @@ -487,10 +303,11 @@ impl PyContext { where F: IntoPyNativeFunc, { - let stringref = |s| PyRef::new_ref(objstr::PyString::from(s), self.str_type(), None); + let stringref = + |s| PyRef::new_ref(objstr::PyString::from(s), self.types.str_type.clone(), None); PyObject::new( PyBuiltinFunction::new_with_name(f.into_func(), stringref(module), stringref(name)), - self.builtin_function_or_method_type(), + self.types.builtin_function_or_method_type.clone(), None, ) } @@ -501,7 +318,7 @@ impl PyContext { { PyObject::new( PyBuiltinMethod::from(f.into_func()), - self.method_descriptor_type(), + self.types.method_descriptor_type.clone(), None, ) } @@ -512,7 +329,7 @@ impl PyContext { { PyObject::new( PyClassMethod::from(self.new_method(f)), - self.classmethod_type(), + self.types.classmethod_type.clone(), None, ) } @@ -522,7 +339,7 @@ impl PyContext { { PyObject::new( PyStaticMethod::from(self.new_method(f)), - self.staticmethod_type(), + self.types.staticmethod_type.clone(), None, ) } @@ -531,7 +348,11 @@ impl PyContext { where F: IntoPyGetterFunc, { - PyObject::new(PyGetSet::with_get(name.into(), f), self.getset_type(), None) + PyObject::new( + PyGetSet::with_get(name.into(), f), + self.types.getset_type.clone(), + None, + ) } pub fn new_getset(&self, name: impl Into, g: G, s: S) -> PyObjectRef @@ -541,15 +362,19 @@ impl PyContext { { PyObject::new( PyGetSet::with_get_set(name.into(), g, s), - self.getset_type(), + self.types.getset_type.clone(), None, ) } pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyCodeRef { - PyObject::new(objcode::PyCode::new(code), self.code_type(), None) - .downcast() - .unwrap() + PyObject::new( + objcode::PyCode::new(code), + self.types.code_type.clone(), + None, + ) + .downcast() + .unwrap() } pub fn new_pyfunction( @@ -561,7 +386,7 @@ impl PyContext { ) -> PyObjectRef { PyObject::new( PyFunction::new(code_obj, scope, defaults, kw_only_defaults), - self.function_type(), + self.types.function_type.clone(), Some(self.new_dict()), ) } @@ -569,7 +394,7 @@ impl PyContext { pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef { PyObject::new( PyBoundMethod::new(object, function), - self.bound_method_type(), + self.types.bound_method_type.clone(), None, ) } @@ -1481,7 +1306,7 @@ pub trait PyClassImpl: PyClassDef { } fn make_class(ctx: &PyContext) -> PyClassRef { - Self::make_class_with_base(ctx, Self::NAME, &ctx.object()) + Self::make_class_with_base(ctx, Self::NAME, &ctx.types.object_type) } fn make_class_with_base(ctx: &PyContext, name: &str, base: &PyClassRef) -> PyClassRef { @@ -1587,14 +1412,3 @@ pub fn hash_iter_unordered<'a, I: IntoIterator>( ) -> PyResult { rustpython_common::hash::hash_iter_unordered(iter, |obj| vm._hash(obj)) } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_type_type() { - // TODO: Write this test - PyContext::new(); - } -} diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 05c0325e8..532ef25e9 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -644,7 +644,7 @@ pub(crate) fn parse(vm: &VirtualMachine, source: &str, mode: Mode) -> PyResult { pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; - let ast_base = py_class!(ctx, "AST", &ctx.object(), PyTpFlags::HAS_DICT, {}); + let ast_base = py_class!(ctx, "AST", &ctx.types.object_type, PyTpFlags::HAS_DICT, {}); py_module!(vm, MODULE_NAME, { // TODO: There's got to be a better way! "alias" => py_class!(ctx, "alias", &ast_base, {}), diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 078dc8028..beb9dbeb2 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -1268,7 +1268,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; // IOBase the abstract base class of the IO Module - let io_base = py_class!(ctx, "_IOBase", &ctx.object(), { + let io_base = py_class!(ctx, "_IOBase", &ctx.types.object_type, { "__enter__" => ctx.new_method(io_base_cm_enter), "__exit__" => ctx.new_method(io_base_cm_exit), "seekable" => ctx.new_method(io_base_seekable), diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs index 64629ac90..e5191b836 100644 --- a/vm/src/stdlib/pystruct.rs +++ b/vm/src/stdlib/pystruct.rs @@ -897,7 +897,7 @@ mod _struct { let fmt_str = match fmt { Either::A(s) => s, Either::B(b) => PyString::from(std::str::from_utf8(b.borrow_value()).unwrap()) - .into_ref_with_type(vm, vm.ctx.str_type())?, + .into_ref_with_type(vm, vm.ctx.types.str_type.clone())?, }; PyStruct { spec, fmt_str }.into_ref_with_type(vm, cls) } diff --git a/vm/src/stdlib/weakref.rs b/vm/src/stdlib/weakref.rs index 6f59eeb6b..6c2f9321d 100644 --- a/vm/src/stdlib/weakref.rs +++ b/vm/src/stdlib/weakref.rs @@ -26,13 +26,13 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; py_module!(vm, "_weakref", { - "ref" => ctx.weakref_type(), - "proxy" => ctx.weakproxy_type(), + "ref" => ctx.types.weakref_type.clone(), + "proxy" => ctx.types.weakproxy_type.clone(), "getweakrefcount" => ctx.new_function(weakref_getweakrefcount), "getweakrefs" => ctx.new_function(weakref_getweakrefs), - "ReferenceType" => ctx.weakref_type(), - "ProxyType" => ctx.weakproxy_type(), - "CallableProxyType" => ctx.weakproxy_type(), + "ReferenceType" => ctx.types.weakref_type.clone(), + "ProxyType" => ctx.types.weakproxy_type.clone(), + "CallableProxyType" => ctx.types.weakproxy_type.clone(), "_remove_dead_weakref" => ctx.new_function(weakref_remove_dead_weakref), }) } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index a8dcda42b..d4f20e87b 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -617,22 +617,10 @@ impl VirtualMachine { } } - pub fn get_type(&self) -> PyClassRef { - self.ctx.type_type() - } - - pub fn get_object(&self) -> PyClassRef { - self.ctx.object() - } - pub fn get_locals(&self) -> PyDictRef { self.current_scope().get_locals() } - pub fn context(&self) -> &PyContext { - &self.ctx - } - // Container of the virtual machine state: pub fn to_str(&self, obj: &PyObjectRef) -> PyResult { if obj.lease_class().is(&self.ctx.types.str_type) { @@ -885,7 +873,7 @@ impl VirtualMachine { pub fn extract_elements(&self, value: &PyObjectRef) -> PyResult> { // Extract elements from item, if possible: let cls = value.class(); - if cls.is(&self.ctx.tuple_type()) { + if cls.is(&self.ctx.types.tuple_type) { value .payload::() .unwrap() @@ -893,7 +881,7 @@ impl VirtualMachine { .iter() .map(|obj| T::try_from_object(self, obj.clone())) .collect() - } else if cls.is(&self.ctx.list_type()) { + } else if cls.is(&self.ctx.types.list_type) { value .payload::() .unwrap() diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index a52d12cf1..f2abd72ab 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -255,7 +255,7 @@ fn new_js_error(vm: &VirtualMachine, err: JsValue) -> PyBaseExceptionRef { pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; py_module!(vm, "_js", { - "JsError" => create_type("JsError", &ctx.type_type(), &ctx.exceptions.exception_type), + "JsError" => create_type("JsError", &ctx.types.type_type, &ctx.exceptions.exception_type), "JsValue" => PyJsValue::make_class(ctx), }) } diff --git a/wasm/lib/src/wasm_builtins.rs b/wasm/lib/src/wasm_builtins.rs index 6f6f9ec04..b428ab186 100644 --- a/wasm/lib/src/wasm_builtins.rs +++ b/wasm/lib/src/wasm_builtins.rs @@ -32,7 +32,7 @@ pub fn make_stdout_object( let flush_method = ctx.new_method(|_self: PyObjectRef| {}); // there's not really any point to storing this class so that there's a consistent type object, // we just want a half-decent repr() output - let cls = py_class!(ctx, "JSStdout", &vm.ctx.object(), { + let cls = py_class!(ctx, "JSStdout", &vm.ctx.types.object_type, { "write" => write_method, "flush" => flush_method, });