Skip to content

Commit 3f03539

Browse files
MaxGraeydcodeIO
authored andcommitted
Add Array#fill (AssemblyScript#250)
1 parent 9c770d8 commit 3f03539

File tree

6 files changed

+2959
-1653
lines changed

6 files changed

+2959
-1653
lines changed

std/assembly/array.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ export class Array<T> {
9999
if (isManaged<T>()) __gc_link(changetype<usize>(this), changetype<usize>(value)); // tslint:disable-line
100100
}
101101

102+
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
103+
var buffer = this.buffer_;
104+
var len = this.length_;
105+
start = start < 0 ? max(len + start, 0) : min(start, len);
106+
end = end < 0 ? max(len + end, 0) : min(end, len);
107+
if (sizeof<T>() == 1) {
108+
memory.fill(
109+
changetype<usize>(buffer) + start + HEADER_SIZE,
110+
<u8>value,
111+
<usize>(end - start)
112+
);
113+
} else {
114+
for (; start < end; ++start) {
115+
storeUnsafe<T,T>(buffer, start, value);
116+
}
117+
}
118+
return this;
119+
}
120+
102121
includes(searchElement: T, fromIndex: i32 = 0): bool {
103122
var length = this.length_;
104123
if (length == 0 || fromIndex >= length) return false;

std/assembly/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ declare class Array<T> {
436436
length: i32;
437437
/** Constructs a new array. */
438438
constructor(capacity?: i32);
439+
fill(value: T, start?: i32, end?: i32): this;
439440
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
440441
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
441442
includes(searchElement: T, fromIndex?: i32): bool;

std/portable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ declare class Array<T> {
247247
[key: number]: T;
248248
length: i32;
249249
constructor(capacity?: i32);
250+
fill(value: T, start?: i32, end?: i32): this;
250251
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
251252
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
252253
includes(searchElement: T, fromIndex?: i32): bool;

0 commit comments

Comments
 (0)