Skip to content

Commit a083f9b

Browse files
committed
Convert RForeignListWrapper to a storage strategy in RList
1 parent d8e06cb commit a083f9b

File tree

5 files changed

+162
-169
lines changed

5 files changed

+162
-169
lines changed

com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/PrecedenceNode.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.oracle.truffle.r.runtime.data.RDoubleVector;
4444
import com.oracle.truffle.r.runtime.data.RExpression;
4545
import com.oracle.truffle.r.runtime.data.RExternalPtr;
46-
import com.oracle.truffle.r.runtime.data.RForeignListWrapper;
4746
import com.oracle.truffle.r.runtime.data.RFunction;
4847
import com.oracle.truffle.r.runtime.data.RInteropScalar;
4948
import com.oracle.truffle.r.runtime.data.RPairList;
@@ -180,12 +179,6 @@ protected int doListRecursive(RList val, boolean recursive,
180179
return doListRecursiveInternal(val, precedenceNode, recursive);
181180
}
182181

183-
@Specialization(guards = "recursive")
184-
protected int doListRecursive(RForeignListWrapper val, boolean recursive,
185-
@Cached("createRecursive()") PrecedenceNode precedenceNode) {
186-
return doListRecursiveInternal(val, precedenceNode, recursive);
187-
}
188-
189182
private static int doListRecursiveInternal(RAbstractListVector val, PrecedenceNode precedenceNode, boolean recursive) {
190183
int precedence = -1;
191184
for (int i = 0; i < val.getLength(); i++) {
@@ -215,12 +208,6 @@ protected int doList(RList val, boolean recursive) {
215208
return LIST_PRECEDENCE;
216209
}
217210

218-
@Specialization(guards = "!recursive")
219-
@SuppressWarnings("unused")
220-
protected int doList(RForeignListWrapper val, boolean recursive) {
221-
return LIST_PRECEDENCE;
222-
}
223-
224211
@Specialization(guards = {"!recursive", "!val.isLanguage()"})
225212
@SuppressWarnings("unused")
226213
protected int doPairList(RPairList val, boolean recursive) {

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RForeignListWrapper.java

Lines changed: 0 additions & 153 deletions
This file was deleted.

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RList.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2828
import com.oracle.truffle.api.interop.InteropLibrary;
29+
import com.oracle.truffle.api.interop.TruffleObject;
2930
import com.oracle.truffle.api.library.CachedLibrary;
3031
import com.oracle.truffle.api.library.ExportLibrary;
3132
import com.oracle.truffle.api.library.ExportMessage;
@@ -64,11 +65,16 @@ public final class RList extends RAbstractListVector implements RMaterializedVec
6465

6566
private RList(Object data, int length) {
6667
super(false);
67-
assert data.getClass().isAssignableFrom(Object[].class) : data;
68+
// if data is array => it must be Object array
69+
assert !data.getClass().isArray() || data.getClass().isAssignableFrom(Object[].class) : data;
6870
setData(data, length);
6971
assert RAbstractVector.verifyVector(this);
7072
}
7173

74+
public static RList createForeignWrapper(TruffleObject obj, int size) {
75+
return new RList(new RListForeignObjData(obj), size);
76+
}
77+
7278
private void setData(Object data, int newLen) {
7379
this.data = data;
7480
if (data instanceof VectorDataWithOwner) {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (c) 2020, 2020, 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 3 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 3 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 3 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package com.oracle.truffle.r.runtime.data;
24+
25+
import com.oracle.truffle.api.dsl.Cached;
26+
import com.oracle.truffle.api.dsl.Cached.Shared;
27+
import com.oracle.truffle.api.interop.InteropLibrary;
28+
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
29+
import com.oracle.truffle.api.interop.TruffleObject;
30+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
31+
import com.oracle.truffle.api.library.CachedLibrary;
32+
import com.oracle.truffle.api.library.ExportLibrary;
33+
import com.oracle.truffle.api.library.ExportMessage;
34+
import com.oracle.truffle.api.profiles.LoopConditionProfile;
35+
import com.oracle.truffle.r.runtime.RInternalError;
36+
import com.oracle.truffle.r.runtime.RType;
37+
import com.oracle.truffle.r.runtime.data.VectorDataLibrary.RandomAccessIterator;
38+
import com.oracle.truffle.r.runtime.data.VectorDataLibrary.SeqIterator;
39+
import com.oracle.truffle.r.runtime.interop.Foreign2R;
40+
import com.oracle.truffle.r.runtime.ops.na.NACheck;
41+
42+
@ExportLibrary(VectorDataLibrary.class)
43+
class RListForeignObjData implements TruffleObject {
44+
protected final Object foreign;
45+
46+
RListForeignObjData(Object foreign) {
47+
this.foreign = foreign;
48+
}
49+
50+
@SuppressWarnings("static-method")
51+
@ExportMessage
52+
public final RType getType() {
53+
return RType.List;
54+
}
55+
56+
@SuppressWarnings("static-method")
57+
@ExportMessage
58+
public NACheck getNACheck() {
59+
return NACheck.getEnabled();
60+
}
61+
62+
@ExportMessage
63+
public int getLength(@CachedLibrary("this.foreign") InteropLibrary interop) {
64+
try {
65+
long result = interop.getArraySize(foreign);
66+
return (int) result;
67+
} catch (UnsupportedMessageException e) {
68+
throw RInternalError.shouldNotReachHere();
69+
}
70+
}
71+
72+
@ExportMessage
73+
public Object[] materialize(@CachedLibrary("this.foreign") InteropLibrary interop,
74+
@Shared("foreign2R") @Cached Foreign2R foreign2R) {
75+
return getListDataCopy(interop, foreign2R);
76+
}
77+
78+
@ExportMessage
79+
public boolean isWriteable() {
80+
return false;
81+
}
82+
83+
@ExportMessage
84+
public RListForeignObjData copy(@SuppressWarnings("unused") boolean deep) {
85+
return new RListForeignObjData(foreign);
86+
}
87+
88+
@ExportMessage
89+
@SuppressWarnings("unused")
90+
public RListForeignObjData copyResized(int newSize, boolean deep, boolean fillNA) {
91+
throw RInternalError.shouldNotReachHere("this method will be removed");
92+
}
93+
94+
@ExportMessage
95+
public Object[] getListDataCopy(@CachedLibrary("this.foreign") InteropLibrary interop,
96+
@Shared("foreign2R") @Cached Foreign2R foreign2R) {
97+
int length = getLength(interop);
98+
Object[] result = new Object[length];
99+
Object foreignObj = foreign;
100+
for (int i = 0; i < length; i++) {
101+
result[i] = getElementAtImpl(foreignObj, i, interop, foreign2R);
102+
}
103+
return result;
104+
}
105+
106+
@ExportMessage
107+
public SeqIterator iterator(@Shared("SeqItLoopProfile") @Cached("createCountingProfile()") LoopConditionProfile loopProfile,
108+
@CachedLibrary("this.foreign") InteropLibrary interop) {
109+
SeqIterator it = new SeqIterator(foreign, getLength(interop));
110+
it.initLoopConditionProfile(loopProfile);
111+
return it;
112+
}
113+
114+
@ExportMessage
115+
public boolean next(SeqIterator it, boolean withWrap,
116+
@Shared("SeqItLoopProfile") @Cached("createCountingProfile()") LoopConditionProfile loopProfile) {
117+
return it.next(loopProfile, withWrap);
118+
}
119+
120+
@ExportMessage
121+
public RandomAccessIterator randomAccessIterator() {
122+
return new RandomAccessIterator(foreign);
123+
}
124+
125+
@ExportMessage
126+
public Object getElementAt(int index,
127+
@CachedLibrary("this.foreign") InteropLibrary interop,
128+
@Shared("foreign2R") @Cached Foreign2R foreign2R) {
129+
return getElementAtImpl(foreign, index, interop, foreign2R);
130+
}
131+
132+
@ExportMessage
133+
public Object getNextElement(SeqIterator it,
134+
@CachedLibrary("this.foreign") InteropLibrary interop,
135+
@Shared("foreign2R") @Cached Foreign2R foreign2R) {
136+
return getElementAtImpl(it.getStore(), it.getIndex(), interop, foreign2R);
137+
}
138+
139+
@ExportMessage
140+
public Object getElement(@SuppressWarnings("unused") RandomAccessIterator it, int index,
141+
@CachedLibrary("this.foreign") InteropLibrary interop,
142+
@Shared("foreign2R") @Cached Foreign2R foreign2R) {
143+
return getElementAtImpl(it.getStore(), index, interop, foreign2R);
144+
}
145+
146+
private static Object getElementAtImpl(Object foreign, int index, InteropLibrary interop, Foreign2R foreign2R) {
147+
try {
148+
return foreign2R.convert(interop.readArrayElement(foreign, index));
149+
} catch (UnsupportedMessageException | InvalidArrayIndexException e) {
150+
throw RInternalError.shouldNotReachHere(e);
151+
}
152+
}
153+
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/interop/TruffleObjectConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
import com.oracle.truffle.r.runtime.data.RDataFactory;
3838
import com.oracle.truffle.r.runtime.data.RDoubleVector;
3939
import com.oracle.truffle.r.runtime.data.RForeignBooleanWrapper;
40-
import com.oracle.truffle.r.runtime.data.RForeignListWrapper;
4140
import com.oracle.truffle.r.runtime.data.RForeignStringWrapper;
4241
import com.oracle.truffle.r.runtime.data.RIntVector;
42+
import com.oracle.truffle.r.runtime.data.RList;
4343
import com.oracle.truffle.r.runtime.data.RStringVector;
4444

4545
public final class TruffleObjectConverter {
@@ -73,7 +73,7 @@ public static Object convert(TruffleObject obj) {
7373
return new RForeignStringWrapper(obj);
7474
case List:
7575
case Null:
76-
return new RForeignListWrapper(obj);
76+
return RList.createForeignWrapper(obj, size);
7777
default:
7878
throw RInternalError.shouldNotReachHere();
7979
}

0 commit comments

Comments
 (0)