From 9822749c92de2d1795df1f57c04778ed9146cb59 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 9 Mar 2023 23:51:45 +0900 Subject: [PATCH] PyNumberBinaryOpSlot -> PyNumberBinaryOp it is not a slot --- vm/src/protocol/mod.rs | 2 +- vm/src/protocol/number.rs | 8 ++++---- vm/src/protocol/sequence.rs | 10 +++------- vm/src/stdlib/builtins.rs | 4 ++-- vm/src/types/slot.rs | 12 +++++------ vm/src/vm/vm_ops.rs | 40 ++++++++++++++----------------------- 6 files changed, 31 insertions(+), 45 deletions(-) diff --git a/vm/src/protocol/mod.rs b/vm/src/protocol/mod.rs index 9c7aa7107..a41d843e9 100644 --- a/vm/src/protocol/mod.rs +++ b/vm/src/protocol/mod.rs @@ -11,6 +11,6 @@ pub use callable::PyCallable; pub use iter::{PyIter, PyIterIter, PyIterReturn}; pub use mapping::{PyMapping, PyMappingMethods}; pub use number::{ - PyNumber, PyNumberBinaryFunc, PyNumberBinaryOpSlot, PyNumberMethods, PyNumberUnaryFunc, + PyNumber, PyNumberBinaryFunc, PyNumberBinaryOp, PyNumberMethods, PyNumberUnaryFunc, }; pub use sequence::{PySequence, PySequenceMethods}; diff --git a/vm/src/protocol/number.rs b/vm/src/protocol/number.rs index a3fae13e3..a4abec025 100644 --- a/vm/src/protocol/number.rs +++ b/vm/src/protocol/number.rs @@ -196,9 +196,9 @@ impl PyNumberMethods { pub fn get_binary_op( &self, - op_slot: &PyNumberBinaryOpSlot, + op_slot: PyNumberBinaryOp, ) -> PyResult<&Option> { - use PyNumberBinaryOpSlot::*; + use PyNumberBinaryOp::*; let binary_op = match op_slot { Add => &self.add, Subtract => &self.subtract, @@ -233,7 +233,7 @@ impl PyNumberMethods { } #[derive(Copy, Clone)] -pub enum PyNumberBinaryOpSlot { +pub enum PyNumberBinaryOp { Add, Subtract, Multiply, @@ -291,7 +291,7 @@ impl PyNumber<'_> { pub fn get_binary_op( &self, - op_slot: &PyNumberBinaryOpSlot, + op_slot: PyNumberBinaryOp, ) -> PyResult<&Option> { self.methods().get_binary_op(op_slot) } diff --git a/vm/src/protocol/sequence.rs b/vm/src/protocol/sequence.rs index 32c65083e..36c66ad8b 100644 --- a/vm/src/protocol/sequence.rs +++ b/vm/src/protocol/sequence.rs @@ -2,7 +2,7 @@ use crate::{ builtins::{type_::PointerSlot, PyList, PyListRef, PySlice, PyTuple, PyTupleRef}, convert::ToPyObject, function::PyArithmeticValue, - protocol::{PyMapping, PyNumberBinaryOpSlot}, + protocol::{PyMapping, PyNumberBinaryOp}, AsObject, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; @@ -118,7 +118,7 @@ impl PySequence<'_> { // if both arguments apear to be sequences, try fallback to __add__ if self.check() && other.to_sequence(vm).check() { - let ret = vm.binary_op1(self.obj, other, &PyNumberBinaryOpSlot::Add)?; + let ret = vm.binary_op1(self.obj, other, PyNumberBinaryOp::Add)?; if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) { return Ok(ret); } @@ -137,11 +137,7 @@ impl PySequence<'_> { // fallback to __mul__ if self.check() { - let ret = vm.binary_op1( - self.obj, - &n.to_pyobject(vm), - &PyNumberBinaryOpSlot::Multiply, - )?; + let ret = vm.binary_op1(self.obj, &n.to_pyobject(vm), PyNumberBinaryOp::Multiply)?; if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) { return Ok(ret); } diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index d25bb404e..95929debc 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -21,7 +21,7 @@ mod builtins { ArgBytesLike, ArgCallable, ArgIntoBool, ArgIterable, ArgMapping, ArgStrOrBytesLike, Either, FuncArgs, KwArgs, OptionalArg, OptionalOption, PosArgs, PyArithmeticValue, }, - protocol::{PyIter, PyIterReturn, PyNumberBinaryOpSlot}, + protocol::{PyIter, PyIterReturn, PyNumberBinaryOp}, py_io, readline::{Readline, ReadlineResult}, stdlib::sys, @@ -605,7 +605,7 @@ mod builtins { modulus, } = args; match modulus { - None => vm.binary_op(&x, &y, &PyNumberBinaryOpSlot::Power, "pow"), + None => vm.binary_op(&x, &y, PyNumberBinaryOp::Power, "pow"), Some(z) => { let try_pow_value = |obj: &PyObject, args: (PyObjectRef, PyObjectRef, PyObjectRef)| diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 01900daeb..9d3483353 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -7,7 +7,7 @@ use crate::{ identifier, protocol::{ PyBuffer, PyIterReturn, PyMapping, PyMappingMethods, PyNumber, PyNumberBinaryFunc, - PyNumberBinaryOpSlot, PyNumberMethods, PyNumberUnaryFunc, PySequence, PySequenceMethods, + PyNumberBinaryOp, PyNumberMethods, PyNumberUnaryFunc, PySequence, PySequenceMethods, }, vm::Context, AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, @@ -163,9 +163,9 @@ pub struct PyNumberSlots { impl PyNumberSlots { pub fn get_left_binary_op( &self, - op_slot: &PyNumberBinaryOpSlot, + op_slot: PyNumberBinaryOp, ) -> PyResult> { - use PyNumberBinaryOpSlot::*; + use PyNumberBinaryOp::*; let binary_op = match op_slot { Add => self.add.load(), Subtract => self.subtract.load(), @@ -200,9 +200,9 @@ impl PyNumberSlots { pub fn get_right_binary_op( &self, - op_slot: &PyNumberBinaryOpSlot, + op_slot: PyNumberBinaryOp, ) -> PyResult> { - use PyNumberBinaryOpSlot::*; + use PyNumberBinaryOp::*; let binary_op = match op_slot { Add => self.right_add.load(), Subtract => self.right_subtract.load(), @@ -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(&PyNumberBinaryOpSlot::$op_slot)?.unwrap()( + num.get_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 9832982f9..af178ca34 100644 --- a/vm/src/vm/vm_ops.rs +++ b/vm/src/vm/vm_ops.rs @@ -2,7 +2,7 @@ use super::{PyMethod, VirtualMachine}; use crate::{ builtins::{PyInt, PyIntRef, PyStr, PyStrRef}, object::{AsObject, PyObject, PyObjectRef, PyResult}, - protocol::{PyIterReturn, PyNumberBinaryOpSlot, PySequence}, + protocol::{PyIterReturn, PyNumberBinaryOp, PySequence}, types::PyComparisonOp, }; use num_traits::ToPrimitive; @@ -10,7 +10,7 @@ use num_traits::ToPrimitive; macro_rules! binary_func { ($fn:ident, $op_slot:ident, $op:expr) => { pub fn $fn(&self, a: &PyObject, b: &PyObject) -> PyResult { - self.binary_op(a, b, &PyNumberBinaryOpSlot::$op_slot, $op) + self.binary_op(a, b, PyNumberBinaryOp::$op_slot, $op) } }; } @@ -21,8 +21,8 @@ macro_rules! inplace_binary_func { self.binary_iop( a, b, - &PyNumberBinaryOpSlot::$iop_slot, - &PyNumberBinaryOpSlot::$op_slot, + PyNumberBinaryOp::$iop_slot, + PyNumberBinaryOp::$op_slot, $op, ) } @@ -131,12 +131,7 @@ impl VirtualMachine { /// b.rop(b,a)[*], a.op(a,b), b.rop(b,a) /// /// [*] 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: &PyNumberBinaryOpSlot, - ) -> PyResult { + 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 mut slot_b = if b.class().is(a.class()) { None @@ -181,7 +176,7 @@ impl VirtualMachine { &self, a: &PyObject, b: &PyObject, - op_slot: &PyNumberBinaryOpSlot, + op_slot: PyNumberBinaryOp, op: &str, ) -> PyResult { let result = self.binary_op1(a, b, op_slot)?; @@ -208,8 +203,8 @@ impl VirtualMachine { &self, a: &PyObject, b: &PyObject, - iop_slot: &PyNumberBinaryOpSlot, - op_slot: &PyNumberBinaryOpSlot, + iop_slot: PyNumberBinaryOp, + op_slot: PyNumberBinaryOp, ) -> PyResult { if let Some(slot) = a.class().slots.number.get_left_binary_op(iop_slot)? { let x = slot(a.to_number(), b, self)?; @@ -224,8 +219,8 @@ impl VirtualMachine { &self, a: &PyObject, b: &PyObject, - iop_slot: &PyNumberBinaryOpSlot, - op_slot: &PyNumberBinaryOpSlot, + iop_slot: PyNumberBinaryOp, + op_slot: PyNumberBinaryOp, op: &str, ) -> PyResult { let result = self.binary_iop1(a, b, iop_slot, op_slot)?; @@ -261,7 +256,7 @@ impl VirtualMachine { inplace_binary_func!(_imatmul, InplaceMatrixMultiply, MatrixMultiply, "@="); pub fn _add(&self, a: &PyObject, b: &PyObject) -> PyResult { - let result = self.binary_op1(a, b, &PyNumberBinaryOpSlot::Add)?; + let result = self.binary_op1(a, b, PyNumberBinaryOp::Add)?; if !result.is(&self.ctx.not_implemented) { return Ok(result); } @@ -275,12 +270,7 @@ impl VirtualMachine { } pub fn _iadd(&self, a: &PyObject, b: &PyObject) -> PyResult { - let result = self.binary_iop1( - a, - b, - &PyNumberBinaryOpSlot::InplaceAdd, - &PyNumberBinaryOpSlot::Add, - )?; + let result = self.binary_iop1(a, b, PyNumberBinaryOp::InplaceAdd, PyNumberBinaryOp::Add)?; if !result.is(&self.ctx.not_implemented) { return Ok(result); } @@ -294,7 +284,7 @@ impl VirtualMachine { } pub fn _mul(&self, a: &PyObject, b: &PyObject) -> PyResult { - let result = self.binary_op1(a, b, &PyNumberBinaryOpSlot::Multiply)?; + let result = self.binary_op1(a, b, PyNumberBinaryOp::Multiply)?; if !result.is(&self.ctx.not_implemented) { return Ok(result); } @@ -318,8 +308,8 @@ impl VirtualMachine { let result = self.binary_iop1( a, b, - &PyNumberBinaryOpSlot::InplaceMultiply, - &PyNumberBinaryOpSlot::Multiply, + PyNumberBinaryOp::InplaceMultiply, + PyNumberBinaryOp::Multiply, )?; if !result.is(&self.ctx.not_implemented) { return Ok(result);