diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java index 4ba8401d17150968c1b236c0e842c89293352777..10f273d4c701eebe429a74fcd142323a33abc6e5 100644 --- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java +++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java @@ -53,6 +53,7 @@ import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RSource; import com.oracle.truffle.r.runtime.ResourceHandlerFactory; import com.oracle.truffle.r.runtime.builtins.RBuiltinKind; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.runtime.data.RList; import com.oracle.truffle.r.runtime.data.RMissing; import com.oracle.truffle.r.runtime.data.RNull; @@ -224,7 +225,7 @@ class ChimneySweeping extends SingleBuiltinDiagnostics { @Override SingleBuiltinDiagnostics init() throws Throwable { super.init(); - try (Context context = diagSuite.fastRSession.createContext()) { + try (Context context = diagSuite.fastRSession.createContext(ContextKind.SHARE_PARENT_RW)) { execInContext(context, () -> { this.castNodes = builtinFactory.getCastNodes(); print(0, "\n*** Chimney-sweeping of '" + builtinName + "' (" + builtinFactory.getBuiltinMetaClass().getName() + ") ***"); @@ -488,7 +489,7 @@ class ChimneySweeping extends SingleBuiltinDiagnostics { String output; try { - output = diagSuite.fastRSession.eval(null, call, null, false); + output = diagSuite.fastRSession.eval(null, call, ContextKind.SHARE_PARENT_RW, false); } catch (Throwable t) { output = "ERROR: " + t.getMessage(); } diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java index de6153e10db2b5d26cc56a79e5e6068024ba5a2b..e9854ddb6eb5438b69197e626b3c82d5b1404533 100644 --- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java +++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java @@ -31,6 +31,7 @@ import java.util.function.Consumer; import com.oracle.truffle.r.nodes.casts.Samples; import com.oracle.truffle.r.runtime.RDeparse; import com.oracle.truffle.r.runtime.RSource; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.runtime.data.RLanguage; import com.oracle.truffle.r.runtime.data.RNull; import com.oracle.truffle.r.runtime.data.RPairList; @@ -56,7 +57,7 @@ class DefaultArgsExtractor { } Map<String, Samples<?>> extractDefaultArgs(String functionName) { - final Context context = fastRSession.createContext(); + final Context context = fastRSession.createContext(ContextKind.SHARE_PARENT_RW); HashMap<String, Samples<?>> samplesMap = new HashMap<>(); try { diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java index 3ae46c272752a27200964ac334771e80abfbbcfe..34be425167cdd1572e912b7c396e205454c2c087 100644 --- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java +++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java @@ -24,6 +24,8 @@ package com.oracle.truffle.r.nodes.test; import com.oracle.truffle.r.runtime.RSource; import com.oracle.truffle.r.runtime.context.RContext; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; + import org.junit.AfterClass; import org.junit.BeforeClass; @@ -45,7 +47,7 @@ public class TestBase { public static void setupClass() { FastRSession session = FastRSession.create(); testVMContext = session.getContext(); - context = session.createContext(); + context = session.createContext(ContextKind.SHARE_PARENT_RW); } @AfterClass 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 42217d82d1178923b6f69228101d75e606315710..70ac71adbb18674a15aa012ab12f1af4f4a758db 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 @@ -35,6 +35,7 @@ import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.graalvm.polyglot.Engine; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -46,13 +47,11 @@ import com.oracle.truffle.r.runtime.FastROptions; import com.oracle.truffle.r.runtime.RInternalError; import com.oracle.truffle.r.runtime.ResourceHandlerFactory; import com.oracle.truffle.r.runtime.Utils; -import com.oracle.truffle.r.runtime.context.ChildContextInfo; import com.oracle.truffle.r.runtime.context.RContext; import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.generate.FastRSession; import com.oracle.truffle.r.test.generate.GnuROneShotRSession; import com.oracle.truffle.r.test.generate.TestOutputManager; -import org.graalvm.polyglot.Engine; /** * Base class for all unit tests. The unit tests are actually arranged as a collection of @@ -639,7 +638,7 @@ public class TestBase { private boolean evalAndCompare(String[] inputs, TestTrait... traitsList) { WhiteList[] whiteLists = TestTrait.collect(traitsList, WhiteList.class); TestTraitsSet traits = new TestTraitsSet(traitsList); - ChildContextInfo contextInfo = traits.context.contains(Context.NonShared) ? fastROutputManager.fastRSession.createContextInfo(ContextKind.SHARE_NOTHING) : null; + ContextKind contextKind = traits.context.contains(Context.NonShared) ? ContextKind.SHARE_NOTHING : ContextKind.SHARE_PARENT_RW; int index = 1; boolean allOk = true; boolean skipFastREval = traits.isIgnored || generatingExpected(); @@ -648,7 +647,7 @@ public class TestBase { if (skipFastREval) { ignoredInputCount++; } else { - String result = fastREval(input, contextInfo, traits.context.contains(Context.LongTimeout)); + String result = fastREval(input, contextKind, traits.context.contains(Context.LongTimeout)); CheckResult checkResult = checkResult(whiteLists, input, traits.preprocessOutput(expected), traits.preprocessOutput(result), traits); result = checkResult.result; @@ -981,12 +980,13 @@ public class TestBase { * Evaluate {@code input} in FastR, returning all (virtual) console output that was produced. If * {@code nonShared} then this must evaluate in a new, non-shared, {@link RContext}. */ - protected String fastREval(String input, ChildContextInfo contextInfo, boolean longTimeout) { + protected String fastREval(String input, ContextKind contextKind, boolean longTimeout) { + assert contextKind != null; microTestInfo.expression = input; String result; try { beforeEval(); - result = fastROutputManager.fastRSession.eval(this, input, contextInfo, longTimeout); + result = fastROutputManager.fastRSession.eval(this, input, contextKind, longTimeout); } catch (Throwable e) { String clazz; if (e instanceof RInternalError && e.getCause() != null) { @@ -1025,7 +1025,7 @@ public class TestBase { if (generatingExpected()) { return expectedOutputManager.getRSession().eval(null, system2Command, null, true); } else { - return fastROutputManager.fastRSession.eval(null, system2Command, null, true); + return fastROutputManager.fastRSession.eval(null, system2Command, ContextKind.SHARE_PARENT_RW, true); } } diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java index c5201e71ff258f8289ac4fca78add57bdfc7f271..11480f6850f0ceb9bd449a35859d4b706fa457fc 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java @@ -13,6 +13,7 @@ package com.oracle.truffle.r.test.builtins; import org.junit.Assert; import org.junit.Test; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; // Checkstyle: stop line length check @@ -41,12 +42,12 @@ public class TestBuiltin_asmatrix extends TestBase { @Test public void testIgnoredMatrixExpression1() { - Assert.assertEquals(expectedOut1, fastREval("{ matrix(1i,10,10) }", null, false)); + Assert.assertEquals(expectedOut1, fastREval("{ matrix(1i,10,10) }", ContextKind.SHARE_PARENT_RW, false)); } @Test public void testIgnoredMatrixExpression2() { - Assert.assertEquals(expectedOut2, fastREval("{ matrix(c(1i,NA),10,10) }", null, false)); + Assert.assertEquals(expectedOut2, fastREval("{ matrix(c(1i,NA),10,10) }", ContextKind.SHARE_PARENT_RW, false)); } @Test 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 22ac16e2b772ab22f228d0ddc4a8d5e8f5ffd63b..c789f9038505c25a428ece451d8f6901126171d4 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 @@ -22,9 +22,15 @@ */ package com.oracle.truffle.r.test.generate; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharsetDecoder; @@ -34,6 +40,17 @@ import java.util.HashMap; import java.util.Map; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.Callable; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Engine; +import org.graalvm.polyglot.Instrument; +import org.graalvm.polyglot.PolyglotException; +import org.graalvm.polyglot.Source; +import org.graalvm.polyglot.Value; +import org.graalvm.polyglot.proxy.ProxyExecutable; import com.oracle.truffle.api.debug.Debugger; import com.oracle.truffle.api.debug.SuspendedCallback; @@ -53,21 +70,6 @@ import com.oracle.truffle.r.runtime.context.RContext; import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; import com.oracle.truffle.r.test.engine.interop.VectorMRTest; -import java.io.IOException; -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.concurrent.Callable; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.graalvm.polyglot.Context; -import org.graalvm.polyglot.Engine; -import org.graalvm.polyglot.Instrument; -import org.graalvm.polyglot.PolyglotException; -import org.graalvm.polyglot.Source; -import org.graalvm.polyglot.Value; -import org.graalvm.polyglot.proxy.ProxyExecutable; -import static org.junit.Assert.fail; public final class FastRSession implements RSession { @@ -86,6 +88,7 @@ public final class FastRSession implements RSession { private final ByteArrayOutputStream output = new ByteArrayOutputStream(); private final TestByteArrayInputStream input = new TestByteArrayInputStream(); + private Engine mainEngine; private Context mainContext; private RContext mainRContext; @@ -124,27 +127,14 @@ public final class FastRSession implements RSession { return null; } - public ChildContextInfo checkContext(ChildContextInfo contextInfoArg) { - ChildContextInfo contextInfo; - if (contextInfoArg == null) { - contextInfo = createContextInfo(ContextKind.SHARE_PARENT_RW); - } else { - contextInfo = contextInfoArg; - } - return contextInfo; - } - - public ChildContextInfo createContextInfo(ContextKind contextKind) { + public Context createContext(ContextKind contextKind) { 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, mainRContext, input, output, output); + ChildContextInfo ctx = ChildContextInfo.create(params, env, contextKind, contextKind == ContextKind.SHARE_NOTHING ? null : mainRContext, input, output, output); RContext.childInfo = ctx; - return ctx; - } - public Context createContext() { - return Context.newBuilder("R").in(input).out(output).err(output).build(); + return Context.newBuilder("R").engine(mainEngine).build(); } private FastRSession() { @@ -163,7 +153,8 @@ public final class FastRSession implements RSession { RStartParams params = new RStartParams(RCmdOptions.parseArguments(Client.R, new String[]{"R", "--vanilla", "--slave", "--silent", "--no-restore"}, false), false); ChildContextInfo info = ChildContextInfo.create(params, null, ContextKind.SHARE_NOTHING, null, input, output, output); RContext.childInfo = info; - mainContext = createContext(); + mainEngine = Engine.newBuilder().in(input).out(output).err(output).build(); + mainContext = Context.newBuilder("R").engine(mainEngine).build(); mainRContext = mainContext.eval(GET_CONTEXT).asHostObject(); } finally { try { @@ -187,8 +178,8 @@ public final class FastRSession implements RSession { private String readLine() { /* - * We cannot use an InputStreamReader because it buffers characters internally, whereas - * readLine() should not buffer across newlines. + * We cannot use an InputStreamReader because it buffers characters internally, whereas readLine() + * should not buffer across newlines. */ ByteBuffer bytes = ByteBuffer.allocate(16); @@ -219,13 +210,13 @@ public final class FastRSession implements RSession { } @Override - public String eval(TestBase testClass, String expression, ChildContextInfo contextInfo, boolean longTimeout) throws Throwable { + public String eval(TestBase testClass, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable { + assert contextKind != null; Timer timer = null; output.reset(); input.setContents(expression); try { - checkContext(contextInfo); - Context evalContext = createContext(); + Context evalContext = createContext(contextKind); // set up some interop objects used by fastr-specific tests: if (testClass != null) { testClass.addPolyglotSymbols(evalContext); @@ -284,7 +275,7 @@ public final class FastRSession implements RSession { } } - private Throwable getWrappedThrowable(PolyglotException e) { + private static Throwable getWrappedThrowable(PolyglotException e) { Object f = getField(e, "impl"); return (Throwable) getField(f, "exception"); } diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java index 56b29da8081513eb271a2fc2514858827b93e050..5533c299b13ce758d1fc5e7f72de478deb5dd4c0 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java @@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit; import com.oracle.truffle.r.runtime.ProcessOutputManager; import com.oracle.truffle.r.runtime.REnvVars; -import com.oracle.truffle.r.runtime.context.ChildContextInfo; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; /** @@ -91,7 +91,7 @@ public class GnuROneShotRSession implements RSession { } @Override - public String eval(TestBase testBase, String expression, ChildContextInfo contextInfo, boolean longTimeout) throws Throwable { + public String eval(TestBase testBase, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable { if (expression.contains("library(") && !TestBase.generatingExpected()) { System.out.println("=============================================="); System.out.println("LIBRARY LOADING WHEN NOT GENERATING EXPECTED OUTPUT"); diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java index 6783accf7580181e9765119ce13e118424d5a2ba..0796d64b58619b24331fd6793b9b03561424e192 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java @@ -22,7 +22,7 @@ */ package com.oracle.truffle.r.test.generate; -import com.oracle.truffle.r.runtime.context.ChildContextInfo; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; /** @@ -42,7 +42,7 @@ public interface RSession { * This result will always be non-null or an exception will be thrown in, say, a timeout * occurring. */ - String eval(TestBase testClass, String expression, ChildContextInfo contextInfo, boolean longTimeout) throws Throwable; + String eval(TestBase testClass, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable; /** * A name to identify the session. diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java index 31ec7d55f06c8a210638e77b0c02096e4e89a264..fca629270d81dada972c55dac040adc7d8111346 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java @@ -13,6 +13,7 @@ package com.oracle.truffle.r.test.library.base; import org.junit.Assert; import org.junit.Test; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; public class TestSimpleVectors extends TestBase { @@ -1071,19 +1072,21 @@ public class TestSimpleVectors extends TestBase { @Test public void testIgnored1() { Assert.assertEquals(TEST_IGNORED1_EXPECTED_VAL, - fastREval("{ x <- c(a=as.raw(10), b=as.raw(11), c=as.raw(12)) ; x[10] }", null, false)); + fastREval("{ x <- c(a=as.raw(10), b=as.raw(11), c=as.raw(12)) ; x[10] }", ContextKind.SHARE_PARENT_RW, false)); } @Test public void testIgnored2() { Assert.assertEquals(TEST_IGNORED2_EXPECTED_VAL, - fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[s] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", null, false)); + fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[s] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", ContextKind.SHARE_PARENT_RW, + false)); } @Test public void testIgnored3() { Assert.assertEquals(TEST_IGNORED3_EXPECTED_VAL, - fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[c(s,s)] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", null, false)); + fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[c(s,s)] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", + ContextKind.SHARE_PARENT_RW, false)); } @Test diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java index 960017643503cdac94caf9edd6d64db5b2efb39a..59975e1a8e6300a6c12d5e9ae4356dc9a616dcba 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java @@ -30,6 +30,7 @@ import org.junit.Test; import com.oracle.truffle.api.interop.java.JavaInterop; import com.oracle.truffle.r.runtime.conn.SeekableMemoryByteChannel; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; import java.io.File; import org.junit.After; @@ -108,12 +109,12 @@ public class TestInterop extends TestBase { @Test public void testHelp() { - assertHelpResult(fastREval("?as.external.byte", null, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); - assertHelpResult(fastREval("help(as.external.byte)", null, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); - assertHelpResult(fastREval("example(as.external.byte)", null, false), null, "byteClass$valueOf(javaByte)", "[1] 123"); + assertHelpResult(fastREval("?as.external.byte", ContextKind.SHARE_PARENT_RW, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); + assertHelpResult(fastREval("help(as.external.byte)", ContextKind.SHARE_PARENT_RW, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); + assertHelpResult(fastREval("example(as.external.byte)", ContextKind.SHARE_PARENT_RW, false), null, "byteClass$valueOf(javaByte)", "[1] 123"); } - private void assertHelpResult(String result, String startsWith, String... contains) { + private static void assertHelpResult(String result, String startsWith, String... contains) { if (startsWith != null) { assertTrue(result.startsWith(startsWith)); }