From 3c4ac0e5a2740f6f9898cfe73f7384f6cd39b7c6 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 10 Mar 2023 00:20:26 +0900 Subject: [PATCH] clean up PyNumber interfaces --- vm/src/protocol/number.rs | 77 ++++++++++++++++----------------------- vm/src/types/slot.rs | 6 +-- vm/src/vm/vm_ops.rs | 6 +-- 3 files changed, 37 insertions(+), 52 deletions(-) diff --git a/vm/src/protocol/number.rs b/vm/src/protocol/number.rs index a4abec025..f9a0ffb02 100644 --- a/vm/src/protocol/number.rs +++ b/vm/src/protocol/number.rs @@ -194,41 +194,37 @@ impl PyNumberMethods { inplace_matrix_multiply: None, }; - pub fn get_binary_op( - &self, - op_slot: PyNumberBinaryOp, - ) -> PyResult<&Option> { + pub fn binary_op(&self, op_slot: PyNumberBinaryOp) -> Option { use PyNumberBinaryOp::*; - let binary_op = match op_slot { - Add => &self.add, - Subtract => &self.subtract, - Multiply => &self.multiply, - Remainder => &self.remainder, - Divmod => &self.divmod, - Power => &self.power, - Lshift => &self.lshift, - Rshift => &self.rshift, - And => &self.and, - Xor => &self.xor, - Or => &self.or, - InplaceAdd => &self.inplace_add, - InplaceSubtract => &self.inplace_subtract, - InplaceMultiply => &self.inplace_multiply, - InplaceRemainder => &self.inplace_remainder, - InplacePower => &self.inplace_power, - InplaceLshift => &self.inplace_lshift, - InplaceRshift => &self.inplace_rshift, - InplaceAnd => &self.inplace_and, - InplaceXor => &self.inplace_xor, - InplaceOr => &self.inplace_or, - FloorDivide => &self.floor_divide, - TrueDivide => &self.true_divide, - InplaceFloorDivide => &self.inplace_floor_divide, - InplaceTrueDivide => &self.inplace_true_divide, - MatrixMultiply => &self.matrix_multiply, - InplaceMatrixMultiply => &self.inplace_matrix_multiply, - }; - Ok(binary_op) + match op_slot { + Add => self.add, + Subtract => self.subtract, + Multiply => self.multiply, + Remainder => self.remainder, + Divmod => self.divmod, + Power => self.power, + Lshift => self.lshift, + Rshift => self.rshift, + And => self.and, + Xor => self.xor, + Or => self.or, + InplaceAdd => self.inplace_add, + InplaceSubtract => self.inplace_subtract, + InplaceMultiply => self.inplace_multiply, + InplaceRemainder => self.inplace_remainder, + InplacePower => self.inplace_power, + InplaceLshift => self.inplace_lshift, + InplaceRshift => self.inplace_rshift, + InplaceAnd => self.inplace_and, + InplaceXor => self.inplace_xor, + InplaceOr => self.inplace_or, + FloorDivide => self.floor_divide, + TrueDivide => self.true_divide, + InplaceFloorDivide => self.inplace_floor_divide, + InplaceTrueDivide => self.inplace_true_divide, + MatrixMultiply => self.matrix_multiply, + InplaceMatrixMultiply => self.inplace_matrix_multiply, + } } } @@ -266,7 +262,7 @@ pub enum PyNumberBinaryOp { #[derive(Copy, Clone)] pub struct PyNumber<'a> { pub obj: &'a PyObject, - methods: &'a PyNumberMethods, + pub(crate) methods: &'a PyNumberMethods, } impl<'a> From<&'a PyObject> for PyNumber<'a> { @@ -285,17 +281,6 @@ impl PyNumber<'_> { obj.class().mro_find_map(|x| x.slots.as_number.load()) } - pub fn methods(&self) -> &PyNumberMethods { - self.methods - } - - pub fn get_binary_op( - &self, - op_slot: PyNumberBinaryOp, - ) -> PyResult<&Option> { - self.methods().get_binary_op(op_slot) - } - // PyNumber_Check pub fn check(obj: &PyObject) -> bool { let methods = &obj.class().slots.number; diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 9d3483353..32975c91a 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -161,7 +161,7 @@ pub struct PyNumberSlots { } impl PyNumberSlots { - pub fn get_left_binary_op( + pub fn left_binary_op( &self, op_slot: PyNumberBinaryOp, ) -> PyResult> { @@ -198,7 +198,7 @@ impl PyNumberSlots { Ok(binary_op) } - pub fn get_right_binary_op( + pub fn right_binary_op( &self, op_slot: PyNumberBinaryOp, ) -> PyResult> { @@ -1281,7 +1281,7 @@ macro_rules! extend_number_slot { if $methods.$method.is_some() { $slots.number.$method.store($methods.$method); $slots.number.$right_method.store(Some(|num, other, vm| { - num.get_binary_op(PyNumberBinaryOp::$op_slot)?.unwrap()( + num.methods.binary_op(PyNumberBinaryOp::$op_slot).unwrap()( other.to_number(), num.obj, vm, diff --git a/vm/src/vm/vm_ops.rs b/vm/src/vm/vm_ops.rs index af178ca34..ccc77b440 100644 --- a/vm/src/vm/vm_ops.rs +++ b/vm/src/vm/vm_ops.rs @@ -132,11 +132,11 @@ impl VirtualMachine { /// /// [*] only when Py_TYPE(a) != Py_TYPE(b) && Py_TYPE(b) is a subclass of Py_TYPE(a) pub fn binary_op1(&self, a: &PyObject, b: &PyObject, op_slot: PyNumberBinaryOp) -> PyResult { - let slot_a = a.class().slots.number.get_left_binary_op(op_slot)?; + let slot_a = a.class().slots.number.left_binary_op(op_slot)?; let mut slot_b = if b.class().is(a.class()) { None } else { - match b.class().slots.number.get_right_binary_op(op_slot)? { + match b.class().slots.number.right_binary_op(op_slot)? { Some(slot_b) if slot_b as usize == slot_a.map(|s| s as usize).unwrap_or_default() => { @@ -206,7 +206,7 @@ impl VirtualMachine { iop_slot: PyNumberBinaryOp, op_slot: PyNumberBinaryOp, ) -> PyResult { - if let Some(slot) = a.class().slots.number.get_left_binary_op(iop_slot)? { + if let Some(slot) = a.class().slots.number.left_binary_op(iop_slot)? { let x = slot(a.to_number(), b, self)?; if !x.is(&self.ctx.not_implemented) { return Ok(x);