changeset 22274:371045b1312d

Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 01 Oct 2015 20:32:49 +0200
parents 0344879ec763
children 141fe31da7a2 260e3cdf11ec
files truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/EngineTruffleObject.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/SLJavaInteropTest.java
diffstat 3 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/EngineTruffleObject.java	Thu Oct 01 15:32:28 2015 +0200
+++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/EngineTruffleObject.java	Thu Oct 01 20:32:49 2015 +0200
@@ -49,6 +49,10 @@
         return ForeignAccess.create(this);
     }
 
+    TruffleObject getDelegate() {
+        return delegate;
+    }
+
     @Override
     public boolean canHandle(TruffleObject obj) {
         return true;
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Thu Oct 01 15:32:28 2015 +0200
+++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Thu Oct 01 20:32:49 2015 +0200
@@ -717,6 +717,12 @@
          */
         public <T> T as(Class<T> representation) throws IOException {
             Object obj = get();
+            if (obj instanceof EngineTruffleObject) {
+                EngineTruffleObject eto = (EngineTruffleObject) obj;
+                if (representation.isInstance(eto.getDelegate())) {
+                    return representation.cast(eto.getDelegate());
+                }
+            }
             if (representation.isInstance(obj)) {
                 return representation.cast(obj);
             }
--- a/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLJavaInteropTest.java	Thu Oct 01 15:32:28 2015 +0200
+++ b/truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLJavaInteropTest.java	Thu Oct 01 20:32:49 2015 +0200
@@ -44,6 +44,7 @@
 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.sl.runtime.SLFunction;
 import com.oracle.truffle.sl.test.instrument.InstrumentationTestMode;
 
 import java.io.ByteArrayOutputStream;
@@ -51,6 +52,8 @@
 import static org.junit.Assert.assertEquals;
 
 import org.junit.After;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -74,7 +77,11 @@
         PolyglotEngine engine = PolyglotEngine.buildNew().setOut(os).build();
         engine.eval(script);
         PolyglotEngine.Value main = engine.findGlobalSymbol("main");
-        Runnable runnable = JavaInterop.asJavaFunction(Runnable.class, (TruffleObject) main.get());
+        final Object value = main.get();
+        assertTrue("It's truffle object", value instanceof TruffleObject);
+        SLFunction rawFunction = main.as(SLFunction.class);
+        assertNotNull("One can get the type of the inner Truffle Object", rawFunction);
+        Runnable runnable = JavaInterop.asJavaFunction(Runnable.class, (TruffleObject) value);
         runnable.run();
 
         assertEquals("Called!\n", os.toString("UTF-8"));