Skip to content

Commit 022fb9a

Browse files
committed
[GR-14700] Implement InteropLibrary messages in PSequence classes
PullRequest: graalpython/801
2 parents dfcd90f + 6413af4 commit 022fb9a

File tree

10 files changed

+410
-96
lines changed

10 files changed

+410
-96
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/InteropLibraryTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -121,6 +121,18 @@ public void testBooleanUnbox() {
121121
assertFalse(somePStr.isBoolean());
122122
}
123123

124+
@Test
125+
public void testPListInsertable() {
126+
org.graalvm.polyglot.Source source = org.graalvm.polyglot.Source.create("python", "import polyglot\nmutableObj = [1,2,3,4]\nprint(polyglot.__element_info__(mutableObj, 0, \"insertable\"))");
127+
assertPrints("False\n", source);
128+
}
129+
130+
@Test
131+
public void testPListRemovable() {
132+
org.graalvm.polyglot.Source source = org.graalvm.polyglot.Source.create("python", "import polyglot\nmutableObj = [1,2,3,4]\nprint(polyglot.__element_info__(mutableObj, 0, \"removable\"))");
133+
assertPrints("True\n", source);
134+
}
135+
124136
@Test
125137
public void testIsNull() {
126138
assertTrue(v("None").isNull());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/PArray.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -26,10 +26,10 @@
2626
package com.oracle.graal.python.builtins.objects.array;
2727

2828
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
29-
import com.oracle.graal.python.runtime.sequence.PSequence;
29+
import com.oracle.graal.python.runtime.sequence.PMutableSequence;
3030
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3131

32-
public class PArray extends PSequence {
32+
public class PArray extends PMutableSequence {
3333

3434
private SequenceStorage store;
3535

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/PByteArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
3131
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3232
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
33-
import com.oracle.graal.python.runtime.sequence.PSequence;
33+
import com.oracle.graal.python.runtime.sequence.PMutableSequence;
3434
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
3535
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
3636
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
@@ -42,7 +42,7 @@
4242
import com.oracle.truffle.api.library.ExportMessage.Ignore;
4343

4444
@ExportLibrary(PythonObjectLibrary.class)
45-
public final class PByteArray extends PSequence implements PIBytesLike {
45+
public final class PByteArray extends PMutableSequence implements PIBytesLike {
4646

4747
private SequenceStorage store;
4848

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 95 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,6 @@ abstract static class SequenceStorageBaseNode extends PNodeWithContext {
285285
protected static final int MAX_SEQUENCE_STORAGES = 13;
286286
protected static final int MAX_ARRAY_STORAGES = 9;
287287

288-
@Child private GetElementType getElementType;
289-
@Child private LenNode lenNode;
290-
291288
protected static boolean isByteStorage(NativeSequenceStorage store) {
292289
return store.getElementType() == ListStorageType.Byte;
293290
}
@@ -320,11 +317,7 @@ protected static boolean isNative(SequenceStorage store) {
320317
return store instanceof NativeSequenceStorage;
321318
}
322319

323-
protected boolean isEmpty(SequenceStorage left) {
324-
if (lenNode == null) {
325-
CompilerDirectives.transferToInterpreterAndInvalidate();
326-
lenNode = insert(LenNode.create());
327-
}
320+
protected boolean isEmpty(LenNode lenNode, SequenceStorage left) {
328321
return lenNode.execute(left) == 0;
329322
}
330323

@@ -411,55 +404,6 @@ protected static boolean isList(ListStorageType et) {
411404
protected static boolean hasStorage(Object source) {
412405
return source instanceof PSequence && !(source instanceof PString);
413406
}
414-
415-
protected boolean isBoolean(SequenceStorage s) {
416-
return getElementType(s) == ListStorageType.Boolean;
417-
}
418-
419-
protected boolean isByte(SequenceStorage s) {
420-
return getElementType(s) == ListStorageType.Byte;
421-
}
422-
423-
protected boolean isByteLike(SequenceStorage s) {
424-
return isByte(s) || isInt(s) || isLong(s);
425-
}
426-
427-
protected boolean isChar(SequenceStorage s) {
428-
return getElementType(s) == ListStorageType.Char;
429-
}
430-
431-
private ListStorageType getElementType(SequenceStorage s) {
432-
if (getElementType == null) {
433-
CompilerDirectives.transferToInterpreterAndInvalidate();
434-
getElementType = insert(GetElementType.create());
435-
}
436-
return getElementType.execute(s);
437-
}
438-
439-
protected boolean isInt(SequenceStorage s) {
440-
return getElementType(s) == ListStorageType.Int;
441-
}
442-
443-
protected boolean isLong(SequenceStorage s) {
444-
return getElementType(s) == ListStorageType.Long;
445-
}
446-
447-
protected boolean isDouble(SequenceStorage s) {
448-
return getElementType(s) == ListStorageType.Double;
449-
}
450-
451-
protected boolean isObject(SequenceStorage s) {
452-
return getElementType(s) == ListStorageType.Generic;
453-
}
454-
455-
protected boolean isTuple(SequenceStorage s) {
456-
return getElementType(s) == ListStorageType.Tuple;
457-
}
458-
459-
protected boolean isList(SequenceStorage s) {
460-
return getElementType(s) == ListStorageType.List;
461-
}
462-
463407
}
464408

465409
abstract static class NormalizingNode extends PNodeWithContext {
@@ -720,7 +664,7 @@ public static GetItemDynamicNode getUncached() {
720664

721665
@GenerateUncached
722666
@ImportStatic(SequenceStorageBaseNode.class)
723-
abstract static class GetItemScalarNode extends Node {
667+
public abstract static class GetItemScalarNode extends Node {
724668

725669
public abstract Object execute(SequenceStorage s, int idx);
726670

@@ -1239,7 +1183,7 @@ public static SetItemNode create(String invalidItemErrorMessage) {
12391183

12401184
@GenerateUncached
12411185
@ImportStatic(SequenceStorageBaseNode.class)
1242-
abstract static class SetItemScalarNode extends Node {
1186+
public abstract static class SetItemScalarNode extends Node {
12431187

12441188
public abstract void execute(SequenceStorage s, int idx, Object value);
12451189

@@ -1840,6 +1784,8 @@ public abstract static class CmpNode extends SequenceStorageBaseNode {
18401784
@Child private BinaryComparisonNode comparisonNode;
18411785
@Child private CoerceToBooleanNode castToBooleanNode;
18421786

1787+
@Child private LenNode lenNode;
1788+
18431789
private final BinCmpOp cmpOp;
18441790

18451791
protected CmpNode(BinCmpOp cmpOp) {
@@ -1848,8 +1794,17 @@ protected CmpNode(BinCmpOp cmpOp) {
18481794

18491795
public abstract boolean execute(VirtualFrame frame, SequenceStorage left, SequenceStorage right);
18501796

1797+
protected boolean isEmpty(SequenceStorage left) {
1798+
if (lenNode == null) {
1799+
CompilerDirectives.transferToInterpreterAndInvalidate();
1800+
lenNode = insert(LenNode.create());
1801+
}
1802+
return lenNode.execute(left) == 0;
1803+
}
1804+
1805+
@SuppressWarnings("unused")
18511806
@Specialization(guards = {"isEmpty(left)", "isEmpty(right)"})
1852-
boolean doEmpty(@SuppressWarnings("unused") SequenceStorage left, @SuppressWarnings("unused") SequenceStorage right) {
1807+
boolean doEmpty(SequenceStorage left, SequenceStorage right) {
18531808
return cmpOp.cmp(0, 0);
18541809
}
18551810

@@ -2627,8 +2582,18 @@ public abstract static class ContainsNode extends SequenceStorageBaseNode {
26272582
@Child private BinaryComparisonNode equalsNode;
26282583
@Child private CoerceToBooleanNode castToBooleanNode;
26292584

2585+
@Child private LenNode lenNode;
2586+
26302587
public abstract boolean execute(VirtualFrame frame, SequenceStorage left, Object item);
26312588

2589+
protected boolean isEmpty(SequenceStorage left) {
2590+
if (lenNode == null) {
2591+
CompilerDirectives.transferToInterpreterAndInvalidate();
2592+
lenNode = insert(LenNode.create());
2593+
}
2594+
return lenNode.execute(left) == 0;
2595+
}
2596+
26322597
@Specialization(guards = "isEmpty(left)")
26332598
@SuppressWarnings("unused")
26342599
boolean doEmpty(SequenceStorage left, Object item) {
@@ -2984,8 +2949,58 @@ public static AppendNode getUncached() {
29842949

29852950
public abstract static class CreateEmptyNode extends SequenceStorageBaseNode {
29862951

2952+
@Child private GetElementType getElementType;
2953+
29872954
public abstract SequenceStorage execute(SequenceStorage s, int cap);
29882955

2956+
private ListStorageType getElementType(SequenceStorage s) {
2957+
if (getElementType == null) {
2958+
CompilerDirectives.transferToInterpreterAndInvalidate();
2959+
getElementType = insert(GetElementType.create());
2960+
}
2961+
return getElementType.execute(s);
2962+
}
2963+
2964+
protected boolean isBoolean(SequenceStorage s) {
2965+
return getElementType(s) == ListStorageType.Boolean;
2966+
}
2967+
2968+
protected boolean isInt(SequenceStorage s) {
2969+
return getElementType(s) == ListStorageType.Int;
2970+
}
2971+
2972+
protected boolean isLong(SequenceStorage s) {
2973+
return getElementType(s) == ListStorageType.Long;
2974+
}
2975+
2976+
protected boolean isByte(SequenceStorage s) {
2977+
return getElementType(s) == ListStorageType.Byte;
2978+
}
2979+
2980+
protected boolean isByteLike(SequenceStorage s) {
2981+
return isByte(s) || isInt(s) || isLong(s);
2982+
}
2983+
2984+
protected boolean isChar(SequenceStorage s) {
2985+
return getElementType(s) == ListStorageType.Char;
2986+
}
2987+
2988+
protected boolean isDouble(SequenceStorage s) {
2989+
return getElementType(s) == ListStorageType.Double;
2990+
}
2991+
2992+
protected boolean isObject(SequenceStorage s) {
2993+
return getElementType(s) == ListStorageType.Generic;
2994+
}
2995+
2996+
protected boolean isTuple(SequenceStorage s) {
2997+
return getElementType(s) == ListStorageType.Tuple;
2998+
}
2999+
3000+
protected boolean isList(SequenceStorage s) {
3001+
return getElementType(s) == ListStorageType.List;
3002+
}
3003+
29893004
@Specialization(guards = "isBoolean(s)")
29903005
BoolSequenceStorage doBoolean(@SuppressWarnings("unused") SequenceStorage s, int cap) {
29913006
return new BoolSequenceStorage(cap);
@@ -3224,7 +3239,8 @@ public static DeleteNode create(String keyTypeErrorMessage) {
32243239
}
32253240
}
32263241

3227-
abstract static class DeleteItemNode extends SequenceStorageBaseNode {
3242+
@GenerateUncached
3243+
public abstract static class DeleteItemNode extends SequenceStorageBaseNode {
32283244

32293245
public abstract void execute(SequenceStorage s, int idx);
32303246

@@ -3366,8 +3382,9 @@ public abstract static class ItemIndexNode extends SequenceStorageBaseNode {
33663382

33673383
public abstract int execute(VirtualFrame frame, SequenceStorage s, double item, int start, int end);
33683384

3369-
@Specialization(guards = "isBoolean(s)")
3370-
int doBoolean(SequenceStorage s, boolean item, int start, int end) {
3385+
@Specialization(guards = "isBoolean(getElementType, s)")
3386+
int doBoolean(SequenceStorage s, boolean item, int start, int end,
3387+
@Cached @SuppressWarnings("unused") GetElementType getElementType) {
33713388
for (int i = start; i < getLength(s, end); i++) {
33723389
if (getItemScalarNode().executeBoolean(s, i) == item) {
33733390
return i;
@@ -3376,8 +3393,9 @@ int doBoolean(SequenceStorage s, boolean item, int start, int end) {
33763393
return -1;
33773394
}
33783395

3379-
@Specialization(guards = "isByte(s)")
3380-
int doByte(SequenceStorage s, byte item, int start, int end) {
3396+
@Specialization(guards = "isByte(getElementType, s)")
3397+
int doByte(SequenceStorage s, byte item, int start, int end,
3398+
@Cached @SuppressWarnings("unused") GetElementType getElementType) {
33813399
for (int i = start; i < getLength(s, end); i++) {
33823400
if (getItemScalarNode().executeByte(s, i) == item) {
33833401
return i;
@@ -3386,8 +3404,9 @@ int doByte(SequenceStorage s, byte item, int start, int end) {
33863404
return -1;
33873405
}
33883406

3389-
@Specialization(guards = "isChar(s)")
3390-
int doChar(SequenceStorage s, char item, int start, int end) {
3407+
@Specialization(guards = "isChar(getElementType, s)")
3408+
int doChar(SequenceStorage s, char item, int start, int end,
3409+
@Cached @SuppressWarnings("unused") GetElementType getElementType) {
33913410
for (int i = start; i < getLength(s, end); i++) {
33923411
if (getItemScalarNode().executeChar(s, i) == item) {
33933412
return i;
@@ -3396,8 +3415,9 @@ int doChar(SequenceStorage s, char item, int start, int end) {
33963415
return -1;
33973416
}
33983417

3399-
@Specialization(guards = "isInt(s)")
3400-
int doInt(SequenceStorage s, int item, int start, int end) {
3418+
@Specialization(guards = "isInt(getElementType, s)")
3419+
int doInt(SequenceStorage s, int item, int start, int end,
3420+
@Cached @SuppressWarnings("unused") GetElementType getElementType) {
34013421
for (int i = start; i < getLength(s, end); i++) {
34023422
if (getItemScalarNode().executeInt(s, i) == item) {
34033423
return i;
@@ -3406,8 +3426,9 @@ int doInt(SequenceStorage s, int item, int start, int end) {
34063426
return -1;
34073427
}
34083428

3409-
@Specialization(guards = "isLong(s)")
3410-
int doLong(SequenceStorage s, long item, int start, int end) {
3429+
@Specialization(guards = "isLong(getElementType, s)")
3430+
int doLong(SequenceStorage s, long item, int start, int end,
3431+
@Cached @SuppressWarnings("unused") GetElementType getElementType) {
34113432
for (int i = start; i < getLength(s, end); i++) {
34123433
if (getItemScalarNode().executeLong(s, i) == item) {
34133434
return i;
@@ -3416,8 +3437,9 @@ int doLong(SequenceStorage s, long item, int start, int end) {
34163437
return -1;
34173438
}
34183439

3419-
@Specialization(guards = "isDouble(s)")
3420-
int doDouble(SequenceStorage s, double item, int start, int end) {
3440+
@Specialization(guards = "isDouble(getElementType, s)")
3441+
int doDouble(SequenceStorage s, double item, int start, int end,
3442+
@Cached @SuppressWarnings("unused") GetElementType getElementType) {
34213443
for (int i = start; i < getLength(s, end); i++) {
34223444
if (getItemScalarNode().executeDouble(s, i) == item) {
34233445
return i;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/PList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -27,13 +27,13 @@
2727

2828
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
2929
import com.oracle.graal.python.nodes.literal.ListLiteralNode;
30-
import com.oracle.graal.python.runtime.sequence.PSequence;
30+
import com.oracle.graal.python.runtime.sequence.PMutableSequence;
3131
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3232
import com.oracle.graal.python.runtime.sequence.storage.SequenceStoreException;
3333
import com.oracle.truffle.api.CompilerDirectives;
3434
import com.oracle.truffle.api.nodes.UnexpectedResultException;
3535

36-
public final class PList extends PSequence {
36+
public final class PList extends PMutableSequence {
3737
private final ListLiteralNode origin;
3838
private SequenceStorage store;
3939

0 commit comments

Comments
 (0)