@@ -1030,7 +1030,6 @@ protected static long doIt(Object object,
1030
1030
abstract static class StrNode extends PythonUnaryBuiltinNode {
1031
1031
@ Child private LookupAndCallUnaryNode callStrNode ;
1032
1032
@ Child private CastToListNode castToListNode ;
1033
- @ Child private ObjectNodes .DefaultObjectReprNode defaultReprNode ;
1034
1033
1035
1034
@ Specialization
1036
1035
Object str (VirtualFrame frame , Object object ,
@@ -1042,7 +1041,7 @@ Object str(VirtualFrame frame, Object object,
1042
1041
@ Cached BranchProfile isLong ,
1043
1042
@ Cached BranchProfile isDouble ,
1044
1043
@ Cached BranchProfile isArray ,
1045
- @ Cached BranchProfile isHostObject ) {
1044
+ @ Cached BranchProfile defaultCase ) {
1046
1045
try {
1047
1046
if (lib .isNull (object )) {
1048
1047
isNull .enter ();
@@ -1100,31 +1099,12 @@ Object str(VirtualFrame frame, Object object,
1100
1099
PForeignArrayIterator iterable = factory ().createForeignArrayIterator (object );
1101
1100
return getCallStrNode ().executeObject (frame , getCastToListNode ().execute (frame , iterable ));
1102
1101
}
1103
- } else if (getContext ().getEnv ().isHostObject (object )) {
1104
- isHostObject .enter ();
1105
- boolean isMetaObject = lib .isMetaObject (object );
1106
- Object metaObject = isMetaObject
1107
- ? object
1108
- : lib .hasMetaObject (object ) ? lib .getMetaObject (object ) : null ;
1109
- if (metaObject != null ) {
1110
- Object displayName = lib .toDisplayString (metaObject );
1111
- String text = createDisplayName (isMetaObject , displayName );
1112
- return PythonUtils .format ("<%s at 0x%x>" , text , PythonAbstractObject .systemHashCode (object ));
1113
- }
1114
1102
}
1115
1103
} catch (UnsupportedMessageException e ) {
1116
1104
// Fall back to the generic impl
1117
1105
}
1118
- return defaultRepr (frame , object );
1119
- }
1120
-
1121
- @ TruffleBoundary
1122
- private static String createDisplayName (boolean isMetaObject , Object object ) {
1123
- StringBuilder sb = new StringBuilder ();
1124
- sb .append (isMetaObject ? "JavaClass[" : "JavaObject[" );
1125
- sb .append (object .toString ());
1126
- sb .append ("]" );
1127
- return sb .toString ();
1106
+ defaultCase .enter ();
1107
+ return defaultConversion (frame , lib , object );
1128
1108
}
1129
1109
1130
1110
private LookupAndCallUnaryNode getCallStrNode () {
@@ -1143,19 +1123,50 @@ private CastToListNode getCastToListNode() {
1143
1123
return castToListNode ;
1144
1124
}
1145
1125
1146
- protected String defaultRepr (VirtualFrame frame , Object object ) {
1147
- if (defaultReprNode == null ) {
1148
- CompilerDirectives .transferToInterpreterAndInvalidate ();
1149
- defaultReprNode = insert (ObjectNodes .DefaultObjectReprNode .create ());
1126
+ protected String defaultConversion (@ SuppressWarnings ("unused" ) VirtualFrame frame , InteropLibrary lib , Object object ) {
1127
+ try {
1128
+ return lib .asString (lib .toDisplayString (object ));
1129
+ } catch (UnsupportedMessageException e ) {
1130
+ throw CompilerDirectives .shouldNotReachHere ("toDisplayString result not convertible to String" );
1150
1131
}
1151
- return defaultReprNode .execute (frame , object );
1152
1132
}
1153
1133
}
1154
1134
1155
1135
@ Builtin (name = __REPR__ , minNumOfPositionalArgs = 1 )
1156
1136
@ GenerateNodeFactory
1157
1137
abstract static class ReprNode extends StrNode {
1158
- protected final String method = __REPR__ ;
1138
+ @ Child private ObjectNodes .DefaultObjectReprNode defaultReprNode ;
1139
+
1140
+ protected String defaultConversion (VirtualFrame frame , @ SuppressWarnings ("unused" ) InteropLibrary lib , Object object ) {
1141
+ try {
1142
+ if (getContext ().getEnv ().isHostObject (object )) {
1143
+ boolean isMetaObject = lib .isMetaObject (object );
1144
+ Object metaObject = null ;
1145
+ if (isMetaObject ) {
1146
+ metaObject = object ;
1147
+ } else if (lib .hasMetaObject (object )) {
1148
+ metaObject = lib .getMetaObject (object );
1149
+ }
1150
+ if (metaObject != null ) {
1151
+ Object displayName = lib .toDisplayString (metaObject );
1152
+ String text = createDisplayName (isMetaObject , displayName );
1153
+ return PythonUtils .format ("<%s at 0x%x>" , text , PythonAbstractObject .systemHashCode (object ));
1154
+ }
1155
+ }
1156
+ } catch (UnsupportedMessageException e ) {
1157
+ // fallthrough to default
1158
+ }
1159
+ if (defaultReprNode == null ) {
1160
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1161
+ defaultReprNode = insert (ObjectNodes .DefaultObjectReprNode .create ());
1162
+ }
1163
+ return defaultReprNode .execute (frame , object );
1164
+ }
1165
+
1166
+ @ TruffleBoundary
1167
+ private static String createDisplayName (boolean isMetaObject , Object object ) {
1168
+ return (isMetaObject ? "JavaClass[" : "JavaObject[" ) + object + "]" ;
1169
+ }
1159
1170
}
1160
1171
1161
1172
@ Builtin (name = __BASES__ , minNumOfPositionalArgs = 1 , isGetter = true , isSetter = false )
0 commit comments