changeset 22312:8322a904a472

Verifying the Truffle/Java interop works OK also in asynchronous mode
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Fri, 16 Oct 2015 09:59:21 +0200
parents 3f2737e9e4e1
children 06c5171e44c8
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolAsynchTest.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolTest.java truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java
diffstat 3 files changed, 77 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolAsynchTest.java	Fri Oct 16 09:59:21 2015 +0200
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.vm;
+
+import com.oracle.truffle.api.vm.PolyglotEngine;
+
+import java.util.concurrent.Executors;
+
+import org.junit.Test;
+
+public class GlobalSymbolAsynchTest extends GlobalSymbolTest {
+    @Test
+    public void marker() {
+    }
+
+    @Override
+    protected PolyglotEngine.Builder createEngineBuilder() {
+        return PolyglotEngine.buildNew().executor(Executors.newSingleThreadExecutor());
+    }
+}
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolTest.java	Fri Oct 16 08:47:52 2015 +0200
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/GlobalSymbolTest.java	Fri Oct 16 09:59:21 2015 +0200
@@ -32,7 +32,6 @@
 
 import java.io.IOException;
 import java.util.List;
-import java.util.concurrent.Executors;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -57,7 +56,7 @@
 
     @Test
     public void globalSymbolFoundByLanguage() throws IOException {
-        PolyglotEngine vm = PolyglotEngine.buildNew().globalSymbol("ahoj", "42").executor(Executors.newSingleThreadExecutor()).build();
+        PolyglotEngine vm = createEngineBuilder().globalSymbol("ahoj", "42").build();
         // @formatter:off
         Object ret = vm.eval(
             Source.fromText("return=ahoj", "Return").withMimeType(L3)
@@ -68,15 +67,19 @@
 
     @Test
     public void globalSymbolFoundByVMUser() throws IOException {
-        PolyglotEngine vm = PolyglotEngine.buildNew().globalSymbol("ahoj", "42").build();
+        PolyglotEngine vm = createEngineBuilder().globalSymbol("ahoj", "42").build();
         PolyglotEngine.Value ret = vm.findGlobalSymbol("ahoj");
         assertNotNull("Symbol found", ret);
         assertEquals("42", ret.get());
     }
 
+    protected PolyglotEngine.Builder createEngineBuilder() {
+        return PolyglotEngine.buildNew();
+    }
+
     @Test
     public void passingArray() throws IOException {
-        PolyglotEngine vm = PolyglotEngine.buildNew().globalSymbol("arguments", new Object[]{"one", "two", "three"}).build();
+        PolyglotEngine vm = createEngineBuilder().globalSymbol("arguments", new Object[]{"one", "two", "three"}).build();
         PolyglotEngine.Value value = vm.findGlobalSymbol("arguments");
         assertFalse("Not instance of array", value.get() instanceof Object[]);
         assertTrue("Instance of TruffleObject", value.get() instanceof TruffleObject);
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Fri Oct 16 08:47:52 2015 +0200
+++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Fri Oct 16 09:59:21 2015 +0200
@@ -525,24 +525,14 @@
 
     @SuppressWarnings("try")
     final Object invokeForeign(final Node foreignNode, final VirtualFrame frame, final TruffleObject receiver) throws IOException {
-        final Object[] res = {null, null};
-        executor.execute(new Runnable() {
-            @Override
-            public void run() {
-                try (final Closeable c = SPI.executionStart(PolyglotEngine.this, -1, debugger, null)) {
-                    res[0] = ForeignAccess.execute(foreignNode, frame, receiver, ForeignAccess.getArguments(frame).toArray());
-                } catch (IOException ex) {
-                    res[1] = ex;
-                } catch (Throwable ex) {
-                    res[1] = ex;
-                }
-            }
-        });
-        exceptionCheck(res);
-        if (res[0] instanceof TruffleObject) {
-            return new EngineTruffleObject(this, (TruffleObject) res[0]);
+        Object res;
+        try (final Closeable c = SPI.executionStart(PolyglotEngine.this, -1, debugger, null)) {
+            res = ForeignAccess.execute(foreignNode, frame, receiver, ForeignAccess.getArguments(frame).toArray());
+        }
+        if (res instanceof TruffleObject) {
+            return new EngineTruffleObject(this, (TruffleObject) res);
         } else {
-            return res[0];
+            return res;
         }
     }
 
@@ -720,8 +710,8 @@
          * @throws IOException in case it is not possible to obtain the value of the object
          * @throws ClassCastException if the value cannot be converted to desired view
          */
-        public <T> T as(Class<T> representation) throws IOException {
-            Object obj = get();
+        public <T> T as(final Class<T> representation) throws IOException {
+            final Object obj = get();
             if (obj instanceof EngineTruffleObject) {
                 EngineTruffleObject eto = (EngineTruffleObject) obj;
                 if (representation.isInstance(eto.getDelegate())) {
@@ -740,7 +730,27 @@
                 return representation.cast(obj);
             }
             if (JAVA_INTEROP_ENABLED) {
-                return JavaInterop.asJavaObject(representation, (TruffleObject) obj);
+                final Object[] ret = {null, null};
+                final CountDownLatch computed = new CountDownLatch(1);
+                executor.execute(new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            ret[0] = JavaInterop.asJavaObject(representation, (TruffleObject) obj);
+                        } catch (Exception ex) {
+                            ret[1] = ex;
+                        } finally {
+                            computed.countDown();
+                        }
+                    }
+                });
+                try {
+                    computed.await();
+                } catch (InterruptedException ex) {
+                    throw new InterruptedIOException(ex.getMessage());
+                }
+                exceptionCheck(ret);
+                return representation.cast(ret[0]);
             }
             throw new ClassCastException("Value cannot be represented as " + representation.getName());
         }