Skip to content
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

Implement TypedArray#slice #792

Merged
merged 4 commits into from
Sep 4, 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
10 changes: 6 additions & 4 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,16 +1137,18 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
indexOf(searchElement: T, fromIndex?: i32): i32;
/** The lastIndexOf() method returns the last index at which a given element can be found in the typed array, or -1 if it is not present. The typed array is searched backwards, starting at fromIndex. */
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
/** Returns copied section of an TypedArray from begin inclusive to end exclusive */
slice(begin?: i32, end?: i32): TypedArray<T>;
/** Returns a new TypedArray of this type on the same ArrayBuffer from begin inclusive to end exclusive. */
subarray(begin?: i32, end?: i32): this;
subarray(begin?: i32, end?: i32): TypedArray<T>;
/** The reduce() method applies a function against an accumulator and each value of the typed array (from left-to-right) has to reduce it to a single value. This method has the same algorithm as Array.prototype.reduce(). */
reduce<W>(callbackfn: (accumulator: W, value: T, index: i32, self: this) => W, initialValue: W): W;
reduce<U>(callbackfn: (accumulator: U, value: T, index: i32, self: this) => U, initialValue: U): U;
/** The reduceRight() method applies a function against an accumulator and each value of the typed array (from left-to-right) has to reduce it to a single value, starting from the end of the array. This method has the same algorithm as Array.prototype.reduceRight(). */
reduceRight<W>(callbackfn: (accumulator: W, value: T, index: i32, self: this) => W, initialValue: W): W;
reduceRight<U>(callbackfn: (accumulator: U, value: T, index: i32, self: this) => U, initialValue: U): U;
/** The some() method tests whether some element in the typed array passes the test implemented by the provided function. This method has the same algorithm as Array.prototype.some().*/
some(callbackfn: (value: T, index: i32, self: this) => bool): bool;
/** The map() method creates a new typed array with the results of calling a provided function on every element in this typed array. This method has the same algorithm as Array.prototype.map().*/
map(callbackfn: (value: T, index: i32, self: this) => T): this;
map(callbackfn: (value: T, index: i32, self: this) => T): TypedArray<T>;
/** The sort() method sorts the elements of a typed array numerically in place and returns the typed array. This method has the same algorithm as Array.prototype.sort(), except that sorts the values numerically instead of as strings. TypedArray is one of the typed array types here. */
sort(callback?: (a: T, b: T) => i32): this;
/** The fill() method fills all the elements of a typed array from a start index to an end index with a static value. This method has the same algorithm as Array.prototype.fill(). */
Expand Down
102 changes: 83 additions & 19 deletions std/assembly/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export class Int8Array extends ArrayBufferView {
return SORT<Int8Array, i8>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int8Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
return SLICE<Int8Array, i8>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
return SUBARRAY<Int8Array, i8>(this, begin, end);
}

