From ddda35a56669ffee302455709734c1adfc26c21f Mon Sep 17 00:00:00 2001 From: Tomas Stupka <tomas.stupka@oracle.com> Date: Mon, 9 Apr 2018 14:35:59 +0200 Subject: [PATCH] a few tests to check the 'new' builtin with java interop disabled --- .../truffle/r/test/ExpectedTestOutput.test | 74 +++++++++++-------- .../com/oracle/truffle/r/test/TestBase.java | 9 ++- .../truffle/r/test/generate/FastRSession.java | 12 ++- .../r/test/library/fastr/TestJavaInterop.java | 7 +- 4 files changed, 67 insertions(+), 35 deletions(-) diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test index 9361a87061..36ae8fe3d5 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test @@ -150895,6 +150895,50 @@ NULL #if (!any(R.version$engine == "FastR")) { c(TRUE, TRUE) } else { tc <- java.type('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestNamesClass'); c('staticField', 'staticMethod') %in% names(tc) } [1] TRUE TRUE +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { 2147483647 } else { tc <- java.type('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass'); to <- new(tc); to$fieldInteger } +[1] 2147483647 + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { 2147483647 } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass'); to$fieldStaticInteger } +[1] 2147483647 + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { TRUE } else { tc <- java.type('java.lang.Boolean'); to <- new(tc, TRUE); to } +[1] TRUE + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { TRUE } else { to <- new('java.lang.Boolean', TRUE); to } +[1] TRUE + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { TRUE } else { to <- new('java/lang/Boolean', TRUE); to } +[1] TRUE + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { cat('Error in .fastr.interop.new(Class, ...) :', '<<<NEWLINE>>>', ' error during Java object instantiation', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass'); new(to) } +Error in .fastr.interop.new(Class, ...) : + error during Java object instantiation + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew#Context.NoJavaInterop# +#if (!any(R.version$engine == "FastR")) { cat('Error in getClass(Class, where = topenv(parent.frame())) :', '<<<NEWLINE>>>', ' “__bogus_class_name__†is not a defined class', '<<<NEWLINE>>>') } else { new('__bogus_class_name__'); } +Error in getClass(Class, where = topenv(parent.frame())) : + “__bogus_class_name__†is not a defined class + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew# +#if (!any(R.version$engine == "FastR")) { cat('Error in getClass(Class, where = topenv(parent.frame())) :', '<<<NEWLINE>>>', ' “__bogus_class_name__†is not a defined class', '<<<NEWLINE>>>') } else { to <- new('__bogus_class_name__'); } +Error in getClass(Class, where = topenv(parent.frame())) : + “__bogus_class_name__†is not a defined class + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew#Context.NoJavaInterop# +#if (!any(R.version$engine == "FastR")) { cat('Error in getClass(Class, where = topenv(parent.frame())) :', '<<<NEWLINE>>>', ' “java.lang.Boolean†is not a defined class', '<<<NEWLINE>>>') } else { new('java.lang.Boolean'); } +Error in getClass(Class, where = topenv(parent.frame())) : + “java.lang.Boolean†is not a defined class + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNew#Context.NoJavaInterop# +#if (!any(R.version$engine == "FastR")) { cat('integer(0)' } else { new('integer'); } +Error: unexpected '}' in "if (!any(R.version$engine == "FastR")) { cat('integer(0)' }" + ##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewArray# #if (!any(R.version$engine == "FastR")) { '[B' } else { a <- new(java.type('byte[]'), 10L); a$getClass()$getName(); } [1] "[B" @@ -151399,36 +151443,6 @@ NULL #if (!any(R.version$engine == "FastR")) { TRUE } else { a <- new(java.type('short[][]'), c(2L, 3L)); is.polyglot.value(a) && length(a) > 0 } [1] TRUE -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { 2147483647 } else { tc <- java.type('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass'); to <- new(tc); to$fieldInteger } -[1] 2147483647 - -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { 2147483647 } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass'); to$fieldStaticInteger } -[1] 2147483647 - -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { TRUE } else { tc <- java.type('java.lang.Boolean'); to <- new(tc, TRUE); to } -[1] TRUE - -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { TRUE } else { to <- new('java.lang.Boolean', TRUE); to } -[1] TRUE - -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { TRUE } else { to <- new('java/lang/Boolean', TRUE); to } -[1] TRUE - -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { cat('Error in .fastr.interop.new(Class, ...) :', '<<<NEWLINE>>>', ' error during Java object instantiation', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass'); new(to) } -Error in .fastr.interop.new(Class, ...) : - error during Java object instantiation - -##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNewWithJavaClass# -#if (!any(R.version$engine == "FastR")) { cat('Error in getClass(Class, where = topenv(parent.frame())) :', '<<<NEWLINE>>>', ' “__bogus_class_name__†is not a defined class', '<<<NEWLINE>>>') } else { to <- new('__bogus_class_name__'); } -Error in getClass(Class, where = topenv(parent.frame())) : - “__bogus_class_name__†is not a defined class - ##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testNonPrimitiveParameter# #if (!any(R.version$engine == "FastR")) { TRUE } else { to <- .fastr.interop.new(java.type('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass')); to$equals(to) } [1] TRUE diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java index 8dd00386cd..7e3bdde2b5 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java @@ -142,6 +142,7 @@ public class TestBase { public enum Context implements TestTrait { NonShared, // Test requires a new non-shared {@link RContext}. + NoJavaInterop, // Test requires a {@link RContext} with disabled host access. LongTimeout; // Test requires a long timeout @Override @@ -647,7 +648,7 @@ public class TestBase { if (skipFastREval) { ignoredInputCount++; } else { - String result = fastREval(input, contextKind, traits.context.contains(Context.LongTimeout)); + String result = fastREval(input, contextKind, traits.context.contains(Context.LongTimeout), !traits.context.contains(Context.NoJavaInterop)); CheckResult checkResult = checkResult(whiteLists, input, traits.preprocessOutput(expected), traits.preprocessOutput(result), traits); result = checkResult.result; @@ -981,12 +982,16 @@ public class TestBase { * {@code nonShared} then this must evaluate in a new, non-shared, {@link RContext}. */ protected String fastREval(String input, ContextKind contextKind, boolean longTimeout) { + return fastREval(input, contextKind, longTimeout, true); + } + + protected String fastREval(String input, ContextKind contextKind, boolean longTimeout, boolean allowHostAccess) { assert contextKind != null; microTestInfo.expression = input; String result; try { beforeEval(); - result = fastROutputManager.fastRSession.eval(this, input, contextKind, longTimeout); + result = fastROutputManager.fastRSession.eval(this, input, contextKind, longTimeout, allowHostAccess); } catch (Throwable e) { String clazz; if (e instanceof RInternalError && e.getCause() != null) { diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java index 9e521c524e..5c9171977d 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java @@ -128,13 +128,17 @@ public final class FastRSession implements RSession { } public Context createContext(ContextKind contextKind) { + return createContext(contextKind, true); + } + + public Context createContext(ContextKind contextKind, boolean allowHostAccess) { RStartParams params = new RStartParams(RCmdOptions.parseArguments(Client.R, new String[]{"R", "--vanilla", "--slave", "--silent", "--no-restore"}, false), false); Map<String, String> env = new HashMap<>(); env.put("TZ", "GMT"); ChildContextInfo ctx = ChildContextInfo.create(params, env, contextKind, contextKind == ContextKind.SHARE_NOTHING ? null : mainRContext, input, output, output); RContext.childInfo = ctx; - return Context.newBuilder("R", "llvm").allowAllAccess(true).engine(mainEngine).build(); + return Context.newBuilder("R", "llvm").allowAllAccess(true).allowHostAccess(allowHostAccess).engine(mainEngine).build(); } private FastRSession() { @@ -211,12 +215,16 @@ public final class FastRSession implements RSession { @Override public String eval(TestBase testClass, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable { + return eval(testClass, expression, contextKind, longTimeout, true); + } + + public String eval(TestBase testClass, String expression, ContextKind contextKind, boolean longTimeout, boolean allowHostAccess) throws Throwable { assert contextKind != null; Timer timer = null; output.reset(); input.setContents(expression); try { - Context evalContext = createContext(contextKind); + Context evalContext = createContext(contextKind, allowHostAccess); // set up some interop objects used by fastr-specific tests: if (testClass != null) { testClass.addPolyglotSymbols(evalContext); diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java index 7ba1df53d5..e5308bf38c 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java @@ -314,7 +314,7 @@ public class TestJavaInterop extends TestBase { } @Test - public void testNewWithJavaClass() { + public void testNew() { assertEvalFastR("tc <- java.type('" + Boolean.class.getName() + "'); to <- new(tc, TRUE); to", "TRUE"); assertEvalFastR("tc <- java.type('" + TEST_CLASS + "'); to <- new(tc); to$fieldInteger", getRValue(Integer.MAX_VALUE)); @@ -326,6 +326,11 @@ public class TestJavaInterop extends TestBase { assertEvalFastR("to <- new('" + TEST_CLASS + "'); new(to)", errorIn(".fastr.interop.new(Class, ...)", "error during Java object instantiation")); assertEvalFastR("to <- new('__bogus_class_name__');", errorIn("getClass(Class, where = topenv(parent.frame()))", "“__bogus_class_name__†is not a defined class")); + + assertEvalFastR(Context.NoJavaInterop, "new('integer'); ", "cat('integer(0)'"); + assertEvalFastR(Context.NoJavaInterop, "new('" + Boolean.class.getName() + "');", + errorIn("getClass(Class, where = topenv(parent.frame()))", "“" + Boolean.class.getName() + "†is not a defined class")); + assertEvalFastR(Context.NoJavaInterop, "new('__bogus_class_name__');", errorIn("getClass(Class, where = topenv(parent.frame()))", "“__bogus_class_name__†is not a defined class")); } @Test -- GitLab