29
29
30
30
import com .oracle .graal .python .PythonLanguage ;
31
31
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
32
- import com .oracle .graal .python .builtins .objects .cext .CExtNodes .PCallCapiFunction ;
33
32
import com .oracle .graal .python .builtins .objects .cext .PythonNativeWrapperLibrary ;
34
33
import com .oracle .graal .python .builtins .objects .function .PArguments .ThreadState ;
35
34
import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
36
35
import com .oracle .graal .python .builtins .objects .str .StringNodes .StringMaterializeNode ;
36
+ import com .oracle .graal .python .builtins .objects .str .StringNodesFactory .StringMaterializeNodeGen ;
37
37
import com .oracle .graal .python .nodes .ErrorMessages ;
38
38
import com .oracle .graal .python .nodes .PRaiseNode ;
39
39
import com .oracle .graal .python .nodes .attributes .LookupAttributeInMRONode ;
40
40
import com .oracle .graal .python .nodes .attributes .LookupInheritedAttributeNode ;
41
41
import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
42
42
import com .oracle .graal .python .nodes .util .CannotCastException ;
43
+ import com .oracle .graal .python .nodes .util .CastToJavaIntExactNode ;
43
44
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
44
45
import com .oracle .graal .python .runtime .sequence .PSequence ;
45
46
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
46
47
import com .oracle .truffle .api .CompilerDirectives ;
47
48
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
49
+ import com .oracle .truffle .api .dsl .Bind ;
48
50
import com .oracle .truffle .api .dsl .Cached ;
49
51
import com .oracle .truffle .api .dsl .Cached .Exclusive ;
50
52
import com .oracle .truffle .api .dsl .Cached .Shared ;
@@ -69,19 +71,7 @@ public PString(Object clazz, Shape instanceShape, CharSequence value) {
69
71
}
70
72
71
73
public String getValue () {
72
- return PString .getValue (value );
73
- }
74
-
75
- public static String getValue (CharSequence charSequence ) {
76
- if (charSequence instanceof LazyString ) {
77
- LazyString s = (LazyString ) charSequence ;
78
- return s .materialize ();
79
- } else if (charSequence instanceof NativeCharSequence ) {
80
- NativeCharSequence s = (NativeCharSequence ) charSequence ;
81
- return s .materialize ();
82
- } else {
83
- return (String ) charSequence ;
84
- }
74
+ return StringMaterializeNodeGen .getUncached ().execute (this );
85
75
}
86
76
87
77
public CharSequence getCharSequence () {
@@ -127,7 +117,7 @@ static int string(PString self, @SuppressWarnings("unused") ThreadState state,
127
117
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
128
118
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
129
119
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
130
- return (( String ) self .value ).length ();
120
+ return CompilerDirectives . castExact ( self .value , String . class ).length ();
131
121
}
132
122
133
123
@ Specialization (guards = {
@@ -138,7 +128,7 @@ static int lazyString(PString self, @SuppressWarnings("unused") ThreadState stat
138
128
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
139
129
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
140
130
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
141
- return (( LazyString ) self .value ).length ();
131
+ return CompilerDirectives . castExact ( self .value , LazyString . class ).length ();
142
132
}
143
133
144
134
@ Specialization (guards = {
@@ -149,21 +139,21 @@ static int nativeString(PString self, @SuppressWarnings("unused") ThreadState st
149
139
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
150
140
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
151
141
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
152
- return (( NativeCharSequence ) self .value ).length ();
142
+ return CompilerDirectives . castExact ( self .value , NativeCharSequence . class ). getMaterialized ( ).length ();
153
143
}
154
144
155
145
@ Specialization (guards = {
156
146
"isNativeString(self.getCharSequence())" , "!isMaterialized(self.getCharSequence())" ,
157
147
"isBuiltin(self, profile) || hasBuiltinLen(self, lookupSelf, lookupString)"
158
148
}, replaces = "nativeString" , limit = "1" )
159
- static int nativeStringMat (PString self , @ SuppressWarnings ("unused" ) ThreadState state ,
149
+ static int nativeStringMat (@ SuppressWarnings ("unused" ) PString self , @ SuppressWarnings ("unused" ) ThreadState state ,
150
+ @ Bind ("getNativeCharSequence(self)" ) NativeCharSequence nativeCharSequence ,
160
151
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
161
152
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
162
153
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ,
163
- @ Cached PCallCapiFunction callCapi ) {
164
- NativeCharSequence ncs = (NativeCharSequence ) self .value ;
165
- ncs .materialize (callCapi );
166
- return ncs .length ();
154
+ @ CachedLibrary ("nativeCharSequence" ) InteropLibrary lib ,
155
+ @ Cached CastToJavaIntExactNode castToJavaIntNode ) {
156
+ return nativeCharSequence .length (lib , castToJavaIntNode );
167
157
}
168
158
169
159
@ Specialization (replaces = {"string" , "lazyString" , "nativeString" , "nativeStringMat" })
@@ -178,6 +168,10 @@ static int subclassedString(PString self, ThreadState state,
178
168
// call the generic implementation in the superclass
179
169
return self .lengthWithState (state , plib , methodLib , gotState , hasLen , ltZero , raiseNode , lib );
180
170
}
171
+
172
+ static NativeCharSequence getNativeCharSequence (PString self ) {
173
+ return (NativeCharSequence ) self .value ;
174
+ }
181
175
}
182
176
183
177
@ ExportMessage
0 commit comments