Skip to content

Commit 9d71429

Browse files
committed
[GR-2798] A few smaller fixes in interop.
PullRequest: fastr/1752
2 parents 2676cbe + ce744e5 commit 9d71429

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/R/polyglot.value_overrides.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,5 @@ as.array.polyglot.value <- function(x, ...) {
3232
as.data.frame.polyglot.value <- function(x, row.names = NULL, optional = FALSE, ..., nm = paste(deparse(substitute(x),
3333
width.cutoff = 500L), collapse = " ")) {
3434
v <- .fastr.interop.asVector(x, recursive=TRUE, dropDimensions=FALSE)
35-
if (!optional && !is.null(v) && is.atomic(v)) {
36-
v <- list(v)
37-
names(v) <- nm
38-
}
39-
as.data.frame(v, row.names=row.names, optional=optional, ...)
35+
as.data.frame(v, row.names=row.names, optional=optional, nm=nm, ...)
4036
}

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Transpose.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
import com.oracle.truffle.api.dsl.Cached;
2828
import com.oracle.truffle.api.dsl.Fallback;
29+
import com.oracle.truffle.api.dsl.ImportStatic;
2930
import com.oracle.truffle.api.dsl.Specialization;
31+
import com.oracle.truffle.api.interop.TruffleObject;
3032
import com.oracle.truffle.api.object.DynamicObject;
3133
import com.oracle.truffle.api.profiles.BranchProfile;
3234
import com.oracle.truffle.api.profiles.LoopConditionProfile;
@@ -43,6 +45,7 @@
4345
import com.oracle.truffle.r.nodes.profile.VectorLengthProfile;
4446
import com.oracle.truffle.r.runtime.RError.Message;
4547
import com.oracle.truffle.r.runtime.RInternalError;
48+
import com.oracle.truffle.r.runtime.RRuntime;
4649
import com.oracle.truffle.r.runtime.RType;
4750
import com.oracle.truffle.r.runtime.builtins.RBuiltin;
4851
import com.oracle.truffle.r.runtime.data.RDataFactory;
@@ -56,9 +59,11 @@
5659
import com.oracle.truffle.r.runtime.data.nodes.VectorAccess;
5760
import com.oracle.truffle.r.runtime.data.nodes.VectorAccess.RandomIterator;
5861
import com.oracle.truffle.r.runtime.data.nodes.VectorReuse;
62+
import com.oracle.truffle.r.runtime.interop.ConvertForeignObjectNode;
5963
import com.oracle.truffle.r.runtime.nodes.RBaseNode;
6064

6165
@RBuiltin(name = "t.default", kind = INTERNAL, parameterNames = {"x"}, behavior = PURE)
66+
@ImportStatic({RRuntime.class, ConvertForeignObjectNode.class})
6267
public abstract class Transpose extends RBuiltinNode.Arg1 {
6368

6469
private final BranchProfile hasDimNamesProfile = BranchProfile.create();
@@ -80,6 +85,10 @@ public abstract class Transpose extends RBuiltinNode.Arg1 {
8085
Casts.noCasts(Transpose.class);
8186
}
8287

88+
protected static Transpose create() {
89+
return TransposeNodeGen.create();
90+
}
91+
8392
public abstract Object execute(RAbstractVector o);
8493

8594
protected boolean isSquare(RAbstractVector vector) {
@@ -274,6 +283,17 @@ private void convertNamesToDimnames(RAbstractVector source, RAbstractVector dest
274283
}
275284
}
276285

286+
@Specialization(guards = {"isForeignObject(x)"})
287+
protected Object transposeForeign(TruffleObject x,
288+
@Cached("create()") ConvertForeignObjectNode convertForeign,
289+
@Cached("create()") Transpose recursive) {
290+
if (convertForeign.isForeignArray(x)) {
291+
RAbstractVector vec = (RAbstractVector) convertForeign.convert(x);
292+
return recursive.execute(vec);
293+
}
294+
throw error(Message.ARGUMENT_NOT_MATRIX);
295+
}
296+
277297
@Fallback
278298
protected RVector<?> transpose(@SuppressWarnings("unused") Object x) {
279299
throw error(Message.ARGUMENT_NOT_MATRIX);

com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/control/RLengthNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@
4242
import com.oracle.truffle.r.runtime.data.RSymbol;
4343
import com.oracle.truffle.r.runtime.data.model.RAbstractContainer;
4444
import com.oracle.truffle.r.runtime.env.REnvironment;
45-
import com.oracle.truffle.r.runtime.interop.ConvertForeignObjectNode;
4645
import com.oracle.truffle.r.runtime.nodes.RBaseNode;
4746

4847
/**
4948
* Gets length of given container. Does not actually dispatch to the 'length' function, which may be
5049
* overridden for some S3/S4 classes. Check if you need to get actual length, or what the 'length'
5150
* function returns, like in {@code seq_along}.
5251
*/
53-
@ImportStatic({Message.class, ConvertForeignObjectNode.class})
52+
@ImportStatic({Message.class})
5453
public abstract class RLengthNode extends RBaseNode {
5554

5655
public abstract int executeInteger(Object value);

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154239,6 +154239,22 @@ Error in .fastr.interop.asChar(97L, 1) :
154239154239
#if (!any(R.version$engine == "FastR")) { 32767 } else { v <- .fastr.interop.asShort(32767); v; }
154240154240
[1] 32767
154241154241

154242+
##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testTranspose#
154243+
#if (!any(R.version$engine == "FastR")) { cat('Error in t.default(ta) : argument is not a matrix', '<<<NEWLINE>>>') } else { ta <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestArraysClass'); t(ta) }
154244+
Error in t.default(ta) : argument is not a matrix
154245+
154246+
##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testTranspose#
154247+
#if (!any(R.version$engine == "FastR")) { t(c('a', 'b', 'c')) } else { ta <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestArraysClass'); t(ta$stringArray) }
154248+
[,1] [,2] [,3]
154249+
[1,] "a" "b" "c"
154250+
154251+
##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testTranspose#
154252+
#if (!any(R.version$engine == "FastR")) { t(matrix(c('a', 'a', 'b', 'b', 'c', 'c'), c(2, 3))) } else { ta <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestArraysClass'); t(ta$stringArray2) }
154253+
[,1] [,2]
154254+
[1,] "a" "a"
154255+
[2,] "b" "b"
154256+
[3,] "c" "c"
154257+
154242154258
##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testUnlist#
154243154259
#if (!any(R.version$engine == "FastR")) { 'character' } else { ta <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestArraysClass'); class(unlist(ta$charArray)) }
154244154260
[1] "character"

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ public void testIdentical() {
656656
assertEvalFastR("b1 <- .fastr.interop.asByte(1); s1 <- .fastr.interop.asShort(1); identical(b1, s1)", "FALSE");
657657
}
658658

659+
@Test
660+
public void testTranspose() {
661+
assertEvalFastR(CREATE_TEST_ARRAYS + " t(ta$stringArray)", "t(c('a', 'b', 'c'))");
662+
assertEvalFastR(CREATE_TEST_ARRAYS + " t(ta$stringArray2)", "t(matrix(c('a', 'a', 'b', 'b', 'c', 'c'), c(2, 3)))");
663+
assertEvalFastR(CREATE_TEST_ARRAYS + " t(ta)", errorIn("t.default(ta)", "argument is not a matrix"));
664+
}
665+
659666
@Test
660667
public void testAsVectorFromArray() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
661668
testAsVectorFromArray("fieldStaticBooleanArray", "logical");

0 commit comments

Comments
 (0)