Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 847dbde

Browse files
authoredAug 15, 2022
feat: make result of StaticArray#slice and StaticArray#concat as instance generics. Deprecate static methods (#2404)
1 parent 5faef3a commit 847dbde

9 files changed

+535
-513
lines changed
 

‎std/assembly/index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,9 @@ declare class Array<T> {
17741774
declare class StaticArray<T> {
17751775
[key: number]: T;
17761776
static fromArray<T>(source: Array<T>): StaticArray<T>;
1777+
/** @deprecated */
17771778
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T>;
1779+
/** @deprecated */
17781780
static slice<T>(source: StaticArray<T>, start?: i32, end?: i32): StaticArray<T>;
17791781
readonly length: i32;
17801782
constructor(length?: i32);
@@ -1794,7 +1796,9 @@ declare class StaticArray<T> {
17941796
every(callbackfn: (value: T, index: i32, array: StaticArray<T>) => bool): bool;
17951797
some(callbackfn: (value: T, index: i32, array: StaticArray<T>) => bool): bool;
17961798
concat(items: Array<T>): Array<T>;
1799+
concat<U extends ArrayLike<T>>(other: U): U;
17971800
slice(from?: i32, to?: i32): Array<T>;
1801+
slice<U extends ArrayLike<T>>(from?: i32, to?: i32): U;
17981802
sort(comparator?: (a: T, b: T) => i32): this;
17991803
join(separator?: string): string;
18001804
reverse(): this;

‎std/assembly/staticarray.ts

+96-81
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,14 @@ export class StaticArray<T> {
3737
return out;
3838
}
3939

40+
/** @deprecated Please use source.concat<StaticArray<T>> instead. */
4041
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T> {
41-
var sourceLen = source.length;
42-
var otherLen = other.length;
43-
var outLen = sourceLen + otherLen;
44-
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
45-
var out = changetype<StaticArray<T>>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
46-
var outStart = changetype<usize>(out);
47-
var sourceSize = <usize>sourceLen << alignof<T>();
48-
if (isManaged<T>()) {
49-
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
50-
let ref = load<usize>(changetype<usize>(source) + offset);
51-
store<usize>(outStart + offset, ref);
52-
__link(changetype<usize>(out), ref, true);
53-
}
54-
outStart += sourceSize;
55-
let otherSize = <usize>otherLen << alignof<T>();
56-
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
57-
let ref = load<usize>(changetype<usize>(other) + offset);
58-
store<usize>(outStart + offset, ref);
59-
__link(changetype<usize>(out), ref, true);
60-
}
61-
} else {
62-
memory.copy(outStart, changetype<usize>(source), sourceSize);
63-
memory.copy(outStart + sourceSize, changetype<usize>(other), <usize>otherLen << alignof<T>());
64-
}
65-
return out;
42+
return source.concat<StaticArray<T>>(other);
6643
}
6744

45+
/** @deprecated Please use source.slice<StaticArray<T>> instead. */
6846
static slice<T>(source: StaticArray<T>, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
69-
var length = source.length;
70-
start = start < 0 ? max(start + length, 0) : min(start, length);
71-
end = end < 0 ? max(end + length, 0) : min(end , length);
72-
length = max(end - start, 0);
73-
var sliceSize = <usize>length << alignof<T>();
74-
var slice = changetype<StaticArray<T>>(__new(sliceSize, idof<StaticArray<T>>()));
75-
var sourcePtr = changetype<usize>(source) + (<usize>start << alignof<T>());
76-
if (isManaged<T>()) {
77-
let off: usize = 0;
78-
while (off < sliceSize) {
79-
let ref = load<usize>(sourcePtr + off);
80-
store<usize>(changetype<usize>(slice) + off, ref);
81-
__link(changetype<usize>(slice), ref, true);
82-
off += sizeof<usize>();
83-
}
84-
} else {
85-
memory.copy(changetype<usize>(slice), sourcePtr, sliceSize);
86-
}
87-
return slice;
47+
return source.slice<StaticArray<T>>(start, end);
8848
}
8949

9050
constructor(length: i32) {
@@ -210,57 +170,112 @@ export class StaticArray<T> {
210170
return -1;
211171
}
212172

213-
concat(other: Array<T>): Array<T> {
214-
var thisLen = this.length;
215-
var otherLen = other.length;
216-
var outLen = thisLen + otherLen;
217-
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
218-
var out = changetype<Array<T>>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
219-
var outStart = out.dataStart;
220-
var thisSize = <usize>thisLen << alignof<T>();
221-
if (isManaged<T>()) {
173+
concat<U extends ArrayLike<T> = Array<T>>(other: U): U {
174+
let sourceLen = this.length;
175+
let otherLen = other.length;
176+
let outLen = sourceLen + otherLen;
177+
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) {
178+
throw new Error(E_INVALIDLENGTH);
179+
}
180+
let sourceSize = <usize>sourceLen << alignof<T>();
181+
let out!: U;
182+
183+
if (out instanceof Array<T>) {
184+
out = changetype<U>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
185+
let outStart = changetype<Array<T>>(out).dataStart;
186+
let otherStart = changetype<Array<T>>(other).dataStart;
222187
let thisStart = changetype<usize>(this);
223-
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
224-
let ref = load<usize>(thisStart + offset);
225-
store<usize>(outStart + offset, ref);
226-
__link(changetype<usize>(out), ref, true);
188+
189+
if (isManaged<T>()) {
190+
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
191+
let ref = load<usize>(thisStart + offset);
192+
store<usize>(outStart + offset, ref);
193+
__link(changetype<usize>(out), ref, true);
194+
}
195+
outStart += sourceSize;
196+
let otherSize = <usize>otherLen << alignof<T>();
197+
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
198+
let ref = load<usize>(otherStart + offset);
199+
store<usize>(outStart + offset, ref);
200+
__link(changetype<usize>(out), ref, true);
201+
}
202+
} else {
203+
memory.copy(outStart, thisStart, sourceSize);
204+
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
227205
}
228-
outStart += thisSize;
229-
let otherStart = other.dataStart;
230-
let otherSize = <usize>otherLen << alignof<T>();
231-
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
232-
let ref = load<usize>(otherStart + offset);
233-
store<usize>(outStart + offset, ref);
234-
__link(changetype<usize>(out), ref, true);
206+
} else if (out instanceof StaticArray<T>) {
207+
out = changetype<U>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
208+
let outStart = changetype<usize>(out);
209+
let otherStart = changetype<usize>(other);
210+
let thisStart = changetype<usize>(this);
211+
212+
if (isManaged<T>()) {
213+
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
214+
let ref = load<usize>(thisStart + offset);
215+
store<usize>(outStart + offset, ref);
216+
__link(outStart, ref, true);
217+
}
218+
outStart += sourceSize;
219+
let otherSize = <usize>otherLen << alignof<T>();
220+
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
221+
let ref = load<usize>(otherStart + offset);
222+
store<usize>(outStart + offset, ref);
223+
__link(outStart, ref, true);
224+
}
225+
} else {
226+
memory.copy(outStart, thisStart, sourceSize);
227+
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
235228
}
236229
} else {
237-
memory.copy(outStart, changetype<usize>(this), thisSize);
238-
memory.copy(outStart + thisSize, other.dataStart, <usize>otherLen << alignof<T>());
230+
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
239231
}
240232
return out;
241233
}
242234

