diff graal/com.oracle.truffle.interop/src/com/oracle/truffle/interop/SymbolInvokerImpl.java @ 21689:ed234a3178af

Behavior of null-like values is now part of the TCK
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 03 Jun 2015 10:17:19 +0200
parents 5fa7935c5de3
children 2f9e4d984d16
line wrap: on
line diff
--- a/graal/com.oracle.truffle.interop/src/com/oracle/truffle/interop/SymbolInvokerImpl.java	Tue Jun 02 21:15:59 2015 -0700
+++ b/graal/com.oracle.truffle.interop/src/com/oracle/truffle/interop/SymbolInvokerImpl.java	Wed Jun 03 10:17:19 2015 +0200
@@ -30,6 +30,7 @@
 import com.oracle.truffle.api.frame.*;
 import com.oracle.truffle.api.impl.*;
 import com.oracle.truffle.api.interop.*;
+import com.oracle.truffle.api.interop.exception.UnsupportedMessageException;
 import com.oracle.truffle.api.nodes.*;
 import com.oracle.truffle.interop.messages.*;
 import com.oracle.truffle.interop.node.*;
@@ -39,10 +40,39 @@
 
     @Override
     protected Object invoke(Object symbol, Object... arr) throws IOException {
-        ForeignObjectAccessNode executeMain = ForeignObjectAccessNode.getAccess(Execute.create(Receiver.create(), arr.length));
-        CallTarget callTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(executeMain, (TruffleObject) symbol, arr));
+        ForeignObjectAccessNode callMain = ForeignObjectAccessNode.getAccess(Execute.create(Receiver.create(), arr.length));
+        CallTarget callMainTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(callMain, (TruffleObject) symbol, arr));
         VirtualFrame frame = Truffle.getRuntime().createVirtualFrame(arr, UNUSED_FRAMEDESCRIPTOR);
-        return callTarget.call(frame);
+        Object ret = callMainTarget.call(frame);
+        if (ret instanceof TruffleObject) {
+            TruffleObject tret = (TruffleObject) ret;
+            Object isBoxedResult;
+            try {
+                ForeignObjectAccessNode isBoxed = ForeignObjectAccessNode.getAccess(IsBoxed.create(Receiver.create()));
+                CallTarget isBoxedTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(isBoxed, tret));
+                isBoxedResult = isBoxedTarget.call(frame);
+            } catch (UnsupportedMessageException ex) {
+                isBoxedResult = false;
+            }
+            if (Boolean.TRUE.equals(isBoxedResult)) {
+                ForeignObjectAccessNode unbox = ForeignObjectAccessNode.getAccess(Unbox.create(Receiver.create()));
+                CallTarget unboxTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(unbox, tret));
+                Object unboxResult = unboxTarget.call(frame);
+                return unboxResult;
+            } else {
+                try {
+                    ForeignObjectAccessNode isNull = ForeignObjectAccessNode.getAccess(IsNull.create(Receiver.create()));
+                    CallTarget isNullTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(isNull, tret));
+                    Object isNullResult = isNullTarget.call(frame);
+                    if (Boolean.TRUE.equals(isNullResult)) {
+                        return null;
+                    }
+                } catch (UnsupportedMessageException ex) {
+                    // fallthrough
+                }
+            }
+        }
+        return ret;
     }
 
     private static class TemporaryRoot extends RootNode {