diff truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java @ 22183:1421041175a7

Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 21 Sep 2015 14:05:33 +0200
parents 67f75f61c974
children 2e7352f9ffa8
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java	Mon Sep 21 13:11:41 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java	Mon Sep 21 14:05:33 2015 +0200
@@ -118,6 +118,19 @@
     protected abstract C createContext(Env env);
 
     /**
+     * Disposes context created by
+     * {@link #createContext(com.oracle.truffle.api.TruffleLanguage.Env)}. A language can be asked
+     * by its user to <em>clean-up</em>. In such case the language is supposed to dispose any
+     * resources acquired before and <em>dispose</em> the <code>context</code> - e.g. render it
+     * useless for any future calls.
+     *
+     * @param context the context {@link #createContext(com.oracle.truffle.api.TruffleLanguage.Env)
+     *            created by the language}
+     */
+    protected void disposeContext(C context) {
+    }
+
+    /**
      * Parses the provided source and generates appropriate AST. The parsing should execute no user
      * code, it should only create the {@link Node} tree to represent the source. The parsing may be
      * performed in a context (specified as another {@link Node}) or without context. The
@@ -238,6 +251,10 @@
         Object getLanguageGlobal() {
             return lang.getLanguageGlobal(ctx);
         }
+
+        void dispose() {
+            lang.disposeContext(ctx);
+        }
     }
 
     /**
@@ -390,6 +407,12 @@
         protected DebugSupportProvider getDebugSupport(TruffleLanguage<?> l) {
             return l.getDebugSupport();
         }
+
+        @Override
+        protected void dispose(TruffleLanguage<?> impl, Env env) {
+            assert impl == env.langCtx.lang;
+            env.langCtx.dispose();
+        }
     }
 
 }