diff --git a/vm/src/builtins/set.rs b/vm/src/builtins/set.rs index 97600b9cdc..b41a7a1c21 100644 --- a/vm/src/builtins/set.rs +++ b/vm/src/builtins/set.rs @@ -28,7 +28,6 @@ pub type SetContentType = dictdatatype::Dict<()>; pub struct PySet { pub(super) inner: PySetInner, } -pub type PySetRef = PyRef; /// frozenset() -> empty frozenset object /// frozenset(iterable) -> frozenset object @@ -403,6 +402,14 @@ macro_rules! multi_args_set { }}; } +impl PySet { + pub fn new_ref(ctx: &PyContext) -> PyRef { + // Initialized empty, as calling __hash__ is required for adding each object to the set + // which requires a VM context - this is done in the set code itself. + PyRef::new_ref(Self::default(), ctx.types.set_type.clone(), None) + } +} + #[pyimpl(with(Hashable, Comparable, Iterable), flags(BASETYPE))] impl PySet { #[pyslot] @@ -692,6 +699,7 @@ impl PyFrozenSet { for elem in it { inner.add(elem, vm)?; } + // FIXME: empty set check Ok(Self { inner }) } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 56b01e0663..1faad11bb0 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -625,7 +625,7 @@ impl ExecutingFrame<'_> { Ok(None) } bytecode::Instruction::BuildSet { size, unpack } => { - let set = vm.ctx.new_set(); + let set = set::PySet::new_ref(&vm.ctx); { let elements = self.pop_multiple(*size as usize); if *unpack { @@ -662,7 +662,7 @@ impl ExecutingFrame<'_> { bytecode::Instruction::SetAdd { i } => { let set_obj = self.nth_value(*i); let item = self.pop_value(); - set::PySetRef::try_from_object(vm, set_obj)?.add(item, vm)?; + PyRef::::try_from_object(vm, set_obj)?.add(item, vm)?; Ok(None) } bytecode::Instruction::MapAdd { i } => { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 9233acb1a1..02c563aaf2 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -10,10 +10,9 @@ use crate::{ bytearray, bytes, code::{self, PyCode}, getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet}, - object, pystr, - set::{self, PyFrozenSet}, - PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, PyInt, PyIntRef, - PyList, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, PyType, PyTypeRef, + object, pystr, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, + PyFrozenSet, PyInt, PyIntRef, PyList, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, + PyType, PyTypeRef, }, dictdatatype::Dict, exceptions, @@ -157,6 +156,8 @@ impl PyContext { self.not_implemented.clone().into() } + // shortcuts for common type + #[inline] pub fn new_int + ToPrimitive>(&self, i: T) -> PyIntRef { if let Some(i) = i.to_i32() { @@ -235,12 +236,6 @@ impl PyContext { PyObject::new(PyList::from(elements), self.types.list_type.clone(), None) } - pub fn new_set(&self) -> set::PySetRef { - // Initialized empty, as calling __hash__ is required for adding each object to the set - // which requires a VM context - this is done in the set code itself. - PyRef::new_ref(set::PySet::default(), self.types.set_type.clone(), None) - } - pub fn new_dict(&self) -> PyDictRef { PyRef::new_ref(PyDict::default(), self.types.dict_type.clone(), None) }