comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLFunctionForeignAccess.java @ 21770:c76742cc2c6f

Polishing inter-operability APIs: Exposing only Message, TruffleObject and ForeignAccess-related classes.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 08 Jun 2015 04:50:13 +0200
parents ed234a3178af
children
comparison
equal deleted inserted replaced
21769:b6309a7d5a44 21770:c76742cc2c6f
23 package com.oracle.truffle.sl.runtime; 23 package com.oracle.truffle.sl.runtime;
24 24
25 import com.oracle.truffle.api.CallTarget; 25 import com.oracle.truffle.api.CallTarget;
26 import com.oracle.truffle.api.Truffle; 26 import com.oracle.truffle.api.Truffle;
27 import com.oracle.truffle.api.frame.VirtualFrame; 27 import com.oracle.truffle.api.frame.VirtualFrame;
28 import com.oracle.truffle.api.interop.ForeignAccessFactory;
29 import com.oracle.truffle.api.interop.InteropPredicate;
30 import com.oracle.truffle.api.interop.exception.UnsupportedMessageException;
31 import com.oracle.truffle.api.interop.messages.Message;
32 import com.oracle.truffle.api.nodes.RootNode; 28 import com.oracle.truffle.api.nodes.RootNode;
33 import com.oracle.truffle.interop.ForeignAccessArguments; 29 import com.oracle.truffle.api.interop.TruffleObject;
34 import com.oracle.truffle.interop.messages.Execute; 30 import com.oracle.truffle.api.interop.ForeignAccess;
35 import com.oracle.truffle.interop.messages.IsNull; 31 import com.oracle.truffle.api.interop.Message;
36 import com.oracle.truffle.interop.messages.Receiver;
37 import com.oracle.truffle.sl.nodes.call.SLDispatchNode; 32 import com.oracle.truffle.sl.nodes.call.SLDispatchNode;
38 import com.oracle.truffle.sl.nodes.call.SLDispatchNodeGen; 33 import com.oracle.truffle.sl.nodes.call.SLDispatchNodeGen;
39 import java.math.BigInteger; 34 import java.math.BigInteger;
35 import java.util.List;
40 36
41 /** 37 /**
42 * Implementation of foreing access for {@link SLFunction}. 38 * Implementation of foreign access for {@link SLFunction}.
43 */ 39 */
44 final class SLFunctionForeignAccess implements ForeignAccessFactory { 40 final class SLFunctionForeignAccess implements ForeignAccess.Factory {
45 public static final ForeignAccessFactory INSTANCE = new SLFunctionForeignAccess(); 41 public static final ForeignAccess INSTANCE = ForeignAccess.create(new SLFunctionForeignAccess());
46 42
47 private SLFunctionForeignAccess() { 43 private SLFunctionForeignAccess() {
48 } 44 }
49 45
50 @Override 46 @Override
51 public InteropPredicate getLanguageCheck() { 47 public boolean canHandle(TruffleObject o) {
52 return (com.oracle.truffle.api.interop.TruffleObject o) -> o instanceof SLFunction; 48 return o instanceof SLFunction;
53 } 49 }
54 50
55 @Override 51 @Override
56 public CallTarget getAccess(Message tree) { 52 public CallTarget accessMessage(Message tree) {
57 if (Execute.create(Receiver.create(), 0).matchStructure(tree)) { 53 if (Message.createExecute(0).equals(tree)) {
58 return Truffle.getRuntime().createCallTarget(new SLForeignCallerRootNode()); 54 return Truffle.getRuntime().createCallTarget(new SLForeignCallerRootNode());
59 } else if (IsNull.create(Receiver.create()).matchStructure(tree)) { 55 } else if (Message.IS_NULL.equals(tree)) {
60 return Truffle.getRuntime().createCallTarget(new SLForeignNullCheckNode()); 56 return Truffle.getRuntime().createCallTarget(new SLForeignNullCheckNode());
61 } else { 57 } else {
62 throw new UnsupportedMessageException(tree.toString() + " not supported"); 58 throw new IllegalArgumentException(tree.toString() + " not supported");
63 } 59 }
64 } 60 }
65 61
66 private static class SLForeignCallerRootNode extends RootNode { 62 private static class SLForeignCallerRootNode extends RootNode {
67 @Child private SLDispatchNode dispatch = SLDispatchNodeGen.create(); 63 @Child private SLDispatchNode dispatch = SLDispatchNodeGen.create();
68 64
69 @Override 65 @Override
70 public Object execute(VirtualFrame frame) { 66 public Object execute(VirtualFrame frame) {
71 SLFunction function = (SLFunction) ForeignAccessArguments.getReceiver(frame.getArguments()); 67 SLFunction function = (SLFunction) ForeignAccess.getReceiver(frame);
72 // the calling convention of interop passes the receiver of a 68 // the calling convention of interop passes the receiver of a
73 // function call (the this object) 69 // function call (the this object)
74 // as an implicit 1st argument; we need to ignore this argument for SL 70 // as an implicit 1st argument; we need to ignore this argument for SL
75 Object[] arguments = ForeignAccessArguments.extractUserArguments(1, frame.getArguments()); 71 List<Object> args = ForeignAccess.getArguments(frame);
76 for (int i = 0; i < arguments.length; i++) { 72 Object[] arr = args.subList(1, args.size()).toArray();
77 if (arguments[i] instanceof Long) { 73 for (int i = 0; i < arr.length; i++) {
74 Object a = arr[i];
75 if (a instanceof Long) {
78 continue; 76 continue;
79 } 77 }
80 if (arguments[i] instanceof BigInteger) { 78 if (a instanceof BigInteger) {
81 continue; 79 continue;
82 } 80 }
83 if (arguments[i] instanceof Number) { 81 if (a instanceof Number) {
84 arguments[i] = ((Number) arguments[i]).longValue(); 82 arr[i] = ((Number) a).longValue();
85 } 83 }
86 } 84 }
87 85 return dispatch.executeDispatch(frame, function, arr);
88 return dispatch.executeDispatch(frame, function, arguments);
89 } 86 }
90 87
91 } 88 }
92 89
93 private static class SLForeignNullCheckNode extends RootNode { 90 private static class SLForeignNullCheckNode extends RootNode {
94 @Override 91 @Override
95 public Object execute(VirtualFrame frame) { 92 public Object execute(VirtualFrame frame) {
96 Object receiver = ForeignAccessArguments.getReceiver(frame.getArguments()); 93 Object receiver = ForeignAccess.getReceiver(frame);
97 return SLNull.SINGLETON == receiver; 94 return SLNull.SINGLETON == receiver;
98 } 95 }
99 } 96 }
100 } 97 }