1
1
/*
2
- * Copyright (c) 2020, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2020, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
package com .oracle .truffle .espresso .runtime .dispatch .staticobject ;
25
25
26
- import com .oracle .truffle .api .CompilerDirectives ;
27
26
import com .oracle .truffle .api .dsl .Bind ;
28
27
import com .oracle .truffle .api .dsl .Cached ;
29
28
import com .oracle .truffle .api .dsl .Cached .Shared ;
30
29
import com .oracle .truffle .api .dsl .GenerateUncached ;
31
30
import com .oracle .truffle .api .dsl .Specialization ;
32
- import com .oracle .truffle .api .interop .ArityException ;
33
31
import com .oracle .truffle .api .interop .InteropLibrary ;
34
32
import com .oracle .truffle .api .interop .InvalidArrayIndexException ;
35
33
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
36
- import com .oracle .truffle .api .interop .UnsupportedTypeException ;
37
34
import com .oracle .truffle .api .library .ExportLibrary ;
38
35
import com .oracle .truffle .api .library .ExportMessage ;
39
36
import com .oracle .truffle .api .profiles .BranchProfile ;
40
37
import com .oracle .truffle .espresso .impl .Method ;
41
- import com .oracle .truffle .espresso .meta .EspressoError ;
42
38
import com .oracle .truffle .espresso .nodes .EspressoNode ;
43
39
import com .oracle .truffle .espresso .nodes .interop .LookupAndInvokeKnownMethodNode ;
44
40
import com .oracle .truffle .espresso .runtime .EspressoException ;
45
41
import com .oracle .truffle .espresso .runtime .InteropUtils ;
46
- import com .oracle .truffle .espresso .runtime .staticobject .StaticObject ;
47
42
import com .oracle .truffle .espresso .runtime .dispatch .messages .GenerateInteropNodes ;
48
43
import com .oracle .truffle .espresso .runtime .dispatch .messages .Shareable ;
44
+ import com .oracle .truffle .espresso .runtime .staticobject .StaticObject ;
49
45
import com .oracle .truffle .espresso .vm .InterpreterToVM ;
50
46
51
47
@ GenerateInteropNodes
@@ -100,6 +96,20 @@ static void writeArrayElement(StaticObject receiver, long index, Object value,
100
96
listSet .listSet (receiver , index , value , error );
101
97
}
102
98
99
+ @ ExportMessage
100
+ static void removeArrayElement (StaticObject receiver , long index ,
101
+ @ Cached ListRemove listRemove ,
102
+ @ Bind ("getMeta().java_util_List_size" ) Method listSizeMethod ,
103
+ @ Cached .Shared ("size" ) @ Cached LookupAndInvokeKnownMethodNode size ,
104
+ @ Cached @ Shared ("error" ) BranchProfile error ) throws UnsupportedMessageException , InvalidArrayIndexException {
105
+ int listSize = (int ) size .execute (receiver , listSizeMethod );
106
+ if (!boundsCheck (index , listSize )) {
107
+ error .enter ();
108
+ throw InvalidArrayIndexException .create (index );
109
+ }
110
+ listRemove .listRemove (receiver , index , error );
111
+ }
112
+
103
113
@ ExportMessage
104
114
static boolean isArrayElementReadable (StaticObject receiver , long index ,
105
115
@ Bind ("getMeta().java_util_List_size" ) Method listSizeMethod ,
@@ -115,14 +125,20 @@ static boolean isArrayElementModifiable(StaticObject receiver, long index,
115
125
}
116
126
117
127
@ ExportMessage
118
- static boolean isArrayElementInsertable (@ SuppressWarnings ("unused" ) StaticObject receiver , @ SuppressWarnings ( "unused" ) long index ) {
119
- // we can't easily determine is the guest list is modifiable or not ,
120
- // so we return true here and let writeArrayElement fail in case the
121
- // associated guest method throws
122
- return true ;
128
+ static boolean isArrayElementInsertable (@ SuppressWarnings ("unused" ) StaticObject receiver , long index ,
129
+ @ Bind ( "getMeta().java_util_List_size" ) Method listSizeMethod ,
130
+ @ Cached . Shared ( "size" ) @ Cached LookupAndInvokeKnownMethodNode size ) {
131
+ int listSize = ( int ) size . execute ( receiver , listSizeMethod );
132
+ return boundsCheck ( index , listSize + 1 ) ;
123
133
}
124
134
125
- // TODO(GR-38619): isArrayElementRemovable
135
+ @ ExportMessage
136
+ static boolean isArrayElementRemovable (@ SuppressWarnings ("unused" ) StaticObject receiver , @ SuppressWarnings ("unused" ) long index ,
137
+ @ Bind ("getMeta().java_util_List_size" ) Method listSizeMethod ,
138
+ @ Cached .Shared ("size" ) @ Cached LookupAndInvokeKnownMethodNode size ) {
139
+ int listSize = (int ) size .execute (receiver , listSizeMethod );
140
+ return boundsCheck (index , listSize );
141
+ }
126
142
127
143
private static boolean boundsCheck (StaticObject receiver , long index , Method listSize ,
128
144
LookupAndInvokeKnownMethodNode size ) {
@@ -135,7 +151,6 @@ private static boolean boundsCheck(long index, int size) {
135
151
136
152
@ GenerateUncached
137
153
abstract static class ListGet extends EspressoNode {
138
- static final int LIMIT = 3 ;
139
154
140
155
public Object listGet (StaticObject receiver , long index , BranchProfile error ) throws InvalidArrayIndexException {
141
156
try {
@@ -161,7 +176,6 @@ static Object doCached(StaticObject receiver, int index,
161
176
162
177
@ GenerateUncached
163
178
abstract static class ListSet extends EspressoNode {
164
- static final int LIMIT = 3 ;
165
179
166
180
public void listSet (StaticObject receiver , long index , Object value , BranchProfile error ) throws InvalidArrayIndexException {
167
181
try {
@@ -172,13 +186,10 @@ public void listSet(StaticObject receiver, long index, Object value, BranchProfi
172
186
throw InvalidArrayIndexException .create (index );
173
187
}
174
188
throw e ; // unexpected exception
175
- } catch (ArityException | UnsupportedTypeException e ) {
176
- CompilerDirectives .transferToInterpreterAndInvalidate ();
177
- throw EspressoError .shouldNotReachHere ();
178
189
}
179
190
}
180
191
181
- protected abstract void execute (StaticObject receiver , int index , Object value ) throws ArityException , UnsupportedTypeException ;
192
+ protected abstract void execute (StaticObject receiver , int index , Object value );
182
193
183
194
@ Specialization
184
195
static void doCached (StaticObject receiver , int index , Object value ,
@@ -190,7 +201,6 @@ static void doCached(StaticObject receiver, int index, Object value,
190
201
191
202
@ GenerateUncached
192
203
abstract static class ListAdd extends EspressoNode {
193
- static final int LIMIT = 3 ;
194
204
195
205
public void listAdd (StaticObject receiver , Object value , BranchProfile error ) throws UnsupportedMessageException {
196
206
try {
@@ -204,13 +214,10 @@ public void listAdd(StaticObject receiver, Object value, BranchProfile error) th
204
214
throw UnsupportedMessageException .create ();
205
215
}
206
216
throw e ; // unexpected exception
207
- } catch (ArityException | UnsupportedTypeException e ) {
208
- CompilerDirectives .transferToInterpreterAndInvalidate ();
209
- throw EspressoError .shouldNotReachHere ();
210
217
}
211
218
}
212
219
213
- protected abstract void execute (StaticObject receiver , Object value ) throws ArityException , UnsupportedTypeException ;
220
+ protected abstract void execute (StaticObject receiver , Object value );
214
221
215
222
@ Specialization
216
223
static void doCached (StaticObject receiver , Object value ,
@@ -219,4 +226,32 @@ static void doCached(StaticObject receiver, Object value,
219
226
lookupAndInvoke .execute (receiver , addMethod , new Object []{value });
220
227
}
221
228
}
229
+
230
+ @ GenerateUncached
231
+ abstract static class ListRemove extends EspressoNode {
232
+
233
+ public void listRemove (StaticObject receiver , long index , BranchProfile error ) throws UnsupportedMessageException , InvalidArrayIndexException {
234
+ try {
235
+ execute (receiver , (int ) index );
236
+ } catch (EspressoException e ) {
237
+ error .enter ();
238
+ if (InterpreterToVM .instanceOf (e .getGuestException (), receiver .getKlass ().getMeta ().java_lang_IndexOutOfBoundsException )) {
239
+ throw InvalidArrayIndexException .create (index );
240
+ }
241
+ if (InterpreterToVM .instanceOf (e .getGuestException (), receiver .getKlass ().getMeta ().java_lang_UnsupportedOperationException )) {
242
+ throw UnsupportedMessageException .create (e );
243
+ }
244
+ throw e ; // unexpected exception
245
+ }
246
+ }
247
+
248
+ protected abstract void execute (StaticObject receiver , int index );
249
+
250
+ @ Specialization
251
+ static void doCached (StaticObject receiver , int index ,
252
+ @ Bind ("getMeta().java_util_List_remove" ) Method removeMethod ,
253
+ @ Cached LookupAndInvokeKnownMethodNode lookupAndInvoke ) {
254
+ lookupAndInvoke .execute (receiver , removeMethod , new Object []{index });
255
+ }
256
+ }
222
257
}
0 commit comments