@@ -183,6 +183,29 @@ protected ForeignBinaryNode(LookupAndCallBinaryNode op, boolean reverse) {
183
183
this .reverse = reverse ;
184
184
}
185
185
186
+ protected static boolean isNegativeNumber (InteropLibrary lib , Object right ) {
187
+ long val = 0 ;
188
+ try {
189
+ if (lib .fitsInByte (right )) {
190
+ val = lib .asByte (right );
191
+ } else if (lib .fitsInShort (right )) {
192
+ val = lib .asShort (right );
193
+ } else if (lib .fitsInInt (right )) {
194
+ val = lib .asInt (right );
195
+ } else if (lib .fitsInLong (right )) {
196
+ val = lib .asLong (right );
197
+ }
198
+ return val < 0 ;
199
+ } catch (UnsupportedMessageException e ) {
200
+ // fall through
201
+ }
202
+ return false ;
203
+ }
204
+
205
+ protected static boolean isPythonLikeSequence (InteropLibrary lib , Object receiver ) {
206
+ return lib .hasArrayElements (receiver ) || lib .isString (receiver );
207
+ }
208
+
186
209
@ Specialization (guards = {"!reverse" , "lib.isBoolean(left)" })
187
210
Object doComparisonBool (Object left , Object right ,
188
211
@ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
@@ -244,11 +267,18 @@ Object doComparisonDoubleR(Object left, Object right,
244
267
}
245
268
246
269
@ SuppressWarnings ("unused" )
247
- @ Specialization (guards = {"!lib.fitsInDouble(left)" , "!lib.fitsInLong(left)" , "!lib.isBoolean(left)" })
270
+ @ Specialization (guards = {"!reverse" , "! lib.fitsInDouble(left)" , "!lib.fitsInLong(left)" , "!lib.isBoolean(left)" })
248
271
public PNotImplemented doGeneric (Object left , Object right ,
249
272
@ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
250
273
return PNotImplemented .NOT_IMPLEMENTED ;
251
274
}
275
+
276
+ @ SuppressWarnings ("unused" )
277
+ @ Specialization (guards = {"reverse" , "!lib.fitsInDouble(right)" , "!lib.fitsInLong(right)" , "!lib.isBoolean(right)" })
278
+ public PNotImplemented doGenericReverse (Object left , Object right ,
279
+ @ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
280
+ return PNotImplemented .NOT_IMPLEMENTED ;
281
+ }
252
282
}
253
283
254
284
@ Builtin (name = __ADD__ , minNumOfPositionalArgs = 2 )
@@ -297,7 +327,7 @@ abstract static class MulNode extends ForeignBinaryNode {
297
327
super (BinaryArithmetic .Mul .create (), false );
298
328
}
299
329
300
- @ Specialization (insertBefore = "doGeneric" , guards = {"lib.hasArrayElements( left)" , "lib.fitsInInt(right)" })
330
+ @ Specialization (insertBefore = "doGeneric" , guards = {"isPythonLikeSequence(lib, left)" , "lib.fitsInInt(right)" })
301
331
static Object doForeignArray (Object left , Object right ,
302
332
@ Cached PRaiseNode raise ,
303
333
@ Cached PythonObjectFactory factory ,
@@ -327,7 +357,7 @@ static Object doForeignArray(Object left, Object right,
327
357
}
328
358
}
329
359
330
- @ Specialization (insertBefore = "doGeneric" , guards = {"lib.hasArrayElements( left)" , "lib.isBoolean(right)" })
360
+ @ Specialization (insertBefore = "doGeneric" , guards = {"isPythonLikeSequence(lib, left)" , "lib.isBoolean(right)" })
331
361
static Object doForeignArrayForeignBoolean (Object left , Object right ,
332
362
@ Cached PRaiseNode raise ,
333
363
@ Cached PythonObjectFactory factory ,
@@ -341,7 +371,7 @@ static Object doForeignArrayForeignBoolean(Object left, Object right,
341
371
}
342
372
343
373
@ SuppressWarnings ("unused" )
344
- @ Specialization (insertBefore = "doGeneric" , guards = {"lib.hasArrayElements( left)" , "isNegativeNumber(lib, right)" })
374
+ @ Specialization (insertBefore = "doGeneric" , guards = {"isPythonLikeSequence(lib, left)" , "isNegativeNumber(lib, right)" })
345
375
static Object doForeignArrayNegativeMult (Object left , Object right ,
346
376
@ Cached PythonObjectFactory factory ,
347
377
@ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
@@ -350,30 +380,12 @@ static Object doForeignArrayNegativeMult(Object left, Object right,
350
380
}
351
381
352
382
@ SuppressWarnings ("unused" )
353
- @ Specialization (insertBefore = "doGeneric" , guards = {"!lib.fitsInDouble(left)" , "!lib.fitsInLong(left)" , "!lib.isBoolean(left)" , "!lib.hasArrayElements( left)" })
383
+ @ Specialization (insertBefore = "doGeneric" , guards = {"!lib.fitsInDouble(left)" , "!lib.fitsInLong(left)" , "!lib.isBoolean(left)" , "!isPythonLikeSequence(lib, left)" })
354
384
PNotImplemented doForeignGeneric (Object left , Object right ,
355
385
@ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
356
386
return PNotImplemented .NOT_IMPLEMENTED ;
357
387
}
358
388
359
- protected static boolean isNegativeNumber (InteropLibrary lib , Object right ) {
360
- long val = 0 ;
361
- try {
362
- if (lib .fitsInByte (right )) {
363
- val = lib .asByte (right );
364
- } else if (lib .fitsInShort (right )) {
365
- val = lib .asShort (right );
366
- } else if (lib .fitsInInt (right )) {
367
- val = lib .asInt (right );
368
- } else if (lib .fitsInLong (right )) {
369
- val = lib .asLong (right );
370
- }
371
- return val < 0 ;
372
- } catch (UnsupportedMessageException e ) {
373
- // fall through
374
- }
375
- return false ;
376
- }
377
389
}
378
390
379
391
@ Builtin (name = __RMUL__ , minNumOfPositionalArgs = 2 )
@@ -383,17 +395,17 @@ abstract static class RMulNode extends ForeignBinaryNode {
383
395
super (BinaryArithmetic .Mul .create (), true );
384
396
}
385
397
386
- @ Specialization (guards = {"lib.hasArrayElements( right)" , "lib.fitsInInt(left)" })
387
- Object doForeignArray (Object left , Object right ,
398
+ @ Specialization (guards = {"isPythonLikeSequence(lib, right)" , "lib.fitsInInt(left)" })
399
+ Object doForeignArray (Object right , Object left ,
388
400
@ Cached PRaiseNode raise ,
389
401
@ Cached PythonObjectFactory factory ,
390
402
@ Cached PForeignToPTypeNode convert ,
391
403
@ CachedLibrary (limit = "1" ) InteropLibrary lib ) {
392
404
return MulNode .doForeignArray (right , left , raise , factory , convert , lib );
393
405
}
394
406
395
- @ Specialization (guards = {"lib.hasArrayElements( right)" , "lib.isBoolean(left)" })
396
- Object doForeignArrayForeignBoolean (Object left , Object right ,
407
+ @ Specialization (guards = {"isPythonLikeSequence(lib, right)" , "lib.isBoolean(left)" })
408
+ Object doForeignArrayForeignBoolean (Object right , Object left ,
397
409
@ Cached PRaiseNode raise ,
398
410
@ Cached PythonObjectFactory factory ,
399
411
@ Cached PForeignToPTypeNode convert ,
@@ -406,8 +418,8 @@ Object doForeignArrayForeignBoolean(Object left, Object right,
406
418
}
407
419
408
420
@ SuppressWarnings ("unused" )
409
- @ Specialization (insertBefore = "doGeneric" , guards = {"!lib.fitsInDouble(right)" , "!lib.fitsInLong(right)" , "!lib.isBoolean(right)" , "!lib.hasArrayElements( right)" })
410
- PNotImplemented doForeignGneeric (Object left , Object right ,
421
+ @ Specialization (insertBefore = "doGeneric" , guards = {"!lib.fitsInDouble(right)" , "!lib.fitsInLong(right)" , "!lib.isBoolean(right)" , "!isPythonLikeSequence(lib, right)" })
422
+ PNotImplemented doForeignGneeric (Object right , Object left ,
411
423
@ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
412
424
return PNotImplemented .NOT_IMPLEMENTED ;
413
425
}
0 commit comments