@@ -8,7 +8,6 @@ use crate::{
8
8
VirtualMachine ,
9
9
} ;
10
10
use crossbeam_utils:: atomic:: AtomicCell ;
11
- use std:: ptr;
12
11
13
12
type UnaryFunc < R = PyObjectRef > = AtomicCell < Option < fn ( PyNumber , & VirtualMachine ) -> PyResult < R > > > ;
14
13
type BinaryFunc < R = PyObjectRef > =
@@ -110,7 +109,6 @@ impl PyObject {
110
109
}
111
110
112
111
#[ derive( Default ) ]
113
- // #[repr(C)]
114
112
pub struct PyNumberMethods {
115
113
/* Number implementations must check *both*
116
114
arguments for proper type and implement the necessary conversions
@@ -138,7 +136,6 @@ pub struct PyNumberMethods {
138
136
pub inplace_subtract : BinaryFunc ,
139
137
pub inplace_multiply : BinaryFunc ,
140
138
pub inplace_remainder : BinaryFunc ,
141
- pub inplace_divmod : BinaryFunc ,
142
139
pub inplace_power : BinaryFunc ,
143
140
pub inplace_lshift : BinaryFunc ,
144
141
pub inplace_rshift : BinaryFunc ,
@@ -184,7 +181,6 @@ impl PyNumberMethods {
184
181
inplace_subtract : AtomicCell :: new ( None ) ,
185
182
inplace_multiply : AtomicCell :: new ( None ) ,
186
183
inplace_remainder : AtomicCell :: new ( None ) ,
187
- inplace_divmod : AtomicCell :: new ( None ) ,
188
184
inplace_power : AtomicCell :: new ( None ) ,
189
185
inplace_lshift : AtomicCell :: new ( None ) ,
190
186
inplace_rshift : AtomicCell :: new ( None ) ,
@@ -199,32 +195,58 @@ impl PyNumberMethods {
199
195
matrix_multiply : AtomicCell :: new ( None ) ,
200
196
inplace_matrix_multiply : AtomicCell :: new ( None ) ,
201
197
} ;
198
+
199
+ pub fn get_binary_op ( & self , op_slot : & PyNumberBinaryOpSlot ) -> PyResult < & BinaryFunc > {
200
+ use PyNumberBinaryOpSlot :: * ;
201
+ let binary_op = match op_slot {
202
+ Add => & self . add ,
203
+ Subtract => & self . subtract ,
204
+ Multiply => & self . multiply ,
205
+ Remainder => & self . remainder ,
206
+ Divmod => & self . divmod ,
207
+ Power => & self . power ,
208
+ Lshift => & self . lshift ,
209
+ Rshift => & self . rshift ,
210
+ And => & self . and ,
211
+ Xor => & self . xor ,
212
+ Or => & self . or ,
213
+ InplaceAdd => & self . inplace_add ,
214
+ InplaceSubtract => & self . inplace_subtract ,
215
+ InplaceMultiply => & self . inplace_multiply ,
216
+ InplaceRemainder => & self . inplace_remainder ,
217
+ InplacePower => & self . inplace_power ,
218
+ InplaceLshift => & self . inplace_lshift ,
219
+ InplaceRshift => & self . inplace_rshift ,
220
+ InplaceAnd => & self . inplace_and ,
221
+ InplaceXor => & self . inplace_xor ,
222
+ InplaceOr => & self . inplace_or ,
223
+ FloorDivide => & self . floor_divide ,
224
+ TrueDivide => & self . true_divide ,
225
+ InplaceFloorDivide => & self . inplace_floor_divide ,
226
+ InplaceTrueDivide => & self . inplace_true_divide ,
227
+ MatrixMultiply => & self . matrix_multiply ,
228
+ InplaceMatrixMultiply => & self . inplace_matrix_multiply ,
229
+ } ;
230
+ Ok ( binary_op)
231
+ }
202
232
}
203
233
204
- pub enum PyNumberMethodsOffset {
234
+ pub enum PyNumberBinaryOpSlot {
205
235
Add ,
206
236
Subtract ,
207
237
Multiply ,
208
238
Remainder ,
209
239
Divmod ,
210
240
Power ,
211
- Negative ,
212
- Positive ,
213
- Absolute ,
214
- Boolean ,
215
- Invert ,
216
241
Lshift ,
217
242
Rshift ,
218
243
And ,
219
244
Xor ,
220
245
Or ,
221
- Int ,
222
- Float ,
223
246
InplaceAdd ,
224
247
InplaceSubtract ,
225
248
InplaceMultiply ,
226
249
InplaceRemainder ,
227
- InplaceDivmod ,
228
250
InplacePower ,
229
251
InplaceLshift ,
230
252
InplaceRshift ,
@@ -235,7 +257,6 @@ pub enum PyNumberMethodsOffset {
235
257
TrueDivide ,
236
258
InplaceFloorDivide ,
237
259
InplaceTrueDivide ,
238
- Index ,
239
260
MatrixMultiply ,
240
261
InplaceMatrixMultiply ,
241
262
}
@@ -262,12 +283,12 @@ impl PyNumber<'_> {
262
283
obj. class ( ) . mro_find_map ( |x| x. slots . as_number . load ( ) )
263
284
}
264
285
265
- pub fn methods < ' a > (
266
- & ' a self ,
267
- op_slot : & ' a PyNumberMethodsOffset ,
268
- vm : & VirtualMachine ,
269
- ) -> PyResult < & BinaryFunc > {
270
- op_slot . method ( self . methods , vm )
286
+ pub fn methods ( & self ) -> & PyNumberMethods {
287
+ self . methods
288
+ }
289
+
290
+ pub fn get_binary_op ( & self , op_slot : & PyNumberBinaryOpSlot ) -> PyResult < & BinaryFunc > {
291
+ self . methods ( ) . get_binary_op ( op_slot )
271
292
}
272
293
273
294
// PyNumber_Check
@@ -284,12 +305,12 @@ impl PyNumber<'_> {
284
305
285
306
// PyIndex_Check
286
307
pub fn is_index ( & self ) -> bool {
287
- self . methods . index . load ( ) . is_some ( )
308
+ self . methods ( ) . index . load ( ) . is_some ( )
288
309
}
289
310
290
311
#[ inline]
291
312
pub fn int ( self , vm : & VirtualMachine ) -> Option < PyResult < PyIntRef > > {
292
- self . methods . int . load ( ) . map ( |f| {
313
+ self . methods ( ) . int . load ( ) . map ( |f| {
293
314
let ret = f ( self , vm) ?;
294
315
let value = if !ret. class ( ) . is ( PyInt :: class ( vm) ) {
295
316
warnings:: warn (
@@ -313,7 +334,7 @@ impl PyNumber<'_> {
313
334
314
335
#[ inline]
315
336
pub fn index ( self , vm : & VirtualMachine ) -> Option < PyResult < PyIntRef > > {
316
- self . methods . index . load ( ) . map ( |f| {
337
+ self . methods ( ) . index . load ( ) . map ( |f| {
317
338
let ret = f ( self , vm) ?;
318
339
let value = if !ret. class ( ) . is ( PyInt :: class ( vm) ) {
319
340
warnings:: warn (
@@ -337,7 +358,7 @@ impl PyNumber<'_> {
337
358
338
359
#[ inline]
339
360
pub fn float ( self , vm : & VirtualMachine ) -> Option < PyResult < PyRef < PyFloat > > > {
340
- self . methods . float . load ( ) . map ( |f| {
361
+ self . methods ( ) . float . load ( ) . map ( |f| {
341
362
let ret = f ( self , vm) ?;
342
363
let value = if !ret. class ( ) . is ( PyFloat :: class ( vm) ) {
343
364
warnings:: warn (
0 commit comments