warpo 3.2.0 → 3.2.2

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.
Files changed (93) hide show
  1. package/assemblyscript/std/README.md +5 -0
  2. package/assemblyscript/std/assembly/array.ts +542 -0
  3. package/assemblyscript/std/assembly/arraybuffer.ts +72 -0
  4. package/assemblyscript/std/assembly/builtins.ts +2312 -0
  5. package/assemblyscript/std/assembly/compat.ts +2 -0
  6. package/assemblyscript/std/assembly/dataview.ts +144 -0
  7. package/assemblyscript/std/assembly/date.ts +383 -0
  8. package/assemblyscript/std/assembly/diagnostics.ts +11 -0
  9. package/assemblyscript/std/assembly/error.ts +39 -0
  10. package/assemblyscript/std/assembly/function.ts +44 -0
  11. package/assemblyscript/std/assembly/iterator.ts +24 -0
  12. package/assemblyscript/std/assembly/map.ts +304 -0
  13. package/assemblyscript/std/assembly/math.ts +3401 -0
  14. package/assemblyscript/std/assembly/memory.ts +110 -0
  15. package/assemblyscript/std/assembly/number.ts +388 -0
  16. package/assemblyscript/std/assembly/object.ts +30 -0
  17. package/assemblyscript/std/assembly/polyfills.ts +24 -0
  18. package/assemblyscript/std/assembly/process.ts +7 -0
  19. package/assemblyscript/std/assembly/promise.ts +172 -0
  20. package/assemblyscript/std/assembly/reference.ts +48 -0
  21. package/assemblyscript/std/assembly/regexp.ts +16 -0
  22. package/assemblyscript/std/assembly/rt/common.ts +79 -0
  23. package/assemblyscript/std/assembly/rt/index-incremental.ts +4 -0
  24. package/assemblyscript/std/assembly/rt/index-radical.ts +4 -0
  25. package/assemblyscript/std/assembly/rt/index.d.ts +37 -0
  26. package/assemblyscript/std/assembly/rt/itcms.ts +415 -0
  27. package/assemblyscript/std/assembly/rt/tcms.ts +276 -0
  28. package/assemblyscript/std/assembly/rt/tlsf.ts +564 -0
  29. package/assemblyscript/std/assembly/rt.ts +71 -0
  30. package/assemblyscript/std/assembly/set.ts +261 -0
  31. package/assemblyscript/std/assembly/shared/feature.ts +69 -0
  32. package/assemblyscript/std/assembly/shared/host.ts +12 -0
  33. package/assemblyscript/std/assembly/shared/runtime.ts +9 -0
  34. package/assemblyscript/std/assembly/shared/target.ts +9 -0
  35. package/assemblyscript/std/assembly/shared/tsconfig.json +9 -0
  36. package/assemblyscript/std/assembly/shared/typeinfo.ts +72 -0
  37. package/assemblyscript/std/assembly/staticarray.ts +446 -0
  38. package/assemblyscript/std/assembly/string.ts +892 -0
  39. package/assemblyscript/std/assembly/symbol.ts +61 -0
  40. package/assemblyscript/std/assembly/table.ts +15 -0
  41. package/assemblyscript/std/assembly/tsconfig.json +4 -0
  42. package/assemblyscript/std/assembly/tuple.ts +36 -0
  43. package/assemblyscript/std/assembly/typedarray.ts +2000 -0
  44. package/assemblyscript/std/assembly/uri.ts +17 -0
  45. package/assemblyscript/std/assembly/util/bytes.ts +90 -0
  46. package/assemblyscript/std/assembly/util/casemap.ts +264 -0
  47. package/assemblyscript/std/assembly/util/error.ts +54 -0
  48. package/assemblyscript/std/assembly/util/hash.ts +117 -0
  49. package/assemblyscript/std/assembly/util/math.ts +1922 -0
  50. package/assemblyscript/std/assembly/util/memory.ts +309 -0
  51. package/assemblyscript/std/assembly/util/number.ts +884 -0
  52. package/assemblyscript/std/assembly/util/sort.ts +282 -0
  53. package/assemblyscript/std/assembly/util/string.ts +1075 -0
  54. package/assemblyscript/std/assembly/util/uri.ts +251 -0
  55. package/assemblyscript/std/assembly/vector.ts +3 -0
  56. package/dist/debug_server/debugSession.d.ts +1 -0
  57. package/dist/debug_server/debugSession.js +15 -6
  58. package/dist/debug_server/debugSession.js.map +1 -1
  59. package/dist/debug_server/debuggerWasmModule.d.ts +3 -0
  60. package/dist/debug_server/debuggerWasmModule.js +47 -2
  61. package/dist/debug_server/debuggerWasmModule.js.map +1 -1
  62. package/dist/debug_server/tests/debugSession.test.js +96 -0
  63. package/dist/debug_server/tests/debugSession.test.js.map +1 -1
  64. package/dist/debug_server/tests/testModule/debugger_closure_stepping.d.ts +1 -0
  65. package/dist/debug_server/tests/testModule/debugger_closure_stepping.js +12 -0
  66. package/dist/debug_server/tests/testModule/debugger_closure_stepping.js.map +1 -0
  67. package/dist/debug_server/tests/testModule/debugger_std_array.d.ts +1 -0
  68. package/dist/debug_server/tests/testModule/debugger_std_array.js +6 -0
  69. package/dist/debug_server/tests/testModule/debugger_std_array.js.map +1 -0
  70. package/dist/debug_server/tests/testModule/debugger_string_stepping.d.ts +1 -0
  71. package/dist/debug_server/tests/testModule/debugger_string_stepping.js +9 -0
  72. package/dist/debug_server/tests/testModule/debugger_string_stepping.js.map +1 -0
  73. package/package.json +7 -7
  74. package/warpo_extension/std/tsconfig.json +4 -0
  75. package/warpo_extension/std/warpo/ffi.ts +16 -0
  76. package/warpo_extension/std/warpo/test/comparison.ts +122 -0
  77. package/warpo_extension/std/warpo/test/env.ts +33 -0
  78. package/warpo_extension/std/warpo/test/expect.ts +101 -0
  79. package/warpo_extension/std/warpo/test/formatPrint.ts +99 -0
  80. package/warpo_extension/std/warpo/test/implement.ts +41 -0
  81. package/warpo_extension/std/warpo/test/index.d.ts +0 -0
  82. package/warpo_extension/std/warpo/test/index.ts +39 -0
  83. package/warpo_extension/std/warpo/test/mockInstrument.ts +26 -0
  84. package/warpo_extension/std/warpo/utf8/const_str.ts +17 -0
  85. package/warpo_extension/std/wasi_snapshot_preview1/LICENSE +202 -0
  86. package/warpo_extension/std/wasi_snapshot_preview1/README.md +1 -0
  87. package/warpo_extension/std/wasi_snapshot_preview1/bindings/wasi_snapshot_preview1.ts +1731 -0
  88. package/warpo_extension/std/wasi_snapshot_preview1/wasi_console.ts +96 -0
  89. package/warpo_extension/std/wasi_snapshot_preview1/wasi_crypto.ts +8 -0
  90. package/warpo_extension/std/wasi_snapshot_preview1/wasi_date.ts +13 -0
  91. package/warpo_extension/std/wasi_snapshot_preview1/wasi_internal.ts +119 -0
  92. package/warpo_extension/std/wasi_snapshot_preview1/wasi_performance.ts +11 -0
  93. package/warpo_extension/std/wasi_snapshot_preview1/wasi_process.ts +191 -0