243-
slice(start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
235+
slice<U extends ArrayLike<T> = Array<T>>(start: i32 = 0, end: i32 = i32.MAX_VALUE): U {
244236
var length = this.length;
245237
start = start < 0 ? max(start + length, 0) : min(start, length);
246-
end = end < 0 ? max(end + length, 0) : min(end , length);
238+
end = end < 0 ? max(end + length, 0) : min(end, length);
247239
length = max(end - start, 0);
248-
var slice = changetype<Array<T>>(__newArray(length, alignof<T>(), idof<Array<T>>()));
249-
var sliceBase = slice.dataStart;
250-
var thisBase = changetype<usize>(this) + (<usize>start << alignof<T>());
251-
if (isManaged<T>()) {
252-
let off = <usize>0;
253-
let end = <usize>length << alignof<usize>();
254-
while (off < end) {
255-
let ref = load<usize>(thisBase + off);
256-
store<usize>(sliceBase + off, ref);
257-
__link(changetype<usize>(slice), ref, true);
258-
off += sizeof<usize>();
240+
241+
var sourceStart = changetype<usize>(this) + (<usize>start << alignof<T>());
242+
var size = <usize>length << alignof<T>();
243+
let out!: U;
244+
245+
if (out instanceof Array<T>) {
246+
// return Array
247+
out = changetype<U>(__newArray(length, alignof<T>(), idof<Array<T>>()));
248+
let outStart = changetype<Array<T>>(out).dataStart;
249+
if (isManaged<T>()) {
250+
let off: usize = 0;
251+
while (off < size) {
252+
let ref = load<usize>(sourceStart + off);
253+
store<usize>(outStart + off, ref);
254+
__link(changetype<usize>(out), ref, true);
255+
off += sizeof<usize>();
256+
}
257+
} else {
258+
memory.copy(outStart, sourceStart, size);
259+
}
260+
} else if (out instanceof StaticArray<T>) {
261+
// return StaticArray
262+
out = changetype<U>(__new(size, idof<StaticArray<T>>()));
263+
let outStart = changetype<usize>(out);
264+
if (isManaged<T>()) {
265+
let off: usize = 0;
266+
while (off < size) {
267+
let ref = load<usize>(sourceStart + off);
268+
store<usize>(outStart + off, ref);
269+
__link(outStart, ref, true);
270+
off += sizeof<usize>();
271+
}
272+
} else {
273+
memory.copy(outStart, sourceStart, size);
259274
}
260275
} else {
261-
memory.copy(sliceBase, thisBase, length << alignof<T>());
276+
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
262277
}
263-
return slice;
278+
return out;
264279
}
265280

266281
findIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {

‎tests/compiler/bindings/esm.debug.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@
24252425
if
24262426
i32.const 528
24272427
i32.const 832
2428-
i32.const 118
2428+
i32.const 78
24292429
i32.const 41
24302430
call $~lib/builtins/abort
24312431
unreachable
@@ -2460,7 +2460,7 @@
24602460
if
24612461
i32.const 528
24622462
i32.const 832
2463-
i32.const 133
2463+
i32.const 93
24642464
i32.const 41
24652465
call $~lib/builtins/abort
24662466
unreachable
@@ -3661,7 +3661,7 @@
36613661
if
36623662
i32.const 224
36633663
i32.const 832
3664-
i32.const 91
3664+
i32.const 51
36653665
i32.const 60
36663666
call $~lib/builtins/abort
36673667
unreachable

‎tests/compiler/bindings/esm.release.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@
17271727
if
17281728
i32.const 1552
17291729
i32.const 1856
1730-
i32.const 118
1730+
i32.const 78
17311731
i32.const 41
17321732
call $~lib/builtins/abort
17331733
unreachable
@@ -1751,7 +1751,7 @@
17511751
if
17521752
i32.const 1552
17531753
i32.const 1856
1754-
i32.const 133
1754+
i32.const 93
17551755
i32.const 41
17561756
call $~lib/builtins/abort
17571757
unreachable
@@ -2832,7 +2832,7 @@
28322832
if
28332833
i32.const 1248
28342834
i32.const 1856
2835-
i32.const 91
2835+
i32.const 51
28362836
i32.const 60
28372837
call $~lib/builtins/abort
28382838
unreachable

‎tests/compiler/bindings/raw.debug.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@
24282428
if
24292429
i32.const 528
24302430
i32.const 832
2431-
i32.const 118
2431+
i32.const 78
24322432
i32.const 41
24332433
call $~lib/builtins/abort
24342434
unreachable
@@ -2463,7 +2463,7 @@
24632463
if
24642464
i32.const 528
24652465
i32.const 832
2466-
i32.const 133
2466+
i32.const 93
24672467
i32.const 41
24682468
call $~lib/builtins/abort
24692469
unreachable
@@ -3664,7 +3664,7 @@
36643664
if
36653665
i32.const 224
36663666
i32.const 832
3667-
i32.const 91
3667+
i32.const 51
36683668
i32.const 60
36693669
call $~lib/builtins/abort
36703670
unreachable

‎tests/compiler/bindings/raw.release.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@
17271727
if
17281728
i32.const 1552
17291729
i32.const 1856
1730-
i32.const 118
1730+
i32.const 78
17311731
i32.const 41
17321732
call $~lib/builtins/abort
17331733
unreachable
@@ -1751,7 +1751,7 @@
17511751
if
17521752
i32.const 1552
17531753
i32.const 1856
1754-
i32.const 133
1754+
i32.const 93
17551755
i32.const 41
17561756
call $~lib/builtins/abort
17571757
unreachable
@@ -2832,7 +2832,7 @@
28322832
if
28332833
i32.const 1248
28342834
i32.const 1856
2835-
i32.const 91
2835+
i32.const 51
28362836
i32.const 60
28372837
call $~lib/builtins/abort
28382838
unreachable
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.