Skip to content

Commit 3451dac

Browse files
committed
[GR-44797] Add type mapping feature for common collection types passed into Espresso.
PullRequest: graal/14752
2 parents 4bd466c + f1f32ef commit 3451dac

28 files changed

+2067
-217
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.truffle.espresso.polyglot.collections;
42+
43+
import java.util.AbstractCollection;
44+
import java.util.Collection;
45+
import java.util.Iterator;
46+
47+
import com.oracle.truffle.espresso.polyglot.Interop;
48+
import com.oracle.truffle.espresso.polyglot.InteropException;
49+
import com.oracle.truffle.espresso.polyglot.UnsupportedMessageException;
50+
51+
public class EspressoForeignCollection<T> extends AbstractCollection<T> implements Collection<T> {
52+
53+
@Override
54+
@SuppressWarnings("unchecked")
55+
public Iterator<T> iterator() {
56+
assert Interop.hasIterator(this);
57+
try {
58+
return EspressoForeignIterator.create(Interop.getIterator(this));
59+
} catch (UnsupportedMessageException e) {
60+
return (Iterator<T>) EspressoForeignIterable.EMPTY_ITERATOR;
61+
}
62+
}
63+
64+
@Override
65+
public int size() {
66+
// (GR-47128) If/When iterator size becomes available through interop, switch to use that
67+
Iterator<T> it = iterator();
68+
int count = 0;
69+
while (it.hasNext()) {
70+
it.next();
71+
count++;
72+
}
73+
return count;
74+
}
75+
76+
@Override
77+
public boolean add(T t) {
78+
// This assumes the presence of a member "add". Known to work for host collections.
79+
try {
80+
return Interop.asBoolean(Interop.invokeMember(this, "add", t));
81+
} catch (InteropException e) {
82+
throw new UnsupportedOperationException();
83+
}
84+
}
85+
86+
@Override
87+
public boolean remove(Object o) {
88+
// This assumes the presence of a member "remove". Known to work for host collections.
89+
try {
90+
return Interop.asBoolean(Interop.invokeMember(this, "remove", o));
91+
} catch (InteropException e) {
92+
throw new UnsupportedOperationException();
93+
}
94+
}
95+
96+
@Override
97+
public boolean removeAll(Collection<?> c) {
98+
boolean modified = false;
99+
for (Object obj : c) {
100+
if (remove(obj)) {
101+
modified = true;
102+
}
103+
}
104+
return modified;
105+
}
106+
107+
@Override
108+
public boolean isEmpty() {
109+
return !iterator().hasNext();
110+
}
111+
112+
@Override
113+
public String toString() {
114+
try {
115+
return Interop.asString(Interop.toDisplayString(this));
116+
} catch (UnsupportedMessageException e) {
117+
return super.toString();
118+
}
119+
}
120+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.truffle.espresso.polyglot.collections;
42+
43+
import java.util.Iterator;
44+
45+
import com.oracle.truffle.espresso.polyglot.Interop;
46+
import com.oracle.truffle.espresso.polyglot.UnsupportedMessageException;
47+
48+
public class EspressoForeignIterable<T> implements Iterable<T> {
49+
50+
public static final Iterator<Object> EMPTY_ITERATOR = new Iterator<Object>() {
51+
@Override
52+
public boolean hasNext() {
53+
return false;
54+
}
55+
56+
@Override
57+
public Object next() {
58+
return null;
59+
}
60+
};
61+
62+
@Override
63+
@SuppressWarnings("unchecked")
64+
public Iterator<T> iterator() {
65+
assert Interop.hasIterator(this);
66+
try {
67+
return EspressoForeignIterator.create(Interop.getIterator(this));
68+
} catch (Exception e) {
69+
return (Iterator<T>) EMPTY_ITERATOR;
70+
}
71+
}
72+
73+
@Override
74+
public String toString() {
75+
try {
76+
return Interop.asString(Interop.toDisplayString(this));
77+
} catch (UnsupportedMessageException e) {
78+
return super.toString();
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.truffle.espresso.polyglot.collections;
42+
43+
import java.util.Iterator;
44+
import java.util.NoSuchElementException;
45+
46+
import com.oracle.truffle.espresso.polyglot.Interop;
47+
import com.oracle.truffle.espresso.polyglot.StopIterationException;
48+
import com.oracle.truffle.espresso.polyglot.UnsupportedMessageException;
49+
50+
public class EspressoForeignIterator<E> implements Iterator<E> {
51+
52+
/* VM creates an instance of EspressoForeignIterator */
53+
static native <T> Iterator<T> create(Object foreignIterator);
54+
55+
@Override
56+
public boolean hasNext() {
57+
try {
58+
return Interop.hasIteratorNextElement(this);
59+
} catch (UnsupportedMessageException e) {
60+
return false;
61+
}
62+
}
63+
64+
@Override
65+
@SuppressWarnings("unchecked")
66+
public E next() {
67+
try {
68+
if (!hasNext()) {
69+
throw new NoSuchElementException();
70+
}
71+
return (E) Interop.getIteratorNextElement(this);
72+
} catch (UnsupportedMessageException | StopIterationException e) {
73+
throw new NoSuchElementException();
74+
}
75+
}
76+
77+
@Override
78+
public String toString() {
79+
try {
80+
return Interop.asString(Interop.toDisplayString(this));
81+
} catch (UnsupportedMessageException e) {
82+
return super.toString();
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)