Skip to content

Commit 97d5462

Browse files
MaxGraeydcodeIO
authored andcommitted
Implement TypedArray#slice (AssemblyScript#792)
1 parent 2672042 commit 97d5462

File tree

7 files changed

+2694
-1901
lines changed

7 files changed

+2694
-1901
lines changed

std/assembly/index.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1137,16 +1137,18 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
11371137
indexOf(searchElement: T, fromIndex?: i32): i32;
11381138
/** 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. */
11391139
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
1140+
/** Returns copied section of an TypedArray from begin inclusive to end exclusive */
1141+
slice(begin?: i32, end?: i32): TypedArray<T>;
11401142
/** Returns a new TypedArray of this type on the same ArrayBuffer from begin inclusive to end exclusive. */
1141-
subarray(begin?: i32, end?: i32): this;
1143+
subarray(begin?: i32, end?: i32): TypedArray<T>;
11421144
/** 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(). */
1143-
reduce<W>(callbackfn: (accumulator: W, value: T, index: i32, self: this) => W, initialValue: W): W;
1145+
reduce<U>(callbackfn: (accumulator: U, value: T, index: i32, self: this) => U, initialValue: U): U;
11441146
/** 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(). */
1145-
reduceRight<W>(callbackfn: (accumulator: W, value: T, index: i32, self: this) => W, initialValue: W): W;
1147+
reduceRight<U>(callbackfn: (accumulator: U, value: T, index: i32, self: this) => U, initialValue: U): U;
11461148
/** 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().*/
11471149
some(callbackfn: (value: T, index: i32, self: this) => bool): bool;
11481150
/** 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().*/
1149-
map(callbackfn: (value: T, index: i32, self: this) => T): this;
1151+
map(callbackfn: (value: T, index: i32, self: this) => T): TypedArray<T>;
11501152
/** 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. */
11511153
sort(callback?: (a: T, b: T) => i32): this;
11521154
/** 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(). */

std/assembly/typedarray.ts

+83-19
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export class Int8Array extends ArrayBufferView {
6464
return SORT<Int8Array, i8>(this, comparator);
6565
}
6666

67-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int8Array {
67+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
68+
return SLICE<Int8Array, i8>(this, begin, end);
69+
}
70+
71+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
6872
return SUBARRAY<Int8Array, i8>(this, begin, end);
6973
}
7074

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

175-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8Array {
179+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array {
180+
return SLICE<Uint8Array, u8>(this, begin, end);
181+
}
182+
183+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array {
176184
return SUBARRAY<Uint8Array, u8>(this, begin, end);
177185
}
178186

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

283-
subarray(start: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
291+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
292+
return SLICE<Uint8ClampedArray, u8>(this, begin, end);
293+
}
294+
295+
subarray(start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
284296
return SUBARRAY<Uint8ClampedArray, u8>(this, start, end);
285297
}
286298

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

391-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int16Array {
403+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
404+
return SLICE<Int16Array, i16>(this, begin, end);
405+
}
406+
407+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
392408
return SUBARRAY<Int16Array, i16>(this, begin, end);
393409
}
394410

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

499-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint16Array {
515+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
516+
return SLICE<Uint16Array, u16>(this, begin, end);
517+
}
518+
519+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
500520
return SUBARRAY<Uint16Array, u16>(this, begin, end);
501521
}
502522

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

607-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int32Array {
627+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
628+
return SLICE<Int32Array, i32>(this, begin, end);
629+
}
630+
631+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
608632
return SUBARRAY<Int32Array, i32>(this, begin, end);
609633
}
610634

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

715-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint32Array {
739+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
740+
return SLICE<Uint32Array, u32>(this, begin, end);
741+
}
742+
743+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
716744
return SUBARRAY<Uint32Array, u32>(this, begin, end);
717745
}
718746

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

823-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int64Array {
851+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
852+
return SLICE<Int64Array, i64>(this, begin, end);
853+
}
854+
855+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
824856
return SUBARRAY<Int64Array, i64>(this, begin, end);
825857
}
826858

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

