Skip to content

Commit 83fa121

Browse files
MaxGraeydcodeIO
authored andcommitted
Add TypedArray#fill (AssemblyScript#274)
1 parent afb8fe7 commit 83fa121

File tree

10 files changed

+4570
-1816
lines changed

10 files changed

+4570
-1816
lines changed

std/assembly/array.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,13 @@ export class Array<T> {
119119
start = start < 0 ? max(len + start, 0) : min(start, len);
120120
end = end < 0 ? max(len + end, 0) : min(end, len);
121121
if (sizeof<T>() == 1) {
122-
memory.fill(
123-
changetype<usize>(buffer) + start + HEADER_SIZE,
124-
<u8>value,
125-
<usize>(end - start)
126-
);
122+
if (start < end) {
123+
memory.fill(
124+
changetype<usize>(buffer) + start + HEADER_SIZE,
125+
<u8>value,
126+
<usize>(end - start)
127+
);
128+
}
127129
} else {
128130
for (; start < end; ++start) {
129131
storeUnsafe<T,T>(buffer, start, value);

std/assembly/internal/typedarray.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,30 @@ export abstract class TypedArray<T,V> {
6363

6464
// copyWithin(target: i32, start: i32, end: i32 = this.length): this
6565

66+
fill(value: V, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
67+
var buffer = this.buffer;
68+
var byteOffset = this.byteOffset;
69+
var len = this.length;
70+
start = start < 0 ? max(len + start, 0) : min(start, len);
71+
end = end < 0 ? max(len + end, 0) : min(end, len);
72+
if (sizeof<T>() == 1) {
73+
if (start < end) {
74+
memory.fill(
75+
changetype<usize>(buffer) + start + byteOffset + AB_HEADER_SIZE,
76+
<u8>value,
77+
<usize>(end - start)
78+
);
79+
}
80+
} else {
81+
for (; start < end; ++start) {
82+
storeUnsafeWithOffset<T,V>(buffer, start, value, byteOffset);
83+
}
84+
}
85+
return this;
86+
}
87+
6688
@inline
67-
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): TypedArray<T,V> {
89+
subarray(begin: i32 = 0, end: i32 = i32.MAX_VALUE): TypedArray<T,V> {
6890
var length = this.length;
6991
if (begin < 0) begin = max(length + begin, 0);
7092
else begin = min(begin, length);

tests/compiler/std/array.optimized.wat

Lines changed: 623 additions & 558 deletions
Large diffs are not rendered by default.

tests/compiler/std/array.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ assert(isArraysEqual<u8>(arr8, <u8[]>[1, 1, 0, 0, 0]));
4444
arr8.fill(2, -2);
4545
assert(isArraysEqual<u8>(arr8, <u8[]>[1, 1, 0, 2, 2]));
4646

47+
arr8.fill(0, 1, 0);
48+
assert(isArraysEqual<u8>(arr8, <u8[]>[1, 1, 0, 2, 2]));
49+
4750
var arr32: u32[] = [1, 2, 3, 4, 5];
4851

4952
arr32.fill(1, 1, 3);
@@ -58,6 +61,9 @@ assert(isArraysEqual<u32>(arr32, <u32[]>[1, 1, 0, 0, 0]));
5861
arr32.fill(2, -2);
5962
assert(isArraysEqual<u32>(arr32, <u32[]>[1, 1, 0, 2, 2]));
6063

64+
arr32.fill(0, 1, 0);
65+
assert(isArraysEqual<u32>(arr32, <u32[]>[1, 1, 0, 2, 2]));
66+
6167
// Array#push/pop //////////////////////////////////////////////////////////////////////////////////
6268

6369
assert(arr.length == 0);

tests/compiler/std/array.untouched.wat

Lines changed: 572 additions & 510 deletions
Large diffs are not rendered by default.

tests/compiler/std/string.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4905,7 +4905,7 @@
49054905
(call $~lib/env/abort
49064906
(i32.const 0)
49074907
(i32.const 776)
4908-
(i32.const 172)
4908+
(i32.const 174)
49094909
(i32.const 42)
49104910
)
49114911
(unreachable)

tests/compiler/std/string.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5893,7 +5893,7 @@
58935893
(call $~lib/env/abort
58945894
(i32.const 0)
58955895
(i32.const 776)
5896-
(i32.const 172)
5896+
(i32.const 174)
58975897
(i32.const 42)
58985898
)
58995899
(unreachable)

0 commit comments

Comments
 (0)