changeset 22279:0fc995134ff3

asJavaObject can work with null value
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Tue, 06 Oct 2015 13:27:47 +0200
parents 85a6db6624ab
children af8c8edec0f2
files truffle/com.oracle.truffle.api.interop.java/src/com/oracle/truffle/api/interop/java/JavaInterop.java truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java
diffstat 2 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.interop.java/src/com/oracle/truffle/api/interop/java/JavaInterop.java	Tue Oct 06 13:08:12 2015 +0200
+++ b/truffle/com.oracle.truffle.api.interop.java/src/com/oracle/truffle/api/interop/java/JavaInterop.java	Tue Oct 06 13:27:47 2015 +0200
@@ -135,9 +135,12 @@
      *
      * @param <T> type of requested and returned value
      * @param type interface modeling structure of <code>foreignObject</code> in <b>Java</b>
-     * @param foreignObject object coming from a {@link TruffleObject Truffle language}
+     * @param foreignObject object coming from a {@link TruffleObject Truffle language}, can be
+     *            <code>null</code>, in such case the returned value will likely be
+     *            <code>null</code> as well
      * @return instance of requested interface granting access to specified
-     *         <code>foreignObject</code>
+     *         <code>foreignObject</code>, can be <code>null</code>, if the foreignObject parameter
+     *         was <code>null</code>
      */
     public static <T> T asJavaObject(Class<T> type, TruffleObject foreignObject) {
         return asJavaObject(type, null, foreignObject);
@@ -151,6 +154,9 @@
             if (!clazz.isInterface()) {
                 throw new IllegalArgumentException();
             }
+            if (foreignObject == null) {
+                return null;
+            }
             if (clazz == List.class && Boolean.TRUE.equals(message(Message.HAS_SIZE, foreignObject))) {
                 Class<?> elementType = Object.class;
                 if (type instanceof ParameterizedType) {
--- a/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Tue Oct 06 13:08:12 2015 +0200
+++ b/truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java	Tue Oct 06 13:27:47 2015 +0200
@@ -247,6 +247,15 @@
     }
 
     @Test
+    public void testNullCanBeCastToAnything() throws Exception {
+        PolyglotEngine.Value retNull = findGlobalSymbol(returnsNull());
+
+        Object res = retNull.invoke(null).as(CompoundObject.class);
+
+        assertNull("Should yield real Java null", res);
+    }
+
+    @Test
     public void testNullInCompoundObject() throws Exception {
         CompoundObject obj = findCompoundSymbol();
         if (obj == null) {