Skip to content

Commit 69fcc62

Browse files
committed
[GR-48697] Support for additional interop messages in inspector-related objects.
PullRequest: graal/15608
2 parents d9622af + 22750f6 commit 69fcc62

File tree

8 files changed

+262
-0
lines changed

8 files changed

+262
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.truffle.tools.chromeinspector.test;
26+
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
29+
30+
import com.oracle.truffle.api.interop.InteropException;
31+
import com.oracle.truffle.api.interop.InteropLibrary;
32+
import com.oracle.truffle.tools.chromeinspector.objects.JSONTruffleArray;
33+
import com.oracle.truffle.tools.chromeinspector.objects.JSONTruffleObject;
34+
import org.graalvm.shadowed.org.json.JSONArray;
35+
import org.graalvm.shadowed.org.json.JSONObject;
36+
37+
import org.junit.Test;
38+
39+
public class InteropTest {
40+
41+
@Test
42+
@SuppressWarnings("unchecked")
43+
public void testJSONTruffleArraySort() throws InteropException {
44+
Object array = new JSONTruffleArray(new JSONArray("[2, 7, 1, 8]"));
45+
InteropLibrary interop = InteropLibrary.getUncached(array);
46+
long length = interop.getArraySize(array);
47+
48+
for (int iter = 1; iter < length; iter++) {
49+
for (int idx = 1; idx < length; idx++) {
50+
Object prev = interop.readArrayElement(array, idx - 1);
51+
Object curr = interop.readArrayElement(array, idx);
52+
if (((Comparable<Object>) prev).compareTo(curr) > 0) {
53+
// swap
54+
interop.writeArrayElement(array, idx - 1, curr);
55+
interop.writeArrayElement(array, idx, prev);
56+
}
57+
}
58+
}
59+
60+
for (int idx = 1; idx < length; idx++) {
61+
Object prev = interop.readArrayElement(array, idx - 1);
62+
Object curr = interop.readArrayElement(array, idx);
63+
assertTrue(((Comparable<Object>) prev).compareTo(curr) <= 0);
64+
}
65+
}
66+
67+
private static void testIdentity(Object theObject, Object sameObject, Object differentObject) {
68+
InteropLibrary interop = InteropLibrary.getUncached(theObject);
69+
assertTrue(interop.hasIdentity(theObject));
70+
assertTrue(interop.isIdentical(theObject, sameObject, interop));
71+
assertFalse(interop.isIdentical(theObject, differentObject, interop));
72+
}
73+
74+
@Test
75+
public void testJSONTruffleArrayIdentity() {
76+
String json = "[42, 211]";
77+
JSONArray jsonArray = new JSONArray(json);
78+
Object array = new JSONTruffleArray(jsonArray);
79+
Object sameArray = new JSONTruffleArray(jsonArray);
80+
Object anotherArray = new JSONTruffleArray(new JSONArray(json));
81+
testIdentity(array, sameArray, anotherArray);
82+
}
83+
84+
@Test
85+
public void testJSONTruffleObjectIdentity() {
86+
String json = "{ answer: 42, question: '?' }";
87+
JSONObject jsonObject = new JSONObject(json);
88+
JSONTruffleObject object = new JSONTruffleObject(jsonObject);
89+
Object sameObject = new JSONTruffleObject(jsonObject);
90+
Object anotherObject = new JSONTruffleObject(new JSONObject(json));
91+
testIdentity(object, sameObject, anotherObject);
92+
}
93+
94+
@Test
95+
public void testJSONKeysIdentity() throws InteropException {
96+
String json = "{ answer: 42, question: '?' }";
97+
JSONObject jsonObject = new JSONObject(json);
98+
JSONTruffleObject truffleObject = new JSONTruffleObject(jsonObject);
99+
InteropLibrary interop = InteropLibrary.getUncached();
100+
Object keys = interop.getMembers(truffleObject);
101+
Object sameKeys = interop.getMembers(truffleObject);
102+
Object otherKeys = interop.getMembers(new JSONTruffleObject(jsonObject));
103+
testIdentity(keys, sameKeys, otherKeys);
104+
}
105+
106+
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/AbstractInspectorArray.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import com.oracle.truffle.api.interop.InteropLibrary;
2828
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
2929
import com.oracle.truffle.api.interop.TruffleObject;
30+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
3031
import com.oracle.truffle.api.library.ExportLibrary;
3132
import com.oracle.truffle.api.library.ExportMessage;
33+
import com.oracle.truffle.api.utilities.TriState;
3234

3335
/**
3436
* A base class for arrays returned by Inspector module.
@@ -42,6 +44,12 @@ abstract class AbstractInspectorArray implements TruffleObject {
4244
@ExportMessage
4345
abstract Object readArrayElement(long index) throws InvalidArrayIndexException;
4446

47+
@ExportMessage
48+
@SuppressWarnings("unused")
49+
void writeArrayElement(long index, Object value) throws InvalidArrayIndexException, UnsupportedMessageException {
50+
throw UnsupportedMessageException.create();
51+
}
52+
4553
@SuppressWarnings("static-method")
4654
@ExportMessage
4755
final boolean hasArrayElements() {
@@ -53,4 +61,20 @@ boolean isArrayElementReadable(long index) {
5361
return index >= 0 && index < getArraySize();
5462
}
5563

64+
@ExportMessage
65+
boolean isArrayElementModifiable(@SuppressWarnings("unused") long index) {
66+
return false;
67+
}
68+
69+
@ExportMessage
70+
boolean isArrayElementInsertable(@SuppressWarnings("unused") long index) {
71+
return false;
72+
}
73+
74+
@ExportMessage
75+
abstract TriState isIdenticalOrUndefined(Object other);
76+
77+
@ExportMessage
78+
abstract int identityHashCode();
79+
5680
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/AbstractInspectorObject.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.truffle.api.library.CachedLibrary;
3737
import com.oracle.truffle.api.library.ExportLibrary;
3838
import com.oracle.truffle.api.library.ExportMessage;
39+
import com.oracle.truffle.api.utilities.TriState;
3940

4041
/**
4142
* A base class for objects returned by Inspector module.
@@ -104,6 +105,21 @@ protected Object instantiate(Object[] arguments) throws UnsupportedMessageExcept
104105
throw UnsupportedMessageException.create();
105106
}
106107

108+
@ExportMessage
109+
TriState isIdenticalOrUndefined(Object other) {
110+
if (getClass() == other.getClass()) {
111+
return TriState.valueOf(this == other);
112+
} else {
113+
return TriState.UNDEFINED;
114+
}
115+
}
116+
117+
@ExportMessage
118+
@TruffleBoundary
119+
int identityHashCode() {
120+
return hashCode();
121+
}
122+
107123
private TruffleObject createMethodExecutable(String name) {
108124
CompilerAsserts.neverPartOfCompilation();
109125
return new MethodExecutable(this, name);

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/Console.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.oracle.truffle.tools.chromeinspector.objects;
2626

27+
import java.util.Arrays;
2728
import java.util.Map;
2829
import java.util.concurrent.ConcurrentHashMap;
2930

@@ -34,6 +35,7 @@
3435
import com.oracle.truffle.api.interop.UnknownIdentifierException;
3536
import com.oracle.truffle.api.interop.UnsupportedMessageException;
3637
import com.oracle.truffle.api.interop.UnsupportedTypeException;
38+
import com.oracle.truffle.api.utilities.TriState;
3739
import com.oracle.truffle.tools.chromeinspector.server.InspectorServerConnection;
3840

3941
/**
@@ -267,6 +269,21 @@ Object readArrayElement(long index) throws InvalidArrayIndexException {
267269
}
268270
return METHOD_NAMES[(int) index];
269271
}
272+
273+
@Override
274+
TriState isIdenticalOrUndefined(Object other) {
275+
if (other instanceof Keys) {
276+
return TriState.TRUE;
277+
} else {
278+
return TriState.UNDEFINED;
279+
}
280+
}
281+
282+
@Override
283+
@CompilerDirectives.TruffleBoundary
284+
int identityHashCode() {
285+
return Arrays.hashCode(METHOD_NAMES);
286+
}
270287
}
271288

272289
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/JSONTruffleArray.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.oracle.truffle.api.CompilerDirectives;
3030
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
31+
import com.oracle.truffle.api.utilities.TriState;
3132
import org.graalvm.shadowed.org.json.JSONArray;
3233

3334
/**
@@ -56,4 +57,33 @@ Object readArrayElement(long index) throws InvalidArrayIndexException {
5657
return getTruffleValueFromJSONValue(value);
5758
}
5859

60+
@Override
61+
@CompilerDirectives.TruffleBoundary
62+
void writeArrayElement(long index, Object value) throws InvalidArrayIndexException {
63+
if (!isArrayElementModifiable(index)) {
64+
throw InvalidArrayIndexException.create(index);
65+
}
66+
json.put((int) index, value);
67+
}
68+
69+
@Override
70+
boolean isArrayElementModifiable(long index) {
71+
return index >= 0 && index < getArraySize();
72+
}
73+
74+
@Override
75+
TriState isIdenticalOrUndefined(Object other) {
76+
if (other instanceof JSONTruffleArray otherArray) {
77+
return TriState.valueOf(json == otherArray.json);
78+
} else {
79+
return TriState.UNDEFINED;
80+
}
81+
}
82+
83+
@Override
84+
@CompilerDirectives.TruffleBoundary
85+
int identityHashCode() {
86+
return json.hashCode();
87+
}
88+
5989
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/JSONTruffleObject.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
3232
import com.oracle.truffle.api.interop.TruffleObject;
3333
import com.oracle.truffle.api.interop.UnknownIdentifierException;
34+
import com.oracle.truffle.api.utilities.TriState;
3435
import org.graalvm.shadowed.org.json.JSONArray;
3536
import org.graalvm.shadowed.org.json.JSONObject;
3637

@@ -99,6 +100,21 @@ static Object getTruffleValueFromJSONValue(Object value) {
99100
}
100101
}
101102

103+
@Override
104+
TriState isIdenticalOrUndefined(Object other) {
105+
if (other instanceof JSONTruffleObject otherObject) {
106+
return TriState.valueOf(json == otherObject.json);
107+
} else {
108+
return TriState.UNDEFINED;
109+
}
110+
}
111+
112+
@Override
113+
@TruffleBoundary
114+
int identityHashCode() {
115+
return json.hashCode();
116+
}
117+
102118
static final class JSONKeys extends AbstractInspectorArray {
103119

104120
private final JSONTruffleObject obj;
@@ -121,6 +137,21 @@ Object readArrayElement(long index) throws InvalidArrayIndexException {
121137
}
122138
return allNames[(int) index];
123139
}
140+
141+
@Override
142+
TriState isIdenticalOrUndefined(Object other) {
143+
if (other instanceof JSONKeys otherKeys) {
144+
return TriState.valueOf(obj == otherKeys.obj);
145+
} else {
146+
return TriState.UNDEFINED;
147+
}
148+
}
149+
150+
@Override
151+
@TruffleBoundary
152+
int identityHashCode() {
153+
return obj.hashCode();
154+
}
124155
}
125156

126157
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/JavaTruffleArray.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
*/
2525
package com.oracle.truffle.tools.chromeinspector.objects;
2626

27+
import java.util.Arrays;
28+
2729
import com.oracle.truffle.api.CompilerDirectives;
2830
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
31+
import com.oracle.truffle.api.utilities.TriState;
2932

3033
public final class JavaTruffleArray extends AbstractInspectorArray {
3134

@@ -48,4 +51,20 @@ Object readArrayElement(long index) throws InvalidArrayIndexException {
4851
}
4952
return array[(int) index];
5053
}
54+
55+
@Override
56+
TriState isIdenticalOrUndefined(Object other) {
57+
if (other instanceof JavaTruffleArray otherArray) {
58+
return TriState.valueOf(array == otherArray.array);
59+
} else {
60+
return TriState.UNDEFINED;
61+
}
62+
}
63+
64+
@Override
65+
@CompilerDirectives.TruffleBoundary
66+
int identityHashCode() {
67+
return Arrays.hashCode(array);
68+
}
69+
5170
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/objects/Keys.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
*/
2525
package com.oracle.truffle.tools.chromeinspector.objects;
2626

27+
import java.util.Arrays;
28+
2729
import com.oracle.truffle.api.CompilerDirectives;
2830
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
2931
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
32+
import com.oracle.truffle.api.utilities.TriState;
3033

3134
final class Keys extends AbstractInspectorArray {
3235

@@ -49,4 +52,20 @@ Object readArrayElement(long index) throws InvalidArrayIndexException {
4952
}
5053
return names[(int) index];
5154
}
55+
56+
@Override
57+
TriState isIdenticalOrUndefined(Object other) {
58+
if (other instanceof Keys otherKeys) {
59+
return TriState.valueOf(names == otherKeys.names);
60+
} else {
61+
return TriState.UNDEFINED;
62+
}
63+
}
64+
65+
@Override
66+
@CompilerDirectives.TruffleBoundary
67+
int identityHashCode() {
68+
return Arrays.hashCode(names);
69+
}
70+
5271
}

0 commit comments

Comments
 (0)