comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/StructuredData.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 b3569a53c24c
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 public final class StructuredData implements TruffleObject {
39
40 private final byte[] buffer;
41 private final Schema schema;
42
43 public StructuredData(byte[] buffer, Schema schema) {
44 this.buffer = buffer;
45 this.schema = schema;
46 }
47
48 public Map<String, Object> getEntry(int index) {
49 return schema.getEntry(buffer, index);
50 }
51
52 public ForeignAccess getForeignAccess() {
53 return ForeignAccess.create(new StructuredDataForeignAccessFactory());
54 }
55
56 private static class StructuredDataForeignAccessFactory implements Factory {
57
58 public boolean canHandle(TruffleObject obj) {
59 return obj instanceof StructuredData;
60 }
61
62 public CallTarget accessMessage(Message tree) {
63 // for simplicity: this StructuredData is read-only
64 if (Message.IS_NULL.equals(tree)) {
65 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
66 } else if (Message.IS_EXECUTABLE.equals(tree)) {
67 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
68 } else if (Message.IS_BOXED.equals(tree)) {
69 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
70 } else if (Message.HAS_SIZE.equals(tree)) {
71 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(true));
72 } else if (Message.READ.equals(tree)) {
73 return Truffle.getRuntime().createCallTarget(new StructuredDataReadNode());
74 } else if (Message.GET_SIZE.equals(tree)) {
75 return Truffle.getRuntime().createCallTarget(new StructuredDataSizeNode());
76 } else {
77 throw new IllegalArgumentException(tree.toString() + " not supported");
78 }
79 }
80 }
81
82 private static class StructuredDataReadNode extends RootNode {
83 protected StructuredDataReadNode() {
84 super(TckLanguage.class, null, null);
85 }
86
87 @Override
88 public Object execute(VirtualFrame frame) {
89 StructuredData data = (StructuredData) ForeignAccess.getReceiver(frame);
90 Number index = (Number) ForeignAccess.getArguments(frame).get(0);
91 return new MapTruffleObject(data.getEntry(index.intValue()));
92 }
93
94 }
95
96 private static class StructuredDataSizeNode extends RootNode {
97 protected StructuredDataSizeNode() {
98 super(TckLanguage.class, null, null);
99 }
100
101 @Override
102 public Object execute(VirtualFrame frame) {
103 StructuredData data = (StructuredData) ForeignAccess.getReceiver(frame);
104 return data.schema.length();
105 }
106
107 }
108 }