changeset 22278:85a6db6624ab

Simplifying the way to obtain Language.globalObject
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Tue, 06 Oct 2015 13:08:12 +0200
parents 412991d0359c
children 0fc995134ff3
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java
diffstat 4 files changed, 68 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java	Tue Oct 06 12:23:07 2015 +0200
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java	Tue Oct 06 13:08:12 2015 +0200
@@ -54,8 +54,8 @@
         PolyglotEngine.Language language = vm.getLanguages().get(L1);
         assertNotNull("L1 language is defined", language);
 
-        Source s = Source.fromText("return nothing", "nothing").withMimeType(L1);
-        Object ret = vm.eval(s).get();
+        Source s = Source.fromText("return nothing", "nothing");
+        Object ret = language.eval(s).get();
         assertNull("nothing is returned", ret);
 
         Object afterInitialization = findLanguageByClass(vm);
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Tue Oct 06 12:23:07 2015 +0200
+++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Tue Oct 06 13:08:12 2015 +0200
@@ -794,7 +794,9 @@
         private void waitForSymbol() throws InterruptedIOException {
             checkThread();
             try {
-                ready.await();
+                if (ready != null) {
+                    ready.await();
+                }
             } catch (InterruptedException ex) {
                 throw (InterruptedIOException) new InterruptedIOException(ex.getMessage()).initCause(ex);
             }
@@ -847,6 +849,35 @@
         }
 
         /**
+         * Evaluates provided source. Ignores the particular {@link Source#getMimeType() MIME type}
+         * and forces evaluation in the context of <code>this</code> language.
+         * 
+         * @param source code snippet to execute
+         * @return a {@link Value} object that holds result of an execution, never <code>null</code>
+         * @throws IOException thrown to signal errors while processing the code
+         */
+        public Value eval(Source source) throws IOException {
+            checkThread();
+            return PolyglotEngine.this.eval(this, source);
+        }
+
+        /**
+         * Returns value representing global object of the language.
+         * <p>
+         * The object is expected to be <code>TruffleObject</code> (e.g. a native object from the
+         * other language) but technically it can be one of Java primitive wrappers ({@link Integer}, {@link Double}, {@link Short}, etc.).
+         *
+         * @return the global object or <code>null</code> if the language does not support such
+         *         concept
+         */
+        public Value getGlobalObject() {
+            checkThread();
+
+            Object[] res = {SPI.languageGlobal(getEnv(true)), null};
+            return res[0] == null ? null : new Value(info.getImpl(true), res, null);
+        }
+
+        /**
          * @deprecated concatenate {@link #getName()} and {@link #getVersion()} the way you want.
          */
         @Deprecated
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Tue Oct 06 12:23:07 2015 +0200
+++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java	Tue Oct 06 13:08:12 2015 +0200
@@ -165,6 +165,11 @@
         return "count";
     }
 
+    @Override
+    protected String globalObject() {
+        return null;
+    }
+
     //
     // Ignore tests working on floats and double
     //
--- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Tue Oct 06 12:23:07 2015 +0200
+++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Tue Oct 06 13:08:12 2015 +0200
@@ -24,10 +24,12 @@
  */
 package com.oracle.truffle.tck;
 
+import com.oracle.truffle.api.TruffleLanguage;
 import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.java.JavaInterop;
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.vm.PolyglotEngine;
+import com.oracle.truffle.api.vm.PolyglotEngine.Language;
 import java.io.IOException;
 import java.lang.reflect.Field;
 import java.util.Random;
@@ -142,6 +144,19 @@
     }
 
     /**
+     * Name of a function to return global object. The function can be executed without providing
+     * any arguments and should return global object of the language, if the language supports it.
+     * Global object is the one accessible via
+     * {@link TruffleLanguage#getLanguageGlobal(java.lang.Object)}.
+     *
+     * @return name of globally exported symbol, return <code>null</code> if the language doesn't
+     *         support the concept of global object
+     */
+    protected String globalObject() {
+        throw new UnsupportedOperationException("globalObject() method not implemented");
+    }
+
+    /**
      * Name of a function that counts number of its invocations in current {@link PolyglotEngine}
      * context. Your function should somehow keep a counter to remember number of its invocations
      * and always increment it. The first invocation should return <code>1</code>, the second
@@ -553,7 +568,21 @@
             }
             assert prev1 == prev2 : "At round " + i + " the same number of invocations " + prev1 + " vs. " + prev2;
         }
+    }
 
+    @Test
+    public void testGlobalObjectIsAccessible() throws Exception {
+        String globalObjectFunction = globalObject();
+        if (globalObjectFunction == null) {
+            return;
+        }
+
+        Language language = vm().getLanguages().get(mimeType());
+        assertNotNull("Langugage for " + mimeType() + " found", language);
+
+        PolyglotEngine.Value function = vm().findGlobalSymbol(globalObjectFunction);
+        Object global = function.invoke(null).get();
+        assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global);
     }
 
     private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception {