Skip to content

Commit 56060ab

Browse files
committed
implemented createEmptySameType/copyResized/WithDimensions in RForeignWrapper-s - used from cbind/rbind/matrix
1 parent f7cd402 commit 56060ab

File tree

11 files changed

+784
-725
lines changed

11 files changed

+784
-725
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@
2222
*/
2323
package com.oracle.truffle.r.nodes.unary;
2424

25+
import com.oracle.truffle.api.dsl.Cached;
26+
import com.oracle.truffle.api.dsl.ImportStatic;
2527
import com.oracle.truffle.api.dsl.Specialization;
28+
import com.oracle.truffle.api.interop.TruffleObject;
2629
import com.oracle.truffle.r.runtime.RError;
30+
import com.oracle.truffle.r.runtime.RRuntime;
2731
import com.oracle.truffle.r.runtime.data.RDataFactory;
2832
import com.oracle.truffle.r.runtime.data.RFunction;
2933
import com.oracle.truffle.r.runtime.data.RMissing;
3034
import com.oracle.truffle.r.runtime.data.RNull;
3135
import com.oracle.truffle.r.runtime.data.RS4Object;
3236
import com.oracle.truffle.r.runtime.data.model.RAbstractVector;
37+
import com.oracle.truffle.r.runtime.env.REnvironment;
38+
import com.oracle.truffle.r.runtime.interop.ForeignArray2R;
3339

40+
@ImportStatic(RRuntime.class)
3441
public abstract class CastToVectorNode extends CastNode {
3542

3643
private final boolean preserveNonVector;
@@ -81,6 +88,29 @@ protected RAbstractVector cast(@SuppressWarnings("unused") RS4Object s4obj) {
8188
throw error(RError.Message.CANNOT_COERCE_S4_TO_VECTOR);
8289
}
8390

91+
@Specialization
92+
protected Object cast(REnvironment env) {
93+
if (preserveNonVector) {
94+
return env;
95+
} else {
96+
return RDataFactory.createList();
97+
}
98+
}
99+
100+
@Specialization(guards = "isForeignObject(truffleObject)")
101+
protected Object castForeign(TruffleObject truffleObject,
102+
@Cached("create()") ForeignArray2R foreignArray2R) {
103+
if (preserveNonVector) {
104+
return truffleObject;
105+
} else {
106+
Object o = foreignArray2R.convert(truffleObject);
107+
if (!RRuntime.isForeignObject(o)) {
108+
return o;
109+
}
110+
}
111+
throw error(RError.Message.CANNOT_COERCE_EXTERNAL_OBJECT_TO_VECTOR, "vector");
112+
}
113+
84114
public static CastToVectorNode create() {
85115
return CastToVectorNodeGen.create(false);
86116
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ protected int doForeignArray(TruffleObject obj, boolean recursive,
268268
if (context.getEnv().isHostObject(obj)) {
269269
Object o = context.getEnv().asHostObject(obj);
270270
Class<?> ct = o.getClass().getComponentType();
271-
int prc = getPrecedence(ct, recursive);
271+
int prc = getPrecedence(ct);
272272
if (prc != -1) {
273273
return prc;
274274
}
@@ -290,14 +290,10 @@ protected int doForeignArray(TruffleObject obj, boolean recursive,
290290
}
291291

292292
@TruffleBoundary
293-
private int getPrecedence(Class<?> ct, boolean recursive) {
294-
if (recursive && ct != null && ct.isArray()) {
295-
return getPrecedence(ct.getComponentType(), true);
293+
private int getPrecedence(Class<?> ct) {
294+
if (ct != null && ct.isArray()) {
295+
return getPrecedence(ct.getComponentType());
296296
}
297-
return getPrecedence(ct);
298-
}
299-
300-
private static int getPrecedence(Class<?> ct) {
301297
if (ct == Integer.class || ct == Byte.class || ct == Short.class || ct == int.class || ct == byte.class || ct == short.class) {
302298
return INT_PRECEDENCE;
303299
} else if (ct == Double.class || ct == Float.class || ct == Long.class || ct == double.class || ct == float.class || ct == long.class) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ private static byte unbox(TruffleObject value, ClassCastException e) throws Runt
8787
throw RInternalError.shouldNotReachHere(e);
8888
}
8989

90+
@Override
91+
public RVector<?> createEmptySameType(int newLength, boolean newIsComplete) {
92+
return RDataFactory.createLogicalVector(new byte[newLength], newIsComplete);
93+
}
94+
9095
private static final class FastPathAccess extends FastPathFromLogicalAccess {
9196

9297
FastPathAccess(RAbstractContainer value) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ private static double unbox(TruffleObject value, ClassCastException e) throws Ru
8888
throw RInternalError.shouldNotReachHere(e);
8989
}
9090

91+
@Override
92+
public RVector<?> createEmptySameType(int newLength, boolean newIsComplete) {
93+
return RDataFactory.createDoubleVector(new double[newLength], newIsComplete);
94+
}
95+
9196
private static final class FastPathAccess extends FastPathFromDoubleAccess {
9297

9398
FastPathAccess(RAbstractContainer value) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ private static int unbox(TruffleObject value, ClassCastException e) throws Runti
8888
throw RInternalError.shouldNotReachHere(e);
8989
}
9090

91+
@Override
92+
public RVector<?> createEmptySameType(int newLength, boolean newIsComplete) {
93+
return RDataFactory.createIntVector(new int[newLength], newIsComplete);
94+
}
95+
9196
private static final class FastPathAccess extends FastPathFromIntAccess {
9297

9398
FastPathAccess(RAbstractContainer value) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ private static String getString(TruffleObject truffleObjectIn, int index) throws
8585
}
8686
}
8787

88+
@Override
89+
public RVector<?> createEmptySameType(int newLength, boolean newIsComplete) {
90+
return RDataFactory.createStringVector(new String[newLength], newIsComplete);
91+
}
92+
8893
private static final class FastPathAccess extends FastPathFromStringAccess {
8994

9095
FastPathAccess(RAbstractContainer value) {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@ public final RAbstractVector copy() {
9797

9898
@Override
9999
public final RAbstractVector copyDropAttributes() {
100-
throw RInternalError.shouldNotReachHere();
100+
return copy();
101101
}
102102

103103
@Override
104104
public final RAbstractVector copyWithNewDimensions(int[] newDimensions) {
105-
throw RInternalError.shouldNotReachHere();
105+
RAbstractVector res = copy();
106+
res.setDimensions(newDimensions);
107+
return res;
106108
}
107109

108110
@Override
@@ -195,16 +197,18 @@ public Object getInternalStore() {
195197

196198
@Override
197199
public final RVector<?> copyResized(int size, boolean fillNA) {
198-
throw RInternalError.shouldNotReachHere();
200+
RAbstractVector v = copy();
201+
return v.copyResized(size, fillNA);
199202
}
200203

201204
@Override
202-
public final RVector<?> copyResizedWithDimensions(int[] newDimensions, boolean fillNA) {
203-
throw RInternalError.shouldNotReachHere();
205+
public RVector<?> copyResizedWithDimensions(int[] newDimensions, boolean fillNA) {
206+
RAbstractVector v = copy();
207+
return v.copyResizedWithDimensions(newDimensions, fillNA);
204208
}
205209

206210
@Override
207-
public final RVector<?> createEmptySameType(int newLength, boolean newIsComplete) {
211+
public RVector<?> createEmptySameType(int newLength, boolean newIsComplete) {
208212
throw RInternalError.shouldNotReachHere();
209213
}
210214

0 commit comments

Comments
 (0)