/
RubyNode.java
285 lines (221 loc) · 9.9 KB
/
RubyNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.nodes;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.dsl.TypeSystemReference;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.yield.YieldDispatchNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyHash;
import org.jruby.truffle.runtime.core.RubyRange;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.rubinius.RubiniusByteArray;
import org.jruby.truffle.runtime.rubinius.RubiniusChannel;
import java.math.BigInteger;
/**
* Base class for most nodes in Ruby.
*
* @see YieldDispatchNode
*/
@TypeSystemReference(RubyTypes.class)
public abstract class RubyNode extends Node {
private final RubyContext context;
public RubyNode(RubyContext context, SourceSection sourceSection) {
super(sourceSection);
assert context != null;
this.context = context;
}
public RubyNode(RubyNode prev) {
this(prev.context, prev.getSourceSection());
}
public abstract Object execute(VirtualFrame frame);
/**
* Ruby's parallel semantic path.
*
* @see DefinedNode
*/
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("expression");
}
public String executeJavaString(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectString(execute(frame));
}
public RubyArray executeArray(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyArray(execute(frame));
}
public RubyBignum executeBignum(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyBignum(execute(frame));
}
public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectBoolean(execute(frame));
}
public int executeIntegerFixnum(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectInteger(execute(frame));
}
public long executeLongFixnum(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectLong(execute(frame));
}
public RubyRange.IntegerFixnumRange executeIntegerFixnumRange(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectIntegerFixnumRange(execute(frame));
}
public RubyRange.LongFixnumRange executeLongFixnumRange(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectLongFixnumRange(execute(frame));
}
public double executeFloat(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectDouble(execute(frame));
}
public Object[] executeObjectArray(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectObjectArray(execute(frame));
}
public RubyRange.ObjectRange executeObjectRange(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectObjectRange(execute(frame));
}
public RubyBasicObject executeRubyBasicObject(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyBasicObject(execute(frame));
}
public RubyBinding executeRubyBinding(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyBinding(execute(frame));
}
public RubyClass executeRubyClass(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyClass(execute(frame));
}
public RubyException executeRubyException(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyException(execute(frame));
}
public RubyFiber executeRubyFiber(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyFiber(execute(frame));
}
public RubyFile executeRubyFile(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyFile(execute(frame));
}
public RubyHash executeRubyHash(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyHash(execute(frame));
}
public RubyMatchData executeRubyMatchData(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyMatchData(execute(frame));
}
public RubyModule executeRubyModule(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyModule(execute(frame));
}
public RubyNilClass executeRubyNilClass(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyNilClass(execute(frame));
}
public RubyProc executeRubyProc(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyProc(execute(frame));
}
public RubyRange executeRubyRange(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyRange(execute(frame));
}
public RubyRegexp executeRubyRegexp(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyRegexp(execute(frame));
}
public RubySymbol executeRubySymbol(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubySymbol(execute(frame));
}
public RubyThread executeRubyThread(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyThread(execute(frame));
}
public RubyTime executeRubyTime(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyTime(execute(frame));
}
public RubyString executeString(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyString(execute(frame));
}
public RubyEncoding executeRubyEncoding(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyEncoding(execute(frame));
}
public UndefinedPlaceholder executeUndefinedPlaceholder(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectUndefinedPlaceholder(execute(frame));
}
public RubiniusByteArray executeRubiniusByteArray(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubiniusByteArray(execute(frame));
}
public RubiniusChannel executeRubiniusChannel(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubiniusChannel(execute(frame));
}
public RubyEncodingConverter executeRubyEncodingConverter(VirtualFrame frame) throws UnexpectedResultException {
return RubyTypesGen.RUBYTYPES.expectRubyEncodingConverter(execute(frame));
}
public Dispatch.DispatchAction executeDispatchAction(VirtualFrame frame) {
throw new UnsupportedOperationException();
}
public LexicalScope executeLexicalScope(VirtualFrame frame) {
throw new UnsupportedOperationException();
}
public void executeVoid(VirtualFrame frame) {
execute(frame);
}
public RubyNode getNonProxyNode() {
return this;
}
public RubyContext getContext() {
return context;
}
public static void notDesignedForCompilation() {
CompilerAsserts.neverPartOfCompilation();
}
public static boolean isNil(Object o) {
return o instanceof RubyNilClass;
}
public static boolean isTrue(boolean b) {
return b;
}
public static boolean isModule(RubyBasicObject o) {
return o instanceof RubyModule;
}
public static boolean isArray(Object o) {
return o instanceof RubyArray;
}
public static boolean isFixnum(Object o) {
return o instanceof Integer || o instanceof Long;
}
public static boolean isBignum(Object o) {
return o instanceof RubyBignum;
}
public static boolean isFloat(Object o) {
return o instanceof Double;
}
public static boolean isFirstFixnum(Object o) {
return o instanceof Integer || o instanceof Long;
}
public static boolean isFirstBignum(Object o) {
return o instanceof RubyBignum;
}
public static boolean isFirstFloat(Object o) {
return o instanceof Double;
}
public static boolean isSecondFixnum(Object a, Object b) {
return b instanceof Integer || b instanceof Long;
}
public static boolean isSecondBignum(Object a, Object b) {
return b instanceof RubyBignum;
}
public static boolean isSecondFloat(Object a, Object b) {
return b instanceof Double;
}
public static boolean isString(Object o) {
return o instanceof RubyString;
}
public RubyBignum bignum(int value) {
return bignum((long) value);
}
public RubyBignum bignum(long value) {
return bignum(BigInteger.valueOf(value));
}
public RubyBignum bignum(BigInteger value) {
return new RubyBignum(getContext().getCoreLibrary().getBignumClass(), value);
}
}