Skip to content

Commit 69f89d2

Browse files
committed
[GR-24462] Eliminate coerce_to_collection{index, length}.
PullRequest: truffleruby/1742
2 parents 6dc2518 + 44f05d8 commit 69f89d2

File tree

7 files changed

+40
-61
lines changed

7 files changed

+40
-61
lines changed

spec/ruby/core/array/fill_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@
207207

208208
not_supported_on :opal do
209209
it "raises an ArgumentError or RangeError for too-large sizes" do
210+
error_types = [RangeError, ArgumentError]
210211
arr = [1, 2, 3]
211-
-> { arr.fill(10, 1, fixnum_max) }.should raise_error(ArgumentError)
212+
-> { arr.fill(10, 1, fixnum_max) }.should raise_error { |err| error_types.should include(err.class) }
212213
-> { arr.fill(10, 1, bignum_value) }.should raise_error(RangeError)
213214
end
214215
end

src/main/java/org/truffleruby/core/VMPrimitiveNodes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ protected long startHashBigNum(DynamicObject salt) {
437437

438438
@Specialization(guards = "!isRubyNumber(salt)")
439439
protected Object startHashNotNumber(Object salt,
440-
@Cached ToRubyIntegerNode toInteger,
440+
@Cached ToRubyIntegerNode toRubyInteger,
441441
@Cached ConditionProfile isIntegerProfile,
442442
@Cached ConditionProfile isLongProfile,
443443
@Cached ConditionProfile isBignumProfile) {
444-
Object result = toInteger.execute(salt);
444+
Object result = toRubyInteger.execute(salt);
445445
if (isIntegerProfile.profile(result instanceof Integer)) {
446446
return getContext().getHashing(this).start((int) result);
447447
} else if (isLongProfile.profile(result instanceof Long)) {
@@ -470,11 +470,11 @@ protected long updateHash(long hash, DynamicObject value) {
470470

471471
@Specialization(guards = "!isRubyNumber(value)")
472472
protected Object updateHash(long hash, Object value,
473-
@Cached ToRubyIntegerNode toInteger,
473+
@Cached ToRubyIntegerNode toRubyInteger,
474474
@Cached ConditionProfile isIntegerProfile,
475475
@Cached ConditionProfile isLongProfile,
476476
@Cached ConditionProfile isBignumProfile) {
477-
Object result = toInteger.execute(value);
477+
Object result = toRubyInteger.execute(value);
478478
if (isIntegerProfile.profile(result instanceof Integer)) {
479479
return Hashing.update(hash, (int) result);
480480
} else if (isLongProfile.profile(result instanceof Long)) {

src/main/java/org/truffleruby/core/hash/HashNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class HashNode extends RubyContextNode {
2323

2424
@Child private CallDispatchHeadNode hashNode;
2525
@Child private ObjectIDNode objectIDNode;
26-
@Child ToRubyIntegerNode toInteger;
26+
@Child private ToRubyIntegerNode toRubyInteger;
2727

2828
private final ConditionProfile isIntegerProfile1 = ConditionProfile.create();
2929
private final ConditionProfile isLongProfile1 = ConditionProfile.create();
@@ -47,12 +47,12 @@ public int hash(Object key, boolean compareByIdentity) {
4747
} else if (isBignumProfile1.profile(Layouts.BIGNUM.isBignum(hashedObject))) {
4848
return BigIntegerOps.hashCode(hashedObject);
4949
} else {
50-
if (toInteger == null) {
50+
if (toRubyInteger == null) {
5151
CompilerDirectives.transferToInterpreterAndInvalidate();
52-
toInteger = insert(ToRubyIntegerNode.create());
52+
toRubyInteger = insert(ToRubyIntegerNode.create());
5353
}
5454

55-
final Object coercedHashedObject = toInteger.execute(hashedObject);
55+
final Object coercedHashedObject = toRubyInteger.execute(hashedObject);
5656

5757
if (isIntegerProfile2.profile(coercedHashedObject instanceof Integer)) {
5858
return (int) coercedHashedObject;

src/main/ruby/truffleruby/core/array.rb

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def bsearch_index
205205
end
206206

207207
def combination(num)
208-
num = Truffle::Type.coerce_to_collection_index num
208+
num = Primitive.rb_num2int num
209209

210210
unless block_given?
211211
return to_enum(:combination, num) do
@@ -271,7 +271,7 @@ def cycle(n = nil)
271271
each { |x| yield x }
272272
end
273273
else
274-
n = Truffle::Type.coerce_to_collection_index n
274+
n = Primitive.rb_num2int n
275275
n.times do
276276
each { |x| yield x }
277277
end
@@ -342,7 +342,7 @@ def empty?
342342

343343
def fetch(idx, default=undefined)
344344
orig = idx
345-
idx = Truffle::Type.coerce_to_collection_index idx
345+
idx = Primitive.rb_num2int idx
346346

347347
idx += size if idx < 0
348348

@@ -392,15 +392,13 @@ def fetch(idx, default=undefined)
392392
return self if right <= left # Nothing to modify
393393

394394
elsif index
395-
left = Truffle::Type.coerce_to_collection_length index
395+
left = Primitive.rb_num2int index
396396
left += size if left < 0
397397
left = 0 if left < 0
398398

399399
if !Primitive.undefined?(length) and length
400400
begin
401-
right = Truffle::Type.coerce_to_collection_length length
402-
rescue ArgumentError
403-
raise RangeError, 'bignum too big to convert into `long'
401+
right = Primitive.rb_num2int length
404402
rescue TypeError
405403
raise ArgumentError, 'second argument must be an Integer'
406404
end
@@ -435,14 +433,14 @@ def fetch(idx, default=undefined)
435433
def first(n = undefined)
436434
return at(0) if Primitive.undefined?(n)
437435

438-
n = Truffle::Type.coerce_to_collection_index(n)
436+
n = Primitive.rb_num2int n
439437
raise ArgumentError, 'Size must be positive' if n < 0
440438

441439
Array.new self[0, n]
442440
end
443441

444442
def flatten(level=-1)
445-
level = Truffle::Type.coerce_to_collection_index level
443+
level = Primitive.rb_num2int level
446444
return self.dup if level == 0
447445

448446
out = self.class.allocate # new_reserved size
@@ -454,7 +452,7 @@ def flatten(level=-1)
454452
def flatten!(level=-1)
455453
Primitive.check_frozen self
456454

457-
level = Truffle::Type.coerce_to_collection_index level
455+
level = Primitive.rb_num2int level
458456
return nil if level == 0
459457

460458
out = self.class.allocate # new_reserved size
@@ -531,7 +529,7 @@ def insert(idx, *items)
531529
Primitive.check_frozen self
532530
return self if items.length == 0
533531

534-
idx = Truffle::Type.coerce_to_collection_index idx
532+
idx = Primitive.rb_num2int idx
535533
idx += (size + 1) if idx < 0 # Negatives add AFTER the element
536534
raise IndexError, "#{idx} out of bounds" if idx < 0
537535

@@ -627,7 +625,7 @@ def last(n=undefined)
627625
return []
628626
end
629627

630-
n = Truffle::Type.coerce_to_collection_index n
628+
n = Primitive.rb_num2int n
631629
return [] if n == 0
632630

633631
raise ArgumentError, 'count must be positive' if n < 0
@@ -646,7 +644,7 @@ def permutation(num=undefined, &block)
646644
if Primitive.undefined? num
647645
num = size
648646
else
649-
num = Truffle::Type.coerce_to_collection_index num
647+
num = Primitive.rb_num2int num
650648
end
651649

652650
if num < 0 || size < num
@@ -687,7 +685,7 @@ def permutation_size(num)
687685
if Primitive.undefined? num
688686
k = self.size
689687
else
690-
k = Truffle::Type.coerce_to_collection_index num
688+
k = Primitive.rb_num2int num
691689
end
692690
descending_factorial(n, k)
693691
end
@@ -905,7 +903,7 @@ def rindex(obj=undefined)
905903
end
906904

907905
def rotate(n=1)
908-
n = Truffle::Type.coerce_to_collection_index n
906+
n = Primitive.rb_num2int n
909907

910908
len = self.length
911909
return Array.new(self) if len <= 1
@@ -916,7 +914,7 @@ def rotate(n=1)
916914
end
917915

918916
def rotate!(n=1)
919-
n = Truffle::Type.coerce_to_collection_index n
917+
n = Primitive.rb_num2int n
920918
Primitive.check_frozen self
921919

922920
len = self.length
@@ -933,7 +931,7 @@ def initialize(rng)
933931
end
934932

935933
def rand(size)
936-
random = Truffle::Type.coerce_to_collection_index @rng.rand(size)
934+
random = Primitive.rb_num2int @rng.rand(size)
937935
raise RangeError, 'random value must be >= 0' if random < 0
938936
raise RangeError, 'random value must be less than Array size' unless random < size
939937

@@ -950,10 +948,10 @@ def sample(count=undefined, options=undefined)
950948
count = nil
951949
else
952950
options = nil
953-
count = Truffle::Type.coerce_to_collection_index count
951+
count = Primitive.rb_num2int count
954952
end
955953
else
956-
count = Truffle::Type.coerce_to_collection_index count
954+
count = Primitive.rb_num2int count
957955
options = Truffle::Type.coerce_to options, Hash, :to_hash
958956
end
959957

@@ -1089,7 +1087,7 @@ def shuffle!(options = undefined)
10891087
end
10901088

10911089
def drop(n)
1092-
n = Truffle::Type.coerce_to_collection_index n
1090+
n = Primitive.rb_num2int n
10931091
raise ArgumentError, 'attempt to drop negative size' if n < 0
10941092

10951093
new_size = size - n
@@ -1175,15 +1173,15 @@ def unshift(*values)
11751173
alias_method :prepend, :unshift
11761174

11771175
private def range_begin(range)
1178-
first = Truffle::Type.coerce_to_collection_index range.begin
1176+
first = Primitive.rb_num2int range.begin
11791177
first += size if first < 0
11801178
first
11811179
end
11821180

11831181
private def range_end(range)
11841182
last = range.end
11851183
return size - 1 if Primitive.nil? last
1186-
last = Truffle::Type.coerce_to_collection_index last
1184+
last = Primitive.rb_num2int last
11871185
last += size if last < 0
11881186
last -=1 if range.exclude_end?
11891187
last
@@ -1201,7 +1199,7 @@ def values_at(*args)
12011199
next if finish < start
12021200
start.upto(finish) { |i| out << at(i) }
12031201
else
1204-
i = Truffle::Type.coerce_to_collection_index elem
1202+
i = Primitive.rb_num2int elem
12051203
out << at(i)
12061204
end
12071205
end
@@ -1527,7 +1525,7 @@ def slice!(start, length=undefined)
15271525
else
15281526
# make sure that negative values are not passed through to the
15291527
# []= assignment
1530-
start = Truffle::Type.coerce_to_collection_index start
1528+
start = Primitive.rb_num2int start
15311529
start = start + size if start < 0
15321530

15331531
# This is to match the MRI behaviour of not extending the array
@@ -1546,8 +1544,8 @@ def slice!(start, length=undefined)
15461544
end
15471545
end
15481546
else
1549-
start = Truffle::Type.coerce_to_collection_index start
1550-
length = Truffle::Type.coerce_to_collection_length length
1547+
start = Primitive.rb_num2int start
1548+
length = Primitive.rb_num2int length
15511549
return nil if length < 0
15521550

15531551
out = self[start, length]

src/main/ruby/truffleruby/core/enumerable.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def cycle(many=nil)
513513
end
514514

515515
if many
516-
many = Truffle::Type.coerce_to_collection_index many
516+
many = Primitive.rb_num2int many
517517
return nil if many <= 0
518518
else
519519
many = nil
@@ -545,7 +545,7 @@ def cycle(many=nil)
545545
end
546546

547547
def drop(n)
548-
n = Truffle::Type.coerce_to_collection_index n
548+
n = Primitive.rb_num2int n
549549
raise ArgumentError, 'attempt to drop negative size' if n < 0
550550

551551
ary = to_a
@@ -567,7 +567,7 @@ def drop_while
567567
end
568568

569569
def each_cons(num)
570-
n = Truffle::Type.coerce_to_collection_index num
570+
n = Primitive.rb_num2int num
571571
raise ArgumentError, "invalid size: #{n}" if n <= 0
572572

573573
unless block_given?
@@ -594,7 +594,7 @@ def each_cons(num)
594594
end
595595

596596
def each_slice(slice_size)
597-
n = Truffle::Type.coerce_to_collection_index slice_size
597+
n = Primitive.rb_num2int slice_size
598598
raise ArgumentError, "invalid slice size: #{n}" if n <= 0
599599

600600
unless block_given?
@@ -916,7 +916,7 @@ def reverse_each(&block)
916916
end
917917

918918
def take(n)
919-
n = Truffle::Type.coerce_to_collection_index n
919+
n = Primitive.rb_num2int n
920920
raise ArgumentError, "attempt to take negative size: #{n}" if n < 0
921921

922922
array = []

src/main/ruby/truffleruby/core/enumerable_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Truffle
3030
module EnumerableHelper
3131
def self.cycle_size(enum_size, many)
3232
if many
33-
many = Truffle::Type.coerce_to_collection_index many
33+
many = Primitive.rb_num2int many
3434
many = 0 if many < 0
3535
Primitive.nil?(enum_size) ? nil : enum_size * many
3636
else

src/main/ruby/truffleruby/core/type.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -396,26 +396,6 @@ def self.clamp_to_int(n)
396396
end
397397
end
398398

399-
def self.coerce_to_collection_index(index)
400-
if fits_into_long?(index)
401-
index
402-
else
403-
index = Primitive.rb_to_int(index)
404-
check_long(index)
405-
index
406-
end
407-
end
408-
409-
def self.coerce_to_collection_length(length)
410-
if fits_into_long?(length)
411-
length
412-
else
413-
length = Primitive.rb_to_int(length)
414-
check_long(length)
415-
length
416-
end
417-
end
418-
419399
def self.coerce_to_float(obj)
420400
case obj
421401
when Float

0 commit comments

Comments
 (0)