From c17951e3f62c6a62f3a196de6e4753dbda40537c Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 5 Mar 2023 22:27:45 +0900 Subject: [PATCH] Refactor AsNumber for PyFloat --- vm/src/builtins/float.rs | 69 ++++++++++++---------------------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 6cb8fb32d..38e152139 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -546,41 +546,34 @@ impl Hashable for PyFloat { impl AsNumber for PyFloat { fn as_number() -> &'static PyNumberMethods { static AS_NUMBER: Lazy = Lazy::new(|| PyNumberMethods { - add: atomic_func!(|num, other, vm| PyFloat::number_float_op( + add: atomic_func!(|num, other, vm| PyFloat::number_op( num, other, - |a, b| a + b, + |a, b, _vm| a + b, vm )), - subtract: atomic_func!(|num, other, vm| PyFloat::number_float_op( + subtract: atomic_func!(|num, other, vm| PyFloat::number_op( num, other, - |a, b| a - b, + |a, b, _vm| a - b, vm )), - multiply: atomic_func!(|num, other, vm| PyFloat::number_float_op( + multiply: atomic_func!(|num, other, vm| PyFloat::number_op( num, other, - |a, b| a * b, + |a, b, _vm| a * b, vm )), - remainder: atomic_func!(|num, other, vm| PyFloat::number_general_op( - num, other, inner_mod, vm - )), - divmod: atomic_func!(|num, other, vm| PyFloat::number_general_op( - num, - other, - inner_divmod, - vm - )), - power: atomic_func!(|num, other, vm| PyFloat::number_general_op( - num, other, float_pow, vm - )), + remainder: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, inner_mod, vm)), + divmod: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, inner_divmod, vm)), + power: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, float_pow, vm)), negative: atomic_func!(|num, vm| { let value = PyFloat::number_downcast(num).value; (-value).to_pyresult(vm) }), - positive: atomic_func!(|num, vm| PyFloat::number_float(num, vm).to_pyresult(vm)), + positive: atomic_func!( + |num, vm| PyFloat::number_downcast_exact(num, vm).to_pyresult(vm) + ), absolute: atomic_func!(|num, vm| { let value = PyFloat::number_downcast(num).value; value.abs().to_pyresult(vm) @@ -590,26 +583,26 @@ impl AsNumber for PyFloat { let value = PyFloat::number_downcast(num).value; try_to_bigint(value, vm).map(|x| vm.ctx.new_int(x)) }), - float: atomic_func!(|num, vm| Ok(PyFloat::number_float(num, vm))), + float: atomic_func!(|num, vm| Ok(PyFloat::number_downcast_exact(num, vm))), floor_divide: atomic_func!(|num, other, vm| { - PyFloat::number_general_op(num, other, inner_floordiv, vm) + PyFloat::number_op(num, other, inner_floordiv, vm) }), true_divide: atomic_func!(|num, other, vm| { - PyFloat::number_general_op(num, other, inner_div, vm) + PyFloat::number_op(num, other, inner_div, vm) }), ..PyNumberMethods::NOT_IMPLEMENTED }); &AS_NUMBER } + + #[inline] + fn clone_exact(zelf: &Py, vm: &VirtualMachine) -> PyRef { + vm.ctx.new_float(zelf.value) + } } impl PyFloat { - fn number_general_op( - number: PyNumber, - other: &PyObject, - op: F, - vm: &VirtualMachine, - ) -> PyResult + fn number_op(number: PyNumber, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult where F: FnOnce(f64, f64, &VirtualMachine) -> R, R: ToPyResult, @@ -620,26 +613,6 @@ impl PyFloat { Ok(vm.ctx.not_implemented()) } } - - fn number_float_op( - number: PyNumber, - other: &PyObject, - op: F, - vm: &VirtualMachine, - ) -> PyResult - where - F: FnOnce(f64, f64) -> f64, - { - Self::number_general_op(number, other, |a, b, _vm| op(a, b), vm) - } - - fn number_float(number: PyNumber, vm: &VirtualMachine) -> PyRef { - if let Some(zelf) = number.obj.downcast_ref_if_exact::(vm) { - zelf.to_owned() - } else { - vm.ctx.new_float(Self::number_downcast(number).value) - } - } } // Retrieve inner float value: