From 788ccffb2b5717b9fffaf8bc8647ee7d294ad6ef Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 9 Mar 2023 22:58:44 +0900 Subject: [PATCH] Make PyNumberMethods static --- vm/src/builtins/bool.rs | 31 +++-------------------- vm/src/builtins/int.rs | 56 +++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 55 deletions(-) diff --git a/vm/src/builtins/bool.rs b/vm/src/builtins/bool.rs index 010212e67..417cc0e8d 100644 --- a/vm/src/builtins/bool.rs +++ b/vm/src/builtins/bool.rs @@ -11,7 +11,6 @@ use crate::{ }; use num_bigint::Sign; use num_traits::Zero; -use once_cell::sync::Lazy; use std::fmt::{Debug, Formatter}; impl ToPyObject for bool { @@ -172,28 +171,9 @@ impl PyBool { } } -macro_rules! int_method { - ($method:ident) => { - PyInt::as_number().$method - }; -} - impl AsNumber for PyBool { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { - add: int_method!(add), - subtract: int_method!(subtract), - multiply: int_method!(multiply), - remainder: int_method!(remainder), - divmod: int_method!(divmod), - power: int_method!(power), - negative: int_method!(negative), - positive: int_method!(positive), - absolute: int_method!(absolute), - boolean: int_method!(boolean), - invert: int_method!(invert), - lshift: int_method!(lshift), - rshift: int_method!(rshift), + static AS_NUMBER: PyNumberMethods = PyNumberMethods { and: Some(|number, other, vm| { PyBool::and(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm) }), @@ -203,13 +183,8 @@ impl AsNumber for PyBool { or: Some(|number, other, vm| { PyBool::or(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm) }), - int: int_method!(int), - float: int_method!(float), - floor_divide: int_method!(floor_divide), - true_divide: int_method!(true_divide), - index: int_method!(index), - ..PyNumberMethods::NOT_IMPLEMENTED - }); + ..PyInt::AS_NUMBER + }; &AS_NUMBER } } diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index de8a062bb..90f611974 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -727,33 +727,7 @@ impl Hashable for PyInt { impl AsNumber for PyInt { fn as_number() -> &'static PyNumberMethods { - static AS_NUMBER: PyNumberMethods = PyNumberMethods { - add: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a + b, vm)), - subtract: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a - b, vm)), - multiply: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a * b, vm)), - remainder: Some(|num, other, vm| PyInt::number_op(num, other, inner_mod, vm)), - divmod: Some(|num, other, vm| PyInt::number_op(num, other, inner_divmod, vm)), - power: Some(|num, other, vm| PyInt::number_op(num, other, inner_pow, vm)), - negative: Some(|num, vm| (&PyInt::number_downcast(num).value).neg().to_pyresult(vm)), - positive: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm).into())), - absolute: Some(|num, vm| PyInt::number_downcast(num).value.abs().to_pyresult(vm)), - boolean: Some(|num, _vm| Ok(PyInt::number_downcast(num).value.is_zero())), - invert: Some(|num, vm| (&PyInt::number_downcast(num).value).not().to_pyresult(vm)), - lshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_lshift, vm)), - rshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_rshift, vm)), - and: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a & b, vm)), - xor: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a ^ b, vm)), - or: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a | b, vm)), - int: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))), - float: Some(|num, vm| { - let zelf = PyInt::number_downcast(num); - try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x)) - }), - floor_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_floordiv, vm)), - true_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_truediv, vm)), - index: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))), - ..PyNumberMethods::NOT_IMPLEMENTED - }; + static AS_NUMBER: PyNumberMethods = PyInt::AS_NUMBER; &AS_NUMBER } @@ -764,6 +738,34 @@ impl AsNumber for PyInt { } impl PyInt { + pub(super) const AS_NUMBER: PyNumberMethods = PyNumberMethods { + add: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a + b, vm)), + subtract: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a - b, vm)), + multiply: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a * b, vm)), + remainder: Some(|num, other, vm| PyInt::number_op(num, other, inner_mod, vm)), + divmod: Some(|num, other, vm| PyInt::number_op(num, other, inner_divmod, vm)), + power: Some(|num, other, vm| PyInt::number_op(num, other, inner_pow, vm)), + negative: Some(|num, vm| (&PyInt::number_downcast(num).value).neg().to_pyresult(vm)), + positive: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm).into())), + absolute: Some(|num, vm| PyInt::number_downcast(num).value.abs().to_pyresult(vm)), + boolean: Some(|num, _vm| Ok(PyInt::number_downcast(num).value.is_zero())), + invert: Some(|num, vm| (&PyInt::number_downcast(num).value).not().to_pyresult(vm)), + lshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_lshift, vm)), + rshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_rshift, vm)), + and: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a & b, vm)), + xor: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a ^ b, vm)), + or: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a | b, vm)), + int: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))), + float: Some(|num, vm| { + let zelf = PyInt::number_downcast(num); + try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x)) + }), + floor_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_floordiv, vm)), + true_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_truediv, vm)), + index: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))), + ..PyNumberMethods::NOT_IMPLEMENTED + }; + fn number_op(number: PyNumber, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult where F: FnOnce(&BigInt, &BigInt, &VirtualMachine) -> R,