Mercurial > hg > truffle
diff truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/TruffleVM.java @ 22135:e70b20f4bb00
Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 07 Sep 2015 17:07:20 +0200 |
parents | c334865b9d42 |
children | 1957c49a979d 3aad794eec0e |
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/TruffleVM.java Fri Sep 04 16:41:38 2015 +0200 +++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/TruffleVM.java Mon Sep 07 17:07:20 2015 +0200 @@ -30,6 +30,8 @@ import com.oracle.truffle.api.debug.*; import com.oracle.truffle.api.impl.*; import com.oracle.truffle.api.instrument.*; +import com.oracle.truffle.api.interop.TruffleObject; +import com.oracle.truffle.api.interop.java.JavaInterop; import com.oracle.truffle.api.source.*; import java.io.*; import java.net.*; @@ -601,6 +603,26 @@ } /** + * Obtains Java view of the object represented by this symbol. The method basically + * delegates to + * {@link JavaInterop#asJavaObject(java.lang.Class, com.oracle.truffle.api.interop.TruffleObject)} + * just handles primitive types as well. + * + * @param <T> the type of the view one wants to obtain + * @param representation the class of the view interface (it has to be an interface) + * @return instance of the view wrapping the object of this symbol + * @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(); + if (representation.isInstance(obj)) { + return representation.cast(obj); + } + return JavaInterop.asJavaObject(representation, (TruffleObject) obj); + } + + /** * Invokes the symbol. If the symbol represents a function, then it should be invoked with * provided arguments. If the symbol represents a field, then first argument (if provided) * should set the value to the field; the return value should be the actual value of the