Skip to content

Sync Array's & TypedArray's "includes" with spec #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,21 @@ export class Array<T> extends ArrayBufferView {
}

includes(value: T, fromIndex: i32 = 0): bool {
return this.indexOf(value, fromIndex) >= 0;
if (isFloat<T>()) {
let length = this.length_;
if (length == 0 || fromIndex >= length) return false;
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
let dataStart = this.dataStart;
while (fromIndex < length) {
let elem = load<T>(dataStart + (<usize>fromIndex << alignof<T>()));
// @ts-ignore
if (elem == value || isNaN(elem) & isNaN(value)) return true;
++fromIndex;
}
return false;
} else {
return this.indexOf(value, fromIndex) >= 0;
}
}

indexOf(value: T, fromIndex: i32 = 0): i32 {
Expand Down
6 changes: 3 additions & 3 deletions std/assembly/dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class DataView {
getFloat64(byteOffset: i32, littleEndian: boolean = false): f64 {
if (
(byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
) throw new RangeError(E_INDEXOUTOFRANGE);
return littleEndian
? load<f64>(this.dataStart + <usize>byteOffset)
: reinterpret<f64>(
Expand All @@ -63,15 +63,15 @@ export class DataView {
getInt16(byteOffset: i32, littleEndian: boolean = false): i16 {
if (
(byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
) throw new RangeError(E_INDEXOUTOFRANGE);
var result: i16 = load<i16>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<i16>(result);
}

getInt32(byteOffset: i32, littleEndian: boolean = false): i32 {
if (
(byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
) throw new RangeError(E_INDEXOUTOFRANGE);
) throw new RangeError(E_INDEXOUTOFRANGE);
var result: i32 = load<i32>(this.dataStart + <usize>byteOffset);
return littleEndian ? result : bswap<i32>(result);
}
Expand Down
17 changes: 16 additions & 1 deletion std/assembly/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,22 @@ function INCLUDES<TArray extends ArrayBufferView, T>(
searchElement: T,
fromIndex: i32,
): bool {
return INDEX_OF<TArray, T>(array, searchElement, fromIndex) >= 0;
if (isFloat<T>()) {
let index: isize = fromIndex;
let length: isize = array.length;
if (length == 0 || index >= length) return false;
if (index < 0) index = max(length + index, 0);
let dataStart = array.dataStart;
while (index < length) {
let elem = load<T>(dataStart + (index << alignof<T>()));
// @ts-ignore
if (elem == searchElement || isNaN(elem) & isNaN(searchElement)) return true;
++index;
}
return false;
} else {
return INDEX_OF<TArray, T>(array, searchElement, fromIndex) >= 0;
}
}

// @ts-ignore: decorator
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/retain-release-sanity.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@
if
i32.const 424
i32.const 376
i32.const 271
i32.const 285
i32.const 20
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/retain-release-sanity.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3909,7 +3909,7 @@
if
i32.const 424
i32.const 376
i32.const 271
i32.const 285
i32.const 20
call $~lib/builtins/abort
unreachable
Expand Down
1,760 changes: 1,029 additions & 731 deletions tests/compiler/std/array.optimized.wat

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/compiler/std/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ var i: i32;

i = arr.indexOf(43, 2);
assert(i == 3);

assert(([NaN] as f32[]).indexOf(NaN) == -1);
assert(([NaN] as f64[]).indexOf(NaN) == -1);
}

// Array#includes //////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -337,6 +340,9 @@ var i: i32;
includes = arr.includes(43, 2);
assert(includes == true);

assert(([NaN] as f32[]).includes(NaN));
assert(([NaN] as f64[]).includes(NaN));

arr.splice(1, 1);

assert(arr.length == 4);
Expand Down
2,325 changes: 1,362 additions & 963 deletions tests/compiler/std/array.untouched.wat

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/compiler/std/dataview.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,7 @@
i32.const 280
i32.const 432
i32.const 48
i32.const 7
i32.const 6
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -2123,7 +2123,7 @@
i32.const 280
i32.const 432
i32.const 66
i32.const 7
i32.const 6
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -2157,7 +2157,7 @@
i32.const 280
i32.const 432
i32.const 74
i32.const 7
i32.const 6
call $~lib/builtins/abort
unreachable
end
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/std/dataview.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3841,7 +3841,7 @@
i32.const 280
i32.const 432
i32.const 48
i32.const 7
i32.const 6
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -3913,7 +3913,7 @@
i32.const 280
i32.const 432
i32.const 66
i32.const 7
i32.const 6
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -3961,7 +3961,7 @@
i32.const 280
i32.const 432
i32.const 74
i32.const 7
i32.const 6
call $~lib/builtins/abort
unreachable
end
Expand Down
Loading