Expand Down Expand Up @@ -172,7 +176,11 @@ export class Uint8Array extends ArrayBufferView {
return SORT<Uint8Array, u8>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array {
return SLICE<Uint8Array, u8>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array {
return SUBARRAY<Uint8Array, u8>(this, begin, end);
}

Expand Down Expand Up @@ -280,7 +288,11 @@ export class Uint8ClampedArray extends ArrayBufferView {
return SORT<Uint8ClampedArray, u8>(this, fn);
}

subarray(start: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
return SLICE<Uint8ClampedArray, u8>(this, begin, end);
}

subarray(start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
return SUBARRAY<Uint8ClampedArray, u8>(this, start, end);
}

Expand Down Expand Up @@ -388,7 +400,11 @@ export class Int16Array extends ArrayBufferView {
return SORT<Int16Array, i16>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int16Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
return SLICE<Int16Array, i16>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
return SUBARRAY<Int16Array, i16>(this, begin, end);
}

Expand Down Expand Up @@ -496,7 +512,11 @@ export class Uint16Array extends ArrayBufferView {
return SORT<Uint16Array, u16>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint16Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
return SLICE<Uint16Array, u16>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
return SUBARRAY<Uint16Array, u16>(this, begin, end);
}

Expand Down Expand Up @@ -604,7 +624,11 @@ export class Int32Array extends ArrayBufferView {
return SORT<Int32Array, i32>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int32Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
return SLICE<Int32Array, i32>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
return SUBARRAY<Int32Array, i32>(this, begin, end);
}

Expand Down Expand Up @@ -712,7 +736,11 @@ export class Uint32Array extends ArrayBufferView {
return SORT<Uint32Array, u32>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint32Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
return SLICE<Uint32Array, u32>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
return SUBARRAY<Uint32Array, u32>(this, begin, end);
}

Expand Down Expand Up @@ -820,7 +848,11 @@ export class Int64Array extends ArrayBufferView {
return SORT<Int64Array, i64>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int64Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
return SLICE<Int64Array, i64>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
return SUBARRAY<Int64Array, i64>(this, begin, end);
}

Expand Down Expand Up @@ -928,7 +960,11 @@ export class Uint64Array extends ArrayBufferView {
return SORT<Uint64Array, u64>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint64Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
return SLICE<Uint64Array, u64>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
return SUBARRAY<Uint64Array, u64>(this, begin, end);
}

Expand Down Expand Up @@ -1036,7 +1072,11 @@ export class Float32Array extends ArrayBufferView {
return SORT<Float32Array, f32>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float32Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
return SLICE<Float32Array, f32>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
return SUBARRAY<Float32Array, f32>(this, begin, end);
}

Expand Down Expand Up @@ -1144,7 +1184,11 @@ export class Float64Array extends ArrayBufferView {
return SORT<Float64Array, f64>(this, comparator);
}

subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float64Array {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
return SLICE<Float64Array, f64>(this, begin, end);
}

subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
return SUBARRAY<Float64Array, f64>(this, begin, end);
}

Expand Down Expand Up @@ -1200,9 +1244,9 @@ function FILL<TArray extends ArrayBufferView, T extends number>(
end: i32
): TArray {
var dataStart = array.dataStart;
var length = array.length;
start = start < 0 ? max(length + start, 0) : min(start, length);
end = end < 0 ? max(length + end, 0) : min(end, length);
var len = array.length;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
if (sizeof<T>() == 1) {
if (start < end) memory.fill(dataStart + <usize>start, <u8>value, <usize>(end - start));
} else {
Expand All @@ -1219,10 +1263,10 @@ function SORT<TArray extends ArrayBufferView, T>(
array: TArray,
comparator: (a: T, b: T) => i32
): TArray {
var length = array.length;
if (length <= 1) return array;
var len = array.length;
if (len <= 1) return array;
var base = array.dataStart;
if (length == 2) {
if (len == 2) {
let a: T = load<T>(base, sizeof<T>()); // a = arr[1]
let b: T = load<T>(base); // b = arr[0]
if (comparator(a, b) < 0) {
Expand All @@ -1231,18 +1275,38 @@ function SORT<TArray extends ArrayBufferView, T>(
}
return array;
}
SORT_IMPL<T>(base, length, comparator);
SORT_IMPL<T>(base, len, comparator);
return array;
}

// @ts-ignore: decorator
@inline
function SLICE<TArray extends ArrayBufferView, T>(
array: TArray,
start: i32,
end: i32
): TArray {
var len = array.length;
start = start < 0 ? max(start + len, 0) : min(start, len);
end = end < 0 ? max(end + len, 0) : min(end , len);
len = max(end - start, 0);
var slice = instantiate<TArray>(len);
memory.copy(
slice.dataStart,
array.dataStart + (<usize>start << alignof<T>()),
<usize>len << alignof<T>()
);
return slice;
}

// @ts-ignore: decorator
@inline
function SUBARRAY<TArray extends ArrayBufferView, T>(
array: TArray,
begin: i32,
end: i32
): TArray {
var len = <i32>array.length;
var len = array.length;
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
end = max(end, begin);
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/dataview.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@
if
i32.const 280
i32.const 376
i32.const 146
i32.const 150
i32.const 44
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/dataview.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@
if
i32.const 280
i32.const 376
i32.const 146
i32.const 150
i32.const 44
call $~lib/builtins/abort
unreachable
Expand Down
Loading