diff truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java @ 22104:cf19259edf87

TruffleVM.eval and Source.withMimeType
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 24 Aug 2015 08:46:21 +0200
parents 2a22cec84114
children b5eaddcdf86a
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java	Mon Aug 24 08:25:31 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java	Mon Aug 24 08:46:21 2015 +0200
@@ -344,7 +344,9 @@
      * @return result of a processing the file, possibly <code>null</code>
      * @throws IOException exception to signal I/O problems or problems with processing the file's
      *             content
+     * @deprecated use {@link #eval(com.oracle.truffle.api.source.Source)}
      */
+    @Deprecated
     public Object eval(URI location) throws IOException {
         checkThread();
         Source s;
@@ -382,7 +384,9 @@
      * @param reader the source of code snippet to execute
      * @return result of an execution, possibly <code>null</code>
      * @throws IOException thrown to signal errors while processing the code
+     * @deprecated use {@link #eval(com.oracle.truffle.api.source.Source)}
      */
+    @Deprecated
     public Object eval(String mimeType, Reader reader) throws IOException {
         checkThread();
         TruffleLanguage<?> l = getTruffleLang(mimeType);
@@ -400,7 +404,9 @@
      * @param code the code snippet to execute
      * @return result of an execution, possibly <code>null</code>
      * @throws IOException thrown to signal errors while processing the code
+     * @deprecated use {@link #eval(com.oracle.truffle.api.source.Source)}
      */
+    @Deprecated
     public Object eval(String mimeType, String code) throws IOException {
         checkThread();
         TruffleLanguage<?> l = getTruffleLang(mimeType);
@@ -410,6 +416,25 @@
         return eval(l, Source.fromText(code, mimeType));
     }
 
+    /**
+     * Evaluates provided source. Chooses language registered for a particular
+     * {@link Source#getMimeType() MIME type} (throws {@link IOException} if there is none). The
+     * language is then allowed to parse and execute the source.
+     *
+     * @param source code snippet to execute
+     * @return result of an execution, possibly <code>null</code>
+     * @throws IOException thrown to signal errors while processing the code
+     */
+    public Object eval(Source source) throws IOException {
+        String mimeType = source.getMimeType();
+        checkThread();
+        TruffleLanguage<?> l = getTruffleLang(mimeType);
+        if (l == null) {
+            throw new IOException("No language for MIME type " + mimeType + " found. Supported types: " + langs.keySet());
+        }
+        return eval(l, source);
+    }
+
     private Object eval(TruffleLanguage<?> l, Source s) throws IOException {
         TruffleVM.findDebuggerSupport(l);
         Debugger[] fillIn = {debugger};