# HG changeset patch # User Jaroslav Tulach # Date 1444130867 -7200 # Node ID 0fc995134ff3dd8b447cc062bb6bb953b14dafb4 # Parent 85a6db6624ab7eaf27b4b26019227e1a4d36f3bc asJavaObject can work with null value diff -r 85a6db6624ab -r 0fc995134ff3 truffle/com.oracle.truffle.api.interop.java/src/com/oracle/truffle/api/interop/java/JavaInterop.java --- 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 type of requested and returned value * @param type interface modeling structure of foreignObject in Java - * @param foreignObject object coming from a {@link TruffleObject Truffle language} + * @param foreignObject object coming from a {@link TruffleObject Truffle language}, can be + * null, in such case the returned value will likely be + * null as well * @return instance of requested interface granting access to specified - * foreignObject + * foreignObject, can be null, if the foreignObject parameter + * was null */ public static T asJavaObject(Class 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) { diff -r 85a6db6624ab -r 0fc995134ff3 truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java --- 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) {