From 220fd7d9eb6d65f1470cdf18fd7440a402781f28 Mon Sep 17 00:00:00 2001 From: Tomas Stupka <tomas.stupka@oracle.com> Date: Wed, 11 Apr 2018 15:16:29 +0200 Subject: [PATCH] in eval.polyglot create source with langId instead of mime-type --- .../r/nodes/builtin/fastr/FastRInterop.java | 54 +++++++++---------- .../truffle/r/test/ExpectedTestOutput.test | 16 +++--- .../r/test/library/fastr/TestInterop.java | 14 ++--- .../src/com/oracle/truffle/r/Main.java | 2 - 4 files changed, 42 insertions(+), 44 deletions(-) diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java index 1fa64d23cd..554c855c75 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java @@ -116,12 +116,12 @@ public class FastRInterop { isTesting = true; } - @RBuiltin(name = "eval.polyglot", visibility = CUSTOM, kind = PRIMITIVE, parameterNames = {"mimeType", "source", "path"}, behavior = COMPLEX) + @RBuiltin(name = "eval.polyglot", visibility = CUSTOM, kind = PRIMITIVE, parameterNames = {"languageId", "source", "path"}, behavior = COMPLEX) public abstract static class Eval extends RBuiltinNode.Arg3 { static { Casts casts = new Casts(Eval.class); - casts.arg("mimeType").allowMissing().mustBe(stringValue()).asStringVector().mustBe(singleElement()).findFirst(); + casts.arg("languageId").allowMissing().mustBe(stringValue()).asStringVector().mustBe(singleElement()).findFirst(); casts.arg("source").allowMissing().mustBe(stringValue()).asStringVector().mustBe(singleElement()).findFirst(); casts.arg("path").allowMissing().mustBe(stringValue()).asStringVector().mustBe(singleElement()).findFirst(); } @@ -129,16 +129,16 @@ public class FastRInterop { @Child private SetVisibilityNode setVisibilityNode = SetVisibilityNode.create(); @Child private Foreign2R foreign2rNode = Foreign2R.create(); - protected DirectCallNode createCall(String mimeType, String source) { - return Truffle.getRuntime().createDirectCallNode(parse(mimeType, source)); + protected DirectCallNode createCall(String languageId, String source) { + return Truffle.getRuntime().createDirectCallNode(parse(languageId, source)); } @SuppressWarnings("unused") - @Specialization(guards = {"cachedMimeType != null", "cachedMimeType.equals(mimeType)", "cachedSource != null", "cachedSource.equals(source)"}) - protected Object evalCached(VirtualFrame frame, String mimeType, String source, RMissing path, - @Cached("mimeType") String cachedMimeType, + @Specialization(guards = {"cachedLanguageId != null", "cachedLanguageId.equals(languageId)", "cachedSource != null", "cachedSource.equals(source)"}) + protected Object evalCached(VirtualFrame frame, String languageId, String source, RMissing path, + @Cached("languageId") String cachedLanguageId, @Cached("source") String cachedSource, - @Cached("createCall(mimeType, source)") DirectCallNode call) { + @Cached("createCall(languageId, source)") DirectCallNode call) { try { return foreign2rNode.execute(call.call(EMPTY_OBJECT_ARRAY)); } finally { @@ -147,29 +147,29 @@ public class FastRInterop { } @Specialization(replaces = "evalCached") - protected Object eval(VirtualFrame frame, String mimeType, String source, @SuppressWarnings("unused") RMissing path) { + protected Object eval(VirtualFrame frame, String languageId, String source, @SuppressWarnings("unused") RMissing path) { try { - return foreign2rNode.execute(parseAndCall(source, mimeType)); + return foreign2rNode.execute(parseAndCall(source, languageId)); } finally { setVisibilityNode.execute(frame, true); } } @TruffleBoundary - private Object parseAndCall(String source, String mimeType) { - return parse(mimeType, source).call(); + private Object parseAndCall(String source, String languageId) { + return parse(languageId, source).call(); } @Specialization() @TruffleBoundary - protected Object eval(@SuppressWarnings("unused") RMissing mimeType, @SuppressWarnings("unused") String source, @SuppressWarnings("unused") RMissing path) { - throw RError.error(this, RError.Message.INVALID_ARG, "mimeType"); + protected Object eval(@SuppressWarnings("unused") RMissing languageId, @SuppressWarnings("unused") String source, @SuppressWarnings("unused") RMissing path) { + throw RError.error(this, RError.Message.INVALID_ARG, "languageId"); } - protected CallTarget parse(String mimeType, String source) { + protected CallTarget parse(String languageId, String source) { CompilerAsserts.neverPartOfCompilation(); - Source sourceObject = RSource.fromTextInternalInvisible(source, RSource.Internal.EVAL_WRAPPER, mimeType); + Source sourceObject = Source.newBuilder(source).name(RSource.Internal.EVAL_WRAPPER.string).language(languageId).internal().build(); try { return RContext.getInstance().getEnv().parse(sourceObject); } catch (Throwable t) { @@ -178,25 +178,25 @@ public class FastRInterop { } @Specialization - protected Object eval(VirtualFrame frame, String mimeType, @SuppressWarnings("unused") String source, String path) { + protected Object eval(VirtualFrame frame, String languageId, @SuppressWarnings("unused") String source, String path) { try { - return foreign2rNode.execute(parseFileAndCall(path, mimeType)); + return foreign2rNode.execute(parseFileAndCall(path, languageId)); } finally { setVisibilityNode.execute(frame, false); } } @Specialization - protected Object eval(VirtualFrame frame, String mimeType, @SuppressWarnings("unused") RMissing source, String path) { + protected Object eval(VirtualFrame frame, String languageId, @SuppressWarnings("unused") RMissing source, String path) { try { - return foreign2rNode.execute(parseFileAndCall(path, mimeType)); + return foreign2rNode.execute(parseFileAndCall(path, languageId)); } finally { setVisibilityNode.execute(frame, false); } } @Specialization - protected Object eval(VirtualFrame frame, @SuppressWarnings("unused") RMissing mimeType, @SuppressWarnings("unused") RMissing source, String path) { + protected Object eval(VirtualFrame frame, @SuppressWarnings("unused") RMissing languageId, @SuppressWarnings("unused") RMissing source, String path) { try { return foreign2rNode.execute(parseFileAndCall(path, null)); } finally { @@ -205,18 +205,18 @@ public class FastRInterop { } @TruffleBoundary - private Object parseFileAndCall(String path, String mimeType) { - return parseFile(path, mimeType).call(); + private Object parseFileAndCall(String path, String languageId) { + return parseFile(path, languageId).call(); } - protected CallTarget parseFile(String path, String mimeType) { + protected CallTarget parseFile(String path, String languageId) { CompilerAsserts.neverPartOfCompilation(); File file = new File(path); try { Builder<IOException, RuntimeException, RuntimeException> sourceBuilder = Source.newBuilder(file).name(file.getName()).internal(); - if (mimeType != null) { - sourceBuilder.mimeType(mimeType); + if (languageId != null) { + sourceBuilder.language(languageId); } Source sourceObject = sourceBuilder.build(); return RContext.getInstance().getEnv().parse(sourceObject); @@ -229,7 +229,7 @@ public class FastRInterop { @Specialization @TruffleBoundary - protected Object eval(@SuppressWarnings("unused") RMissing source, @SuppressWarnings("unused") RMissing mimeType, @SuppressWarnings("unused") RMissing path) { + protected Object eval(@SuppressWarnings("unused") RMissing source, @SuppressWarnings("unused") RMissing languageId, @SuppressWarnings("unused") RMissing path) { throw RError.error(this, RError.Message.INVALID_ARG, "'source' or 'path'"); } } 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 36ae8fe3d5..aa4e0e0934 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 @@ -143218,23 +143218,23 @@ NULL [1] "Hello, World!" "second line" ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEval# -#if (!any(R.version$engine == "FastR")) { 1 } else { eval.polyglot('application/x-r', '1') } +#if (!any(R.version$engine == "FastR")) { 1 } else { eval.polyglot('R', '1') } [1] 1 ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEval# -#if (!any(R.version$engine == "FastR")) { 16 } else { eval.polyglot('application/x-r', '14 + 2') } +#if (!any(R.version$engine == "FastR")) { 16 } else { eval.polyglot('R', '14 + 2') } [1] 16 ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEval# -#if (!any(R.version$engine == "FastR")) { 1L } else { eval.polyglot('application/x-r', '1L') } +#if (!any(R.version$engine == "FastR")) { 1L } else { eval.polyglot('R', '1L') } [1] 1 ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEval# -#if (!any(R.version$engine == "FastR")) { TRUE } else { eval.polyglot('application/x-r', 'TRUE') } +#if (!any(R.version$engine == "FastR")) { TRUE } else { eval.polyglot('R', 'TRUE') } [1] TRUE ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEval# -#if (!any(R.version$engine == "FastR")) { as.character(123) } else { eval.polyglot('application/x-r', 'as.character(123)') } +#if (!any(R.version$engine == "FastR")) { as.character(123) } else { eval.polyglot('R', 'as.character(123)') } [1] "123" ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEvalFile# @@ -143242,15 +143242,15 @@ NULL Error in eval.polyglot() : invalid 'source' or 'path' argument ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEvalFile# -#if (!any(R.version$engine == "FastR")) { cat('Error in eval.polyglot(, "abc", ) : invalid mimeType argument\n') } else { eval.polyglot(,'abc',) } -Error in eval.polyglot(, "abc", ) : invalid mimeType argument +#if (!any(R.version$engine == "FastR")) { cat('Error in eval.polyglot(, "abc", ) : invalid languageId argument\n') } else { eval.polyglot(,'abc',) } +Error in eval.polyglot(, "abc", ) : invalid languageId argument ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEvalFile# #if (!any(R.version$engine == "FastR")) { cat('[1] "Error reading file: /a/b.R"\n') } else { tryCatch(eval.polyglot(path="/a/b.R"), error = function(e) e$message) } [1] "Error reading file: /a/b.R" ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEvalFile# -#if (!any(R.version$engine == "FastR")) { x<-c(1);cat(x) } else { fileConn<-file("_testInteropEvalFile_testScript_.R");writeLines(c("x<-c(1)","cat(x)"), fileConn);close(fileConn);eval.polyglot(mimeType="application/x-r", path="_testInteropEvalFile_testScript_.R") } +#if (!any(R.version$engine == "FastR")) { x<-c(1);cat(x) } else { fileConn<-file("_testInteropEvalFile_testScript_.R");writeLines(c("x<-c(1)","cat(x)"), fileConn);close(fileConn);eval.polyglot(languageId="R", path="_testInteropEvalFile_testScript_.R") } 1 ##com.oracle.truffle.r.test.library.fastr.TestInterop.testInteropEvalFile# #if (!any(R.version$engine == "FastR")) { x<-c(1);cat(x) } else { fileConn<-file("_testInteropEvalFile_testScript_.R");writeLines(c("x<-c(1)","cat(x)"), fileConn);close(fileConn);eval.polyglot(path="_testInteropEvalFile_testScript_.R") } 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 29f1efb24b..d28d9bee70 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 @@ -53,11 +53,11 @@ public class TestInterop extends TestBase { @Test public void testInteropEval() { - assertEvalFastR("eval.polyglot('application/x-r', '14 + 2')", "16"); - assertEvalFastR("eval.polyglot('application/x-r', '1')", "1"); - assertEvalFastR("eval.polyglot('application/x-r', '1L')", "1L"); - assertEvalFastR("eval.polyglot('application/x-r', 'TRUE')", "TRUE"); - assertEvalFastR("eval.polyglot('application/x-r', 'as.character(123)')", "as.character(123)"); + assertEvalFastR("eval.polyglot('R', '14 + 2')", "16"); + assertEvalFastR("eval.polyglot('R', '1')", "1"); + assertEvalFastR("eval.polyglot('R', '1L')", "1L"); + assertEvalFastR("eval.polyglot('R', 'TRUE')", "TRUE"); + assertEvalFastR("eval.polyglot('R', 'as.character(123)')", "as.character(123)"); } @Test @@ -70,7 +70,7 @@ public class TestInterop extends TestBase { @Test public void testInteropEvalFile() { - assertEvalFastR("fileConn<-file(\"" + TEST_EVAL_FILE + "\");writeLines(c(\"x<-c(1)\",\"cat(x)\"), fileConn);close(fileConn);eval.polyglot(mimeType=\"application/x-r\", path=\"" + + assertEvalFastR("fileConn<-file(\"" + TEST_EVAL_FILE + "\");writeLines(c(\"x<-c(1)\",\"cat(x)\"), fileConn);close(fileConn);eval.polyglot(languageId=\"R\", path=\"" + TEST_EVAL_FILE + "\")", "x<-c(1);cat(x)"); assertEvalFastR("fileConn<-file(\"" + TEST_EVAL_FILE + "\");writeLines(c(\"x<-c(1)\",\"cat(x)\"), fileConn);close(fileConn);eval.polyglot(path=\"" + TEST_EVAL_FILE + "\")", @@ -78,7 +78,7 @@ public class TestInterop extends TestBase { assertEvalFastR("tryCatch(eval.polyglot(path=\"/a/b.R\"), error = function(e) e$message)", "cat('[1] \"Error reading file: /a/b.R\"\\n')"); assertEvalFastR("eval.polyglot()", "cat('Error in eval.polyglot() : invalid \\'source\\' or \\'path\\' argument\\n')"); - assertEvalFastR("eval.polyglot(,'abc',)", "cat('Error in eval.polyglot(, \"abc\", ) : invalid mimeType argument\\n')"); + assertEvalFastR("eval.polyglot(,'abc',)", "cat('Error in eval.polyglot(, \"abc\", ) : invalid languageId argument\\n')"); } @Test diff --git a/documentation/tutorials/debugging/InteropDebugging/src/com/oracle/truffle/r/Main.java b/documentation/tutorials/debugging/InteropDebugging/src/com/oracle/truffle/r/Main.java index b8d073b001..e2f50e1a28 100644 --- a/documentation/tutorials/debugging/InteropDebugging/src/com/oracle/truffle/r/Main.java +++ b/documentation/tutorials/debugging/InteropDebugging/src/com/oracle/truffle/r/Main.java @@ -34,8 +34,6 @@ import java.io.IOException; public class Main { - private static final String R_MIME_TYPE = "application/x-r"; - /** * @param args the command line arguments * @throws java.io.IOException -- GitLab