comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/StructuredDataEntry.java @ 22496:b3569a53c24c

Refactor and improve ComplexNumber sequence tests
author Matthias Grimmer <grimmer@ssw.jku.at>
date Mon, 14 Dec 2015 14:20:08 +0100
parents
children 9bba3a7b34be
comparison
equal deleted inserted replaced
22495:aeba89e1d8da 22496:b3569a53c24c
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 com.oracle.truffle.api.CallTarget;
28 import com.oracle.truffle.api.Truffle;
29 import com.oracle.truffle.api.frame.VirtualFrame;
30 import com.oracle.truffle.api.interop.ForeignAccess;
31 import com.oracle.truffle.api.interop.ForeignAccess.Factory;
32 import com.oracle.truffle.api.interop.Message;
33 import com.oracle.truffle.api.interop.TruffleObject;
34 import com.oracle.truffle.api.nodes.RootNode;
35
36 final class StructuredDataEntry implements TruffleObject {
37
38 private final byte[] buffer;
39 private final Schema schema;
40 private final int index;
41
42 StructuredDataEntry(byte[] buffer, Schema schema, int index) {
43 this.buffer = buffer;
44 this.schema = schema;
45 this.index = index;
46 }
47
48 public ForeignAccess getForeignAccess() {
49 return ForeignAccess.create(new StructuredDataEntryForeignAccessFactory());
50 }
51
52 private static class StructuredDataEntryForeignAccessFactory implements Factory {
53
54 public boolean canHandle(TruffleObject obj) {
55 return obj instanceof StructuredDataEntry;
56 }
57
58 public CallTarget accessMessage(Message tree) {
59 // for simplicity: this StructuredData is read-only
60 if (Message.IS_NULL.equals(tree)) {
61 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
62 } else if (Message.IS_EXECUTABLE.equals(tree)) {
63 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
64 } else if (Message.IS_BOXED.equals(tree)) {
65 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
66 } else if (Message.HAS_SIZE.equals(tree)) {
67 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(false));
68 } else if (Message.READ.equals(tree)) {
69 return Truffle.getRuntime().createCallTarget(new StructuredDataEntryReadNode());
70 } else {
71 throw new IllegalArgumentException(tree.toString() + " not supported");
72 }
73 }
74 }
75
76 private static class StructuredDataEntryReadNode extends RootNode {
77 protected StructuredDataEntryReadNode() {
78 super(TckLanguage.class, null, null);
79 }
80
81 @Override
82 public Object execute(VirtualFrame frame) {
83 StructuredDataEntry data = (StructuredDataEntry) ForeignAccess.getReceiver(frame);
84 String name = (String) ForeignAccess.getArguments(frame).get(0);
85 return data.schema.get(data.buffer, data.index, name);
86 }
87
88 }
89 }