Add Array#concat and add a return type for Array#push (#214)

This commit is contained in:
LiaoPeng
2018-10-30 22:07:53 +08:00
committed by Daniel Wirtz
parent c11605d10c
commit d864977a1a
6 changed files with 2895 additions and 1718 deletions

View File

@ -181,6 +181,25 @@ export class Array<T> {
return newLength;
}
concat(items: Array<T>): Array<T> {
var thisLen: isize = this.length_;
var otherLen = (items == null) ? 0 : items.length_;
var outLen = thisLen + otherLen;
var out: Array<T> = new Array<T>(outLen);
if (thisLen) {
memory.copy(changetype<usize>(out.buffer_) + HEADER_SIZE,
changetype<usize>(this.buffer_) + HEADER_SIZE,
<usize>(thisLen << alignof<T>()));
}
if (otherLen) {
memory.copy(changetype<usize>(out.buffer_) + HEADER_SIZE + <usize>(thisLen << alignof<T>()),
changetype<usize>(items.buffer_) + HEADER_SIZE,
<usize>(otherLen << alignof<T>()));
}
return out;
}
pop(): T {
var length = this.length_;
if (length < 1) throw new RangeError("Array is empty");

View File

@ -444,7 +444,8 @@ declare class Array<T> {
includes(searchElement: T, fromIndex?: i32): bool;
indexOf(searchElement: T, fromIndex?: i32): i32;
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
push(element: T): void;
push(element: T): i32;
concat(items: T[]): T[];
pop(): T;
forEach(callbackfn: (value: T, index: i32, array: Array<T>) => void): void;
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U>;