56
56
import com .oracle .graal .python .builtins .objects .cext .CExtNodesFactory .ObjectUpcallNodeGen ;
57
57
import com .oracle .graal .python .builtins .objects .cext .CExtNodesFactory .ToJavaNodeGen ;
58
58
import com .oracle .graal .python .builtins .objects .cext .CExtNodesFactory .ToSulongNodeGen ;
59
+ import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .BoolNativeWrapper ;
59
60
import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .ByteNativeWrapper ;
60
61
import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .DoubleNativeWrapper ;
61
62
import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .IntNativeWrapper ;
@@ -222,6 +223,10 @@ protected static boolean isNativeNull(Object object) {
222
223
return object instanceof PythonNativeNull ;
223
224
}
224
225
226
+ protected static boolean isMaterialized (PrimitiveNativeWrapper wrapper ) {
227
+ return wrapper .getMaterializedObject () != null ;
228
+ }
229
+
225
230
protected TruffleObject importCAPISymbol (String name ) {
226
231
TruffleObject capiLibrary = (TruffleObject ) getContext ().getCapiLibrary ();
227
232
if (readSymbolNode == null ) {
@@ -254,9 +259,8 @@ Object doString(String str,
254
259
}
255
260
256
261
@ Specialization
257
- Object doBoolean (boolean b ,
258
- @ Cached ("createBinaryProfile()" ) ConditionProfile noWrapperProfile ) {
259
- return PythonObjectNativeWrapper .wrap (factory ().createInt (b ), noWrapperProfile );
262
+ Object doBoolean (boolean b ) {
263
+ return BoolNativeWrapper .create (b );
260
264
}
261
265
262
266
@ Specialization
@@ -270,9 +274,8 @@ Object doLong(long l) {
270
274
}
271
275
272
276
@ Specialization
273
- Object doDouble (double d ,
274
- @ Cached ("createBinaryProfile()" ) ConditionProfile noWrapperProfile ) {
275
- return PythonObjectNativeWrapper .wrap (factory ().createFloat (d ), noWrapperProfile );
277
+ Object doDouble (double d ) {
278
+ return DoubleNativeWrapper .create (d );
276
279
}
277
280
278
281
@ Specialization
@@ -347,6 +350,11 @@ public abstract static class AsPythonObjectNode extends CExtBaseNode {
347
350
348
351
@ Child GetClassNode getClassNode ;
349
352
353
+ @ Specialization (guards = "!isMaterialized(object)" )
354
+ boolean doBoolNativeWrapper (BoolNativeWrapper object ) {
355
+ return object .getValue ();
356
+ }
357
+
350
358
@ Specialization (guards = "!isMaterialized(object)" )
351
359
byte doByteNativeWrapper (ByteNativeWrapper object ) {
352
360
return object .getValue ();
@@ -436,10 +444,6 @@ protected boolean isForeignObject(TruffleObject obj) {
436
444
return getClassNode .execute (obj ) == getCore ().lookupType (PythonBuiltinClassType .TruffleObject );
437
445
}
438
446
439
- protected static boolean isMaterialized (PrimitiveNativeWrapper wrapper ) {
440
- return wrapper .getMaterializedObject () != null ;
441
- }
442
-
443
447
@ TruffleBoundary
444
448
public static Object doSlowPath (PythonCore core , Object object ) {
445
449
if (object instanceof PythonNativeWrapper ) {
@@ -464,28 +468,39 @@ public abstract static class MaterializeDelegateNode extends CExtBaseNode {
464
468
465
469
@ Child GetClassNode getClassNode ;
466
470
467
- @ Specialization
471
+ @ Specialization (guards = "!isMaterialized(object)" )
472
+ PInt doBoolNativeWrapper (BoolNativeWrapper object ) {
473
+ PInt materializedInt = factory ().createInt (object .getValue ());
474
+ object .setMaterializedObject (materializedInt );
475
+ materializedInt .setNativeWrapper (object );
476
+ return materializedInt ;
477
+ }
478
+
479
+ @ Specialization (guards = "!isMaterialized(object)" )
468
480
PInt doByteNativeWrapper (ByteNativeWrapper object ) {
469
481
PInt materializedInt = factory ().createInt (object .getValue ());
470
482
object .setMaterializedObject (materializedInt );
483
+ materializedInt .setNativeWrapper (object );
471
484
return materializedInt ;
472
485
}
473
486
474
- @ Specialization
487
+ @ Specialization ( guards = "!isMaterialized(object)" )
475
488
PInt doIntNativeWrapper (IntNativeWrapper object ) {
476
489
PInt materializedInt = factory ().createInt (object .getValue ());
477
490
object .setMaterializedObject (materializedInt );
491
+ materializedInt .setNativeWrapper (object );
478
492
return materializedInt ;
479
493
}
480
494
481
- @ Specialization
495
+ @ Specialization ( guards = "!isMaterialized(object)" )
482
496
PInt doLongNativeWrapper (LongNativeWrapper object ) {
483
497
PInt materializedInt = factory ().createInt (object .getValue ());
484
498
object .setMaterializedObject (materializedInt );
499
+ materializedInt .setNativeWrapper (object );
485
500
return materializedInt ;
486
501
}
487
502
488
- @ Specialization
503
+ @ Specialization ( guards = "!isMaterialized(object)" )
489
504
PFloat doDoubleNativeWrapper (DoubleNativeWrapper object ) {
490
505
PFloat materializedInt = factory ().createFloat (object .getValue ());
491
506
object .setMaterializedObject (materializedInt );
@@ -512,6 +527,12 @@ public static MaterializeDelegateNode create() {
512
527
}
513
528
}
514
529
530
+ public abstract static class GetNativeWrapper extends CExtBaseNode {
531
+
532
+ public abstract PythonNativeWrapper execute (Object obj );
533
+
534
+ }
535
+
515
536
/**
516
537
* Does the same conversion as the native function {@code to_java}. The node tries to avoid
517
538
* calling the native function for resolving native handles.
@@ -973,6 +994,31 @@ public static AsDouble create() {
973
994
return value .getValue ();
974
995
}
975
996
997
+ @ Specialization
998
+ double doBoolNativeWrapper (BoolNativeWrapper object ) {
999
+ return PInt .intValue (object .getValue ());
1000
+ }
1001
+
1002
+ @ Specialization
1003
+ double doByteNativeWrapper (ByteNativeWrapper object ) {
1004
+ return object .getValue ();
1005
+ }
1006
+
1007
+ @ Specialization
1008
+ double doIntNativeWrapper (IntNativeWrapper object ) {
1009
+ return object .getValue ();
1010
+ }
1011
+
1012
+ @ Specialization
1013
+ double doLongNativeWrapper (LongNativeWrapper object ) {
1014
+ return object .getValue ();
1015
+ }
1016
+
1017
+ @ Specialization
1018
+ double doDoubleNativeWrapper (DoubleNativeWrapper object ) {
1019
+ return object .getValue ();
1020
+ }
1021
+
976
1022
// TODO: this should just use the builtin constructor node so we don't duplicate the corner
977
1023
// cases
978
1024
@ Fallback
@@ -1036,6 +1082,16 @@ long run(PFloat value) {
1036
1082
return (long ) value .getValue ();
1037
1083
}
1038
1084
1085
+ @ Specialization
1086
+ long doBoolNativeWrapper (BoolNativeWrapper object ) {
1087
+ return PInt .intValue (object .getValue ());
1088
+ }
1089
+
1090
+ @ Specialization
1091
+ long doByteNativeWrapper (ByteNativeWrapper object ) {
1092
+ return object .getValue ();
1093
+ }
1094
+
1039
1095
@ Specialization
1040
1096
long doIntNativeWrapper (IntNativeWrapper object ) {
1041
1097
return object .getValue ();
0 commit comments