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 2 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
2 changes: 2 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,7 @@ declare class StaticArray<T> {
[key: number]: T;
static fromArray<T>(source: Array<T>): StaticArray<T>;
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 @@ -1795,6 +1796,7 @@ declare class StaticArray<T> {
some(callbackfn: (value: T, index: i32, array: StaticArray<T>) => bool): bool;
concat(items: Array<T>): Array<T>;
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
75 changes: 40 additions & 35 deletions std/assembly/staticarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
import { Runtime } from "shared/runtime";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { idof } from "./builtins";
import { idof, isArray } from "./builtins";
import { Array } from "./array";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
Expand Down Expand Up @@ -65,26 +65,9 @@ export class StaticArray<T> {
return out;
}

/** @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 @@ -284,27 +267,49 @@ export class StaticArray<T> {
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 sourceBase = changetype<usize>(this) + (<usize>start << alignof<T>());
var size = <usize>length << alignof<T>();
let res!: U;

if (isArray<U>()) {
// return Array
res = changetype<U>(__newArray(length, alignof<T>(), idof<Array<T>>()));
let targetBase = changetype<Array<T>>(res).dataStart;
if (isManaged<T>()) {
let off: usize = 0;
while (off < size) {
let ref = load<usize>(sourceBase + off);
store<usize>(targetBase + off, ref);
__link(changetype<usize>(res), ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(targetBase, sourceBase, size);
}
} else {
memory.copy(sliceBase, thisBase, length << alignof<T>());
assert(res instanceof StaticArray<T>);
// return StaticArray
res = changetype<U>(__new(size, idof<StaticArray<T>>()));
let targetBase = changetype<usize>(res);
if (isManaged<T>()) {
let off: usize = 0;
while (off < size) {
let ref = load<usize>(sourceBase + off);
store<usize>(targetBase + off, ref);
__link(targetBase, ref, true);
off += sizeof<usize>();
}
} else {
memory.copy(targetBase, sourceBase, size);
}
}
return slice;
return res;
}

findIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
Expand Down
89 changes: 48 additions & 41 deletions tests/compiler/std/staticarray.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
if
i32.const 64
i32.const 128
i32.const 118
i32.const 101
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -168,7 +168,7 @@
if
i32.const 64
i32.const 128
i32.const 133
i32.const 116
i32.const 41
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -5832,7 +5832,7 @@
local.get $1
i32.const 0
global.get $~lib/builtins/i32.MAX_VALUE
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand Down Expand Up @@ -5896,7 +5896,7 @@
local.get $1
i32.const 1
i32.const 3
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand Down Expand Up @@ -5964,7 +5964,7 @@
local.get $1
i32.const 1
global.get $~lib/builtins/i32.MAX_VALUE
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand All @@ -5987,7 +5987,7 @@
local.get $1
i32.const 0
i32.const 50
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand All @@ -6008,7 +6008,7 @@
local.get $1
i32.const 100
global.get $~lib/builtins/i32.MAX_VALUE
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand All @@ -6028,7 +6028,7 @@
local.get $1
i32.const -1
global.get $~lib/builtins/i32.MAX_VALUE
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand Down Expand Up @@ -6072,7 +6072,7 @@
local.get $1
i32.const -2
i32.const -2
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand All @@ -6092,7 +6092,7 @@
local.get $1
i32.const 2
i32.const -2
call $~lib/staticarray/StaticArray.slice<~lib/string/String>
call $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>>
local.tee $0
i32.store offset=4
local.get $0
Expand Down Expand Up @@ -7295,7 +7295,7 @@
if
i32.const 656
i32.const 128
i32.const 91
i32.const 74
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -7490,7 +7490,7 @@
global.set $~lib/memory/__stack_pointer
local.get $8
)
(func $~lib/staticarray/StaticArray.slice<~lib/string/String> (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(func $~lib/staticarray/StaticArray<~lib/string/String>#slice<~lib/staticarray/StaticArray<~lib/string/String>> (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
Expand All @@ -7499,6 +7499,7 @@
(local $8 i32)
(local $9 i32)
(local $10 i32)
(local $11 i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
Expand Down Expand Up @@ -7571,61 +7572,67 @@
i32.gt_s
select
local.set $3
local.get $3
local.get $0
local.get $1
i32.const 2
i32.shl
i32.add
local.set $6
local.get $3
i32.const 2
i32.shl
local.set $7
i32.const 0
drop
i32.const 1
drop
global.get $~lib/memory/__stack_pointer
local.get $6
local.get $7
i32.const 8
call $~lib/rt/itcms/__new
local.tee $7
local.tee $4
i32.store
local.get $0
local.get $1
i32.const 2
i32.shl
i32.add
local.set $8
local.get $4
local.set $5
i32.const 1
drop
i32.const 0
local.set $4
local.set $8
loop $while-continue|0
local.get $4
local.get $6
local.get $8
local.get $7
i32.lt_u
local.set $5
local.get $5
local.set $9
local.get $9
if
local.get $6
local.get $8
local.get $4
i32.add
i32.load
local.set $9
local.get $7
local.get $4
local.set $10
local.get $5
local.get $8
i32.add
local.get $9
local.get $10
i32.store
local.get $7
local.get $9
local.get $5
local.get $10
i32.const 1
call $~lib/rt/itcms/__link
local.get $4
local.get $8
i32.const 4
i32.add
local.set $4
local.set $8
br $while-continue|0
end
end
local.get $7
local.set $10
local.get $4
local.set $11
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
local.get $10
local.get $11
)
(func $~lib/staticarray/StaticArray<~lib/string/String>#__get (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
Expand All @@ -7645,7 +7652,7 @@
if
i32.const 64
i32.const 128
i32.const 118
i32.const 101
i32.const 41
call $~lib/builtins/abort
unreachable
Expand All @@ -7669,7 +7676,7 @@
if
i32.const 1152
i32.const 128
i32.const 122
i32.const 105
i32.const 40
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -7722,7 +7729,7 @@
if
i32.const 656
i32.const 128
i32.const 261
i32.const 244
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down
Loading