From fb7393e4bb41ea1136b119e6754ac43fdc37829d Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Tue, 21 Mar 2023 22:17:14 +0200 Subject: [PATCH] impl create PyNumberSlots from PyNumberMethods --- derive-impl/src/pyclass.rs | 4 +-- vm/src/protocol/number.rs | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/derive-impl/src/pyclass.rs b/derive-impl/src/pyclass.rs index 6477111dc..90fe03ddd 100644 --- a/derive-impl/src/pyclass.rs +++ b/derive-impl/src/pyclass.rs @@ -703,7 +703,6 @@ where let tokens = { 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 => @@ -713,9 +712,10 @@ where quote_spanned! { span => slots.#slot_ident.store(Some(PointerSlot::from(Self::#ident()))); } - } else if STATIC_SLOTS.contains(&&slot_name.as_str()) { + } else if "as_number" == slot_name.as_str() { quote_spanned! { span => slots.#slot_ident = Self::#ident(); + slots.number = Self::#ident().into(); } } else { quote_spanned! { span => diff --git a/vm/src/protocol/number.rs b/vm/src/protocol/number.rs index 050b3837b..55a57c231 100644 --- a/vm/src/protocol/number.rs +++ b/vm/src/protocol/number.rs @@ -293,6 +293,64 @@ pub struct PyNumberSlots { pub inplace_matrix_multiply: AtomicCell>, } +impl From<&PyNumberMethods> for PyNumberSlots { + fn from(value: &PyNumberMethods) -> Self { + // right_* functions will use the same left function as PyNumberMethods + // allows both f(self, other) and f(other, self) + Self { + add: AtomicCell::new(value.add), + subtract: AtomicCell::new(value.subtract), + multiply: AtomicCell::new(value.multiply), + remainder: AtomicCell::new(value.remainder), + divmod: AtomicCell::new(value.divmod), + power: AtomicCell::new(value.power), + negative: AtomicCell::new(value.negative), + positive: AtomicCell::new(value.positive), + absolute: AtomicCell::new(value.absolute), + boolean: AtomicCell::new(value.boolean), + invert: AtomicCell::new(value.invert), + lshift: AtomicCell::new(value.lshift), + rshift: AtomicCell::new(value.rshift), + and: AtomicCell::new(value.and), + xor: AtomicCell::new(value.xor), + or: AtomicCell::new(value.or), + int: AtomicCell::new(value.int), + float: AtomicCell::new(value.float), + right_add: AtomicCell::new(value.add), + right_subtract: AtomicCell::new(value.subtract), + right_multiply: AtomicCell::new(value.multiply), + right_remainder: AtomicCell::new(value.remainder), + right_divmod: AtomicCell::new(value.divmod), + right_power: AtomicCell::new(value.power), + right_lshift: AtomicCell::new(value.lshift), + right_rshift: AtomicCell::new(value.rshift), + right_and: AtomicCell::new(value.and), + right_xor: AtomicCell::new(value.xor), + right_or: AtomicCell::new(value.or), + inplace_add: AtomicCell::new(value.inplace_add), + inplace_subtract: AtomicCell::new(value.inplace_subtract), + inplace_multiply: AtomicCell::new(value.inplace_multiply), + inplace_remainder: AtomicCell::new(value.inplace_remainder), + inplace_power: AtomicCell::new(value.inplace_power), + inplace_lshift: AtomicCell::new(value.inplace_lshift), + inplace_rshift: AtomicCell::new(value.inplace_rshift), + inplace_and: AtomicCell::new(value.inplace_and), + inplace_xor: AtomicCell::new(value.inplace_xor), + inplace_or: AtomicCell::new(value.inplace_or), + floor_divide: AtomicCell::new(value.floor_divide), + true_divide: AtomicCell::new(value.true_divide), + right_floor_divide: AtomicCell::new(value.floor_divide), + right_true_divide: AtomicCell::new(value.true_divide), + inplace_floor_divide: AtomicCell::new(value.inplace_floor_divide), + inplace_true_divide: AtomicCell::new(value.inplace_true_divide), + index: AtomicCell::new(value.index), + matrix_multiply: AtomicCell::new(value.matrix_multiply), + right_matrix_multiply: AtomicCell::new(value.matrix_multiply), + inplace_matrix_multiply: AtomicCell::new(value.inplace_matrix_multiply), + } + } +} + impl PyNumberSlots { pub fn left_binary_op(&self, op_slot: PyNumberBinaryOp) -> Option { use PyNumberBinaryOp::*;