Skip to content

Commit dfd6c58

Browse files
committed
simplify InteropMap and InteropArray
1 parent 27bcc01 commit dfd6c58

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/InteropArray.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@
4040
*/
4141
package com.oracle.graal.python.runtime.interop;
4242

43-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
43+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4444
import com.oracle.truffle.api.interop.ForeignAccess;
4545
import com.oracle.truffle.api.interop.MessageResolution;
4646
import com.oracle.truffle.api.interop.Resolve;
4747
import com.oracle.truffle.api.interop.TruffleObject;
4848
import com.oracle.truffle.api.nodes.Node;
4949

50-
public class InteropArray implements TruffleObject {
51-
52-
private final Object[] array;
50+
public final class InteropArray implements TruffleObject {
51+
@CompilationFinal(dimensions = 1) private final Object[] array;
5352

5453
InteropArray(Object[] array) {
5554
this.array = array;
@@ -60,11 +59,11 @@ public ForeignAccess getForeignAccess() {
6059
return InteropArrayMRForeign.ACCESS;
6160
}
6261

63-
public Object getKeyAt(int pos) {
62+
private Object get(int pos) {
6463
return array[pos];
6564
}
6665

67-
public int size() {
66+
private int size() {
6867
return array.length;
6968
}
7069

@@ -77,24 +76,25 @@ static class InteropArrayMR {
7776

7877
@Resolve(message = "READ")
7978
abstract static class Read extends Node {
80-
@TruffleBoundary
81-
public Object access(InteropArray target, Number index) {
82-
return target.getKeyAt(index.intValue());
79+
Object access(InteropArray target, int index) {
80+
return target.get(index);
81+
}
82+
83+
Object access(InteropArray target, long index) {
84+
return target.get((int) index);
8385
}
8486
}
8587

8688
@Resolve(message = "HAS_SIZE")
8789
abstract static class HasSize extends Node {
88-
89-
public boolean access(@SuppressWarnings("unused") InteropArray target) {
90+
boolean access(@SuppressWarnings("unused") InteropArray target) {
9091
return true;
9192
}
9293
}
9394

9495
@Resolve(message = "GET_SIZE")
9596
abstract static class GetSize extends Node {
96-
97-
public Object access(InteropArray target) {
97+
int access(InteropArray target) {
9898
return target.size();
9999
}
100100
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/InteropMap.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,57 +58,55 @@
5858
* A container class used to store per-node attributes used by the instrumentation framework.
5959
*
6060
*/
61-
public class InteropMap implements TruffleObject {
61+
public final class InteropMap implements TruffleObject {
62+
private final Map<String, Object> data;
6263

63-
private final Map<String, Object> data = new HashMap<>();
64+
public InteropMap(Map<String, Object> data) {
65+
this.data = data;
66+
}
6467

6568
@Override
6669
public ForeignAccess getForeignAccess() {
6770
return InteropMapMRForeign.ACCESS;
6871
}
6972

7073
@TruffleBoundary
71-
public void addProperty(String name, Object value) {
72-
data.put(name, value);
73-
}
74-
75-
@TruffleBoundary
76-
public int size() {
74+
private int size() {
7775
return data.size();
7876
}
7977

8078
@TruffleBoundary
81-
public Object getProperty(String name) {
82-
assert hasProperty(name);
79+
private Object getKey(String name) {
80+
assert hasKey(name);
8381
return data.get(name);
8482
}
8583

8684
@TruffleBoundary
87-
public boolean hasProperty(String name) {
85+
private boolean hasKey(String name) {
8886
return data.containsKey(name);
8987
}
9088

9189
@TruffleBoundary
92-
public TruffleObject getPropertyNames() {
90+
private TruffleObject getKeys() {
9391
return new InteropArray(data.keySet().toArray());
9492
}
9593

9694
@TruffleBoundary
9795
public static InteropMap fromPDict(PDict dict) {
98-
InteropMap map = new InteropMap();
96+
Map<String, Object> map = new HashMap<>();
9997
for (DictEntry s : dict.getDictStorage().entries()) {
100-
map.addProperty(s.getKey().toString(), s.getValue());
98+
map.put(s.getKey().toString(), s.getValue());
10199
}
102-
return map;
100+
return new InteropMap(map);
103101
}
104102

105103
@TruffleBoundary
106104
public static InteropMap fromPythonObject(PythonObject globals) {
107-
InteropMap map = new InteropMap();
105+
Map<String, Object> map = new HashMap<>();
108106
for (String name : globals.getAttributeNames()) {
109-
map.addProperty(name, globals.getAttribute(name));
107+
map.put(name, globals.getAttribute(name));
110108
}
111-
return map;
109+
return new InteropMap(map);
112110
}
113111

114112
static boolean isInstance(TruffleObject object) {
@@ -120,30 +118,29 @@ static class InteropMapMR {
120118

121119
@Resolve(message = "READ")
122120
abstract static class Read extends Node {
123-
public Object access(InteropMap target, String key) {
124-
return target.getProperty(key);
121+
Object access(InteropMap target, String key) {
122+
return target.getKey(key);
125123
}
126124
}
127125

128126
@Resolve(message = "HAS_KEYS")
129127
abstract static class HasKeys extends Node {
130-
131-
public Object access(@SuppressWarnings("unused") Object target) {
128+
boolean access(@SuppressWarnings("unused") Object target) {
132129
return true;
133130
}
134131
}
135132

136133
@Resolve(message = "KEYS")
137134
abstract static class Keys extends Node {
138-
public Object access(InteropMap target) {
139-
return target.getPropertyNames();
135+
Object access(InteropMap target) {
136+
return target.getKeys();
140137
}
141138
}
142139

143140
@Resolve(message = "KEY_INFO")
144141
abstract static class KeyInfoMR extends Node {
145-
public Object access(InteropMap target, Object key) {
146-
if (key instanceof String && target.hasProperty((String) key)) {
142+
int access(InteropMap target, Object key) {
143+
if (key instanceof String && target.hasKey((String) key)) {
147144
return KeyInfo.READABLE;
148145
} else {
149146
return 0;

0 commit comments

Comments
 (0)