# HG changeset patch # User Jaroslav Tulach # Date 1444129692 -7200 # Node ID 85a6db6624ab7eaf27b4b26019227e1a4d36f3bc # Parent 412991d0359c0672f4975c034cbfdb4d2e41ae29 Simplifying the way to obtain Language.globalObject diff -r 412991d0359c -r 85a6db6624ab truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java --- 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); diff -r 412991d0359c -r 85a6db6624ab truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java --- 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 this language. + * + * @param source code snippet to execute + * @return a {@link Value} object that holds result of an execution, never null + * @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. + *

+ * The object is expected to be TruffleObject (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 null 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 diff -r 412991d0359c -r 85a6db6624ab 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 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 // diff -r 412991d0359c -r 85a6db6624ab truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java --- 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 null 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 1, 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 {