@@ -0,0 +1,5 @@
1
+ # Standard library
2
+
3
+ Standard library components for use with `tsc` (portable) and `asc` (assembly).
4
+
5
+ Base configurations (.json) and definition files (.d.ts) are relevant to `tsc` only and not used by `asc`.
@@ -0,0 +1,542 @@
1
+ /// <reference path="./rt/index.d.ts" />
2
+
3
+ import { BLOCK_MAXSIZE } 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 { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
8
+ import { idof, isArray as builtin_isArray } from "./builtins";
9
+ import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
10
+
11
+ // @ts-ignore: decorator
12
+ @inline @lazy const MIN_SIZE: usize = 8;
13
+
14
+ class ArrayIterator<T> implements Iterator<T> {
15
+ current: i32 = 0;
16
+ constructor(private readonly array: Array<T>) {}
17
+ next(): IteratorResult<T> {
18
+ const current = this.current;
19
+ ++this.current;
20
+ if (current >= this.array.length) return IteratorResult.done<T>();
21
+ return IteratorResult.fromValue<T>(unchecked(this.array[current]));
22
+ }
23
+ }
24
+
25
+ /** Ensures that the given array has _at least_ the specified backing size. */
26
+ function ensureCapacity(array: usize, newSize: usize, alignLog2: u32, canGrow: bool = true): void {
27
+ // Depends on the fact that Arrays mimic ArrayBufferView
28
+ let oldCapacity = <usize>changetype<ArrayBufferView>(array).byteLength;
29
+ if (newSize > oldCapacity >>> alignLog2) {
30
+ if (newSize > BLOCK_MAXSIZE >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
31
+ let oldData = changetype<usize>(changetype<ArrayBufferView>(array).buffer);
32
+ // Grows old capacity by factor of two.
33
+ // Make sure we don't reach BLOCK_MAXSIZE for new grown capacity.
34
+ let newCapacity = max(newSize, MIN_SIZE) << alignLog2;
35
+ if (canGrow) newCapacity = max(min(oldCapacity << 1, BLOCK_MAXSIZE), newCapacity);
36
+ let newData = __renew(oldData, newCapacity);
37
+ // __new / __renew already init memory range as zeros in Incremental runtime.
38
+ // So try to avoid this.
39
+ if (newData != oldData) {
40
+ // oldData has been free'd
41
+ store<usize>(array, newData, offsetof<ArrayBufferView>("buffer"));
42
+ store<usize>(array, newData, offsetof<ArrayBufferView>("dataStart"));
43
+ __link(array, changetype<usize>(newData), false);
44
+ }
45
+ store<u32>(array, <u32>newCapacity, offsetof<ArrayBufferView>("byteLength"));
46
+ }
47
+ }
48
+
49
+ export class Array<T> implements Iterable<T> {
50
+ [key: number]: T;
51
+
52
+ // Mimicking ArrayBufferView isn't strictly necessary here but is done to allow glue code
53
+ // to work with typed and normal arrays interchangeably. Technically, normal arrays do not need
54
+ // `dataStart` (equals `buffer`) and `byteLength` (equals computed `buffer.byteLength`), but the
55
+ // block is 16 bytes anyway so it's fine to have a couple extra fields in there.
56
+
57
+ private buffer: ArrayBuffer;
58
+
59
+
60
+ @unsafe readonly dataStart: usize;
61
+ private byteLength: i32; // Uses here as capacity
62
+
63
+ // Also note that Array<T> with non-nullable T must guard against uninitialized null values
64
+ // whenever an element is accessed. Otherwise, the compiler wouldn't be able to guarantee
65
+ // type-safety anymore. For lack of a better word, such an array is "holey".
66
+
67
+ private length_: i32;
68
+
69
+ static isArray<U>(value: U): bool {
70
+ return isReference<U>() ? changetype<usize>(value) != 0 && builtin_isArray(value) : false;
71
+ }
72
+
73
+ static create<T>(capacity: i32 = 0): Array<T> {
74
+ WARNING("'Array.create' is deprecated. Use 'new Array' instead, making sure initial elements are initialized.");
75
+ let array = new Array<T>(capacity);
76
+ array.length = 0;
77
+ return array;
78
+ }
79
+
80
+ constructor(length: i32 = 0) {
81
+ if (<u32>length > (<u32>BLOCK_MAXSIZE) >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
82
+ // reserve capacity for at least MIN_SIZE elements
83
+ let bufferSize = max(<usize>length, MIN_SIZE) << alignof<T>();
84
+ let buffer = changetype<ArrayBuffer>(__new(bufferSize, idof<ArrayBuffer>()));
85
+ this.buffer = buffer; // links
86
+ this.dataStart = changetype<usize>(buffer);
87
+ this.byteLength = <i32>bufferSize;
88
+ this.length_ = length;
89
+ }
90
+
91
+ get length(): i32 {
92
+ return this.length_;
93
+ }
94
+
95
+ set length(newLength: i32) {
96
+ ensureCapacity(changetype<usize>(this), newLength, alignof<T>(), false);
97
+ this.length_ = newLength;
98
+ }
99
+
100
+ every(fn: (value: T, index: i32, array: Array<T>) => bool): bool {
101
+ for (let i = 0, len = this.length_; i < min(len, this.length_); ++i) {
102
+ if (!fn(load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this)) return false;
103
+ }
104
+ return true;
105
+ }
106
+
107
+ findIndex(fn: (value: T, index: i32, array: Array<T>) => bool): i32 {
108
+ for (let i = 0, len = this.length_; i < min(len, this.length_); ++i) {
109
+ if (fn(load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this)) return i;
110
+ }
111
+ return -1;
112
+ }
113
+
114
+ findLastIndex(fn: (value: T, index: i32, array: Array<T>) => bool): i32 {
115
+ for (let i = this.length_ - 1; i >= 0; --i) {
116
+ if (fn(load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this)) return i;
117
+ }
118
+ return -1;
119
+ }
120
+
121
+
122
+ @operator("[]") private __get(index: i32): T {
123
+ if (<u32>index >= <u32>this.length_) throw new RangeError(E_INDEXOUTOFRANGE);
124
+ let value = load<T>(this.dataStart + ((<usize>index) << alignof<T>()));
125
+ if (isReference<T>()) {
126
+ if (!isNullable<T>()) {
127
+ if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
128
+ }
129
+ }
130
+ return value;
131
+ }
132
+
133
+
134
+ @unsafe @operator("{}") private __uget(index: i32): T {
135
+ return load<T>(this.dataStart + ((<usize>index) << alignof<T>()));
136
+ }
137
+
138
+
139
+ @operator("[]=") private __set(index: i32, value: T): void {
140
+ if (<u32>index >= <u32>this.length_) {
141
+ if (index < 0) throw new RangeError(E_INDEXOUTOFRANGE);
142
+ ensureCapacity(changetype<usize>(this), index + 1, alignof<T>());
143
+ this.length_ = index + 1;
144
+ }
145
+ store<T>(this.dataStart + ((<usize>index) << alignof<T>()), value);
146
+ if (isManaged<T>()) {
147
+ __link(changetype<usize>(this), changetype<usize>(value), true);
148
+ }
149
+ }
150
+
151
+ at(index: i32): T {
152
+ let len = this.length_;
153
+ index += select(0, len, index >= 0);
154
+ if (<u32>index >= <u32>len) throw new RangeError(E_INDEXOUTOFRANGE);
155
+ let value = load<T>(this.dataStart + ((<usize>index) << alignof<T>()));
156
+ if (isReference<T>()) {
157
+ if (!isNullable<T>()) {
158
+ if (!changetype<usize>(value)) throw new Error(E_HOLEYARRAY);
159
+ }
160
+ }
161
+ return value;
162
+ }
163
+
164
+ fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
165
+ if (isManaged<T>()) {
166
+ FILL<usize>(this.dataStart, this.length_, changetype<usize>(value), start, end);
167
+ __link(changetype<usize>(this), changetype<usize>(value), false);
168
+ } else {
169
+ FILL<T>(this.dataStart, this.length_, value, start, end);
170
+ }
171
+ return this;
172
+ }
173
+
174
+ includes(value: T, fromIndex: i32 = 0): bool {
175
+ if (isFloat<T>()) {
176
+ let len = this.length_;
177
+ if (len == 0 || fromIndex >= len) return false;
178
+ if (fromIndex < 0) fromIndex = max(len + fromIndex, 0);
179
+ let ptr = this.dataStart;
180
+ while (fromIndex < len) {
181
+ let elem = load<T>(ptr + ((<usize>fromIndex) << alignof<T>()));
182
+ // @ts-ignore
183
+ if (elem == value || isNaN(elem) & isNaN(value)) return true;
184
+ ++fromIndex;
185
+ }
186
+ return false;
187
+ } else {
188
+ return this.indexOf(value, fromIndex) >= 0;
189
+ }
190
+ }
191
+
192
+ indexOf(value: T, fromIndex: i32 = 0): i32 {
193
+ let len = this.length_;
194
+ if (len == 0 || fromIndex >= len) return -1;
195
+ if (fromIndex < 0) fromIndex = max(len + fromIndex, 0);
196
+ let ptr = this.dataStart;
197
+ while (fromIndex < len) {
198
+ if (load<T>(ptr + ((<usize>fromIndex) << alignof<T>())) == value) return fromIndex;
199
+ ++fromIndex;
200
+ }
201
+ return -1;
202
+ }
203
+
204
+ lastIndexOf(value: T, fromIndex: i32 = this.length_): i32 {
205
+ let len = this.length_;
206
+ if (len == 0) return -1;
207
+ if (fromIndex < 0) fromIndex = len + fromIndex;
208
+ else if (fromIndex >= len) fromIndex = len - 1;
209
+ let ptr = this.dataStart;
210
+ while (fromIndex >= 0) {
211
+ if (load<T>(ptr + ((<usize>fromIndex) << alignof<T>())) == value) return fromIndex;
212
+ --fromIndex;
213
+ }
214
+ return -1;
215
+ }
216
+
217
+ push(value: T): i32 {
218
+ let oldLen = this.length_;
219
+ let len = oldLen + 1;
220
+ ensureCapacity(changetype<usize>(this), len, alignof<T>());
221
+ if (isManaged<T>()) {
222
+ store<usize>(this.dataStart + ((<usize>oldLen) << alignof<T>()), changetype<usize>(value));
223
+ __link(changetype<usize>(this), changetype<usize>(value), true);
224
+ } else {
225
+ store<T>(this.dataStart + ((<usize>oldLen) << alignof<T>()), value);
226
+ }
227
+ this.length_ = len;
228
+ return len;
229
+ }
230
+
231
+ concat(other: Array<T>): Array<T> {
232
+ let thisLen = this.length_;
233
+ let otherLen = other.length_;
234
+ let outLen = thisLen + otherLen;
235
+ if (<u32>outLen > (<u32>BLOCK_MAXSIZE) >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
236
+ let out = changetype<Array<T>>(__newArray(outLen, alignof<T>(), idof<Array<T>>()));
237
+ let outStart = out.dataStart;
238
+ let thisSize = (<usize>thisLen) << alignof<T>();
239
+ if (isManaged<T>()) {
240
+ let thisStart = this.dataStart;
241
+ for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
242
+ let ref = load<usize>(thisStart + offset);
243
+ store<usize>(outStart + offset, ref);
244
+ __link(changetype<usize>(out), ref, true);
245
+ }
246
+ outStart += thisSize;
247
+ let otherStart = other.dataStart;
248
+ let otherSize = (<usize>otherLen) << alignof<T>();
249
+ for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
250
+ let ref = load<usize>(otherStart + offset);
251
+ store<usize>(outStart + offset, ref);
252
+ __link(changetype<usize>(out), ref, true);
253
+ }
254
+ } else {
255
+ memory.copy(outStart, this.dataStart, thisSize);
256
+ memory.copy(outStart + thisSize, other.dataStart, (<usize>otherLen) << alignof<T>());
257
+ }
258
+ return out;
259
+ }
260
+
261
+ copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Array<T> {
262
+ let ptr = this.dataStart;
263
+ let len = this.length_;
264
+
265
+ end = min<i32>(end, len);
266
+
267
+ let to = target < 0 ? max(len + target, 0) : min(target, len);
268
+ let from = start < 0 ? max(len + start, 0) : min(start, len);
269
+ let last = end < 0 ? max(len + end, 0) : min(end, len);
270
+ let count = min(last - from, len - to);
271
+
272
+ memory.copy(
273
+ // is memmove
274
+ ptr + ((<usize>to) << alignof<T>()),
275
+ ptr + ((<usize>from) << alignof<T>()),
276
+ (<usize>count) << alignof<T>()
277
+ );
278
+ return this;
279
+ }
280
+
281
+ pop(): T {
282
+ let len = this.length_;
283
+ if (len < 1) throw new RangeError(E_EMPTYARRAY);
284
+ let val = load<T>(this.dataStart + ((<usize>--len) << alignof<T>()));
285
+ this.length_ = len;
286
+ return val;
287
+ }
288
+
289
+ forEach(fn: (value: T, index: i32, array: Array<T>) => void): void {
290
+ for (let i = 0, len = this.length_; i < min(len, this.length_); ++i) {
291
+ fn(load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this);
292
+ }
293
+ }
294
+
295
+
296
+ @inline
297
+ map<U>(fn: (value: T, index: i32, array: Array<T>) => U): Array<U> {
298
+ return this.mapImpl<U>(fn.index);
299
+ }
300
+ private mapImpl<U>(fnIndex: i32): Array<U> {
301
+ let len = this.length_;
302
+ let out = changetype<Array<U>>(__newArray(len, alignof<U>(), idof<Array<U>>()));
303
+ let outStart = out.dataStart;
304
+ for (let i = 0; i < min(len, this.length_); ++i) {
305
+ let result = call_indirect<U>(fnIndex, load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this);
306
+ store<U>(outStart + ((<usize>i) << alignof<U>()), result);
307
+ if (isManaged<U>()) {
308
+ __link(changetype<usize>(out), changetype<usize>(result), true);
309
+ }
310
+ }
311
+ return out;
312
+ }
313
+
314
+
315
+ @inline
316
+ filter(fn: (value: T, index: i32, array: Array<T>) => bool): Array<T> {
317
+ return this.filterImpl(fn.index);
318
+ }
319
+
320
+ private filterImpl(fnIndex: i32): Array<T> {
321
+ let result = changetype<Array<T>>(__newArray(0, alignof<T>(), idof<Array<T>>()));
322
+ for (let i = 0, len = this.length_; i < min(len, this.length_); ++i) {
323
+ let value = load<T>(this.dataStart + ((<usize>i) << alignof<T>()));
324
+ if (call_indirect<bool>(fnIndex, value, i, this)) result.push(value);
325
+ }
326
+ return result;
327
+ }
328
+
329
+
330
+ @inline
331
+ reduce<U>(fn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U {
332
+ return this.reduceImpl(fn.index, initialValue);
333
+ }
334
+ private reduceImpl<U>(fnIndex: i32, initialValue: U): U {
335
+ let acc = initialValue;
336
+ for (let i = 0, len = this.length_; i < min(len, this.length_); ++i) {
337
+ acc = call_indirect<U>(fnIndex, acc, load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this);
338
+ }
339
+ return acc;
340
+ }
341
+
342
+
343
+ @inline
344
+ reduceRight<U>(fn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U {
345
+ return this.reduceRightImpl(fn.index, initialValue);
346
+ }
347
+ private reduceRightImpl<U>(fnIndex: i32, initialValue: U): U {
348
+ let acc = initialValue;
349
+ for (let i = this.length_ - 1; i >= 0; --i) {
350
+ acc = call_indirect<U>(fnIndex, acc, load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this);
351
+ }
352
+ return acc;
353
+ }
354
+
355
+ shift(): T {
356
+ let len = this.length_;
357
+ if (len < 1) throw new RangeError(E_EMPTYARRAY);
358
+ let base = this.dataStart;
359
+ let element = load<T>(base);
360
+ let lastIndex = len - 1;
361
+ memory.copy(base, base + sizeof<T>(), (<usize>lastIndex) << alignof<T>());
362
+ if (isReference<T>()) {
363
+ store<usize>(base + ((<usize>lastIndex) << alignof<T>()), 0);
364
+ } else {
365
+ // @ts-ignore
366
+ store<T>(base + ((<usize>lastIndex) << alignof<T>()), <T>0);
367
+ }
368
+ this.length_ = lastIndex;
369
+ return element;
370
+ }
371
+
372
+ some(fn: (value: T, index: i32, array: Array<T>) => bool): bool {
373
+ for (let i = 0, len = this.length_; i < min(len, this.length_); ++i) {
374
+ if (fn(load<T>(this.dataStart + ((<usize>i) << alignof<T>())), i, this)) return true;
375
+ }
376
+ return false;
377
+ }
378
+
379
+ unshift(value: T): i32 {
380
+ let len = this.length_ + 1;
381
+ ensureCapacity(changetype<usize>(this), len, alignof<T>());
382
+ let ptr = this.dataStart;
383
+ memory.copy(ptr + sizeof<T>(), ptr, (<usize>(len - 1)) << alignof<T>());
384
+ store<T>(ptr, value);
385
+ if (isManaged<T>()) {
386
+ __link(changetype<usize>(this), changetype<usize>(value), true);
387
+ }
388
+ this.length_ = len;
389
+ return len;
390
+ }
391
+
392
+ slice(start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
393
+ let len = this.length_;
394
+ start = start < 0 ? max(start + len, 0) : min(start, len);
395
+ end = end < 0 ? max(end + len, 0) : min(end, len);
396
+ len = max(end - start, 0);
397
+ let slice = changetype<Array<T>>(__newArray(len, alignof<T>(), idof<Array<T>>()));
398
+ let sliceBase = slice.dataStart;
399
+ let thisBase = this.dataStart + ((<usize>start) << alignof<T>());
400
+ if (isManaged<T>()) {
401
+ let off = <usize>0;
402
+ let end = (<usize>len) << alignof<usize>();
403
+ while (off < end) {
404
+ let ref = load<usize>(thisBase + off);
405
+ store<usize>(sliceBase + off, ref);
406
+ __link(changetype<usize>(slice), ref, true);
407
+ off += sizeof<usize>();
408
+ }
409
+ } else {
410
+ memory.copy(sliceBase, thisBase, len << alignof<T>());
411
+ }
412
+ return slice;
413
+ }
414
+
415
+ splice(start: i32, deleteCount: i32 = i32.MAX_VALUE): Array<T> {
416
+ let len = this.length_;
417
+ start = start < 0 ? max<i32>(len + start, 0) : min<i32>(start, len);
418
+ deleteCount = max<i32>(min<i32>(deleteCount, len - start), 0);
419
+ let result = changetype<Array<T>>(__newArray(deleteCount, alignof<T>(), idof<Array<T>>()));
420
+ let resultStart = result.dataStart;
421
+ let thisStart = this.dataStart;
422
+ let thisBase = thisStart + ((<usize>start) << alignof<T>());
423
+ memory.copy(resultStart, thisBase, (<usize>deleteCount) << alignof<T>());
424
+ let offset = start + deleteCount;
425
+ if (len != offset) {
426
+ memory.copy(thisBase, thisStart + ((<usize>offset) << alignof<T>()), (<usize>(len - offset)) << alignof<T>());
427
+ }
428
+ this.length_ = len - deleteCount;
429
+ return result;
430
+ }
431
+
432
+ reverse(): Array<T> {
433
+ REVERSE<T>(this.dataStart, this.length_);
434
+ return this;
435
+ }
436
+
437
+ sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): Array<T> {
438
+ SORT<T>(this.dataStart, this.length_, comparator);
439
+ return this;
440
+ }
441
+
442
+ join(separator: string = ","): string {
443
+ let ptr = this.dataStart;
444
+ let len = this.length_;
445
+ if (isBoolean<T>()) return joinBooleanArray(ptr, len, separator);
446
+ if (isInteger<T>()) return joinIntegerArray<T>(ptr, len, separator);
447
+ if (isFloat<T>()) return joinFloatArray<T>(ptr, len, separator);
448
+
449
+ if (ASC_SHRINK_LEVEL < 1) {
450
+ if (isString<T>()) return joinStringArray(ptr, len, separator);
451
+ }
452
+ // For rest objects and arrays use general join routine
453
+ if (isReference<T>()) return joinReferenceArray<T>(ptr, len, separator);
454
+ ERROR("unsupported element type");
455
+ return <string>unreachable();
456
+ }
457
+
458
+ flat(): T {
459
+ if (!isArray<T>()) {
460
+ ERROR("Cannot call flat() on Array<T> where T is not an Array.");
461
+ }
462
+ // Get the length and data start values
463
+ let ptr = this.dataStart;
464
+ let len = this.length_;
465
+
466
+ // calculate the end size with an initial pass
467
+ let size = 0;
468
+ for (let i = 0; i < len; ++i) {
469
+ let child = load<usize>(ptr + (i << alignof<T>()));
470
+ size += child == 0 ? 0 : load<i32>(child, offsetof<T>("length_"));
471
+ }
472
+
473
+ // calculate the byteLength of the resulting backing ArrayBuffer
474
+ const align = alignof<valueof<T>>();
475
+ let byteLength = (<usize>size) << align;
476
+ let outBuffer = changetype<ArrayBuffer>(__new(byteLength, idof<ArrayBuffer>()));
477
+
478
+ // create the return value and initialize it
479
+ let outArray = changetype<T>(__new(offsetof<T>(), idof<T>()));
480
+ store<i32>(changetype<usize>(outArray), size, offsetof<T>("length_"));
481
+
482
+ // byteLength, dataStart, and buffer are all readonly
483
+ store<i32>(changetype<usize>(outArray), byteLength, offsetof<T>("byteLength"));
484
+ store<usize>(changetype<usize>(outArray), changetype<usize>(outBuffer), offsetof<T>("dataStart"));
485
+ store<usize>(changetype<usize>(outArray), changetype<usize>(outBuffer), offsetof<T>("buffer"));
486
+ __link(changetype<usize>(outArray), changetype<usize>(outBuffer), false);
487
+
488
+ // set the elements
489
+ let resultOffset: usize = 0;
490
+ for (let i = 0; i < len; ++i) {
491
+ // for each child
492
+ let child = load<usize>(ptr + ((<usize>i) << alignof<T>()));
493
+
494
+ // ignore null arrays
495
+ if (!child) continue;
496
+
497
+ // copy the underlying buffer data to the result buffer
498
+ let childDataLength = (<usize>load<i32>(child, offsetof<T>("length_"))) << align;
499
+ memory.copy(
500
+ changetype<usize>(outBuffer) + resultOffset,
501
+ load<usize>(child, offsetof<T>("dataStart")),
502
+ childDataLength
503
+ );
504
+
505
+ // advance the result length
506
+ resultOffset += childDataLength;
507
+ }
508
+
509
+ // if the `valueof<T>` type is managed, we must link each reference
510
+ if (isManaged<valueof<T>>()) {
511
+ for (let i = 0; i < size; ++i) {
512
+ let ref = load<usize>(changetype<usize>(outBuffer) + ((<usize>i) << usize(alignof<valueof<T>>())));
513
+ __link(changetype<usize>(outBuffer), ref, true);
514
+ }
515
+ }
516
+
517
+ return outArray;
518
+ }
519
+
520
+ toString(): string {
521
+ return this.join();
522
+ }
523
+
524
+ [Symbol.iterator](): ArrayIterator<T> {
525
+ return new ArrayIterator<T>(this);
526
+ }
527
+
528
+ // RT integration
529
+
530
+ @unsafe private __visit(cookie: u32): void {
531
+ if (isManaged<T>()) {
532
+ let cur = this.dataStart;
533
+ let end = cur + ((<usize>this.length_) << alignof<T>());
534
+ while (cur < end) {
535
+ let val = load<usize>(cur);
536
+ if (val) __visit(val, cookie);
537
+ cur += sizeof<usize>();
538
+ }
539
+ }
540
+ __visit(changetype<usize>(this.buffer), cookie);
541
+ }
542
+ }
@@ -0,0 +1,72 @@
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 { idof } from "./builtins";
6
+ import { E_INVALIDLENGTH } from "./util/error";
7
+
8
+ export abstract class ArrayBufferView {
9
+ readonly buffer: ArrayBuffer;
10
+
11
+
12
+ @unsafe readonly dataStart: usize;
13
+ readonly byteLength: i32;
14
+
15
+ get byteOffset(): i32 {
16
+ return <i32>(this.dataStart - changetype<usize>(this.buffer));
17
+ }
18
+
19
+ protected constructor(length: i32, alignLog2: i32) {
20
+ if (<u32>length > (<u32>BLOCK_MAXSIZE) >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
21
+ let buffer = changetype<ArrayBuffer>(__new((length = length << alignLog2), idof<ArrayBuffer>()));
22
+ this.buffer = buffer; // links
23
+ this.dataStart = changetype<usize>(buffer);
24
+ this.byteLength = length;
25
+ }
26
+ }
27
+
28
+
29
+ @final export class ArrayBuffer {
30
+ static isView<T>(value: T): bool {
31
+ if (isNullable<T>()) {
32
+ if (changetype<usize>(value) == 0) return false;
33
+ }
34
+ if (value instanceof Int8Array) return true;
35
+ if (value instanceof Uint8Array) return true;
36
+ if (value instanceof Uint8ClampedArray) return true;
37
+ if (value instanceof Int16Array) return true;
38
+ if (value instanceof Uint16Array) return true;
39
+ if (value instanceof Int32Array) return true;
40
+ if (value instanceof Uint32Array) return true;
41
+ if (value instanceof Int64Array) return true;
42
+ if (value instanceof Uint64Array) return true;
43
+ if (value instanceof Float32Array) return true;
44
+ if (value instanceof Float64Array) return true;
45
+ if (value instanceof DataView) return true;
46
+ return false;
47
+ }
48
+
49
+ constructor(length: i32) {
50
+ if (<u32>length > <u32>BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
51
+ let buffer = changetype<ArrayBuffer>(__new(<usize>length, idof<ArrayBuffer>()));
52
+ return buffer;
53
+ }
54
+
55
+ get byteLength(): i32 {
56
+ return changetype<OBJECT>(changetype<usize>(this) - TOTAL_OVERHEAD).rtSize;
57
+ }
58
+
59
+ slice(begin: i32 = 0, end: i32 = BLOCK_MAXSIZE): ArrayBuffer {
60
+ let length = this.byteLength;
61
+ begin = begin < 0 ? max(length + begin, 0) : min(begin, length);
62
+ end = end < 0 ? max(length + end, 0) : min(end, length);
63
+ let outSize = <usize>max(end - begin, 0);
64
+ let out = changetype<ArrayBuffer>(__new(outSize, idof<ArrayBuffer>()));
65
+ memory.copy(changetype<usize>(out), changetype<usize>(this) + <usize>begin, outSize);
66
+ return out;
67
+ }
68
+
69
+ toString(): string {
70
+ return "[object ArrayBuffer]";
71
+ }
72
+ }