comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/impl/TckLanguage.java @ 22525:89db2519ef18

Moving implementation classes outside of the tck package to make the Javadoc of the TCK API cleaner
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 23 Dec 2015 07:43:06 +0100
parents truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TckLanguage.java@9bba3a7b34be
children
comparison
equal deleted inserted replaced
22524:579d21e36582 22525:89db2519ef18
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.impl;
26
27 import com.oracle.truffle.api.CallTarget;
28 import com.oracle.truffle.api.CompilerDirectives;
29 import com.oracle.truffle.api.Truffle;
30 import com.oracle.truffle.api.TruffleLanguage;
31 import com.oracle.truffle.api.TruffleLanguage.Env;
32 import com.oracle.truffle.api.frame.MaterializedFrame;
33 import com.oracle.truffle.api.frame.VirtualFrame;
34 import com.oracle.truffle.api.instrument.Visualizer;
35 import com.oracle.truffle.api.instrument.WrapperNode;
36 import com.oracle.truffle.api.interop.ForeignAccess;
37 import com.oracle.truffle.api.interop.Message;
38 import com.oracle.truffle.api.interop.TruffleObject;
39 import com.oracle.truffle.api.nodes.Node;
40 import com.oracle.truffle.api.nodes.RootNode;
41 import com.oracle.truffle.api.source.Source;
42
43 import java.io.IOException;
44
45 @TruffleLanguage.Registration(mimeType = "application/x-tck", name = "TCK", version = "1.0")
46 public final class TckLanguage extends TruffleLanguage<Env> {
47 public static final TckLanguage INSTANCE = new TckLanguage();
48
49 @Override
50 protected Env createContext(Env env) {
51 return env;
52 }
53
54 @Override
55 protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException {
56 final RootNode root;
57 final String txt = code.getCode();
58 if (txt.startsWith("TCK42:")) {
59 int nextColon = txt.indexOf(":", 6);
60 String mimeType = txt.substring(6, nextColon);
61 Source toParse = Source.fromText(txt.substring(nextColon + 1), "").withMimeType(mimeType);
62 root = new MultiplyNode(toParse);
63 } else {
64 final double value = Double.parseDouble(txt);
65 root = RootNode.createConstantNode(value);
66 }
67 return Truffle.getRuntime().createCallTarget(root);
68 }
69
70 @Override
71 protected Object findExportedSymbol(Env context, String globalName, boolean onlyExplicit) {
72 return null;
73 }
74
75 @Override
76 protected Object getLanguageGlobal(Env context) {
77 return null;
78 }
79
80 @Override
81 protected boolean isObjectOfLanguage(Object object) {
82 return false;
83 }
84
85 @Override
86 protected Visualizer getVisualizer() {
87 return null;
88 }
89
90 @Override
91 protected boolean isInstrumentable(Node node) {
92 return false;
93 }
94
95 @Override
96 protected WrapperNode createWrapperNode(Node node) {
97 throw new UnsupportedOperationException();
98 }
99
100 @Override
101 protected Object evalInContext(Source source, Node node, MaterializedFrame mFrame) throws IOException {
102 throw new IOException();
103 }
104
105 private static final class MultiplyNode extends RootNode implements TruffleObject, ForeignAccess.Factory {
106 private final Source code;
107
108 public MultiplyNode(Source toParse) {
109 super(TckLanguage.class, null, null);
110 this.code = toParse;
111 }
112
113 @Override
114 public Object execute(VirtualFrame frame) {
115 Env env = TckLanguage.INSTANCE.findContext(TckLanguage.INSTANCE.createFindContextNode());
116 if (frame.getArguments().length == 0) {
117 return this;
118 }
119 try {
120 CallTarget call = env.parse(code, (String) frame.getArguments()[1], (String) frame.getArguments()[2]);
121 return call.call(6, 7);
122 } catch (IOException ex) {
123 throw new AssertionError("Cannot parse " + code, ex);
124 }
125 }
126
127 @Override
128 public ForeignAccess getForeignAccess() {
129 return ForeignAccess.create(this);
130 }
131
132 @Override
133 public boolean canHandle(TruffleObject obj) {
134 return obj instanceof MultiplyNode;
135 }
136
137 @Override
138 public CallTarget accessMessage(Message tree) {
139 if (tree == Message.IS_EXECUTABLE) {
140 return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(Boolean.TRUE));
141 } else if (Message.createExecute(2).equals(tree)) {
142 return Truffle.getRuntime().createCallTarget(this);
143 } else {
144 throw new IllegalArgumentException("" + tree);
145 }
146 }
147
148 }
149
150 public static Number expectNumber(Object o) {
151 if (o instanceof Number) {
152 return (Number) o;
153 }
154 CompilerDirectives.transferToInterpreter();
155 throw new IllegalArgumentException(o + " not a Number");
156 }
157
158 public static String expectString(Object o) {
159 if (o instanceof String) {
160 return (String) o;
161 }
162 CompilerDirectives.transferToInterpreter();
163 throw new IllegalArgumentException(o + " not a String");
164 }
165
166 public static TruffleObject expectTruffleObject(Object o) {
167 if (o instanceof TruffleObject) {
168 return (TruffleObject) o;
169 }
170 CompilerDirectives.transferToInterpreter();
171 throw new IllegalArgumentException(o + " not a TruffleObject");
172 }
173
174 public static int checkBounds(int idx, int size) {
175 if (idx < 0 || idx >= size) {
176 CompilerDirectives.transferToInterpreter();
177 throw new IndexOutOfBoundsException("Index: " + idx + " Size: " + size);
178 }
179 return idx;
180 }
181
182 }