5656import com .oracle .graal .python .builtins .objects .cext .CExtNodesFactory .ObjectUpcallNodeGen ;
5757import com .oracle .graal .python .builtins .objects .cext .CExtNodesFactory .ToJavaNodeGen ;
5858import com .oracle .graal .python .builtins .objects .cext .CExtNodesFactory .ToSulongNodeGen ;
59+ import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .BoolNativeWrapper ;
5960import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .ByteNativeWrapper ;
6061import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .DoubleNativeWrapper ;
6162import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .IntNativeWrapper ;
@@ -222,6 +223,10 @@ protected static boolean isNativeNull(Object object) {
222223 return object instanceof PythonNativeNull ;
223224 }
224225
226+ protected static boolean isMaterialized (PrimitiveNativeWrapper wrapper ) {
227+ return wrapper .getMaterializedObject () != null ;
228+ }
229+
225230 protected TruffleObject importCAPISymbol (String name ) {
226231 TruffleObject capiLibrary = (TruffleObject ) getContext ().getCapiLibrary ();
227232 if (readSymbolNode == null ) {
@@ -254,9 +259,8 @@ Object doString(String str,
254259 }
255260
256261 @ 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 );
260264 }
261265
262266 @ Specialization
@@ -270,9 +274,8 @@ Object doLong(long l) {
270274 }
271275
272276 @ 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 );
276279 }
277280
278281 @ Specialization
@@ -347,6 +350,11 @@ public abstract static class AsPythonObjectNode extends CExtBaseNode {
347350
348351 @ Child GetClassNode getClassNode ;
349352
353+ @ Specialization (guards = "!isMaterialized(object)" )
354+ boolean doBoolNativeWrapper (BoolNativeWrapper object ) {
355+ return object .getValue ();
356+ }
357+
350358 @ Specialization (guards = "!isMaterialized(object)" )
351359 byte doByteNativeWrapper (ByteNativeWrapper object ) {
352360 return object .getValue ();
@@ -436,10 +444,6 @@ protected boolean isForeignObject(TruffleObject obj) {
436444 return getClassNode .execute (obj ) == getCore ().lookupType (PythonBuiltinClassType .TruffleObject );
437445 }
438446
439- protected static boolean isMaterialized (PrimitiveNativeWrapper wrapper ) {
440- return wrapper .getMaterializedObject () != null ;
441- }
442-
443447 @ TruffleBoundary
444448 public static Object doSlowPath (PythonCore core , Object object ) {
445449 if (object instanceof PythonNativeWrapper ) {
@@ -464,28 +468,39 @@ public abstract static class MaterializeDelegateNode extends CExtBaseNode {
464468
465469 @ Child GetClassNode getClassNode ;
466470
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)" )
468480 PInt doByteNativeWrapper (ByteNativeWrapper object ) {
469481 PInt materializedInt = factory ().createInt (object .getValue ());
470482 object .setMaterializedObject (materializedInt );
483+ materializedInt .setNativeWrapper (object );
471484 return materializedInt ;
472485 }
473486
474- @ Specialization
487+ @ Specialization ( guards = "!isMaterialized(object)" )
475488 PInt doIntNativeWrapper (IntNativeWrapper object ) {
476489 PInt materializedInt = factory ().createInt (object .getValue ());
477490 object .setMaterializedObject (materializedInt );
491+ materializedInt .setNativeWrapper (object );
478492 return materializedInt ;
479493 }
480494
481- @ Specialization
495+ @ Specialization ( guards = "!isMaterialized(object)" )
482496 PInt doLongNativeWrapper (LongNativeWrapper object ) {
483497 PInt materializedInt = factory ().createInt (object .getValue ());
484498 object .setMaterializedObject (materializedInt );
499+ materializedInt .setNativeWrapper (object );
485500 return materializedInt ;
486501 }
487502
488- @ Specialization
503+ @ Specialization ( guards = "!isMaterialized(object)" )
489504 PFloat doDoubleNativeWrapper (DoubleNativeWrapper object ) {
490505 PFloat materializedInt = factory ().createFloat (object .getValue ());
491506 object .setMaterializedObject (materializedInt );
@@ -512,6 +527,12 @@ public static MaterializeDelegateNode create() {
512527 }
513528 }
514529
530+ public abstract static class GetNativeWrapper extends CExtBaseNode {
531+
532+ public abstract PythonNativeWrapper execute (Object obj );
533+
534+ }
535+
515536 /**
516537 * Does the same conversion as the native function {@code to_java}. The node tries to avoid
517538 * calling the native function for resolving native handles.
@@ -973,6 +994,31 @@ public static AsDouble create() {
973994 return value .getValue ();
974995 }
975996
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+
9761022 // TODO: this should just use the builtin constructor node so we don't duplicate the corner
9771023 // cases
9781024 @ Fallback
@@ -1036,6 +1082,16 @@ long run(PFloat value) {
10361082 return (long ) value .getValue ();
10371083 }
10381084
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+
10391095 @ Specialization
10401096 long doIntNativeWrapper (IntNativeWrapper object ) {
10411097 return object .getValue ();
0 commit comments