Skip to content

Commit 708dee7

Browse files
committed
TruffleRLanguage#toString: do not print scalar NA as vector
1 parent 348edd7 commit 708dee7

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ protected String toString(RContext context, Object value) {
150150
return value.toString();
151151
}
152152

153+
// special class designated to exchange NA values with the outside world
154+
// this value is a scalar, the only way to get it is via getArrayMember on an R vector
155+
if (value instanceof RInteropNA) {
156+
return "NA";
157+
}
158+
153159
// the debugger also passes result of TruffleRLanguage.findMetaObject() to this method
154160
Object unwrapped = value;
155161
// print promises by other means than the "print" function to avoid evaluating them
@@ -165,10 +171,6 @@ protected String toString(RContext context, Object value) {
165171
if (RMissingHelper.isMissing(unwrapped)) {
166172
return "missing";
167173
}
168-
// special class designated to exchange NA values with the outside world
169-
if (unwrapped instanceof RInteropNA) {
170-
unwrapped = ((RInteropNA) unwrapped).getValue();
171-
}
172174

173175
// the value unwrapped from an RPromise can be primitive Java type, but now we know that we
174176
// are dealing with primitive that is supposed to be treated as R vector

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/tck/JavaEmbeddingTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public void testToString() {
6060
assertEquals("[1] 1", context.eval("R", "1").toString());
6161
assertEquals("[1] TRUE", context.eval("R", "TRUE").toString());
6262
assertEquals("[1] NA", context.eval("R", "NA").toString());
63+
// NA scalar value:
64+
assertEquals("NA", context.eval("R", "NA").getArrayElement(0).toString());
6365
// @formatter:off
6466
String dataFrameExpected =
6567
" x y\n" +

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/tck/ToStringTesterInstrument.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ protected void onEnter(VirtualFrame frame) {
4545
testData.byteAsString = env.toString(rLanguage, (byte) 42);
4646
testData.doubleAsString = env.toString(rLanguage, 42.5);
4747
testData.stringAsString = env.toString(rLanguage, "Hello");
48-
testData.booleanAsString = env.toString(rLanguage, true);
48+
testData.trueAsString = env.toString(rLanguage, true);
49+
testData.falseAsString = env.toString(rLanguage, false);
4950
}
5051
});
5152
}
@@ -55,6 +56,7 @@ public static final class TestData {
5556
public String byteAsString;
5657
public String doubleAsString;
5758
public String stringAsString;
58-
public String booleanAsString;
59+
public String trueAsString;
60+
public String falseAsString;
5961
}
6062
}

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/tck/TruffleRLanguageTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public void testToString() {
5454
assertEquals("42", testData.byteAsString);
5555
assertEquals("42.5", testData.doubleAsString);
5656
assertEquals("Hello", testData.stringAsString);
57-
assertEquals("true", testData.booleanAsString);
57+
assertEquals("TRUE", testData.trueAsString);
58+
assertEquals("FALSE", testData.falseAsString);
5859
}
5960

6061
}

0 commit comments

Comments
 (0)