/
RubyProc.java
358 lines (291 loc) · 12.8 KB
/
RubyProc.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Chad Fowler <chadfowler@chadfowler.com>
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2005 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2007 Miguel Covarrubias <mlcovarrubias@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.JumpException;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.lexer.yacc.SimpleSourcePosition;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Binding;
import org.jruby.runtime.Block;
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.MethodBlock;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.DataType;
import java.util.Arrays;
/**
* @author jpetersen
*/
@JRubyClass(name="Proc")
public class RubyProc extends RubyObject implements DataType {
private Block block = Block.NULL_BLOCK;
private Block.Type type;
private ISourcePosition sourcePosition;
protected RubyProc(Ruby runtime, RubyClass rubyClass, Block.Type type) {
super(runtime, rubyClass);
this.type = type;
}
protected RubyProc(Ruby runtime, RubyClass rubyClass, Block.Type type, ISourcePosition sourcePosition) {
this(runtime, rubyClass, type);
this.sourcePosition = sourcePosition;
}
protected RubyProc(Ruby runtime, RubyClass rubyClass, Block.Type type, String file, int line) {
this(runtime, rubyClass, type, new SimpleSourcePosition(file, line));
}
public static RubyClass createProcClass(Ruby runtime) {
RubyClass procClass = runtime.defineClass("Proc", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
runtime.setProc(procClass);
procClass.setClassIndex(ClassIndex.PROC);
procClass.setReifiedClass(RubyProc.class);
procClass.defineAnnotatedMethods(RubyProc.class);
return procClass;
}
public Block getBlock() {
return block;
}
// Proc class
@Deprecated
public static RubyProc newProc(Ruby runtime, Block.Type type) {
throw runtime.newRuntimeError("deprecated RubyProc.newProc with no block; do not use");
}
public static RubyProc newProc(Ruby runtime, Block block, Block.Type type) {
return newProc(runtime, block, type, null);
}
public static RubyProc newProc(Ruby runtime, Block block, Block.Type type, ISourcePosition sourcePosition) {
RubyProc proc = new RubyProc(runtime, runtime.getProc(), type, sourcePosition);
proc.setup(block);
return proc;
}
public static RubyProc newProc(Ruby runtime, Block block, Block.Type type, String file, int line) {
RubyProc proc = new RubyProc(runtime, runtime.getProc(), type, file, line);
proc.setup(block);
return proc;
}
/**
* Create a new instance of a Proc object. We override this method (from RubyClass)
* since we need to deal with special case of Proc.new with no arguments or block arg. In
* this case, we need to check previous frame for a block to consume.
*/
@JRubyMethod(name = "new", rest = true, meta = true)
public static IRubyObject newInstance(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
// No passed in block, lets check next outer frame for one ('Proc.new')
if (!block.isGiven()) block = context.getCurrentFrame().getBlock();
// This metaclass == recv check seems gross, but MRI seems to do the same:
// if (!proc && ruby_block->block_obj && CLASS_OF(ruby_block->block_obj) == klass) {
if (block.isGiven() && block.getProcObject() != null && block.getProcObject().getMetaClass() == recv) {
return block.getProcObject();
}
RubyProc obj = new RubyProc(context.runtime, (RubyClass)recv, Block.Type.PROC);
obj.setup(block);
obj.callMethod(context, "initialize", args, block);
return obj;
}
private void setup(Block procBlock) {
if (!procBlock.isGiven()) {
throw getRuntime().newArgumentError("tried to create Proc object without a block");
}
if (isLambda()) {
// TODO: warn "tried to create Proc object without a block"
}
if (isThread()) {
// binding for incoming proc must not share frame
Binding oldBinding = procBlock.getBinding();
Binding newBinding = new Binding(
oldBinding.getSelf(),
oldBinding.getFrame().duplicate(),
oldBinding.getVisibility(),
oldBinding.getDynamicScope(),
oldBinding.getMethod(),
oldBinding.getFile(),
oldBinding.getLine());
block = new Block(procBlock.getBody(), newBinding);
// modify the block with a new backref/lastline-grabbing scope
StaticScope oldScope = block.getBody().getStaticScope();
StaticScope newScope = oldScope.duplicate();
block.getBody().setStaticScope(newScope);
} else {
// just use as is
block = procBlock;
}
// force file/line info into the new block's binding
block.getBinding().setFile(block.getBody().getFile());
block.getBinding().setLine(block.getBody().getLine());
block.type = type;
block.setProcObject(this);
// pre-request dummy scope to avoid clone overhead in lightweight blocks
block.getBinding().getDummyScope(block.getBody().getStaticScope());
}
@JRubyMethod(name = "clone")
@Override
public IRubyObject rbClone() {
RubyProc newProc = newProc(getRuntime(), block, type, sourcePosition);
// TODO: CLONE_SETUP here
return newProc;
}
@JRubyMethod(name = "dup")
@Override
public IRubyObject dup() {
return newProc(getRuntime(), block, type, sourcePosition);
}
@JRubyMethod(name = "==", required = 1)
public IRubyObject op_equal(IRubyObject other) {
return getRuntime().newBoolean(other instanceof RubyProc &&
(this == other || this.block.equals(((RubyProc)other).block)));
}
@Override
public IRubyObject to_s() {
return to_s19();
}
@JRubyMethod(name = "to_s", alias = "inspect")
public IRubyObject to_s19() {
StringBuilder sb = new StringBuilder("#<Proc:0x" + Integer.toString(block.hashCode(), 16) + "@" +
block.getBody().getFile() + ":" + (block.getBody().getLine() + 1));
if (isLambda()) sb.append(" (lambda)");
sb.append(">");
return RubyString.newString(getRuntime(), sb.toString());
}
@JRubyMethod(name = "binding")
public IRubyObject binding() {
return getRuntime().newBinding(block.getBinding());
}
public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block) {
return call19(context, args, block);
}
public IRubyObject call(ThreadContext context, IRubyObject[] args) {
return call(context, args, null, Block.NULL_BLOCK);
}
/**
* For Type.LAMBDA, ensures that the args have the correct arity.
*
* For others, transforms the given arguments appropriately for the given arity (i.e. trimming to one arg for fixed
* arity of one, etc.)
*/
public static IRubyObject[] prepareArgs(ThreadContext context, Block.Type type, Arity arity, IRubyObject[] args) {
if (arity == null) {
return args;
}
if (args == null) {
return IRubyObject.NULL_ARRAY;
}
if (type == Block.Type.LAMBDA) {
arity.checkArity(context.runtime, args.length);
return args;
}
boolean isFixed = arity.isFixed();
int required = arity.required();
int actual = args.length;
// for procs and blocks, single array passed to multi-arg must be spread
if (arity != Arity.ONE_ARGUMENT && required != 0 &&
(isFixed || arity != Arity.OPTIONAL) &&
actual == 1 && args[0].respondsTo("to_ary")) {
args = args[0].convertToArray().toJavaArray();
actual = args.length;
}
// fixed arity > 0 with mismatch needs a new args array
if (isFixed && required > 0 && required != actual) {
IRubyObject[] newArgs = Arrays.copyOf(args, required);
// pad with nil
if (required > actual) {
Helpers.fillNil(newArgs, actual, required, context.runtime);
}
args = newArgs;
}
return args;
}
@JRubyMethod(name = {"call", "[]", "yield", "==="}, rest = true)
public IRubyObject call19(ThreadContext context, IRubyObject[] args, Block blockCallArg) {
IRubyObject[] preppedArgs = prepareArgs(context, type, block.arity(), args);
return call(context, preppedArgs, null, blockCallArg);
}
public IRubyObject call(ThreadContext context, IRubyObject[] args, IRubyObject self, Block passedBlock) {
assert args != null;
Block newBlock;
// bind to new self, if given
if (self == null) {
newBlock = block;
} else {
newBlock = block.cloneBlockAndFrame();
newBlock.getBinding().setSelf(self);
}
return newBlock.call(context, args, passedBlock);
}
@JRubyMethod(name = "arity")
public RubyFixnum arity() {
return getRuntime().newFixnum(block.arity().getValue());
}
@JRubyMethod(name = "to_proc")
public RubyProc to_proc() {
return this;
}
@JRubyMethod
public IRubyObject source_location(ThreadContext context) {
Ruby runtime = context.runtime;
if (sourcePosition != null) {
return runtime.newArray(runtime.newString(sourcePosition.getFile()),
runtime.newFixnum(sourcePosition.getLine() + 1 /*zero-based*/));
} else if (block != null) {
Binding binding = block.getBinding();
return runtime.newArray(runtime.newString(binding.getFile()),
runtime.newFixnum(binding.getLine() + 1 /*zero-based*/));
}
return runtime.getNil();
}
@JRubyMethod
public IRubyObject parameters(ThreadContext context) {
BlockBody body = this.getBlock().getBody();
if (body instanceof MethodBlock) return ((MethodBlock) body).getMethod().parameters(context);
return Helpers.parameterListToParameters(context.runtime,
body.getParameterList(), isLambda());
}
@JRubyMethod(name = "lambda?")
public IRubyObject lambda_p(ThreadContext context) {
return context.runtime.newBoolean(isLambda());
}
private boolean isLambda() {
return type.equals(Block.Type.LAMBDA);
}
private boolean isProc() {
return type.equals(Block.Type.PROC);
}
private boolean isThread() {
return type.equals(Block.Type.THREAD);
}
}