# HG changeset patch # User Chris Seaton # Date 1448219451 0 # Node ID fecae2501987ac92bde7b4e849ef872d75d00a30 # Parent a6a1efa177d2aba0b726e024755ec571b601b4d2 SL: refine interop eval, rename to just eval, and add tests. diff -r a6a1efa177d2 -r fecae2501987 truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java --- 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"; } // diff -r a6a1efa177d2 -r fecae2501987 truffle/com.oracle.truffle.sl.test/src/tests/Eval.output --- /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 diff -r a6a1efa177d2 -r fecae2501987 truffle/com.oracle.truffle.sl.test/src/tests/Eval.sl --- /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()); +} diff -r a6a1efa177d2 -r fecae2501987 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/builtins/SLEvalBuiltin.java --- 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. + *

+ * 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, "").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); + } + } diff -r a6a1efa177d2 -r fecae2501987 truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java --- 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); } }