40
40
*/
41
41
package com .oracle .graal .python .builtins .objects .cext ;
42
42
43
+ import com .oracle .graal .python .builtins .objects .cext .CArrayWrapperMRFactory .GetTypeIDNodeGen ;
43
44
import com .oracle .graal .python .builtins .objects .cext .CArrayWrappers .CArrayWrapper ;
44
45
import com .oracle .graal .python .builtins .objects .cext .CArrayWrappers .CByteArrayWrapper ;
45
46
import com .oracle .graal .python .builtins .objects .cext .CArrayWrappers .CStringWrapper ;
47
+ import com .oracle .graal .python .builtins .objects .cext .CExtNodes .CExtBaseNode ;
48
+ import com .oracle .graal .python .builtins .objects .ints .PInt ;
49
+ import com .oracle .graal .python .nodes .SpecialMethodNames ;
46
50
import com .oracle .truffle .api .CompilerDirectives ;
51
+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
52
+ import com .oracle .truffle .api .dsl .ImportStatic ;
53
+ import com .oracle .truffle .api .dsl .Specialization ;
47
54
import com .oracle .truffle .api .interop .ForeignAccess ;
48
55
import com .oracle .truffle .api .interop .Message ;
49
56
import com .oracle .truffle .api .interop .MessageResolution ;
@@ -58,19 +65,24 @@ public class CArrayWrapperMR {
58
65
59
66
@ Resolve (message = "READ" )
60
67
abstract static class ReadNode extends Node {
61
-
62
68
public char access (CStringWrapper object , int idx ) {
63
69
String s = object .getDelegate ();
64
70
if (idx >= 0 && idx < s .length ()) {
65
71
return s .charAt (idx );
66
72
} else if (idx == s .length ()) {
67
73
return '\0' ;
68
74
}
75
+ CompilerDirectives .transferToInterpreter ();
69
76
throw UnknownIdentifierException .raise (Integer .toString (idx ));
70
77
}
71
78
72
- public byte access (CByteArrayWrapper object , long idx ) {
73
- return access (object , (int ) idx );
79
+ public char access (CStringWrapper object , long idx ) {
80
+ try {
81
+ return access (object , PInt .intValueExact (idx ));
82
+ } catch (ArithmeticException e ) {
83
+ CompilerDirectives .transferToInterpreter ();
84
+ throw UnknownIdentifierException .raise (Long .toString (idx ));
85
+ }
74
86
}
75
87
76
88
public byte access (CByteArrayWrapper object , int idx ) {
@@ -80,8 +92,51 @@ public byte access(CByteArrayWrapper object, int idx) {
80
92
} else if (idx == arr .length ) {
81
93
return (byte ) 0 ;
82
94
}
95
+ CompilerDirectives .transferToInterpreter ();
83
96
throw UnknownIdentifierException .raise (Integer .toString (idx ));
84
97
}
98
+
99
+ public byte access (CByteArrayWrapper object , long idx ) {
100
+ try {
101
+ return access (object , PInt .intValueExact (idx ));
102
+ } catch (ArithmeticException e ) {
103
+ CompilerDirectives .transferToInterpreter ();
104
+ throw UnknownIdentifierException .raise (Long .toString (idx ));
105
+ }
106
+ }
107
+ }
108
+
109
+ @ SuppressWarnings ("unknown-message" )
110
+ @ Resolve (message = "com.oracle.truffle.llvm.spi.GetDynamicType" )
111
+ abstract static class GetDynamicTypeNode extends Node {
112
+ @ Child private GetTypeIDNode getTypeId = GetTypeIDNodeGen .create ();
113
+
114
+ public Object access (CStringWrapper object ) {
115
+ return getTypeId .execute (object );
116
+ }
117
+ }
118
+
119
+ @ ImportStatic (SpecialMethodNames .class )
120
+ abstract static class GetTypeIDNode extends CExtBaseNode {
121
+
122
+ @ Child private PCallNativeNode callUnaryNode = PCallNativeNode .create ();
123
+
124
+ @ CompilationFinal private TruffleObject funGetByteArrayTypeID ;
125
+
126
+ public abstract Object execute (Object delegate );
127
+
128
+ @ Specialization
129
+ Object doTuple (CStringWrapper object ) {
130
+ return callGetByteArrayTypeID (object .getDelegate ().length ());
131
+ }
132
+
133
+ private Object callGetByteArrayTypeID (long len ) {
134
+ if (funGetByteArrayTypeID == null ) {
135
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
136
+ funGetByteArrayTypeID = importCAPISymbol (NativeCAPISymbols .FUN_GET_BYTE_ARRAY_TYPE_ID );
137
+ }
138
+ return callUnaryNode .execute (funGetByteArrayTypeID , new Object []{len });
139
+ }
85
140
}
86
141
87
142
@ Resolve (message = "HAS_SIZE" )
0 commit comments