Skip to content

Commit

Permalink
Add support for Array subclass methods to return Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Fedorov committed Oct 26, 2021
1 parent 2d94d3b commit 01859b6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
7 changes: 7 additions & 0 deletions spec/truffleruby.mspec
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ class MSpecScript
# Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty
set :next, %w[
spec/ruby/core/nil/nil_spec.rb
spec/ruby/core/array/drop_spec.rb
spec/ruby/core/array/drop_while_spec.rb
spec/ruby/core/array/take_spec.rb
spec/ruby/core/array/take_while_spec.rb
spec/ruby/core/array/uniq_spec.rb
spec/ruby/core/array/multiply_spec.rb
spec/ruby/core/array/slice_spec.rb
]

set :tags_patterns, [
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/truffleruby/core/array/ArrayIndexNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.truffleruby.builtins.Primitive;
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
import org.truffleruby.core.array.library.ArrayStoreLibrary;
import org.truffleruby.core.klass.RubyClass;
import org.truffleruby.language.RubyContextSourceNode;
import org.truffleruby.language.RubyNode;

Expand Down Expand Up @@ -123,17 +122,16 @@ protected RubyArray readInBounds(RubyArray array, int index, int length,
? length
: size - index;
final Object slice = cowNode.execute(array, index, end);
return createArrayOfSameClass(array, slice, end);
return createArray(array, slice, end);
}

protected static boolean indexInBounds(RubyArray array, int index) {
return index >= 0 && index <= array.size;
}

protected RubyArray createArrayOfSameClass(RubyArray array, Object store, int size) {
final RubyClass logicalClass = array.getLogicalClass();
protected RubyArray createArray(RubyArray array, Object store, int size) {
RubyArray newArray = new RubyArray(
logicalClass,
coreLibrary().arrayClass,
getLanguage().arrayShape,
store,
size);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/truffleruby/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ public abstract static class MulNode extends PrimitiveArrayArgumentsNode {

@Specialization(guards = "count == 0")
protected RubyArray mulZero(RubyArray array, int count) {
final RubyClass logicalClass = array.getLogicalClass();
return new RubyArray(
logicalClass,
coreLibrary().arrayClass,
getLanguage().arrayShape,
ArrayStoreLibrary.INITIAL_STORE,
0);
Expand Down Expand Up @@ -197,9 +196,8 @@ protected RubyArray mulOther(RubyArray array, int count,
profileAndReportLoopCount(loopProfile, n);
}

final RubyClass logicalClass = array.getLogicalClass();
return new RubyArray(
logicalClass,
coreLibrary().arrayClass,
getLanguage().arrayShape,
newStore,
newSize);
Expand All @@ -217,9 +215,8 @@ protected RubyArray mulLong(RubyArray array, long count) {

@Specialization(guards = { "isEmptyArray(array)" })
protected RubyArray mulEmpty(RubyArray array, long count) {
final RubyClass logicalClass = array.getLogicalClass();
return new RubyArray(
logicalClass,
coreLibrary().arrayClass,
getLanguage().arrayShape,
ArrayStoreLibrary.INITIAL_STORE,
0);
Expand Down
12 changes: 3 additions & 9 deletions src/main/ruby/truffleruby/core/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def <=>(other)

def *(count)
result = Primitive.array_mul(self, count)

if !Primitive.undefined?(result)
result
elsif str = Truffle::Type.rb_check_convert_type(count, String, :to_str)
Expand Down Expand Up @@ -443,9 +444,9 @@ def first(n = undefined)

def flatten(level=-1)
level = Primitive.rb_num2int level
return self.dup if level == 0
return Array.new(self) if level == 0

out = self.class.allocate # new_reserved size
out = [] # new_reserved size
Primitive.array_flatten_helper(self, out, level)
out
end
Expand Down Expand Up @@ -1591,13 +1592,6 @@ def delete_range(index, del_length)
end
private :delete_range

def uniq(&block)
copy_of_same_class = dup
result = super(&block)
Primitive.steal_array_storage(copy_of_same_class, result)
copy_of_same_class
end

def uniq!(&block)
Primitive.check_frozen self
result = uniq(&block)
Expand Down
1 change: 1 addition & 0 deletions src/main/ruby/truffleruby/core/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ class Array
alias_method :take, :take
alias_method :drop_while, :drop_while
alias_method :take_while, :take_while
alias_method :uniq, :uniq
alias_method :sum, :sum
alias_method :all?, :all?
alias_method :none?, :none?
Expand Down

0 comments on commit 01859b6

Please sign in to comment.