@@ -3,7 +3,7 @@ use crate::{
3
3
builtins:: { PyInt , PyIntRef , PyStrInterned } ,
4
4
function:: PyArithmeticValue ,
5
5
object:: { AsObject , PyObject , PyObjectRef , PyResult } ,
6
- protocol:: PyIterReturn ,
6
+ protocol:: { PyIterReturn , PyNumberMethodsOffset } ,
7
7
types:: PyComparisonOp ,
8
8
} ;
9
9
@@ -174,6 +174,62 @@ impl VirtualMachine {
174
174
} )
175
175
}
176
176
177
+ #[ allow( unused_variables) ]
178
+ fn binary_op1 ( & self , a : & PyObject , b : & PyObject , op_slot : PyNumberMethodsOffset ) -> PyResult {
179
+ let num_a = a. to_number ( ) ;
180
+ let num_b = b. to_number ( ) ;
181
+
182
+ // ! `multiply` should be changed after this function works normally!!
183
+ let slot_a = num_a. methods ( ) . multiply . load ( ) ;
184
+ let slot_b = num_b. methods ( ) . multiply . load ( ) ;
185
+
186
+ if let Some ( slot_a) = slot_a {
187
+ if let Some ( slot_b) = slot_b {
188
+ // Check if a is subclass of b
189
+ if a. fast_isinstance ( & b. class ( ) ) {
190
+ let ret = slot_b ( & num_b, a, self ) ?;
191
+ if ret. rich_compare_bool (
192
+ self . ctx . not_implemented . as_object ( ) ,
193
+ PyComparisonOp :: Ne ,
194
+ self ,
195
+ ) ? {
196
+ return Ok ( ret) ;
197
+ }
198
+ }
199
+ }
200
+ let ret = slot_a ( & num_a, b, self ) ?;
201
+ if ret. rich_compare_bool (
202
+ self . ctx . not_implemented . as_object ( ) ,
203
+ PyComparisonOp :: Ne ,
204
+ self ,
205
+ ) ? {
206
+ return Ok ( ret) ;
207
+ }
208
+ }
209
+ // No slot_a
210
+ if let Some ( slot_b) = slot_b {
211
+ let ret = slot_b ( & num_b, a, self ) ?;
212
+ if ret. rich_compare_bool (
213
+ self . ctx . not_implemented . as_object ( ) ,
214
+ PyComparisonOp :: Ne ,
215
+ self ,
216
+ ) ? {
217
+ return Ok ( ret) ;
218
+ }
219
+ }
220
+
221
+ // Both slot_a & slot_b don't exist.
222
+ // Ok(self.ctx.not_implemented())
223
+
224
+ self . call_or_reflection (
225
+ a,
226
+ b,
227
+ identifier ! ( self , __mul__) ,
228
+ identifier ! ( self , __rmul__) ,
229
+ |vm, a, b| Err ( vm. new_unsupported_binop_error ( a, b, "*" ) ) ,
230
+ )
231
+ }
232
+
177
233
pub fn _sub ( & self , a : & PyObject , b : & PyObject ) -> PyResult {
178
234
self . call_or_reflection (
179
235
a,
@@ -219,13 +275,15 @@ impl VirtualMachine {
219
275
}
220
276
221
277
pub fn _mul ( & self , a : & PyObject , b : & PyObject ) -> PyResult {
222
- self . call_or_reflection (
223
- a,
224
- b,
225
- identifier ! ( self , __mul__) ,
226
- identifier ! ( self , __rmul__) ,
227
- |vm, a, b| Err ( vm. new_unsupported_binop_error ( a, b, "*" ) ) ,
228
- )
278
+ self . binary_op1 ( a, b, PyNumberMethodsOffset :: Multiply )
279
+
280
+ // self.call_or_reflection(
281
+ // a,
282
+ // b,
283
+ // identifier!(self, __mul__),
284
+ // identifier!(self, __rmul__),
285
+ // |vm, a, b| Err(vm.new_unsupported_binop_error(a, b, "*")),
286
+ // )
229
287
}
230
288
231
289
pub fn _imul ( & self , a : & PyObject , b : & PyObject ) -> PyResult {
0 commit comments