Skip to content

Commit 80a015b

Browse files
committed
[GR-38619] Espresso List remove interop.
PullRequest: graal/16197
2 parents 58a8bdc + 5723502 commit 80a015b

File tree

2 files changed

+60
-23
lines changed
  • espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso

2 files changed

+60
-23
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/meta/Meta.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ public Meta(EspressoContext context) {
857857
java_util_List_set = java_util_List.requireDeclaredMethod(Name.set, Signature.Object_int_Object);
858858
java_util_List_size = java_util_List.requireDeclaredMethod(Name.size, Signature._int);
859859
java_util_List_add = java_util_List.requireDeclaredMethod(Name.add, Signature._boolean_Object);
860+
java_util_List_remove = java_util_List.requireDeclaredMethod(Name.remove, Signature.Object_int);
860861
assert java_util_List.isInterface();
861862

862863
java_util_Set = knownKlass(Type.java_util_Set);
@@ -1562,6 +1563,7 @@ private DiffVersionLoadHelper diff() {
15621563
public final Method java_util_List_set;
15631564
public final Method java_util_List_size;
15641565
public final Method java_util_List_add;
1566+
public final Method java_util_List_remove;
15651567

15661568
public final ObjectKlass java_util_Set;
15671569
public final Method java_util_Set_add;

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/runtime/dispatch/staticobject/ListInterop.java

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,29 +23,25 @@
2323

2424
package com.oracle.truffle.espresso.runtime.dispatch.staticobject;
2525

26-
import com.oracle.truffle.api.CompilerDirectives;
2726
import com.oracle.truffle.api.dsl.Bind;
2827
import com.oracle.truffle.api.dsl.Cached;
2928
import com.oracle.truffle.api.dsl.Cached.Shared;
3029
import com.oracle.truffle.api.dsl.GenerateUncached;
3130
import com.oracle.truffle.api.dsl.Specialization;
32-
import com.oracle.truffle.api.interop.ArityException;
3331
import com.oracle.truffle.api.interop.InteropLibrary;
3432
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
3533
import com.oracle.truffle.api.interop.UnsupportedMessageException;
36-
import com.oracle.truffle.api.interop.UnsupportedTypeException;
3734
import com.oracle.truffle.api.library.ExportLibrary;
3835
import com.oracle.truffle.api.library.ExportMessage;
3936
import com.oracle.truffle.api.profiles.BranchProfile;
4037
import com.oracle.truffle.espresso.impl.Method;
41-
import com.oracle.truffle.espresso.meta.EspressoError;
4238
import com.oracle.truffle.espresso.nodes.EspressoNode;
4339
import com.oracle.truffle.espresso.nodes.interop.LookupAndInvokeKnownMethodNode;
4440
import com.oracle.truffle.espresso.runtime.EspressoException;
4541
import com.oracle.truffle.espresso.runtime.InteropUtils;
46-
import com.oracle.truffle.espresso.runtime.staticobject.StaticObject;
4742
import com.oracle.truffle.espresso.runtime.dispatch.messages.GenerateInteropNodes;
4843
import com.oracle.truffle.espresso.runtime.dispatch.messages.Shareable;
44+
import com.oracle.truffle.espresso.runtime.staticobject.StaticObject;
4945
import com.oracle.truffle.espresso.vm.InterpreterToVM;
5046

5147
@GenerateInteropNodes
@@ -100,6 +96,20 @@ static void writeArrayElement(StaticObject receiver, long index, Object value,
10096
listSet.listSet(receiver, index, value, error);
10197
}
10298

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+
103113
@ExportMessage
104114
static boolean isArrayElementReadable(StaticObject receiver, long index,
105115
@Bind("getMeta().java_util_List_size") Method listSizeMethod,
@@ -115,14 +125,20 @@ static boolean isArrayElementModifiable(StaticObject receiver, long index,
115125
}
116126

117127
@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);
123133
}
124134

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+
}
126142

127143
private static boolean boundsCheck(StaticObject receiver, long index, Method listSize,
128144
LookupAndInvokeKnownMethodNode size) {
@@ -135,7 +151,6 @@ private static boolean boundsCheck(long index, int size) {
135151

136152
@GenerateUncached
137153
abstract static class ListGet extends EspressoNode {
138-
static final int LIMIT = 3;
139154

140155
public Object listGet(StaticObject receiver, long index, BranchProfile error) throws InvalidArrayIndexException {
141156
try {
@@ -161,7 +176,6 @@ static Object doCached(StaticObject receiver, int index,
161176

162177
@GenerateUncached
163178
abstract static class ListSet extends EspressoNode {
164-
static final int LIMIT = 3;
165179

166180
public void listSet(StaticObject receiver, long index, Object value, BranchProfile error) throws InvalidArrayIndexException {
167181
try {
@@ -172,13 +186,10 @@ public void listSet(StaticObject receiver, long index, Object value, BranchProfi
172186
throw InvalidArrayIndexException.create(index);
173187
}
174188
throw e; // unexpected exception
175-
} catch (ArityException | UnsupportedTypeException e) {
176-
CompilerDirectives.transferToInterpreterAndInvalidate();
177-
throw EspressoError.shouldNotReachHere();
178189
}
179190
}
180191

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);
182193

183194
@Specialization
184195
static void doCached(StaticObject receiver, int index, Object value,
@@ -190,7 +201,6 @@ static void doCached(StaticObject receiver, int index, Object value,
190201

191202
@GenerateUncached
192203
abstract static class ListAdd extends EspressoNode {
193-
static final int LIMIT = 3;
194204

195205
public void listAdd(StaticObject receiver, Object value, BranchProfile error) throws UnsupportedMessageException {
196206
try {
@@ -204,13 +214,10 @@ public void listAdd(StaticObject receiver, Object value, BranchProfile error) th
204214
throw UnsupportedMessageException.create();
205215
}
206216
throw e; // unexpected exception
207-
} catch (ArityException | UnsupportedTypeException e) {
208-
CompilerDirectives.transferToInterpreterAndInvalidate();
209-
throw EspressoError.shouldNotReachHere();
210217
}
211218
}
212219

213-
protected abstract void execute(StaticObject receiver, Object value) throws ArityException, UnsupportedTypeException;
220+
protected abstract void execute(StaticObject receiver, Object value);
214221

215222
@Specialization
216223
static void doCached(StaticObject receiver, Object value,
@@ -219,4 +226,32 @@ static void doCached(StaticObject receiver, Object value,
219226
lookupAndInvoke.execute(receiver, addMethod, new Object[]{value});
220227
}
221228
}
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+
}
222257
}

0 commit comments

Comments
 (0)