diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 0089bd8fd..1b6f5ad52 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -3,7 +3,7 @@ use crate::common::hash::PyHash; use crate::{ function::FuncArgs, types::PyComparisonOp, utils::Either, IdProtocol, ItemProtocol, PyArithmeticValue, PyAttributes, PyClassImpl, PyComparisonValue, PyContext, PyObject, - PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine, + PyObjectPtr, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine, }; /// object() @@ -296,14 +296,14 @@ impl PyBaseObject { } #[pyslot] - fn slot_hash(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyResult { + fn slot_hash(zelf: PyObjectPtr, _vm: &VirtualMachine) -> PyResult { Ok(zelf.get_id() as _) } /// Return hash(self). #[pymethod(magic)] fn hash(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { - Self::slot_hash(&zelf, vm) + zelf.with_ptr(|zelf| Self::slot_hash(zelf, vm)) } } diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index 46406d8a4..e905803c9 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -230,7 +230,7 @@ impl PyObjectRef { .class() .mro_find_map(|cls| cls.slots.hash.load()) .unwrap(); // hash always exist - hash(self, vm) + self.with_ptr(|zelf| hash(zelf, vm)) } // const hash_not_implemented: fn(&PyObjectRef, &VirtualMachine) ->PyResult = crate::types::Unhashable::slot_hash; diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index d70c1041f..9886b2589 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -3,7 +3,9 @@ use crate::common::{ rc::PyRc, static_cell, }; -pub use crate::pyobjectrc::{PyObject, PyObjectRef, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef}; +pub use crate::pyobjectrc::{ + PyObject, PyObjectPtr, PyObjectRef, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef, +}; use crate::{ builtins::{ builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef}, diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 8e8b1d714..f6094a792 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -4,8 +4,8 @@ use crate::{ function::{FromArgs, FuncArgs, IntoPyResult, OptionalArg}, protocol::{PyBuffer, PyIterReturn, PyMappingMethods}, utils::Either, - IdProtocol, PyComparisonValue, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, - VirtualMachine, + IdProtocol, PyComparisonValue, PyObjectPtr, PyObjectRef, PyRef, PyResult, PyValue, + TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use num_traits::ToPrimitive; @@ -129,7 +129,7 @@ impl Default for PyTypeFlags { pub(crate) type GenericMethod = fn(&PyObjectRef, FuncArgs, &VirtualMachine) -> PyResult; pub(crate) type AsMappingFunc = fn(&PyObjectRef, &VirtualMachine) -> PyMappingMethods; -pub(crate) type HashFunc = fn(&PyObjectRef, &VirtualMachine) -> PyResult; +pub(crate) type HashFunc = fn(PyObjectPtr, &VirtualMachine) -> PyResult; // CallFunc = GenericMethod pub(crate) type GetattroFunc = fn(PyObjectRef, PyStrRef, &VirtualMachine) -> PyResult; pub(crate) type SetattroFunc = @@ -192,8 +192,8 @@ fn as_mapping_wrapper(zelf: &PyObjectRef, _vm: &VirtualMachine) -> PyMappingMeth } } -fn hash_wrapper(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult { - let hash_obj = vm.call_special_method(zelf.clone(), "__hash__", ())?; +fn hash_wrapper(zelf: PyObjectPtr, vm: &VirtualMachine) -> PyResult { + let hash_obj = vm.call_special_method((*zelf).clone(), "__hash__", ())?; match hash_obj.payload_if_subclass::(vm) { Some(py_int) => Ok(rustpython_common::hash::hash_bigint(py_int.as_bigint())), None => Err(vm.new_type_error("__hash__ method should return an integer".to_owned())), @@ -476,7 +476,7 @@ pub trait GetDescriptor: PyValue { pub trait Hashable: PyValue { #[inline] #[pyslot] - fn slot_hash(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn slot_hash(zelf: PyObjectPtr, vm: &VirtualMachine) -> PyResult { if let Some(zelf) = zelf.downcast_ref() { Self::hash(zelf, vm) } else { @@ -487,7 +487,7 @@ pub trait Hashable: PyValue { #[inline] #[pymethod] fn __hash__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { - Self::slot_hash(&zelf, vm) + zelf.with_ptr(|zelf| Self::slot_hash(zelf, vm)) } fn hash(zelf: &PyRef, vm: &VirtualMachine) -> PyResult; @@ -499,7 +499,7 @@ impl Hashable for T where T: Unhashable, { - fn slot_hash(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn slot_hash(zelf: PyObjectPtr, vm: &VirtualMachine) -> PyResult { Err(vm.new_type_error(format!("unhashable type: '{}'", zelf.class().name()))) }