Skip to content

feat: make result of StaticArray#slice and StaticArray#concat as instance generics. Deprecate static methods #2404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,9 @@ declare class Array<T> {
declare class StaticArray<T> {
[key: number]: T;
static fromArray<T>(source: Array<T>): StaticArray<T>;
/** @deprecated */
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T>;
/** @deprecated */
static slice<T>(source: StaticArray<T>, start?: i32, end?: i32): StaticArray<T>;
readonly length: i32;
constructor(length?: i32);
Expand All @@ -1794,7 +1796,9 @@ declare class StaticArray<T> {
every(callbackfn: (value: T, index: i32, array: StaticArray<T>) => bool): bool;
some(callbackfn: (value: T, index: i32, array: StaticArray<T>) => bool): bool;
concat(items: Array<T>): Array<T>;
concat<U extends ArrayLike<T>>(other: U): U;
slice(from?: i32, to?: i32): Array<T>;
slice<U extends ArrayLike<T>>(from?: i32, to?: i32): U;
sort(comparator?: (a: T, b: T) => i32): this;
join(separator?: string): string;
reverse(): this;
Expand Down
177 changes: 96 additions & 81 deletions std/assembly/staticarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,14 @@ export class StaticArray<T> {
return out;
}

/** @deprecated Please use source.concat<StaticArray<T>> instead. */
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T> {
var sourceLen = source.length;
var otherLen = other.length;
var outLen = sourceLen + otherLen;
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
var out = changetype<StaticArray<T>>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
var outStart = changetype<usize>(out);
var sourceSize = <usize>sourceLen << alignof<T>();
if (isManaged<T>()) {
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
let ref = load<usize>(changetype<usize>(source) + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
outStart += sourceSize;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let ref = load<usize>(changetype<usize>(other) + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
} else {
memory.copy(outStart, changetype<usize>(source), sourceSize);
memory.copy(outStart + sourceSize, changetype<usize>(other), <usize>otherLen << alignof<T>());
}
return out;
return source.concat<StaticArray<T>>(other);
}

/** @deprecated Please use source.slice<StaticArray<T>> instead. */
static slice<T>(source: StaticArray<T>, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
var length = source.length;
start = start < 0 ? max(start + length, 0) : min(start, length);
end = end < 0 ? max(end + length, 0) : min(end , length);
length = max(end - start, 0);
var sliceSize = <usize>length << alignof<T>();
var slice = changetype<StaticArray<T>>(__new(sliceSize, idof<StaticArray<T>>()));
var sourcePtr = changetype<usize>(source) + (<usize>start << alignof<T>());
if (isManaged<T>()) {
let off: usize = 0;
while (off < sliceSize) {
let ref = load<usize>(sourcePtr + off);
store<usize>(changetype<usize>(slice) + off, ref);
__link(changetype<usize>(slice), ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(changetype<usize>(slice), sourcePtr, sliceSize);
}
return slice;
return source.slice<StaticArray<T>>(start, end);
}

constructor(length: i32) {
Expand Down Expand Up @@ -210,57 +170,112 @@ export class StaticArray<T> {
return -1;
}

concat(other: Array<T>): Array<T> {
var thisLen = this.length;
var otherLen = other.length;
var outLen = thisLen + otherLen;
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
var out = changetype<Array<T>>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
var outStart = out.dataStart;
var thisSize = <usize>thisLen << alignof<T>();
if (isManaged<T>()) {
concat<U extends ArrayLike<T> = Array<T>>(other: U): U {
let sourceLen = this.length;
let otherLen = other.length;
let outLen = sourceLen + otherLen;
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) {
throw new Error(E_INVALIDLENGTH);
}
let sourceSize = <usize>sourceLen << alignof<T>();
let out!: U;

if (out instanceof Array<T>) {
out = changetype<U>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
let outStart = changetype<Array<T>>(out).dataStart;
let otherStart = changetype<Array<T>>(other).dataStart;
let thisStart = changetype<usize>(this);
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
let ref = load<usize>(thisStart + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);

if (isManaged<T>()) {
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
let ref = load<usize>(thisStart + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
outStart += sourceSize;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let ref = load<usize>(otherStart + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
}
} else {
memory.copy(outStart, thisStart, sourceSize);
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
}
outStart += thisSize;
let otherStart = other.dataStart;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let ref = load<usize>(otherStart + offset);
store<usize>(outStart + offset, ref);
__link(changetype<usize>(out), ref, true);
} else if (out instanceof StaticArray<T>) {
out = changetype<U>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
let outStart = changetype<usize>(out);
let otherStart = changetype<usize>(other);
let thisStart = changetype<usize>(this);

if (isManaged<T>()) {
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
let ref = load<usize>(thisStart + offset);
store<usize>(outStart + offset, ref);
__link(outStart, ref, true);
}
outStart += sourceSize;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let ref = load<usize>(otherStart + offset);
store<usize>(outStart + offset, ref);
__link(outStart, ref, true);
}
} else {
memory.copy(outStart, thisStart, sourceSize);
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
}
} else {
memory.copy(outStart, changetype<usize>(this), thisSize);
memory.copy(outStart + thisSize, other.dataStart, <usize>otherLen << alignof<T>());
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
}
return out;
}

slice(start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
slice<U extends ArrayLike<T> = Array<T>>(start: i32 = 0, end: i32 = i32.MAX_VALUE): U {
var length = this.length;
start = start < 0 ? max(start + length, 0) : min(start, length);
end = end < 0 ? max(end + length, 0) : min(end , length);
end = end < 0 ? max(end + length, 0) : min(end, length);
length = max(end - start, 0);
var slice = changetype<Array<T>>(__newArray(length, alignof<T>(), idof<Array<T>>()));
var sliceBase = slice.dataStart;
var thisBase = changetype<usize>(this) + (<usize>start << alignof<T>());
if (isManaged<T>()) {
let off = <usize>0;
let end = <usize>length << alignof<usize>();
while (off < end) {
let ref = load<usize>(thisBase + off);
store<usize>(sliceBase + off, ref);
__link(changetype<usize>(slice), ref, true);
off += sizeof<usize>();

var sourceStart = changetype<usize>(this) + (<usize>start << alignof<T>());
var size = <usize>length << alignof<T>();
let out!: U;

if (out instanceof Array<T>) {
// return Array
out = changetype<U>(__newArray(length, alignof<T>(), idof<Array<T>>()));
let outStart = changetype<Array<T>>(out).dataStart;
if (isManaged<T>()) {
let off: usize = 0;
while (off < size) {
let ref = load<usize>(sourceStart + off);
store<usize>(outStart + off, ref);
__link(changetype<usize>(out), ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(outStart, sourceStart, size);
}
} else if (out instanceof StaticArray<T>) {
// return StaticArray
out = changetype<U>(__new(size, idof<StaticArray<T>>()));
let outStart = changetype<usize>(out);
if (isManaged<T>()) {
let off: usize = 0;
while (off < size) {
let ref = load<usize>(sourceStart + off);
store<usize>(outStart + off, ref);
__link(outStart, ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(outStart, sourceStart, size);
}
} else {
memory.copy(sliceBase, thisBase, length << alignof<T>());
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
}
return slice;
return out;
}

findIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/bindings/esm.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2425,7 +2425,7 @@
if
i32.const 528
i32.const 832
i32.const 118
i32.const 78
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2460,7 +2460,7 @@
if
i32.const 528
i32.const 832
i32.const 133
i32.const 93
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3661,7 +3661,7 @@
if
i32.const 224
i32.const 832
i32.const 91
i32.const 51
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/bindings/esm.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@
if
i32.const 1552
i32.const 1856
i32.const 118
i32.const 78
i32.const 41
call $~lib/builtins/abort
unreachable
Expand All @@ -1751,7 +1751,7 @@
if
i32.const 1552
i32.const 1856
i32.const 133
i32.const 93
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2832,7 +2832,7 @@
if
i32.const 1248
i32.const 1856
i32.const 91
i32.const 51
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/bindings/raw.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2428,7 +2428,7 @@
if
i32.const 528
i32.const 832
i32.const 118
i32.const 78
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2463,7 +2463,7 @@
if
i32.const 528
i32.const 832
i32.const 133
i32.const 93
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3664,7 +3664,7 @@
if
i32.const 224
i32.const 832
i32.const 91
i32.const 51
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/bindings/raw.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@
if
i32.const 1552
i32.const 1856
i32.const 118
i32.const 78
i32.const 41
call $~lib/builtins/abort
unreachable
Expand All @@ -1751,7 +1751,7 @@
if
i32.const 1552
i32.const 1856
i32.const 133
i32.const 93
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2832,7 +2832,7 @@
if
i32.const 1248
i32.const 1856
i32.const 91
i32.const 51
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down
Loading