Skip to content

Commit 19d06a3

Browse files
MaxGraeydcodeIO
authored andcommitted
Implement TypedArray#copyWithin (AssemblyScript#830)
1 parent c65a056 commit 19d06a3

File tree

8 files changed

+3506
-2218
lines changed

8 files changed

+3506
-2218
lines changed

examples/n-body/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"tsbuild": "tsc -p assembly -t ES2017 -m commonjs --outDir build",
1111
"build": "npm run asbuild && npm run tsbuild",
1212
"server": "http-server . -o -c-1",
13-
"test": "node --noliftoff --nowasm-tier-up --wasm-lazy-compilation --wasm-no-bounds-checks --expose-gc tests"
13+
"test": "node --wasm-lazy-compilation --wasm-no-bounds-checks --expose-gc tests"
1414
},
1515
"devDependencies": {
1616
"http-server": "^0.11.1",

std/assembly/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,8 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
11411141
slice(begin?: i32, end?: i32): TypedArray<T>;
11421142
/** Returns a new TypedArray of this type on the same ArrayBuffer from begin inclusive to end exclusive. */
11431143
subarray(begin?: i32, end?: i32): TypedArray<T>;
1144+
/** The copyWithin() method copies the sequence of array elements within the array to the position starting at target. The copy is taken from the index positions of the second and third arguments start and end. The end argument is optional and defaults to the length of the array. */
1145+
copyWithin(target: i32, start: i32, end?: i32): this;
11441146
/** 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(). */
11451147
reduce<U>(callbackfn: (accumulator: U, value: T, index: i32, self: this) => U, initialValue: U): U;
11461148
/** 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(). */

std/assembly/typedarray.ts

+69
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export class Int8Array extends ArrayBufferView {
7272
return SUBARRAY<Int8Array, i8>(this, begin, end);
7373
}
7474

75+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Int8Array {
76+
return COPY_WITHIN<Int8Array, i8>(this, target, start, end);
77+
}
78+
7579
reduce<T>(
7680
fn: (accumulator: T, value: i8, index: i32, array: Int8Array) => T,
7781
initialValue: T,
@@ -188,6 +192,10 @@ export class Uint8Array extends ArrayBufferView {
188192
return SUBARRAY<Uint8Array, u8>(this, begin, end);
189193
}
190194

195+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Uint8Array {
196+
return COPY_WITHIN<Uint8Array, u8>(this, target, start, end);
197+
}
198+
191199
reduce<T>(
192200
fn: (accumulator: T, value: u8, index: i32, array: Uint8Array) => T,
193201
initialValue: T,
@@ -304,6 +312,10 @@ export class Uint8ClampedArray extends ArrayBufferView {
304312
return SUBARRAY<Uint8ClampedArray, u8>(this, start, end);
305313
}
306314

315+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Uint8ClampedArray {
316+
return COPY_WITHIN<Uint8ClampedArray, u8>(this, target, start, end);
317+
}
318+
307319
reduce<T>(
308320
fn: (accumulator: T, value: u8, index: i32, array: Uint8ClampedArray) => T,
309321
initialValue: T,
@@ -420,6 +432,10 @@ export class Int16Array extends ArrayBufferView {
420432
return SUBARRAY<Int16Array, i16>(this, begin, end);
421433
}
422434

435+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Int16Array {
436+
return COPY_WITHIN<Int16Array, i16>(this, target, start, end);
437+
}
438+
423439
reduce<T>(
424440
fn: (accumulator: T, value: i16, index: i32, array: Int16Array) => T,
425441
initialValue: T,
@@ -536,6 +552,10 @@ export class Uint16Array extends ArrayBufferView {
536552
return SUBARRAY<Uint16Array, u16>(this, begin, end);
537553
}
538554

555+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Uint16Array {
556+
return COPY_WITHIN<Uint16Array, u16>(this, target, start, end);
557+
}
558+
539559
reduce<T>(
540560
fn: (accumulator: T, value: u16, index: i32, array: Uint16Array) => T,
541561
initialValue: T,
@@ -652,6 +672,10 @@ export class Int32Array extends ArrayBufferView {
652672
return SUBARRAY<Int32Array, i32>(this, begin, end);
653673
}
654674

675+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Int32Array {
676+
return COPY_WITHIN<Int32Array, i32>(this, target, start, end);
677+
}
678+
655679
reduce<T>(
656680
fn: (accumulator: T, value: i32, index: i32, array: Int32Array) => T,
657681
initialValue: T,
@@ -768,6 +792,10 @@ export class Uint32Array extends ArrayBufferView {
768792
return SUBARRAY<Uint32Array, u32>(this, begin, end);
769793
}
770794

795+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Uint32Array {
796+
return COPY_WITHIN<Uint32Array, u32>(this, target, start, end);
797+
}
798+
771799
reduce<T>(
772800
fn: (accumulator: T, value: u32, index: i32, array: Uint32Array) => T,
773801
initialValue: T,
@@ -884,6 +912,10 @@ export class Int64Array extends ArrayBufferView {
884912
return SUBARRAY<Int64Array, i64>(this, begin, end);
885913
}
886914

915+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Int64Array {
916+
return COPY_WITHIN<Int64Array, i64>(this, target, start, end);
917+
}
918+
887919
reduce<T>(
888920
fn: (accumulator: T, value: i64, index: i32, array: Int64Array) => T,
889921
initialValue: T,
@@ -1000,6 +1032,10 @@ export class Uint64Array extends ArrayBufferView {
10001032
return SUBARRAY<Uint64Array, u64>(this, begin, end);
10011033
}
10021034

1035+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Uint64Array {
1036+
return COPY_WITHIN<Uint64Array, u64>(this, target, start, end);
1037+
}
1038+
10031039
reduce<T>(
10041040
fn: (accumulator: T, value: u64, index: i32, array: Uint64Array) => T,
10051041
initialValue: T,
@@ -1116,6 +1152,10 @@ export class Float32Array extends ArrayBufferView {
11161152
return SUBARRAY<Float32Array, f32>(this, begin, end);
11171153
}
11181154

1155+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Float32Array {
1156+
return COPY_WITHIN<Float32Array, f32>(this, target, start, end);
1157+
}
1158+
11191159
reduce<T>(
11201160
fn: (accumulator: T, value: f32, index: i32, array: Float32Array) => T,
11211161
initialValue: T,
@@ -1232,6 +1272,10 @@ export class Float64Array extends ArrayBufferView {
12321272
return SUBARRAY<Float64Array, f64>(this, begin, end);
12331273
}
12341274

1275+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Float64Array {
1276+
return COPY_WITHIN<Float64Array, f64>(this, target, start, end);
1277+
}
1278+
12351279
reduce<T>(
12361280
fn: (accumulator: T, value: f64, index: i32, array: Float64Array) => T,
12371281
initialValue: T,
@@ -1361,6 +1405,31 @@ function SUBARRAY<TArray extends ArrayBufferView, T>(
13611405
return out;
13621406
}
13631407

1408+
// @ts-ignore: decorator
1409+
@inline
1410+
function COPY_WITHIN<TArray extends ArrayBufferView, T>(
1411+
array: TArray,
1412+
target: i32,
1413+
start: i32,
1414+
end: i32
1415+
): TArray {
1416+
var len = array.length;
1417+
var dataStart = array.dataStart;
1418+
1419+
end = min<i32>(end, len);
1420+
var to = target < 0 ? max(len + target, 0) : min(target, len);
1421+
var from = start < 0 ? max(len + start, 0) : min(start, len);
1422+
var last = end < 0 ? max(len + end, 0) : min(end, len);
1423+
var count = min(last - from, len - to);
1424+
1425+
memory.copy(
1426+
dataStart + (<usize>to << alignof<T>()),
1427+
dataStart + (<usize>from << alignof<T>()),
1428+
<usize>count << alignof<T>()
1429+
);
1430+
return array;
1431+
}
1432+
13641433
// @ts-ignore: decorator
13651434
@inline
13661435
function REDUCE<TArray extends ArrayBufferView, T, TRet>(

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 154
1637+
i32.const 158
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 154
3330+
i32.const 158
33313331
i32.const 44
33323332
call $~lib/builtins/abort
33333333
unreachable

0 commit comments

Comments
 (0)