toilscript 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/NOTICE +94 -94
- package/README.md +101 -114
- package/bin/asc.js +0 -0
- package/bin/asinit.js +6 -6
- package/dist/asc.generated.d.ts +10027 -0
- package/dist/asc.js +24474 -0
- package/dist/asc.js.map +7 -0
- package/dist/importmap.json +9 -0
- package/dist/toilscript.generated.d.ts +11242 -0
- package/dist/toilscript.js +337 -0
- package/dist/toilscript.js.map +7 -0
- package/dist/web.js +22 -0
- package/lib/binaryen.d.ts +2 -2
- package/lib/binaryen.js +2 -2
- package/package.json +115 -114
- package/std/README.md +6 -6
- package/std/assembly/array.ts +550 -550
- package/std/assembly/arraybuffer.ts +77 -77
- package/std/assembly/atomics.ts +127 -127
- package/std/assembly/bindings/asyncify.ts +16 -16
- package/std/assembly/bindings/dom.ts +291 -291
- package/std/assembly/bindings/node.ts +6 -6
- package/std/assembly/bitflags.ts +53 -53
- package/std/assembly/builtins.ts +2650 -2650
- package/std/assembly/byteslice.ts +177 -177
- package/std/assembly/compat.ts +2 -2
- package/std/assembly/console.ts +42 -42
- package/std/assembly/crypto.ts +9 -9
- package/std/assembly/dataview.ts +181 -181
- package/std/assembly/date.ts +375 -375
- package/std/assembly/diagnostics.ts +11 -11
- package/std/assembly/encoding.ts +151 -151
- package/std/assembly/endian.ts +45 -45
- package/std/assembly/error.ts +44 -44
- package/std/assembly/fixedarray.ts +173 -173
- package/std/assembly/fixedmap.ts +326 -326
- package/std/assembly/fixedset.ts +275 -275
- package/std/assembly/function.ts +42 -42
- package/std/assembly/index.d.ts +2892 -2891
- package/std/assembly/iterator.ts +35 -35
- package/std/assembly/map.ts +269 -269
- package/std/assembly/math.ts +3289 -3289
- package/std/assembly/memory.ts +123 -123
- package/std/assembly/number.ts +388 -388
- package/std/assembly/object.ts +36 -36
- package/std/assembly/performance.ts +9 -9
- package/std/assembly/pointer.ts +80 -80
- package/std/assembly/polyfills.ts +27 -27
- package/std/assembly/process.ts +50 -50
- package/std/assembly/reference.ts +48 -48
- package/std/assembly/regexp.ts +12 -12
- package/std/assembly/rt/README.md +83 -83
- package/std/assembly/rt/common.ts +81 -81
- package/std/assembly/rt/index-incremental.ts +2 -2
- package/std/assembly/rt/index-memory.ts +1 -1
- package/std/assembly/rt/index-minimal.ts +2 -2
- package/std/assembly/rt/index-stub.ts +1 -1
- package/std/assembly/rt/index.d.ts +37 -37
- package/std/assembly/rt/itcms.ts +419 -419
- package/std/assembly/rt/memory-runtime.ts +94 -94
- package/std/assembly/rt/rtrace.ts +15 -15
- package/std/assembly/rt/stub.ts +133 -133
- package/std/assembly/rt/tcms.ts +254 -254
- package/std/assembly/rt/tlsf.ts +592 -592
- package/std/assembly/rt.ts +90 -90
- package/std/assembly/set.ts +225 -225
- package/std/assembly/shared/feature.ts +68 -68
- package/std/assembly/shared/runtime.ts +13 -13
- package/std/assembly/shared/target.ts +11 -11
- package/std/assembly/shared/tsconfig.json +11 -11
- package/std/assembly/shared/typeinfo.ts +72 -72
- package/std/assembly/staticarray.ts +423 -423
- package/std/assembly/string.ts +850 -850
- package/std/assembly/symbol.ts +114 -114
- package/std/assembly/table.ts +16 -16
- package/std/assembly/toilscript.ts +16 -0
- package/std/assembly/tsconfig.json +6 -6
- package/std/assembly/typedarray.ts +1954 -1954
- package/std/assembly/uri.ts +17 -17
- package/std/assembly/util/bytes.ts +107 -107
- package/std/assembly/util/casemap.ts +497 -497
- package/std/assembly/util/error.ts +58 -58
- package/std/assembly/util/hash.ts +117 -117
- package/std/assembly/util/math.ts +1922 -1922
- package/std/assembly/util/memory.ts +290 -290
- package/std/assembly/util/number.ts +873 -873
- package/std/assembly/util/sort.ts +313 -313
- package/std/assembly/util/string.ts +1202 -1202
- package/std/assembly/util/uri.ts +275 -275
- package/std/assembly/vector.ts +4 -4
- package/std/assembly.json +16 -16
- package/std/portable/index.d.ts +461 -461
- package/std/portable/index.js +416 -416
- package/std/portable.json +11 -11
- package/std/types/assembly/index.d.ts +1 -1
- package/std/types/assembly/package.json +2 -2
- package/std/types/portable/index.d.ts +1 -1
- package/std/types/portable/package.json +2 -2
- package/tsconfig-base.json +13 -13
- package/util/README.md +23 -23
- package/util/browser/fs.js +1 -1
- package/util/browser/module.js +5 -5
- package/util/browser/path.js +520 -520
- package/util/browser/process.js +59 -59
- package/util/browser/url.js +23 -23
- package/util/cpu.d.ts +9 -9
- package/util/cpu.js +42 -42
- package/util/find.d.ts +6 -6
- package/util/find.js +20 -20
- package/util/node.d.ts +21 -21
- package/util/node.js +34 -34
- package/util/options.d.ts +70 -70
- package/util/options.js +262 -262
- package/util/terminal.d.ts +52 -52
- package/util/terminal.js +35 -35
- package/util/text.d.ts +26 -26
- package/util/text.js +114 -114
- package/util/tsconfig.json +9 -9
- package/util/web.d.ts +11 -11
- package/util/web.js +33 -33
|
@@ -1,423 +1,423 @@
|
|
|
1
|
-
/// <reference path="./rt/index.d.ts" />
|
|
2
|
-
|
|
3
|
-
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
|
|
4
|
-
import { Runtime } from "shared/runtime";
|
|
5
|
-
import { COMPARATOR, SORT } from "./util/sort";
|
|
6
|
-
import { REVERSE, FILL } from "./util/bytes";
|
|
7
|
-
import { idof } from "./builtins";
|
|
8
|
-
import { Array } from "./array";
|
|
9
|
-
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
|
|
10
|
-
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
|
|
11
|
-
|
|
12
|
-
@final
|
|
13
|
-
export class StaticArray<T> {
|
|
14
|
-
[key: number]: T;
|
|
15
|
-
|
|
16
|
-
// Note that the interface of StaticArray instances must be a semantically
|
|
17
|
-
// compatible subset of Array<T> in order for syntax highlighting to work
|
|
18
|
-
// properly, for instance when creating static arrays from array literals.
|
|
19
|
-
// The additionally provided static methods take care of dealing with static
|
|
20
|
-
// arrays exclusively, without having to convert to Array<T> first.
|
|
21
|
-
|
|
22
|
-
static fromArray<T>(source: Array<T>): StaticArray<T> {
|
|
23
|
-
let length = source.length;
|
|
24
|
-
let outSize = <usize>length << alignof<T>();
|
|
25
|
-
let out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
|
|
26
|
-
if (isManaged<T>()) {
|
|
27
|
-
let sourcePtr = source.dataStart;
|
|
28
|
-
for (let i = 0; i < length; ++i) {
|
|
29
|
-
let off = <usize>i << alignof<T>();
|
|
30
|
-
let ref = load<usize>(sourcePtr + off);
|
|
31
|
-
store<usize>(changetype<usize>(out) + off, ref);
|
|
32
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
33
|
-
__link(changetype<usize>(out), ref, true);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
memory.copy(changetype<usize>(out), source.dataStart, outSize);
|
|
38
|
-
}
|
|
39
|
-
return out;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** @deprecated Please use source.concat<StaticArray<T>> instead. */
|
|
43
|
-
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T> {
|
|
44
|
-
return source.concat<StaticArray<T>>(other);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** @deprecated Please use source.slice<StaticArray<T>> instead. */
|
|
48
|
-
static slice<T>(source: StaticArray<T>, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
|
|
49
|
-
return source.slice<StaticArray<T>>(start, end);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
constructor(length: i32) {
|
|
53
|
-
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
|
|
54
|
-
let outSize = <usize>length << alignof<T>();
|
|
55
|
-
let out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
|
|
56
|
-
if (ASC_RUNTIME != Runtime.Incremental) {
|
|
57
|
-
memory.fill(changetype<usize>(out), 0, outSize);
|
|
58
|
-
}
|
|
59
|
-
return out;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get length(): i32 {
|
|
63
|
-
return changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize >>> alignof<T>();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
at(index: i32): T {
|
|
67
|
-
let len = this.length;
|
|
68
|
-
index += select(0, len, index >= 0);
|
|
69
|
-
if (<u32>index >= <u32>len) throw new RangeError(E_INDEXOUTOFRANGE);
|
|
70
|
-
let value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
|
|
71
|
-
if (isReference<T>()) {
|
|
72
|
-
if (!isNullable<T>()) {
|
|
73
|
-
if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return value;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
@operator("[]") private __get(index: i32): T {
|
|
80
|
-
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
|
|
81
|
-
let value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
|
|
82
|
-
if (isReference<T>()) {
|
|
83
|
-
if (!isNullable<T>()) {
|
|
84
|
-
if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return value;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
@unsafe @operator("{}") private __uget(index: i32): T {
|
|
91
|
-
return load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
@operator("[]=") private __set(index: i32, value: T): void {
|
|
95
|
-
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
|
|
96
|
-
this.__uset(index, value);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
@unsafe @operator("{}=") private __uset(index: i32, value: T): void {
|
|
100
|
-
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
|
|
101
|
-
if (isManaged<T>()) {
|
|
102
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
103
|
-
__link(changetype<usize>(this), changetype<usize>(value), true);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
|
|
109
|
-
if (isManaged<T>()) {
|
|
110
|
-
FILL<usize>(changetype<usize>(this), this.length, changetype<usize>(value), start, end);
|
|
111
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
112
|
-
__link(changetype<usize>(this), changetype<usize>(value), false);
|
|
113
|
-
}
|
|
114
|
-
} else {
|
|
115
|
-
FILL<T>(changetype<usize>(this), this.length, value, start, end);
|
|
116
|
-
}
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): StaticArray<T> {
|
|
121
|
-
let ptr = changetype<usize>(this);
|
|
122
|
-
let len = this.length;
|
|
123
|
-
|
|
124
|
-
end = min<i32>(end, len);
|
|
125
|
-
|
|
126
|
-
let to = target < 0 ? max(len + target, 0) : min(target, len);
|
|
127
|
-
let from = start < 0 ? max(len + start, 0) : min(start, len);
|
|
128
|
-
let last = end < 0 ? max(len + end, 0) : min(end, len);
|
|
129
|
-
let count = min(last - from, len - to);
|
|
130
|
-
|
|
131
|
-
memory.copy( // is memmove
|
|
132
|
-
ptr + (<usize>to << alignof<T>()),
|
|
133
|
-
ptr + (<usize>from << alignof<T>()),
|
|
134
|
-
<usize>count << alignof<T>()
|
|
135
|
-
);
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
includes(value: T, fromIndex: i32 = 0): bool {
|
|
140
|
-
if (isFloat<T>()) {
|
|
141
|
-
let length = this.length;
|
|
142
|
-
if (length == 0 || fromIndex >= length) return false;
|
|
143
|
-
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
|
|
144
|
-
while (fromIndex < length) {
|
|
145
|
-
let elem = load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>()));
|
|
146
|
-
// @ts-ignore
|
|
147
|
-
if (elem == value || isNaN(elem) & isNaN(value)) return true;
|
|
148
|
-
++fromIndex;
|
|
149
|
-
}
|
|
150
|
-
return false;
|
|
151
|
-
} else {
|
|
152
|
-
return this.indexOf(value, fromIndex) >= 0;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
indexOf(value: T, fromIndex: i32 = 0): i32 {
|
|
157
|
-
let length = this.length;
|
|
158
|
-
if (length == 0 || fromIndex >= length) return -1;
|
|
159
|
-
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
|
|
160
|
-
while (fromIndex < length) {
|
|
161
|
-
if (load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>())) == value) return fromIndex;
|
|
162
|
-
++fromIndex;
|
|
163
|
-
}
|
|
164
|
-
return -1;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
lastIndexOf(value: T, fromIndex: i32 = this.length): i32 {
|
|
168
|
-
let length = this.length;
|
|
169
|
-
if (length == 0) return -1;
|
|
170
|
-
if (fromIndex < 0) fromIndex = length + fromIndex;
|
|
171
|
-
else if (fromIndex >= length) fromIndex = length - 1;
|
|
172
|
-
while (fromIndex >= 0) {
|
|
173
|
-
if (load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>())) == value) return fromIndex;
|
|
174
|
-
--fromIndex;
|
|
175
|
-
}
|
|
176
|
-
return -1;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
concat<U extends ArrayLike<T> = Array<T>>(other: U): U {
|
|
180
|
-
let sourceLen = this.length;
|
|
181
|
-
let otherLen = other.length;
|
|
182
|
-
let outLen = sourceLen + otherLen;
|
|
183
|
-
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) {
|
|
184
|
-
throw new Error(E_INVALIDLENGTH);
|
|
185
|
-
}
|
|
186
|
-
let sourceSize = <usize>sourceLen << alignof<T>();
|
|
187
|
-
let out = changetype<U>(this); // FIXME: instanceof needs *some* value
|
|
188
|
-
|
|
189
|
-
if (out instanceof Array<T>) {
|
|
190
|
-
out = changetype<U>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
|
|
191
|
-
// ^ FIXME: Function returns type U, but can't __newArray(U extends Array<T>)
|
|
192
|
-
let outStart = changetype<Array<T>>(out).dataStart;
|
|
193
|
-
let otherStart = changetype<Array<T>>(other).dataStart;
|
|
194
|
-
let thisStart = changetype<usize>(this);
|
|
195
|
-
|
|
196
|
-
if (isManaged<T>()) {
|
|
197
|
-
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
|
|
198
|
-
let ref = load<usize>(thisStart + offset);
|
|
199
|
-
store<usize>(outStart + offset, ref);
|
|
200
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
201
|
-
__link(changetype<usize>(out), ref, true);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
outStart += sourceSize;
|
|
205
|
-
let otherSize = <usize>otherLen << alignof<T>();
|
|
206
|
-
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
|
|
207
|
-
let ref = load<usize>(otherStart + offset);
|
|
208
|
-
store<usize>(outStart + offset, ref);
|
|
209
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
210
|
-
__link(changetype<usize>(out), ref, true);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
memory.copy(outStart, thisStart, sourceSize);
|
|
215
|
-
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
|
|
216
|
-
}
|
|
217
|
-
} else if (out instanceof StaticArray<T>) {
|
|
218
|
-
out = changetype<U>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
|
|
219
|
-
let outStart = changetype<usize>(out);
|
|
220
|
-
let otherStart = changetype<usize>(other);
|
|
221
|
-
let thisStart = changetype<usize>(this);
|
|
222
|
-
|
|
223
|
-
if (isManaged<T>()) {
|
|
224
|
-
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
|
|
225
|
-
let ref = load<usize>(thisStart + offset);
|
|
226
|
-
store<usize>(outStart + offset, ref);
|
|
227
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
228
|
-
__link(changetype<usize>(out), ref, true);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
outStart += sourceSize;
|
|
232
|
-
let otherSize = <usize>otherLen << alignof<T>();
|
|
233
|
-
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
|
|
234
|
-
let ref = load<usize>(otherStart + offset);
|
|
235
|
-
store<usize>(outStart + offset, ref);
|
|
236
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
237
|
-
__link(changetype<usize>(out), ref, true);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
} else {
|
|
241
|
-
memory.copy(outStart, thisStart, sourceSize);
|
|
242
|
-
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
|
|
243
|
-
}
|
|
244
|
-
} else {
|
|
245
|
-
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
|
|
246
|
-
}
|
|
247
|
-
return out;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
slice<U extends ArrayLike<T> = Array<T>>(start: i32 = 0, end: i32 = i32.MAX_VALUE): U {
|
|
251
|
-
let length = this.length;
|
|
252
|
-
start = start < 0 ? max(start + length, 0) : min(start, length);
|
|
253
|
-
end = end < 0 ? max(end + length, 0) : min(end, length);
|
|
254
|
-
length = max(end - start, 0);
|
|
255
|
-
|
|
256
|
-
let sourceStart = changetype<usize>(this) + (<usize>start << alignof<T>());
|
|
257
|
-
let size = <usize>length << alignof<T>();
|
|
258
|
-
let out = changetype<U>(this); // FIXME: instanceof needs *some* value
|
|
259
|
-
|
|
260
|
-
if (out instanceof Array<T>) {
|
|
261
|
-
// return Array
|
|
262
|
-
out = changetype<U>(__newArray(length, alignof<T>(), idof<Array<T>>()));
|
|
263
|
-
// ^ FIXME: Function returns type U, but can't __newArray(U extends Array<T>)
|
|
264
|
-
let outStart = changetype<Array<T>>(out).dataStart;
|
|
265
|
-
if (isManaged<T>()) {
|
|
266
|
-
let off: usize = 0;
|
|
267
|
-
while (off < size) {
|
|
268
|
-
let ref = load<usize>(sourceStart + off);
|
|
269
|
-
store<usize>(outStart + off, ref);
|
|
270
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
271
|
-
__link(changetype<usize>(out), ref, true);
|
|
272
|
-
}
|
|
273
|
-
off += sizeof<usize>();
|
|
274
|
-
}
|
|
275
|
-
} else {
|
|
276
|
-
memory.copy(outStart, sourceStart, size);
|
|
277
|
-
}
|
|
278
|
-
} else if (out instanceof StaticArray<T>) {
|
|
279
|
-
// return StaticArray
|
|
280
|
-
out = changetype<U>(__new(size, idof<StaticArray<T>>()));
|
|
281
|
-
let outStart = changetype<usize>(out);
|
|
282
|
-
if (isManaged<T>()) {
|
|
283
|
-
let off: usize = 0;
|
|
284
|
-
while (off < size) {
|
|
285
|
-
let ref = load<usize>(sourceStart + off);
|
|
286
|
-
store<usize>(outStart + off, ref);
|
|
287
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
288
|
-
__link(outStart, ref, true);
|
|
289
|
-
}
|
|
290
|
-
off += sizeof<usize>();
|
|
291
|
-
}
|
|
292
|
-
} else {
|
|
293
|
-
memory.copy(outStart, sourceStart, size);
|
|
294
|
-
}
|
|
295
|
-
} else {
|
|
296
|
-
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
|
|
297
|
-
}
|
|
298
|
-
return out;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
findIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
|
|
302
|
-
for (let i = 0, len = this.length; i < len; ++i) {
|
|
303
|
-
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return i;
|
|
304
|
-
}
|
|
305
|
-
return -1;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
findLastIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
|
|
309
|
-
for (let i = this.length - 1; i >= 0; --i) {
|
|
310
|
-
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return i;
|
|
311
|
-
}
|
|
312
|
-
return -1;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
forEach(fn: (value: T, index: i32, array: StaticArray<T>) => void): void {
|
|
316
|
-
for (let i = 0, len = this.length; i < len; ++i) {
|
|
317
|
-
fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
map<U>(fn: (value: T, index: i32, array: StaticArray<T>) => U): Array<U> {
|
|
322
|
-
let len = this.length;
|
|
323
|
-
let out = changetype<Array<U>>(__newArray(len, alignof<U>(), idof<Array<U>>()));
|
|
324
|
-
let outStart = out.dataStart;
|
|
325
|
-
for (let i = 0; i < len; ++i) {
|
|
326
|
-
let result = fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
327
|
-
store<U>(outStart + (<usize>i << alignof<U>()), result);
|
|
328
|
-
if (isManaged<U>()) {
|
|
329
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
330
|
-
__link(changetype<usize>(out), changetype<usize>(result), true);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return out;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
filter(fn: (value: T, index: i32, array: StaticArray<T>) => bool): Array<T> {
|
|
338
|
-
let result = changetype<Array<T>>(__newArray(0, alignof<T>(), idof<Array<T>>()));
|
|
339
|
-
for (let i = 0, len = this.length; i < len; ++i) {
|
|
340
|
-
let value = load<T>(changetype<usize>(this) + (<usize>i << alignof<T>()));
|
|
341
|
-
if (fn(value, i, this)) result.push(value);
|
|
342
|
-
}
|
|
343
|
-
return result;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
reduce<U>(
|
|
347
|
-
fn: (previousValue: U, currentValue: T, currentIndex: i32, array: StaticArray<T>) => U,
|
|
348
|
-
initialValue: U
|
|
349
|
-
): U {
|
|
350
|
-
let acc = initialValue;
|
|
351
|
-
for (let i = 0, len = this.length; i < len; ++i) {
|
|
352
|
-
acc = fn(acc, load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
353
|
-
}
|
|
354
|
-
return acc;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
reduceRight<U>(
|
|
358
|
-
fn: (previousValue: U, currentValue: T, currentIndex: i32, array: StaticArray<T>) => U,
|
|
359
|
-
initialValue: U
|
|
360
|
-
): U {
|
|
361
|
-
let acc = initialValue;
|
|
362
|
-
for (let i = this.length - 1; i >= 0; --i) {
|
|
363
|
-
acc = fn(acc, load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
364
|
-
}
|
|
365
|
-
return acc;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
every(fn: (value: T, index: i32, array: StaticArray<T>) => bool): bool {
|
|
369
|
-
for (let i = 0, len = this.length; i < len; ++i) {
|
|
370
|
-
if (!fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return false;
|
|
371
|
-
}
|
|
372
|
-
return true;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
some(fn: (value: T, index: i32, array: StaticArray<T>) => bool): bool {
|
|
376
|
-
for (let i = 0, len = this.length; i < len; ++i) {
|
|
377
|
-
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return true;
|
|
378
|
-
}
|
|
379
|
-
return false;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): StaticArray<T> {
|
|
383
|
-
SORT<T>(changetype<usize>(this), this.length, comparator);
|
|
384
|
-
return this;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
join(separator: string = ","): string {
|
|
388
|
-
if (isBoolean<T>()) return joinBooleanArray(changetype<usize>(this), this.length, separator);
|
|
389
|
-
if (isInteger<T>()) return joinIntegerArray<T>(changetype<usize>(this), this.length, separator);
|
|
390
|
-
if (isFloat<T>()) return joinFloatArray<T>(changetype<usize>(this), this.length, separator);
|
|
391
|
-
if (ASC_SHRINK_LEVEL < 1) {
|
|
392
|
-
if (isString<T>()) return joinStringArray(changetype<usize>(this), this.length, separator);
|
|
393
|
-
}
|
|
394
|
-
if (isReference<T>()) return joinReferenceArray<T>(changetype<usize>(this), this.length, separator);
|
|
395
|
-
ERROR("unspported element type");
|
|
396
|
-
return <string>unreachable();
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
reverse(): StaticArray<T> {
|
|
400
|
-
REVERSE<T>(changetype<usize>(this), this.length);
|
|
401
|
-
return this;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
toString(): string {
|
|
405
|
-
return this.join();
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
// RT integration
|
|
409
|
-
|
|
410
|
-
@unsafe private __visit(cookie: u32): void {
|
|
411
|
-
if (ASC_RUNTIME != Runtime.Memory) {
|
|
412
|
-
if (isManaged<T>()) {
|
|
413
|
-
let cur = changetype<usize>(this);
|
|
414
|
-
let end = cur + changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize;
|
|
415
|
-
while (cur < end) {
|
|
416
|
-
let val = load<usize>(cur);
|
|
417
|
-
if (val) __visit(val, cookie);
|
|
418
|
-
cur += sizeof<usize>();
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
1
|
+
/// <reference path="./rt/index.d.ts" />
|
|
2
|
+
|
|
3
|
+
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
|
|
4
|
+
import { Runtime } from "shared/runtime";
|
|
5
|
+
import { COMPARATOR, SORT } from "./util/sort";
|
|
6
|
+
import { REVERSE, FILL } from "./util/bytes";
|
|
7
|
+
import { idof } from "./builtins";
|
|
8
|
+
import { Array } from "./array";
|
|
9
|
+
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
|
|
10
|
+
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
|
|
11
|
+
|
|
12
|
+
@final
|
|
13
|
+
export class StaticArray<T> {
|
|
14
|
+
[key: number]: T;
|
|
15
|
+
|
|
16
|
+
// Note that the interface of StaticArray instances must be a semantically
|
|
17
|
+
// compatible subset of Array<T> in order for syntax highlighting to work
|
|
18
|
+
// properly, for instance when creating static arrays from array literals.
|
|
19
|
+
// The additionally provided static methods take care of dealing with static
|
|
20
|
+
// arrays exclusively, without having to convert to Array<T> first.
|
|
21
|
+
|
|
22
|
+
static fromArray<T>(source: Array<T>): StaticArray<T> {
|
|
23
|
+
let length = source.length;
|
|
24
|
+
let outSize = <usize>length << alignof<T>();
|
|
25
|
+
let out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
|
|
26
|
+
if (isManaged<T>()) {
|
|
27
|
+
let sourcePtr = source.dataStart;
|
|
28
|
+
for (let i = 0; i < length; ++i) {
|
|
29
|
+
let off = <usize>i << alignof<T>();
|
|
30
|
+
let ref = load<usize>(sourcePtr + off);
|
|
31
|
+
store<usize>(changetype<usize>(out) + off, ref);
|
|
32
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
33
|
+
__link(changetype<usize>(out), ref, true);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
memory.copy(changetype<usize>(out), source.dataStart, outSize);
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @deprecated Please use source.concat<StaticArray<T>> instead. */
|
|
43
|
+
static concat<T>(source: StaticArray<T>, other: StaticArray<T>): StaticArray<T> {
|
|
44
|
+
return source.concat<StaticArray<T>>(other);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @deprecated Please use source.slice<StaticArray<T>> instead. */
|
|
48
|
+
static slice<T>(source: StaticArray<T>, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
|
|
49
|
+
return source.slice<StaticArray<T>>(start, end);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
constructor(length: i32) {
|
|
53
|
+
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
|
|
54
|
+
let outSize = <usize>length << alignof<T>();
|
|
55
|
+
let out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
|
|
56
|
+
if (ASC_RUNTIME != Runtime.Incremental) {
|
|
57
|
+
memory.fill(changetype<usize>(out), 0, outSize);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get length(): i32 {
|
|
63
|
+
return changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize >>> alignof<T>();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
at(index: i32): T {
|
|
67
|
+
let len = this.length;
|
|
68
|
+
index += select(0, len, index >= 0);
|
|
69
|
+
if (<u32>index >= <u32>len) throw new RangeError(E_INDEXOUTOFRANGE);
|
|
70
|
+
let value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
|
|
71
|
+
if (isReference<T>()) {
|
|
72
|
+
if (!isNullable<T>()) {
|
|
73
|
+
if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@operator("[]") private __get(index: i32): T {
|
|
80
|
+
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
|
|
81
|
+
let value = load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
|
|
82
|
+
if (isReference<T>()) {
|
|
83
|
+
if (!isNullable<T>()) {
|
|
84
|
+
if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@unsafe @operator("{}") private __uget(index: i32): T {
|
|
91
|
+
return load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@operator("[]=") private __set(index: i32, value: T): void {
|
|
95
|
+
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
|
|
96
|
+
this.__uset(index, value);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@unsafe @operator("{}=") private __uset(index: i32, value: T): void {
|
|
100
|
+
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
|
|
101
|
+
if (isManaged<T>()) {
|
|
102
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
103
|
+
__link(changetype<usize>(this), changetype<usize>(value), true);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
|
|
109
|
+
if (isManaged<T>()) {
|
|
110
|
+
FILL<usize>(changetype<usize>(this), this.length, changetype<usize>(value), start, end);
|
|
111
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
112
|
+
__link(changetype<usize>(this), changetype<usize>(value), false);
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
FILL<T>(changetype<usize>(this), this.length, value, start, end);
|
|
116
|
+
}
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): StaticArray<T> {
|
|
121
|
+
let ptr = changetype<usize>(this);
|
|
122
|
+
let len = this.length;
|
|
123
|
+
|
|
124
|
+
end = min<i32>(end, len);
|
|
125
|
+
|
|
126
|
+
let to = target < 0 ? max(len + target, 0) : min(target, len);
|
|
127
|
+
let from = start < 0 ? max(len + start, 0) : min(start, len);
|
|
128
|
+
let last = end < 0 ? max(len + end, 0) : min(end, len);
|
|
129
|
+
let count = min(last - from, len - to);
|
|
130
|
+
|
|
131
|
+
memory.copy( // is memmove
|
|
132
|
+
ptr + (<usize>to << alignof<T>()),
|
|
133
|
+
ptr + (<usize>from << alignof<T>()),
|
|
134
|
+
<usize>count << alignof<T>()
|
|
135
|
+
);
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
includes(value: T, fromIndex: i32 = 0): bool {
|
|
140
|
+
if (isFloat<T>()) {
|
|
141
|
+
let length = this.length;
|
|
142
|
+
if (length == 0 || fromIndex >= length) return false;
|
|
143
|
+
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
|
|
144
|
+
while (fromIndex < length) {
|
|
145
|
+
let elem = load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>()));
|
|
146
|
+
// @ts-ignore
|
|
147
|
+
if (elem == value || isNaN(elem) & isNaN(value)) return true;
|
|
148
|
+
++fromIndex;
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
} else {
|
|
152
|
+
return this.indexOf(value, fromIndex) >= 0;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
indexOf(value: T, fromIndex: i32 = 0): i32 {
|
|
157
|
+
let length = this.length;
|
|
158
|
+
if (length == 0 || fromIndex >= length) return -1;
|
|
159
|
+
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
|
|
160
|
+
while (fromIndex < length) {
|
|
161
|
+
if (load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>())) == value) return fromIndex;
|
|
162
|
+
++fromIndex;
|
|
163
|
+
}
|
|
164
|
+
return -1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
lastIndexOf(value: T, fromIndex: i32 = this.length): i32 {
|
|
168
|
+
let length = this.length;
|
|
169
|
+
if (length == 0) return -1;
|
|
170
|
+
if (fromIndex < 0) fromIndex = length + fromIndex;
|
|
171
|
+
else if (fromIndex >= length) fromIndex = length - 1;
|
|
172
|
+
while (fromIndex >= 0) {
|
|
173
|
+
if (load<T>(changetype<usize>(this) + (<usize>fromIndex << alignof<T>())) == value) return fromIndex;
|
|
174
|
+
--fromIndex;
|
|
175
|
+
}
|
|
176
|
+
return -1;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
concat<U extends ArrayLike<T> = Array<T>>(other: U): U {
|
|
180
|
+
let sourceLen = this.length;
|
|
181
|
+
let otherLen = other.length;
|
|
182
|
+
let outLen = sourceLen + otherLen;
|
|
183
|
+
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) {
|
|
184
|
+
throw new Error(E_INVALIDLENGTH);
|
|
185
|
+
}
|
|
186
|
+
let sourceSize = <usize>sourceLen << alignof<T>();
|
|
187
|
+
let out = changetype<U>(this); // FIXME: instanceof needs *some* value
|
|
188
|
+
|
|
189
|
+
if (out instanceof Array<T>) {
|
|
190
|
+
out = changetype<U>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
|
|
191
|
+
// ^ FIXME: Function returns type U, but can't __newArray(U extends Array<T>)
|
|
192
|
+
let outStart = changetype<Array<T>>(out).dataStart;
|
|
193
|
+
let otherStart = changetype<Array<T>>(other).dataStart;
|
|
194
|
+
let thisStart = changetype<usize>(this);
|
|
195
|
+
|
|
196
|
+
if (isManaged<T>()) {
|
|
197
|
+
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
|
|
198
|
+
let ref = load<usize>(thisStart + offset);
|
|
199
|
+
store<usize>(outStart + offset, ref);
|
|
200
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
201
|
+
__link(changetype<usize>(out), ref, true);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
outStart += sourceSize;
|
|
205
|
+
let otherSize = <usize>otherLen << alignof<T>();
|
|
206
|
+
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
|
|
207
|
+
let ref = load<usize>(otherStart + offset);
|
|
208
|
+
store<usize>(outStart + offset, ref);
|
|
209
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
210
|
+
__link(changetype<usize>(out), ref, true);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
memory.copy(outStart, thisStart, sourceSize);
|
|
215
|
+
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
|
|
216
|
+
}
|
|
217
|
+
} else if (out instanceof StaticArray<T>) {
|
|
218
|
+
out = changetype<U>(__new(<usize>outLen << alignof<T>(), idof<StaticArray<T>>()));
|
|
219
|
+
let outStart = changetype<usize>(out);
|
|
220
|
+
let otherStart = changetype<usize>(other);
|
|
221
|
+
let thisStart = changetype<usize>(this);
|
|
222
|
+
|
|
223
|
+
if (isManaged<T>()) {
|
|
224
|
+
for (let offset: usize = 0; offset < sourceSize; offset += sizeof<T>()) {
|
|
225
|
+
let ref = load<usize>(thisStart + offset);
|
|
226
|
+
store<usize>(outStart + offset, ref);
|
|
227
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
228
|
+
__link(changetype<usize>(out), ref, true);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
outStart += sourceSize;
|
|
232
|
+
let otherSize = <usize>otherLen << alignof<T>();
|
|
233
|
+
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
|
|
234
|
+
let ref = load<usize>(otherStart + offset);
|
|
235
|
+
store<usize>(outStart + offset, ref);
|
|
236
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
237
|
+
__link(changetype<usize>(out), ref, true);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
memory.copy(outStart, thisStart, sourceSize);
|
|
242
|
+
memory.copy(outStart + sourceSize, otherStart, <usize>otherLen << alignof<T>());
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
slice<U extends ArrayLike<T> = Array<T>>(start: i32 = 0, end: i32 = i32.MAX_VALUE): U {
|
|
251
|
+
let length = this.length;
|
|
252
|
+
start = start < 0 ? max(start + length, 0) : min(start, length);
|
|
253
|
+
end = end < 0 ? max(end + length, 0) : min(end, length);
|
|
254
|
+
length = max(end - start, 0);
|
|
255
|
+
|
|
256
|
+
let sourceStart = changetype<usize>(this) + (<usize>start << alignof<T>());
|
|
257
|
+
let size = <usize>length << alignof<T>();
|
|
258
|
+
let out = changetype<U>(this); // FIXME: instanceof needs *some* value
|
|
259
|
+
|
|
260
|
+
if (out instanceof Array<T>) {
|
|
261
|
+
// return Array
|
|
262
|
+
out = changetype<U>(__newArray(length, alignof<T>(), idof<Array<T>>()));
|
|
263
|
+
// ^ FIXME: Function returns type U, but can't __newArray(U extends Array<T>)
|
|
264
|
+
let outStart = changetype<Array<T>>(out).dataStart;
|
|
265
|
+
if (isManaged<T>()) {
|
|
266
|
+
let off: usize = 0;
|
|
267
|
+
while (off < size) {
|
|
268
|
+
let ref = load<usize>(sourceStart + off);
|
|
269
|
+
store<usize>(outStart + off, ref);
|
|
270
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
271
|
+
__link(changetype<usize>(out), ref, true);
|
|
272
|
+
}
|
|
273
|
+
off += sizeof<usize>();
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
memory.copy(outStart, sourceStart, size);
|
|
277
|
+
}
|
|
278
|
+
} else if (out instanceof StaticArray<T>) {
|
|
279
|
+
// return StaticArray
|
|
280
|
+
out = changetype<U>(__new(size, idof<StaticArray<T>>()));
|
|
281
|
+
let outStart = changetype<usize>(out);
|
|
282
|
+
if (isManaged<T>()) {
|
|
283
|
+
let off: usize = 0;
|
|
284
|
+
while (off < size) {
|
|
285
|
+
let ref = load<usize>(sourceStart + off);
|
|
286
|
+
store<usize>(outStart + off, ref);
|
|
287
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
288
|
+
__link(outStart, ref, true);
|
|
289
|
+
}
|
|
290
|
+
off += sizeof<usize>();
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
memory.copy(outStart, sourceStart, size);
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
ERROR("Only Array<T> and StaticArray<T> accept for 'U' parameter");
|
|
297
|
+
}
|
|
298
|
+
return out;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
findIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
|
|
302
|
+
for (let i = 0, len = this.length; i < len; ++i) {
|
|
303
|
+
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return i;
|
|
304
|
+
}
|
|
305
|
+
return -1;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
findLastIndex(fn: (value: T, index: i32, array: StaticArray<T>) => bool): i32 {
|
|
309
|
+
for (let i = this.length - 1; i >= 0; --i) {
|
|
310
|
+
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return i;
|
|
311
|
+
}
|
|
312
|
+
return -1;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
forEach(fn: (value: T, index: i32, array: StaticArray<T>) => void): void {
|
|
316
|
+
for (let i = 0, len = this.length; i < len; ++i) {
|
|
317
|
+
fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
map<U>(fn: (value: T, index: i32, array: StaticArray<T>) => U): Array<U> {
|
|
322
|
+
let len = this.length;
|
|
323
|
+
let out = changetype<Array<U>>(__newArray(len, alignof<U>(), idof<Array<U>>()));
|
|
324
|
+
let outStart = out.dataStart;
|
|
325
|
+
for (let i = 0; i < len; ++i) {
|
|
326
|
+
let result = fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
327
|
+
store<U>(outStart + (<usize>i << alignof<U>()), result);
|
|
328
|
+
if (isManaged<U>()) {
|
|
329
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
330
|
+
__link(changetype<usize>(out), changetype<usize>(result), true);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return out;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
filter(fn: (value: T, index: i32, array: StaticArray<T>) => bool): Array<T> {
|
|
338
|
+
let result = changetype<Array<T>>(__newArray(0, alignof<T>(), idof<Array<T>>()));
|
|
339
|
+
for (let i = 0, len = this.length; i < len; ++i) {
|
|
340
|
+
let value = load<T>(changetype<usize>(this) + (<usize>i << alignof<T>()));
|
|
341
|
+
if (fn(value, i, this)) result.push(value);
|
|
342
|
+
}
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
reduce<U>(
|
|
347
|
+
fn: (previousValue: U, currentValue: T, currentIndex: i32, array: StaticArray<T>) => U,
|
|
348
|
+
initialValue: U
|
|
349
|
+
): U {
|
|
350
|
+
let acc = initialValue;
|
|
351
|
+
for (let i = 0, len = this.length; i < len; ++i) {
|
|
352
|
+
acc = fn(acc, load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
353
|
+
}
|
|
354
|
+
return acc;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
reduceRight<U>(
|
|
358
|
+
fn: (previousValue: U, currentValue: T, currentIndex: i32, array: StaticArray<T>) => U,
|
|
359
|
+
initialValue: U
|
|
360
|
+
): U {
|
|
361
|
+
let acc = initialValue;
|
|
362
|
+
for (let i = this.length - 1; i >= 0; --i) {
|
|
363
|
+
acc = fn(acc, load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this);
|
|
364
|
+
}
|
|
365
|
+
return acc;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
every(fn: (value: T, index: i32, array: StaticArray<T>) => bool): bool {
|
|
369
|
+
for (let i = 0, len = this.length; i < len; ++i) {
|
|
370
|
+
if (!fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return false;
|
|
371
|
+
}
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
some(fn: (value: T, index: i32, array: StaticArray<T>) => bool): bool {
|
|
376
|
+
for (let i = 0, len = this.length; i < len; ++i) {
|
|
377
|
+
if (fn(load<T>(changetype<usize>(this) + (<usize>i << alignof<T>())), i, this)) return true;
|
|
378
|
+
}
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): StaticArray<T> {
|
|
383
|
+
SORT<T>(changetype<usize>(this), this.length, comparator);
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
join(separator: string = ","): string {
|
|
388
|
+
if (isBoolean<T>()) return joinBooleanArray(changetype<usize>(this), this.length, separator);
|
|
389
|
+
if (isInteger<T>()) return joinIntegerArray<T>(changetype<usize>(this), this.length, separator);
|
|
390
|
+
if (isFloat<T>()) return joinFloatArray<T>(changetype<usize>(this), this.length, separator);
|
|
391
|
+
if (ASC_SHRINK_LEVEL < 1) {
|
|
392
|
+
if (isString<T>()) return joinStringArray(changetype<usize>(this), this.length, separator);
|
|
393
|
+
}
|
|
394
|
+
if (isReference<T>()) return joinReferenceArray<T>(changetype<usize>(this), this.length, separator);
|
|
395
|
+
ERROR("unspported element type");
|
|
396
|
+
return <string>unreachable();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
reverse(): StaticArray<T> {
|
|
400
|
+
REVERSE<T>(changetype<usize>(this), this.length);
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
toString(): string {
|
|
405
|
+
return this.join();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// RT integration
|
|
409
|
+
|
|
410
|
+
@unsafe private __visit(cookie: u32): void {
|
|
411
|
+
if (ASC_RUNTIME != Runtime.Memory) {
|
|
412
|
+
if (isManaged<T>()) {
|
|
413
|
+
let cur = changetype<usize>(this);
|
|
414
|
+
let end = cur + changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize;
|
|
415
|
+
while (cur < end) {
|
|
416
|
+
let val = load<usize>(cur);
|
|
417
|
+
if (val) __visit(val, cookie);
|
|
418
|
+
cur += sizeof<usize>();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|