Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pass compile
  • Loading branch information
qingshi163 committed Mar 20, 2023
commit 521da91ecfc1b281b10886b7f0b0c97e2aed6891
7 changes: 6 additions & 1 deletion derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,9 @@ where
let slot_ident = Ident::new(&slot_ident.to_string().to_lowercase(), slot_ident.span());
let slot_name = slot_ident.to_string();
let tokens = {
const NON_ATOMIC_SLOTS: &[&str] = &["as_buffer", "as_number"];
const NON_ATOMIC_SLOTS: &[&str] = &["as_buffer"];
const POINTER_SLOTS: &[&str] = &["as_sequence", "as_mapping"];
const STATIC_SLOTS: &[&str] = &["as_number"];
if NON_ATOMIC_SLOTS.contains(&slot_name.as_str()) {
quote_spanned! { span =>
slots.#slot_ident = Some(Self::#ident as _);
Expand All @@ -711,6 +712,10 @@ where
quote_spanned! { span =>
slots.#slot_ident.store(Some(PointerSlot::from(Self::#ident())));
}
} else if STATIC_SLOTS.contains(&slot_name.as_str()) {
quote_spanned! { span =>
slots.#slot_ident = Some(Self::#ident());
}
} else {
quote_spanned! { span =>
slots.#slot_ident.store(Some(Self::#ident as _));
Expand Down
3 changes: 1 addition & 2 deletions vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ impl AsNumber for PyBool {
or: Some(|a, b, vm| {
PyBool::or(a.to_owned(), b.to_owned(), vm).to_pyresult(vm)
}),
// ..PyInt::AS_NUMBER
..*PyInt::as_number()
..PyInt::AS_NUMBER
};
&AS_NUMBER
}
Expand Down
12 changes: 9 additions & 3 deletions vm/src/protocol/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ pub type PyNumberBinaryFunc = fn(&PyObject, &PyObject, &VirtualMachine) -> PyRes
#[derive(Copy, Clone)]
pub struct PyNumber<'a>(&'a PyObject);

impl Deref for PyNumber<'_> {
impl<'a> Deref for PyNumber<'a> {
type>

fn deref(&self) -> &Self::Target {
self.0
}
}

impl<'a> PyNumber<'a> {
pub(crate) fn obj(self) -> &'a PyObject {
self.0
}
}

macro_rules! load_pynumber_method {
($cls:expr, $x:ident, $y:ident) => {{
let class = $cls;
if let Some(ext) = class.heaptype_ext {
if let Some(ext) = class.heaptype_ext.as_ref() {
ext.number_slots.$y.load()
} else if let Some(methods) = class.slots.as_number {
methods.$x
Expand All @@ -44,7 +50,7 @@ macro_rules! load_pynumber_method {
impl PyNumber<'_> {
pub fn check(obj: &PyObject) -> bool {
let class = obj.class();
if let Some(ext) = class.heaptype_ext {
if let Some(ext) = class.heaptype_ext.as_ref() {
ext.number_slots.int.load().is_some()
|| ext.number_slots.index.load().is_some()
|| ext.number_slots.float.load().is_some()
Expand Down
12 changes: 6 additions & 6 deletions vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
};
use crossbeam_utils::atomic::AtomicCell;
use num_traits::{Signed, ToPrimitive};
use std::{borrow::Borrow, cmp::Ordering};
use std::{borrow::Borrow, cmp::Ordering, ops::Deref};

#[macro_export]
macro_rules! atomic_func {
Expand Down Expand Up @@ -199,21 +199,21 @@ pub(crate) fn len_wrapper(obj: &PyObject, vm: &VirtualMachine) -> PyResult<usize
}

fn int_wrapper(num: PyNumber, vm: &VirtualMachine) -> PyResult<PyRef<PyInt>> {
let ret = vm.call_special_method(num.obj.to_owned(), identifier!(vm, __int__), ())?;
let ret = vm.call_special_method(num.deref().to_owned(), identifier!(vm, __int__), ())?;
ret.downcast::<PyInt>().map_err(|obj| {
vm.new_type_error(format!("__int__ returned non-int (type {})", obj.class()))
})
}

fn index_wrapper(num: PyNumber, vm: &VirtualMachine) -> PyResult<PyRef<PyInt>> {
let ret = vm.call_special_method(num.obj.to_owned(), identifier!(vm, __index__), ())?;
let ret = vm.call_special_method(num.deref().to_owned(), identifier!(vm, __index__), ())?;
ret.downcast::<PyInt>().map_err(|obj| {
vm.new_type_error(format!("__index__ returned non-int (type {})", obj.class()))
})
}

fn float_wrapper(num: PyNumber, vm: &VirtualMachine) -> PyResult<PyRef<PyFloat>> {
let ret = vm.call_special_method(num.obj.to_owned(), identifier!(vm, __float__), ())?;
let ret = vm.call_special_method(num.deref().to_owned(), identifier!(vm, __float__), ())?;
ret.downcast::<PyFloat>().map_err(|obj| {
vm.new_type_error(format!(
"__float__ returned non-float (type {})",
Expand Down Expand Up @@ -1242,11 +1242,11 @@ pub trait AsNumber: PyPayload {
fn as_number() -> &'static PyNumberMethods;

fn number_downcast(zelf: PyNumber) -> &Py<Self> {
zelf.downcast_ref().unwrap()
unsafe { zelf.obj().downcast_unchecked_ref() }
}

fn number_downcast_exact(zelf: PyNumber, vm: &VirtualMachine) -> PyRef<Self> {
if let Some(zelf) = zelf.downcast_ref_if_exact(vm) {
if let Some(zelf) = zelf.downcast_ref_if_exact::<Self>(vm) {
zelf.to_owned()
} else {
Self::clone_exact(Self::number_downcast(zelf), vm)
Expand Down