-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathRubyEnumerator.java
More file actions
618 lines (509 loc) · 25.5 KB
/
RubyEnumerator.java
File metadata and controls
618 lines (509 loc) · 25.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.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-v20.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) 2006 Michael Studman <me@michaelstudman.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.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.exceptions.StopIteration;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallBlock;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.Signature;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ArraySupport;
import org.jruby.util.ByteList;
import java.util.Spliterator;
import java.util.stream.Stream;
import static org.jruby.api.Access.enumeratorClass;
import static org.jruby.api.Convert.*;
import static org.jruby.api.Define.defineClass;
import static org.jruby.api.Error.argumentError;
import static org.jruby.api.Error.typeError;
import static org.jruby.runtime.Helpers.arrayOf;
import static org.jruby.runtime.ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR;
import static org.jruby.runtime.ThreadContext.CALL_KEYWORD;
import static org.jruby.runtime.ThreadContext.hasKeywords;
import static org.jruby.runtime.ThreadContext.hasNonemptyKeywords;
import static org.jruby.runtime.ThreadContext.resetCallInfo;
import static org.jruby.runtime.Visibility.PRIVATE;
/**
* Implementation of Ruby's Enumerator module.
*/
@JRubyModule(name="Enumerator", include="Enumerable")
public class RubyEnumerator extends RubyObject implements java.util.Iterator<Object> {
public static final String GENERATOR = "@__generator__";
public static final String LOOKAHEAD = "@__lookahead__";
public static final String FEEDVALUE = "@__feedvalue__";
public static final String OBJECT = "@__object__";
public static final String METHOD = "@__method__";
public static final String ARGS = "@__args__";
/** target for each operation */
private IRubyObject object;
/** method to invoke for each operation */
private String method;
/** args to each method */
private IRubyObject[] methodArgs;
/** args contains a keywords argument */
private boolean methodArgsHasKeywords;
/** A value or proc to provide the size of the Enumerator contents*/
private IRubyObject size;
/** Function object for lazily computing size (used for internally created enumerators) */
private SizeFn sizeFn;
private FeedValue feedValue;
public static RubyClass defineEnumerator(ThreadContext context, RubyClass Object, RubyModule Enumerable) {
RubyClass Enumerator = defineClass(context, "Enumerator", Object, RubyEnumerator::new).
include(context, Enumerable).
defineMethods(context, RubyEnumerator.class);
Enumerator.defineClassUnder(context, "FeedValue", Object, NOT_ALLOCATABLE_ALLOCATOR).defineMethods(context, FeedValue.class);
Enumerator.setConstantVisibility(context, "FeedValue", true);
return Enumerator;
}
/**
* Internal Enumerator::FeedValue class to be shared between enumerator and its next-er Fiber.
* In essence, a `Struct.new(:value)` like container but for finer control implemented as native.
*/
public static class FeedValue extends RubyObject {
private volatile IRubyObject value;
private FeedValue(ThreadContext context, RubyClass type) {
super(context.runtime, type);
value = context.nil;
}
@Deprecated(since = "10.0.0.0")
FeedValue(Ruby runtime) {
this(runtime.getCurrentContext());
}
FeedValue(ThreadContext context) {
this(context, (RubyClass) enumeratorClass(context).getConstantAt(context, "FeedValue", true));
}
@JRubyMethod
public IRubyObject value() { return value; }
@JRubyMethod(name = "value=")
public IRubyObject set_value(IRubyObject value) {
return this.value = value;
}
@JRubyMethod
public IRubyObject use_value(ThreadContext context) {
IRubyObject value = this.value;
this.value = context.nil;
return value;
}
}
private RubyEnumerator(Ruby runtime, RubyClass type) {
super(runtime, type);
initialize(runtime, runtime.getNil(), RubyString.newEmptyString(runtime), NULL_ARRAY);
}
private RubyEnumerator(Ruby runtime, RubyClass type, IRubyObject object, RubySymbol method, IRubyObject[] args,
IRubyObject size, SizeFn sizeFn, boolean keywords) {
super(runtime, type);
initialize(runtime, object, method, args, size, sizeFn, keywords);
}
private RubyEnumerator(Ruby runtime, RubyClass type, IRubyObject object, IRubyObject method, IRubyObject[] args) {
super(runtime, type);
initialize(runtime, object, method, args);
}
/**
* Transform object into an Enumerator with the given size
*/
public static <T extends IRubyObject> RubyEnumerator enumWithSize(ThreadContext context, final T object, String method, IRubyObject[] args, SizeFn<T> sizeFn) {
Ruby runtime = context.runtime;
return new RubyEnumerator(runtime, runtime.getEnumerator(), object, runtime.fastNewSymbol(method), args, null, sizeFn, false);
}
public static <T extends IRubyObject> IRubyObject enumeratorizeWithSize(ThreadContext context, final T object, String method, IRubyObject[] args, SizeFn<T> sizeFn, boolean keywords) {
Ruby runtime = context.runtime;
return new RubyEnumerator(runtime, runtime.getEnumerator(), object, runtime.fastNewSymbol(method), args, null, sizeFn, keywords);
}
public static <T extends IRubyObject> IRubyObject enumeratorizeWithSize(ThreadContext context, final T object, String method, IRubyObject[] args, SizeFn<T> sizeFn) {
return enumeratorizeWithSize(context, object, method, args, sizeFn, false);
}
public static <T extends IRubyObject> IRubyObject enumeratorizeWithSize(ThreadContext context, T object, String method, SizeFn<T> sizeFn) {
return enumeratorizeWithSize(context, object, method, NULL_ARRAY, sizeFn);
}
public static <T extends IRubyObject> RubyEnumerator enumWithSize(ThreadContext context, T object, String method, SizeFn<T> sizeFn) {
return enumWithSize(context, object, method, NULL_ARRAY, sizeFn);
}
public static IRubyObject enumeratorizeWithSize(ThreadContext context, IRubyObject object, String method, IRubyObject arg, IRubyObject size) {
Ruby runtime = context.runtime;
return new RubyEnumerator(runtime, runtime.getEnumerator(), object, runtime.fastNewSymbol(method), new IRubyObject[] { arg }, size, null, false);
}
public static IRubyObject enumeratorize(Ruby runtime, IRubyObject object, String method) {
return new RubyEnumerator(runtime, runtime.getEnumerator(), object, runtime.fastNewSymbol(method), NULL_ARRAY);
}
public static IRubyObject enumeratorize(Ruby runtime, IRubyObject object, String method, IRubyObject arg) {
return new RubyEnumerator(runtime, runtime.getEnumerator(), object, runtime.fastNewSymbol(method), new IRubyObject[] { arg });
}
public static IRubyObject enumeratorize(Ruby runtime, IRubyObject object, String method, IRubyObject... args) {
return new RubyEnumerator(runtime, runtime.getEnumerator(), object, runtime.fastNewSymbol(method), args);
}
public static IRubyObject enumeratorize(Ruby runtime, RubyClass type, IRubyObject object, String method) {
return new RubyEnumerator(runtime, type, object, runtime.fastNewSymbol(method), NULL_ARRAY);
}
public static IRubyObject enumeratorize(Ruby runtime, RubyClass type, IRubyObject object, String method, IRubyObject arg) {
return new RubyEnumerator(runtime, type, object, runtime.fastNewSymbol(method), new IRubyObject[] {arg});
}
public static IRubyObject enumeratorize(Ruby runtime, RubyClass type, IRubyObject object, String method, IRubyObject[] args) {
return new RubyEnumerator(runtime, type, object, runtime.fastNewSymbol(method), args);
}
// used internally to create lazy without block (from Enumerator/Enumerable)
// and used internally to create enum from Enumerator::Lazy#eager
@JRubyMethod(name = "__from", meta = true, required = 2, optional = 2, checkArity = false, visibility = PRIVATE, keywords = true)
public static IRubyObject __from(ThreadContext context, IRubyObject klass, IRubyObject[] args) {
boolean keywords = hasNonemptyKeywords(ThreadContext.resetCallInfo(context));
int argc = Arity.checkArgumentCount(context, args, 2, 4);
// Lazy.__from(enum, method, *args, size)
IRubyObject object = args[0];
IRubyObject method = args[1];
IRubyObject[] methodArgs;
IRubyObject size = null; SizeFn sizeFn = null;
if (argc > 2) {
methodArgs = ((RubyArray) args[2]).toJavaArrayMaybeUnsafe();
if (argc > 3) size = args[3];
} else {
methodArgs = NULL_ARRAY;
}
RubyEnumerator instance = (RubyEnumerator) ((RubyClass) klass).allocate(context);
if (size == null) {
sizeFn = RubyEnumerable::size;
}
instance.initialize(context.runtime, object, method, methodArgs, size, sizeFn, keywords);
// set: @receiver = obj.object, @method = obj.method || :each, *@args = obj.args || []
// (for Lazy#inspect)
instance.setInstanceVariable("@receiver", object);
instance.setInstanceVariable("@method", method);
instance.setInstanceVariable("@args", RubyArray.newArrayNoCopyLight(context.runtime, methodArgs));
return instance;
}
@Override
public IRubyObject initialize(ThreadContext context) {
return initialize(context, Block.NULL_BLOCK);
}
@JRubyMethod(name = "initialize", visibility = PRIVATE)
public IRubyObject initialize(ThreadContext context, Block block) {
return initializeWithSize(context, null, block);
}
@JRubyMethod(name = "initialize", visibility = PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject size, Block block) {
return initializeWithSize(context, size, block);
}
private static void checkSize(ThreadContext context, IRubyObject size) {
if (size != null &&
!(size.isNil() || size.respondsTo("call")) &&
!(size instanceof RubyFloat && ((RubyFloat) size).value == Float.POSITIVE_INFINITY) &&
!(size instanceof RubyInteger)) {
throw typeError(context, size, "Integer");
}
}
private IRubyObject initializeWithSize(ThreadContext context, IRubyObject size, Block block) {
checkSize(context, size);
IRubyObject object = context.runtime.getGenerator().newInstance(context, NULL_ARRAY, block);
IRubyObject method = asSymbol(context, "each");
return initialize(context.runtime, object, method, NULL_ARRAY, size, null, false);
}
private IRubyObject initialize(Ruby runtime, IRubyObject object, IRubyObject method, IRubyObject[] methodArgs) {
return initialize(runtime, object, method, methodArgs, null, null, false);
}
private IRubyObject initialize(Ruby runtime, IRubyObject object, IRubyObject method, IRubyObject[] methodArgs,
IRubyObject size, SizeFn sizeFn, boolean keywords) {
this.object = object;
this.method = method.asJavaString();
this.methodArgs = methodArgs;
this.methodArgsHasKeywords = keywords;
this.size = size;
this.sizeFn = sizeFn;
setInstanceVariable(OBJECT, object);
setInstanceVariable(METHOD, method);
setInstanceVariable(ARGS, RubyArray.newArrayMayCopy(runtime, methodArgs));
setInstanceVariable(GENERATOR, runtime.getNil());
setInstanceVariable(LOOKAHEAD, RubyArray.newArray(runtime, 4));
setInstanceVariable(FEEDVALUE, this.feedValue = new FeedValue(runtime));
return this;
}
@JRubyMethod(name = "dup")
@Override
public IRubyObject dup() {
// JRUBY-5013: Enumerator needs to copy private fields in order to have a valid structure
RubyEnumerator copy = (RubyEnumerator) super.dup();
copy.object = this.object;
copy.method = this.method;
copy.methodArgs = this.methodArgs;
copy.methodArgsHasKeywords = this.methodArgsHasKeywords;
copy.size = this.size;
copy.sizeFn = this.sizeFn;
copy.feedValue = new FeedValue(getRuntime());
return copy;
}
/**
* Send current block and supplied args to method on target. According to MRI
* Block may not be given and "each" should just ignore it and call on through to
* underlying method.
*/
@JRubyMethod
public IRubyObject each(ThreadContext context, Block block) {
if (!block.isGiven()) return this;
return __each__(context, block);
}
private IRubyObject __each__(ThreadContext context, final Block block) {
if (methodArgsHasKeywords) context.callInfo = CALL_KEYWORD;
return object.callMethod(context, method, methodArgs,
CallBlock.newCallClosure(context, this, block.getSignature(), (ctx, args, blk) -> {
IRubyObject ret;
if (args.length == 1) {
ret = block.yield(ctx, args[0]);
} else {
ret = block.yieldValues(ctx, args);
}
IRubyObject val = feedValue.use_value(ctx);
return val.isNil() ? ret : val;
})
);
}
@JRubyMethod(rest = true)
public IRubyObject each(ThreadContext context, IRubyObject[] args, Block block) {
if (args.length == 0) return each(context, block);
final int mlen = methodArgs.length;
IRubyObject[] newArgs = new IRubyObject[mlen + args.length];
ArraySupport.copy(methodArgs, newArgs, 0, mlen);
ArraySupport.copy(args, newArgs, mlen, args.length);
return new RubyEnumerator(context.runtime, getType(), object, asSymbol(context, method), newArgs,
size, sizeFn, methodArgsHasKeywords).each(context, block);
}
@JRubyMethod(name = "inspect")
public IRubyObject inspect(ThreadContext context) {
Ruby runtime = context.runtime;
if (runtime.isInspecting(this)) return inspect(context, true);
try {
runtime.registerInspecting(this);
return inspect(context, false);
} finally {
runtime.unregisterInspecting(this);
}
}
private IRubyObject inspect(ThreadContext context, boolean recurse) {
ByteList bytes = new ByteList(new byte[] {'#', '<'});
bytes.append(getMetaClass().getName(context).getBytes());
bytes.append((byte)':').append((byte)' ');
RubyString result = RubyString.newStringNoCopy(context.runtime, bytes);
if (recurse) {
return result.catString("...>");
} else {
result.catWithCodeRange(RubyObject.inspect(context, object));
result.cat(':');
result.catWithCodeRange(getMethod().asString());
if (methodArgs.length > 0) {
result.cat('(');
for (int i= 0; i < methodArgs.length; i++) {
if (methodArgsHasKeywords && i == 0) break;
result.catWithCodeRange(RubyObject.inspect(context, methodArgs[i]));
if (i < methodArgs.length - 1) {
result.catString(", ");
} else {
result.cat(')');
}
}
}
result.cat('>');
return result;
}
}
@JRubyMethod
public IRubyObject each_with_object(final ThreadContext context, IRubyObject arg, Block block) {
return block.isGiven() ? RubyEnumerable.each_with_objectCommon(context, this, block, arg) :
enumeratorizeWithSize(context, this, "each_with_object", new IRubyObject[]{arg}, RubyEnumerator::size);
}
@JRubyMethod
public IRubyObject with_object(ThreadContext context, final IRubyObject arg, final Block block) {
return block.isGiven() ? RubyEnumerable.each_with_objectCommon(context, this, block, arg) : enumeratorizeWithSize(context, this, "with_object", new IRubyObject[]{arg}, RubyEnumerator::size);
}
@JRubyMethod()
public IRubyObject each_entry(ThreadContext context, final Block block) {
return block.isGiven() ? RubyEnumerable.each_entryCommon(context, this, NULL_ARRAY, block) : enumeratorize(context.runtime, getType(), this, "each_entry", NULL_ARRAY);
}
@JRubyMethod()
public IRubyObject each_entry(ThreadContext context, final IRubyObject arg0, final Block block) {
return block.isGiven() ? RubyEnumerable.each_entryCommon(context, this, arg0, block) : enumeratorize(context.runtime, getType(), this, "each_entry", arrayOf(arg0));
}
@JRubyMethod(rest = true)
public IRubyObject each_entry(ThreadContext context, final IRubyObject[] args, final Block block) {
return block.isGiven() ? RubyEnumerable.each_entryCommon(context, this, args, block) : enumeratorize(context.runtime, getType(), this, "each_entry", args);
}
@JRubyMethod(name = "each_slice")
public IRubyObject each_slice(ThreadContext context, IRubyObject arg, final Block block) {
int size = toInt(context, arg);
if (size <= 0) throw argumentError(context, "invalid size");
return block.isGiven() ? RubyEnumerable.each_sliceCommon(context, this, size, block) :
enumeratorize(context.runtime, getType(), this, "each_slice", arg);
}
@JRubyMethod(name = "each_cons")
public IRubyObject each_cons(ThreadContext context, IRubyObject arg, final Block block) {
int size = toInt(context, arg);
if (size <= 0) throw argumentError(context, "invalid size");
return block.isGiven() ? RubyEnumerable.each_consCommon(context, this, size, block) :
enumeratorize(context.runtime, getType(), this, "each_cons", arg);
}
@JRubyMethod
public final IRubyObject size(ThreadContext context) {
if (sizeFn != null) return sizeFn.size(context, object, methodArgs);
IRubyObject size = this.size;
if (size != null) {
return size.respondsTo("call") ? size.callMethod(context, "call") : size;
}
return context.nil;
}
public long size() {
var context = getRuntime().getCurrentContext();
final IRubyObject size = size(context);
return size instanceof RubyNumeric numeric ? numeric.asLong(context) : -1;
}
/**
* A size method suitable for lambda method reference implementation of {@link SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])}
*
* @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])
*/
private static IRubyObject size(ThreadContext context, RubyEnumerator recv, IRubyObject[] args) {
return recv.size(context);
}
private IRubyObject with_index_common(ThreadContext context, final Block block, final String rubyMethodName, IRubyObject arg) {
final int index = arg.isNil() ? 0 : toInt(context, arg);
if (!block.isGiven()) {
return arg.isNil() ?
enumeratorizeWithSize(context, this, rubyMethodName, RubyEnumerator::size) :
enumeratorizeWithSize(context, this, rubyMethodName, new IRubyObject[]{asFixnum(context, index)}, RubyEnumerator::size);
}
return RubyEnumerable.callEach(context, sites(context).each, this, new RubyEnumerable.EachWithIndex(block, index));
}
@JRubyMethod
public IRubyObject each_with_index(ThreadContext context, final Block block) {
return with_index_common(context, block, "each_with_index", context.nil);
}
@JRubyMethod(name = "with_index")
public IRubyObject with_index(ThreadContext context, final Block block) {
return with_index_common(context, block, "with_index", context.nil);
}
@JRubyMethod(name = "with_index")
public IRubyObject with_index(ThreadContext context, IRubyObject arg, final Block block) {
return with_index_common(context, block, "with_index", arg);
}
// java.util.Iterator :
@Override
public synchronized boolean hasNext() {
ThreadContext context = metaClass.runtime.getCurrentContext();
try {
// We don't care about the result, just whether it succeeds.
sites(context).peek.call(context, this, this);
return true;
} catch (StopIteration si) {
return false;
}
}
private IRubyObject getMethod() {
return getInstanceVariable(METHOD);
}
@Override
public Object next() {
ThreadContext context = metaClass.runtime.getCurrentContext();
return sites(context).next.call(context, this, this).toJava(java.lang.Object.class);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
// Java 8 stream support :
public Stream<Object> stream() {
return stream(false);
}
public Stream<Object> stream(final boolean parallel) {
return java.util.stream.StreamSupport.stream(spliterator(), parallel);
}
public Spliterator<Object> spliterator() {
final long size = size();
// we do not have Array detection - assume immutable
int mod = java.util.Spliterator.IMMUTABLE;
if (size >= 0) mod |= java.util.Spliterator.SIZED;
return java.util.Spliterators.spliterator(this, size, mod);
}
public Spliterator<Object> spliterator(final int mod) {
return java.util.Spliterators.spliterator(this, size(), mod);
}
/**
* "Function" type for java-created enumerators with size. Should be implemented so that calls to
* SizeFn#size are kept in sync with the size of the created enum (i.e. if the object underlying an enumerator
* changes, calls to SizeFn#size should reflect that change).
*
* @param <T> the enumerated object's type
*/
public interface SizeFn<T extends IRubyObject> {
IRubyObject size(ThreadContext context, T self, IRubyObject[] args);
}
private static JavaSites.FiberSites sites(ThreadContext context) {
return context.sites.Fiber;
}
/** MRI: enumerator_s_produce
*
*/
@JRubyMethod(meta = true, optional = 2, checkArity = false, keywords = true)
public static IRubyObject produce(ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
IRubyObject size = UNDEF;
int argc = args.length;
if (hasKeywords(resetCallInfo(context))) {
argc--;
IRubyObject maybeSize = ArgsUtil.extractKeywordArg(context, (RubyHash) args[args.length - 1], "size");
if (maybeSize != null) {
size = maybeSize;
}
}
size = size == UNDEF ?
context.runtime.getFloat().getConstant("INFINITY") :
convertToFeasibleSizeValue(context, size);
Arity.checkArgumentCount(context, argc, 0, 1);
if (!block.isGiven()) throw argumentError(context, "no block given");
IRubyObject init = argc == 0 ? null : args[0];
RubyProducer producer = RubyProducer.newProducer(context, init, block, size);
return enumeratorizeWithSize(context, producer, "each", RubyProducer::size);
}
private static IRubyObject convertToFeasibleSizeValue(ThreadContext context, IRubyObject obj) {
if (obj.isNil()) {
return obj;
} else if (obj.respondsTo("call")) {
return obj;
} else if (obj instanceof RubyFloat flote && flote.value == Double.POSITIVE_INFINITY) {
return obj;
} else {
return toInteger(context, obj);
}
}
@Deprecated(since = "9.4.3.0")
public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block) {
IRubyObject size = Arity.checkArgumentCount(context, args, 0, 1) == 1 ? args[0] : null;
return initializeWithSize(context, size, block);
}
@Deprecated(since = "9.4.3.0")
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
return initialize(context, args, Block.NULL_BLOCK);
}
}