changeset 22408:fecae2501987

SL: refine interop eval, rename to just eval, and add tests.
author Chris Seaton <chris.seaton@oracle.com>
date Sun, 22 Nov 2015 19:10:51 +0000
parents a6a1efa177d2
children 8a4f54878160
files truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java truffle/com.oracle.truffle.sl.test/src/tests/Eval.output truffle/com.oracle.truffle.sl.test/src/tests/Eval.sl truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLEvalBuiltin.java truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java
diffstat 5 files changed, 42 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Sat Nov 21 09:27:39 2015 -0800
+++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Sun Nov 22 19:10:51 2015 +0000
@@ -180,7 +180,7 @@
 
     @Override
     protected String evaluateSource() {
-        return "interopEval";
+        return "eval";
     }
 
     //
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.sl.test/src/tests/Eval.output	Sun Nov 22 19:10:51 2015 +0000
@@ -0,0 +1,1 @@
+16
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.sl.test/src/tests/Eval.sl	Sun Nov 22 19:10:51 2015 +0000
@@ -0,0 +1,4 @@
+function main() {  
+  eval("application/x-sl", "function foo() { return 14 + 2; }");
+  println(foo());
+}
--- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLEvalBuiltin.java	Sat Nov 21 09:27:39 2015 -0800
+++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLEvalBuiltin.java	Sun Nov 22 19:10:51 2015 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * The Universal Permissive License (UPL), Version 1.0
@@ -40,8 +40,12 @@
  */
 package com.oracle.truffle.sl.builtins;
 
+import com.oracle.truffle.api.CallTarget;
 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
+import com.oracle.truffle.api.dsl.Cached;
 import com.oracle.truffle.api.dsl.Specialization;
+import com.oracle.truffle.api.frame.VirtualFrame;
+import com.oracle.truffle.api.nodes.DirectCallNode;
 import com.oracle.truffle.api.nodes.NodeInfo;
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.source.SourceSection;
@@ -49,23 +53,44 @@
 import java.io.IOException;
 
 /**
- * Builtin function to parse a text in other language.
+ * Builtin function to evaluate source code in any supported language.
+ * <p>
+ * The call target is cached against the mime type and the source code, so that if they are the same
+ * each time then a direct call will be made to a cached AST, allowing it to be compiled and
+ * possibly inlined.
  */
-@NodeInfo(shortName = "interopEval")
+@NodeInfo(shortName = "eval")
 public abstract class SLEvalBuiltin extends SLBuiltinNode {
 
     public SLEvalBuiltin() {
-        super(SourceSection.createUnavailable(SLLanguage.builtinKind, "interopEval"));
+        super(SourceSection.createUnavailable(SLLanguage.builtinKind, "eval"));
+    }
+
+    @SuppressWarnings("unused")
+    @Specialization(guards = {"stringsEqual(mimeType, cachedMimeType)", "stringsEqual(code, cachedCode)"})
+    public Object evalCached(VirtualFrame frame, String mimeType, String code, @Cached("mimeType") String cachedMimeType, @Cached("code") String cachedCode,
+                    @Cached("create(parse(mimeType, code))") DirectCallNode callNode) {
+        return callNode.call(frame, new Object[]{});
     }
 
     @TruffleBoundary
-    @Specialization
-    public Object interopEval(String mimeType, String code) {
-        Source source = Source.fromText(code, "<unknown>").withMimeType(mimeType);
+    @Specialization(contains = "evalCached")
+    public Object evalUncached(String mimeType, String code) {
+        return parse(mimeType, code).call();
+    }
+
+    protected CallTarget parse(String mimeType, String code) {
+        final Source source = Source.fromText(code, "(eval)").withMimeType(mimeType);
+
         try {
-            return getContext().evalAny(source);
+            return getContext().parse(source);
         } catch (IOException ex) {
             throw new IllegalArgumentException(ex);
         }
     }
+
+    protected boolean stringsEqual(String a, String b) {
+        return a.equals(b);
+    }
+
 }
--- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java	Sat Nov 21 09:27:39 2015 -0800
+++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java	Sun Nov 22 19:10:51 2015 +0000
@@ -40,6 +40,7 @@
  */
 package com.oracle.truffle.sl.runtime;
 
+import com.oracle.truffle.api.CallTarget;
 import com.oracle.truffle.api.ExecutionContext;
 import com.oracle.truffle.api.TruffleLanguage;
 import com.oracle.truffle.api.dsl.NodeFactory;
@@ -234,7 +235,7 @@
         return a;
     }
 
-    public Object evalAny(Source source) throws IOException {
-        return env.parse(source).call();
+    public CallTarget parse(Source source) throws IOException {
+        return env.parse(source);
     }
 }