931-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint64Array {
963+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
964+
return SLICE<Uint64Array, u64>(this, begin, end);
965+
}
966+
967+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
932968
return SUBARRAY<Uint64Array, u64>(this, begin, end);
933969
}
934970

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

1039-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float32Array {
1075+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
1076+
return SLICE<Float32Array, f32>(this, begin, end);
1077+
}
1078+
1079+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
10401080
return SUBARRAY<Float32Array, f32>(this, begin, end);
10411081
}
10421082

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

1147-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float64Array {
1187+
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
1188+
return SLICE<Float64Array, f64>(this, begin, end);
1189+
}
1190+
1191+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
11481192
return SUBARRAY<Float64Array, f64>(this, begin, end);
11491193
}
11501194

@@ -1200,9 +1244,9 @@ function FILL<TArray extends ArrayBufferView, T extends number>(
12001244
end: i32
12011245
): TArray {
12021246
var dataStart = array.dataStart;
1203-
var length = array.length;
1204-
start = start < 0 ? max(length + start, 0) : min(start, length);
1205-
end = end < 0 ? max(length + end, 0) : min(end, length);
1247+
var len = array.length;
1248+
start = start < 0 ? max(len + start, 0) : min(start, len);
1249+
end = end < 0 ? max(len + end, 0) : min(end, len);
12061250
if (sizeof<T>() == 1) {
12071251
if (start < end) memory.fill(dataStart + <usize>start, <u8>value, <usize>(end - start));
12081252
} else {
@@ -1219,10 +1263,10 @@ function SORT<TArray extends ArrayBufferView, T>(
12191263
array: TArray,
12201264
comparator: (a: T, b: T) => i32
12211265
): TArray {
1222-
var length = array.length;
1223-
if (length <= 1) return array;
1266+
var len = array.length;
1267+
if (len <= 1) return array;
12241268
var base = array.dataStart;
1225-
if (length == 2) {
1269+
if (len == 2) {
12261270
let a: T = load<T>(base, sizeof<T>()); // a = arr[1]
12271271
let b: T = load<T>(base); // b = arr[0]
12281272
if (comparator(a, b) < 0) {
@@ -1231,18 +1275,38 @@ function SORT<TArray extends ArrayBufferView, T>(
12311275
}
12321276
return array;
12331277
}
1234-
SORT_IMPL<T>(base, length, comparator);
1278+
SORT_IMPL<T>(base, len, comparator);
12351279
return array;
12361280
}
12371281

1282+
// @ts-ignore: decorator
1283+
@inline
1284+
function SLICE<TArray extends ArrayBufferView, T>(
1285+
array: TArray,
1286+
start: i32,
1287+
end: i32
1288+
): TArray {
1289+
var len = array.length;
1290+
start = start < 0 ? max(start + len, 0) : min(start, len);
1291+
end = end < 0 ? max(end + len, 0) : min(end , len);
1292+
len = max(end - start, 0);
1293+
var slice = instantiate<TArray>(len);
1294+
memory.copy(
1295+
slice.dataStart,
1296+
array.dataStart + (<usize>start << alignof<T>()),
1297+
<usize>len << alignof<T>()
1298+
);
1299+
return slice;
1300+
}
1301+
12381302
// @ts-ignore: decorator
12391303
@inline
12401304
function SUBARRAY<TArray extends ArrayBufferView, T>(
12411305
array: TArray,
12421306
begin: i32,
12431307
end: i32
12441308
): TArray {
1245-
var len = <i32>array.length;
1309+
var len = array.length;
12461310
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
12471311
end = end < 0 ? max(len + end, 0) : min(end, len);
12481312
end = max(end, begin);

tests/compiler/std/dataview.optimized.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@
16341634
if
16351635
i32.const 280
16361636
i32.const 376
1637-
i32.const 146
1637+
i32.const 150
16381638
i32.const 44
16391639
call $~lib/builtins/abort
16401640
unreachable

tests/compiler/std/dataview.untouched.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -3327,7 +3327,7 @@
33273327
if
33283328
i32.const 280
33293329
i32.const 376
3330-
i32.const 146
3330+
i32.const 150
33313331
i32.const 44
33323332
call $~lib/builtins/abort
33333333
unreachable

0 commit comments

Comments
 (0)