# HG changeset patch # User Jaroslav Tulach # Date 1443724369 -7200 # Node ID 371045b1312d412bafa29882e6c3f7bfe6c0f8f1 # Parent 0344879ec76387d0f872beeec813aff7a8c2541d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method. diff -r 0344879ec763 -r 371045b1312d truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/EngineTruffleObject.java --- 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; diff -r 0344879ec763 -r 371045b1312d 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 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 as(Class 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); } diff -r 0344879ec763 -r 371045b1312d truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLJavaInteropTest.java --- 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"));