Skip to content

Commit

Permalink
Make Array#[] compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Nov 11, 2013
1 parent 3980f91 commit 9a58a42
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
87 changes: 58 additions & 29 deletions opal/core/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,50 +227,79 @@ def ==(other)
end

def [](index, length = undefined)
%x{
var size = self.length;
if Range === index
%x{
var exclude = index.exclude,
length = #{Opal.coerce_to `index.end`, Integer, :to_int},
index = #{Opal.coerce_to `index.begin`, Integer, :to_int},
size = self.length;
if (typeof index !== 'number' && !index._isNumber) {
if (index._isRange) {
var exclude = index.exclude;
length = index.end;
index = index.begin;
if (index < 0) {
index += size;
}
if (index > size) {
return nil;
}
if (index > 2147483648) {
#{raise RangeError, "bignum too big to convert into `long'"};
}
if (length < 0) {
length += size;
}
if (index > size || index < 0) {
return nil;
}
if (!exclude) length += 1;
return self.slice(index, length);
if (length < 0) {
length += size;
}
else {
#{ raise "bad arg for Array#[]" };
if (length > 2147483648) {
#{raise RangeError, "bignum too big to convert into `long'"};
}
}
if (index < 0) {
index += size;
if (!exclude) {
length += 1;
}
return self.slice(index, length);
}
else
index = Opal.coerce_to index, Integer, :to_int

if (length !== undefined) {
if (length < 0 || index > size || index < 0) {
return nil;
%x{
var size = self.length;
if (index < 0) {
index += size;
}
return self.slice(index, index + length);
}
else {
if (index >= size || index < 0) {
if (index > 2147483648) {
#{raise RangeError, "bignum too big to convert into `long'"};
}
if (index < 0) {
return nil;
}
return self[index];
if (length === undefined) {
if (index >= size || index < 0) {
return nil;
}
return self[index];
}
else {
length = #{Opal.coerce_to length, Integer, :to_int};
if (length > 2147483648) {
#{raise RangeError, "bignum too big to convert into `long'"};
}
if (length < 0 || index > size || index < 0) {
return nil;
}
return self.slice(index, index + length);
}
}
}
end
end

def []=(index, value, extra = undefined)
Expand Down
8 changes: 0 additions & 8 deletions spec/filters/bugs/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@

fails "Array#dup creates a new array containing all elements or the original"

fails "Array#[] tries to convert the passed argument to an Integer using #to_int"
fails "Array#[] accepts Range instances having a negative m and both signs for n with [m..n] and [m...n]"
fails "Array#[] tries to convert Range elements to Integers using #to_int with [m..n] and [m...n]"
fails "Array#[] returns nil if range start is not in the array with [m..n]"
fails "Array#[] raises a RangeError when the start index is out of range of Fixnum"
fails "Array#[] raises a RangeError when the start index is out of range of Fixnum"
fails "Array#[] raises a RangeError when the length is out of range of Fixnum"
fails "Array#[] raises a RangeError when the length is out of range of Fixnum"
fails "Array.[] can unpack 2 or more nested referenced array"

fails "Array#[]= calls to_ary on its rhs argument for multi-element sets"
Expand Down

0 comments on commit 9a58a42

Please sign in to comment.