/
MethodNodes.java
290 lines (217 loc) · 10.7 KB
/
MethodNodes.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
286
287
288
289
290
/*
* Copyright (c) 2015 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.core;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import com.oracle.truffle.api.object.*;
import com.oracle.truffle.api.source.NullSourceSection;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.ast.ArgsNode;
import org.jruby.runtime.ArgumentDescriptor;
import org.jruby.runtime.Helpers;
import org.jruby.truffle.nodes.core.BasicObjectNodes.ReferenceEqualNode;
import org.jruby.truffle.nodes.objects.ClassNode;
import org.jruby.truffle.nodes.objects.ClassNodeGen;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.UndefinedPlaceholder;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.methods.InternalMethod;
import org.jruby.truffle.runtime.object.BasicObjectType;
import java.util.EnumSet;
@CoreClass(name = "Method")
public abstract class MethodNodes {
public static class MethodType extends BasicObjectType {
}
public static final MethodType METHOD_TYPE = new MethodType();
private static final HiddenKey RECEIVER_IDENTIFIER = new HiddenKey("receiver");
public static final Property RECEIVER_PROPERTY;
private static final HiddenKey METHOD_IDENTIFIER = new HiddenKey("method");
public static final Property METHOD_PROPERTY;
private static final DynamicObjectFactory METHOD_FACTORY;
static {
final Shape.Allocator allocator = RubyBasicObject.LAYOUT.createAllocator();
RECEIVER_PROPERTY = Property.create(RECEIVER_IDENTIFIER, allocator.locationForType(Object.class, EnumSet.of(LocationModifier.Final, LocationModifier.NonNull)), 0);
METHOD_PROPERTY = Property.create(METHOD_IDENTIFIER, allocator.locationForType(InternalMethod.class, EnumSet.of(LocationModifier.Final, LocationModifier.NonNull)), 0);
final Shape shape = RubyBasicObject.LAYOUT.createShape(METHOD_TYPE)
.addProperty(RECEIVER_PROPERTY)
.addProperty(METHOD_PROPERTY);
METHOD_FACTORY = shape.createFactory();
}
public static RubyBasicObject createMethod(RubyClass rubyClass, Object receiver, InternalMethod method) {
return new RubyBasicObject(rubyClass, METHOD_FACTORY.newInstance(receiver, method));
}
public static Object getReceiver(RubyBasicObject method) {
assert method.getDynamicObject().getShape().hasProperty(RECEIVER_IDENTIFIER);
return RECEIVER_PROPERTY.get(method.getDynamicObject(), true);
}
public static InternalMethod getMethod(RubyBasicObject method) {
assert method.getDynamicObject().getShape().hasProperty(METHOD_IDENTIFIER);
return (InternalMethod) METHOD_PROPERTY.get(method.getDynamicObject(), true);
}
@CoreMethod(names = { "==", "eql?" }, required = 1)
public abstract static class EqualNode extends CoreMethodArrayArgumentsNode {
@Child protected ReferenceEqualNode referenceEqualNode;
public EqualNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
protected boolean areSame(VirtualFrame frame, Object left, Object right) {
if (referenceEqualNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
referenceEqualNode = insert(BasicObjectNodesFactory.ReferenceEqualNodeFactory.create(getContext(), getSourceSection(), null, null));
}
return referenceEqualNode.executeReferenceEqual(frame, left, right);
}
@Specialization(guards = "isRubyMethod(b)")
public boolean equal(VirtualFrame frame, RubyBasicObject a, RubyBasicObject b) {
return areSame(frame, getReceiver(a), getReceiver(b)) && getMethod(a) == getMethod(b);
}
@Specialization(guards = "!isRubyMethod(b)")
public boolean equal(RubyBasicObject a, Object b) {
return false;
}
}
@CoreMethod(names = "arity")
public abstract static class ArityNode extends CoreMethodArrayArgumentsNode {
public ArityNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@Specialization
public int arity(RubyBasicObject method) {
return getMethod(method).getSharedMethodInfo().getArity().getArityNumber();
}
}
@CoreMethod(names = "call", needsBlock = true, argumentsAsArray = true)
public abstract static class CallNode extends CoreMethodArrayArgumentsNode {
@Child private IndirectCallNode callNode;
public CallNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
callNode = Truffle.getRuntime().createIndirectCallNode();
}
@Specialization
public Object call(VirtualFrame frame, RubyBasicObject method, Object[] arguments, UndefinedPlaceholder block) {
return doCall(frame, method, arguments, null);
}
@Specialization
public Object doCall(VirtualFrame frame, RubyBasicObject method, Object[] arguments, RubyProc block) {
// TODO(CS 11-Jan-15) should use a cache and DirectCallNode here so that we can inline - but it's
// incompatible with our current dispatch chain.
final InternalMethod internalMethod = getMethod(method);
return callNode.call(frame, getMethod(method).getCallTarget(), RubyArguments.pack(
internalMethod,
internalMethod.getDeclarationFrame(),
getReceiver(method),
block,
arguments));
}
}
@CoreMethod(names = "name")
public abstract static class NameNode extends CoreMethodArrayArgumentsNode {
public NameNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@Specialization
public RubySymbol name(RubyBasicObject method) {
CompilerDirectives.transferToInterpreter();
return getContext().getSymbol(getMethod(method).getName());
}
}
@CoreMethod(names = "owner")
public abstract static class OwnerNode extends CoreMethodArrayArgumentsNode {
public OwnerNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@Specialization
public RubyModule owner(RubyBasicObject method) {
return getMethod(method).getDeclaringModule();
}
}
@CoreMethod(names = "parameters")
public abstract static class ParametersNode extends CoreMethodArrayArgumentsNode {
public ParametersNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@TruffleBoundary
@Specialization
public RubyArray parameters(RubyBasicObject method) {
final ArgsNode argsNode = getMethod(method).getSharedMethodInfo().getParseTree().findFirstChild(ArgsNode.class);
final ArgumentDescriptor[] argsDesc = Helpers.argsNodeToArgumentDescriptors(argsNode);
return (RubyArray) getContext().toTruffle(Helpers.argumentDescriptorsToParameters(getContext().getRuntime(),
argsDesc, true));
}
}
@CoreMethod(names = "receiver")
public abstract static class ReceiverNode extends CoreMethodArrayArgumentsNode {
public ReceiverNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@Specialization
public Object receiver(RubyBasicObject method) {
return getReceiver(method);
}
}
@CoreMethod(names = "source_location")
public abstract static class SourceLocationNode extends CoreMethodArrayArgumentsNode {
public SourceLocationNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@Specialization
public Object sourceLocation(RubyBasicObject method) {
CompilerDirectives.transferToInterpreter();
SourceSection sourceSection = getMethod(method).getSharedMethodInfo().getSourceSection();
if (sourceSection instanceof NullSourceSection) {
return nil();
} else {
RubyString file = getContext().makeString(sourceSection.getSource().getName());
return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(),
file, sourceSection.getStartLine());
}
}
}
@CoreMethod(names = "unbind")
public abstract static class UnbindNode extends CoreMethodArrayArgumentsNode {
@Child private ClassNode classNode;
public UnbindNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
classNode = ClassNodeGen.create(context, sourceSection, null);
}
@Specialization
public RubyUnboundMethod unbind(VirtualFrame frame, RubyBasicObject method) {
CompilerDirectives.transferToInterpreter();
RubyClass receiverClass = classNode.executeGetClass(frame, getReceiver(method));
return new RubyUnboundMethod(getContext().getCoreLibrary().getUnboundMethodClass(), receiverClass, getMethod(method));
}
}
@CoreMethod(names = "to_proc")
public abstract static class ToProcNode extends CoreMethodArrayArgumentsNode {
public ToProcNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@Specialization
public RubyProc toProc(RubyBasicObject methodObject) {
final InternalMethod method = getMethod(methodObject);
return new RubyProc(
getContext().getCoreLibrary().getProcClass(),
RubyProc.Type.LAMBDA,
method.getSharedMethodInfo(),
method.getCallTarget(),
method.getCallTarget(),
method.getCallTarget(),
method.getDeclarationFrame(),
method,
getReceiver(methodObject),
null);
}
}
}