comparison graal/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/impl/SymbolInvokerImpl.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
children
comparison
equal deleted inserted replaced
21769:b6309a7d5a44 21770:c76742cc2c6f
1 /*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.interop.impl;
26
27 import java.io.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.api.frame.*;
31 import com.oracle.truffle.api.impl.*;
32 import com.oracle.truffle.api.interop.ForeignAccess;
33 import com.oracle.truffle.api.interop.Message;
34 import com.oracle.truffle.api.interop.TruffleObject;
35 import com.oracle.truffle.api.nodes.*;
36
37 public final class SymbolInvokerImpl extends SymbolInvoker {
38 static final FrameDescriptor UNUSED_FRAMEDESCRIPTOR = new FrameDescriptor();
39
40 @Override
41 protected Object invoke(Object symbol, Object... arr) throws IOException {
42 if (symbol instanceof String) {
43 return symbol;
44 }
45 if (symbol instanceof Number) {
46 return symbol;
47 }
48 if (symbol instanceof Boolean) {
49 return symbol;
50 }
51 Node executeMain = Message.createExecute(arr.length).createNode();
52 CallTarget callTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(executeMain, (TruffleObject) symbol, arr));
53 VirtualFrame frame = Truffle.getRuntime().createVirtualFrame(arr, UNUSED_FRAMEDESCRIPTOR);
54 Object ret = callTarget.call(frame);
55 if (ret instanceof TruffleObject) {
56 TruffleObject tret = (TruffleObject) ret;
57 Object isBoxedResult;
58 try {
59 Node isBoxed = Message.IS_BOXED.createNode();
60 CallTarget isBoxedTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(isBoxed, tret));
61 isBoxedResult = isBoxedTarget.call(frame);
62 } catch (IllegalArgumentException ex) {
63 isBoxedResult = false;
64 }
65 if (Boolean.TRUE.equals(isBoxedResult)) {
66 Node unbox = Message.UNBOX.createNode();
67 CallTarget unboxTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(unbox, tret));
68 Object unboxResult = unboxTarget.call(frame);
69 return unboxResult;
70 } else {
71 try {
72 Node isNull = Message.IS_NULL.createNode();
73 CallTarget isNullTarget = Truffle.getRuntime().createCallTarget(new TemporaryRoot(isNull, tret));
74 Object isNullResult = isNullTarget.call(frame);
75 if (Boolean.TRUE.equals(isNullResult)) {
76 return null;
77 }
78 } catch (IllegalArgumentException ex) {
79 // fallthrough
80 }
81 }
82 }
83 return ret;
84 }
85
86 private static class TemporaryRoot extends RootNode {
87 @Child private Node foreignAccess;
88 private final TruffleObject function;
89 private final Object[] args;
90
91 public TemporaryRoot(Node foreignAccess, TruffleObject function, Object... args) {
92 this.foreignAccess = foreignAccess;
93 this.function = function;
94 this.args = args;
95 }
96
97 @Override
98 public Object execute(VirtualFrame frame) {
99 return ForeignAccess.execute(foreignAccess, frame, function, args);
100 }
101 }
102
103 }