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 ;
51
53
import com .oracle .truffle .api .dsl .Specialization ;
52
54
import com .oracle .truffle .api .interop .InteropLibrary ;
55
+ import com .oracle .truffle .api .interop .UnsupportedMessageException ;
53
56
import com .oracle .truffle .api .library .CachedLibrary ;
54
57
import com .oracle .truffle .api .library .ExportLibrary ;
55
58
import com .oracle .truffle .api .library .ExportMessage ;
@@ -69,19 +72,7 @@ public PString(Object clazz, Shape instanceShape, CharSequence value) {
69
72
}
70
73
71
74
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
- }
75
+ return StringMaterializeNodeGen .getUncached ().execute (this );
85
76
}
86
77
87
78
public CharSequence getCharSequence () {
@@ -127,7 +118,7 @@ static int string(PString self, @SuppressWarnings("unused") ThreadState state,
127
118
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
128
119
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
129
120
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
130
- return (( String ) self .value ).length ();
121
+ return CompilerDirectives . castExact ( self .value , String . class ).length ();
131
122
}
132
123
133
124
@ Specialization (guards = {
@@ -138,7 +129,7 @@ static int lazyString(PString self, @SuppressWarnings("unused") ThreadState stat
138
129
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
139
130
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
140
131
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
141
- return (( LazyString ) self .value ).length ();
132
+ return CompilerDirectives . castExact ( self .value , LazyString . class ).length ();
142
133
}
143
134
144
135
@ Specialization (guards = {
@@ -149,21 +140,21 @@ static int nativeString(PString self, @SuppressWarnings("unused") ThreadState st
149
140
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
150
141
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
151
142
@ SuppressWarnings ("unused" ) @ Shared ("lookupString" ) @ Cached LookupAttributeInMRONode .Dynamic lookupString ) {
152
- return (( NativeCharSequence ) self .value ).length ();
143
+ return CompilerDirectives . castExact ( self .value , NativeCharSequence . class ). getMaterialized ( ).length ();
153
144
}
154
145
155
146
@ Specialization (guards = {
156
147
"isNativeString(self.getCharSequence())" , "!isMaterialized(self.getCharSequence())" ,
157
148
"isBuiltin(self, profile) || hasBuiltinLen(self, lookupSelf, lookupString)"
158
149
}, replaces = "nativeString" , limit = "1" )
159
- static int nativeStringMat (PString self , @ SuppressWarnings ("unused" ) ThreadState state ,
150
+ static int nativeStringMat (@ SuppressWarnings ("unused" ) PString self , @ SuppressWarnings ("unused" ) ThreadState state ,
151
+ @ Bind ("getNativeCharSequence(self)" ) NativeCharSequence nativeCharSequence ,
160
152
@ SuppressWarnings ("unused" ) @ Shared ("builtinProfile" ) @ Cached IsBuiltinClassProfile profile ,
161
153
@ SuppressWarnings ("unused" ) @ Shared ("lookupSelf" ) @ Cached LookupInheritedAttributeNode .Dynamic lookupSelf ,
162
154
@ 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 ();
155
+ @ CachedLibrary ("nativeCharSequence" ) InteropLibrary lib ,
156
+ @ Cached CastToJavaIntExactNode castToJavaIntNode ) {
157
+ return nativeCharSequence .length (lib , castToJavaIntNode );
167
158
}
168
159
169
160
@ Specialization (replaces = {"string" , "lazyString" , "nativeString" , "nativeStringMat" })
@@ -178,6 +169,10 @@ static int subclassedString(PString self, ThreadState state,
178
169
// call the generic implementation in the superclass
179
170
return self .lengthWithState (state , plib , methodLib , gotState , hasLen , ltZero , raiseNode , lib );
180
171
}
172
+
173
+ static NativeCharSequence getNativeCharSequence (PString self ) {
174
+ return (NativeCharSequence ) self .value ;
175
+ }
181
176
}
182
177
183
178
@ ExportMessage
0 commit comments