comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/MapTruffleObject.java @ 22495:aeba89e1d8da

Add ComplexNumber sequence tests
author Matthias Grimmer <grimmer@ssw.jku.at>
date Fri, 11 Dec 2015 15:20:27 +0100
parents
children
comparison
equal deleted inserted replaced
22489:28227895fa35 22495:aeba89e1d8da
1 /*
2 * Copyright (c) 2015, 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.tck;
26
27 import java.util.Map;
28
29 import com.oracle.truffle.api.CallTarget;
30 import com.oracle.truffle.api.Truffle;
31 import com.oracle.truffle.api.frame.VirtualFrame;
32 import com.oracle.truffle.api.interop.ForeignAccess;
33 import com.oracle.truffle.api.interop.ForeignAccess.Factory;
34 import com.oracle.truffle.api.interop.Message;
35 import com.oracle.truffle.api.interop.TruffleObject;
36 import com.oracle.truffle.api.nodes.RootNode;
37
38 @SuppressWarnings({"rawtypes", "unchecked"})
39 public final class MapTruffleObject implements TruffleObject {
40
41 private final Map map;
42
43 public MapTruffleObject(Map map) {
44 this.map = map;
45 }
46
47 public ForeignAccess getForeignAccess() {
48 return ForeignAccess.create(new MapForeignAccessFactory());
49 }
50
51 private static class MapForeignAccessFactory implements Factory {
52
53 public boolean canHandle(TruffleObject obj) {
54 return obj instanceof MapTruffleObject;
55 }
56
57 public CallTarget accessMessage(Message tree) {
58 if (Message.IS_NULL.equals(tree)) {
59 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
60 } else if (Message.IS_EXECUTABLE.equals(tree)) {
61 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
62 } else if (Message.IS_BOXED.equals(tree)) {
63 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
64 } else if (Message.HAS_SIZE.equals(tree)) {
65 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(true));
66 } else if (Message.READ.equals(tree)) {
67 return Truffle.getRuntime().createCallTarget(new MapReadNode());
68 } else if (Message.WRITE.equals(tree)) {
69 return Truffle.getRuntime().createCallTarget(new MapWriteNode());
70 } else if (Message.GET_SIZE.equals(tree)) {
71 return Truffle.getRuntime().createCallTarget(new MapSizeNode());
72 } else {
73 throw new IllegalArgumentException(tree.toString() + " not supported");
74 }
75 }
76 }
77
78 private static class MapWriteNode extends RootNode {
79 protected MapWriteNode() {
80 super(TckLanguage.class, null, null);
81 }
82
83 @Override
84 public Object execute(VirtualFrame frame) {
85 MapTruffleObject map = (MapTruffleObject) ForeignAccess.getReceiver(frame);
86 Object key = ForeignAccess.getArguments(frame).get(0);
87 Object value = ForeignAccess.getArguments(frame).get(1);
88 map.map.put(key, value);
89 return value;
90 }
91 }
92
93 private static class MapReadNode extends RootNode {
94 protected MapReadNode() {
95 super(TckLanguage.class, null, null);
96 }
97
98 @Override
99 public Object execute(VirtualFrame frame) {
100 MapTruffleObject map = (MapTruffleObject) ForeignAccess.getReceiver(frame);
101 Object key = ForeignAccess.getArguments(frame).get(0);
102 return map.map.get(key);
103 }
104
105 }
106
107 private static class MapSizeNode extends RootNode {
108 protected MapSizeNode() {
109 super(TckLanguage.class, null, null);
110 }
111
112 @Override
113 public Object execute(VirtualFrame frame) {
114 MapTruffleObject map = (MapTruffleObject) ForeignAccess.getReceiver(frame);
115 return map.map.size();
116 }
117
118 }
119
120 }