Skip to content

Commit 76f4450

Browse files
authored
Rework loop statement compilation / general cleanup (#1046)
1 parent 228cbbf commit 76f4450

File tree

250 files changed

+48864
-31801
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

250 files changed

+48864
-31801
lines changed

bin/asinit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ console.log([
5353
" TypeScript configuration inheriting recommended AssemblyScript settings.",
5454
"",
5555
colors.cyan(" ./assembly/index.ts"),
56-
" Exemplary entry file being compiled to WebAssembly to get you started.",
56+
" Example entry file being compiled to WebAssembly to get you started.",
5757
"",
5858
colors.cyan(" ./build"),
5959
" Build artifact directory where compiled WebAssembly files are stored.",
@@ -65,7 +65,7 @@ console.log([
6565
" Main file loading the WebAssembly module and exporting its exports.",
6666
"",
6767
colors.cyan(" ./tests/index.js"),
68-
" Exemplary test to check that your module is indeed working.",
68+
" Example test to check that your module is indeed working.",
6969
"",
7070
colors.cyan(" ./package.json"),
7171
" Package info containing the necessary commands to compile to WebAssembly.",
@@ -122,7 +122,7 @@ rl.question(colors.white("Do you want to proceed?") + " [Y/n] ", answer => {
122122
" ^ The optimized WebAssembly module using default optimization settings.",
123123
" You can change the optimization settings in '" + colors.cyan("package.json")+ "'.",
124124
"",
125-
"To run the exemplary tests, do:",
125+
"To run the tests, do:",
126126
"",
127127
colors.white(" npm test"),
128128
"",

lib/loader/README.md

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,25 @@ Besides demangling classes exported from your entry file to a handy object struc
4343
```
4444

4545
* **__getString**(ptr: `number`): `string`<br />
46-
Reads (copies) the value of a string from the module's memory.
46+
Copies a string's value from the module's memory.
4747

4848
```ts
4949
var str = module.__getString(ptr);
5050
...
5151
```
5252

53-
* **__allocArray**(id: `number`, values: `number[]`): `number`<br />
54-
Allocates a new array in the module's memory and returns a reference (pointer) to it.
55-
Automatically retains interior pointers. The `id` is the unique runtime id of the respective array class. If you are using `Int32Array` for example, the best way to know the id is an `export const INT32ARRAY_ID = idof<Int32Array>()`. When done with the array, make sure to release it.
56-
57-
```ts
58-
var ptr = module.__retain(module.__allocArray(module.INT32ARRAY, [1, 2, 3]));
59-
...
60-
module.__release(ptr);
61-
```
53+
* **__getArrayBuffer**(ptr: `number`): `ArrayBuffer`<br />
54+
Copies an ArrayBuffer's value from the module's memory.
6255

6356
* **__getArray**(ptr: `number`): `number[]`<br />
64-
Reads (copies) the values of an array from the module's memory.
57+
Copies an array's values from the module's memory. Infers the array type from RTTI.
6558

6659
```ts
6760
var arr = module.__getArray(ptr);
6861
...
6962
```
7063

71-
* **__getArrayView**(ptr: `number`): `TypedArray`<br />
72-
Gets a view on the values of an array in the module's memory. This differs from `__getArray` in that the data isn't copied but remains *live* in both directions. That's faster but also unsafe because if the array grows or becomes released, the view will no longer represent the correct memory region and modifying its values in this state will most likely corrupt memory. Use, but use with care.
73-
74-
If the type of the array is known beforehand, the following even faster and even more unsafe helpers can be used that don't do any type checking:
64+
If the type of the array is known beforehand, the following slightly faster helpers that don't infer the type can be used:
7565

7666
**__getInt8Array**(ptr: `number`): `Int8Array`<br />
7767
**__getUint8Array**(ptr: `number`): `Uint8Array`<br />
@@ -85,8 +75,24 @@ Besides demangling classes exported from your entry file to a handy object struc
8575
**__getFloat32Array**(ptr: `number`): `Float32Array`<br />
8676
**__getFloat64Array**(ptr: `number`): `Float64Array`
8777

88-
* **__getArrayBuffer**(ptr: `number`): `ArrayBuffer`<br />
89-
Reads (copies) the data of an ArrayBuffer from the module's memory.
78+
* **__getArrayView**(ptr: `number`): `TypedArray`<br />
79+
Gets a live view on the values of an array in the module's memory. Infers the array type from RTTI.
80+
81+
This differs from `__getArray` in that the data isn't copied but remains *live* in both directions. That's faster but also unsafe because if the array grows or becomes released, the view will no longer represent the correct memory region and modifying its values in this state will most likely corrupt memory. Use, but use with care.
82+
83+
If the type of the array is known beforehand, the following slightly faster helpers that don't infer the type can be used:
84+
85+
**__getInt8ArrayView**(ptr: `number`): `Int8Array`<br />
86+
**__getUint8ArrayView**(ptr: `number`): `Uint8Array`<br />
87+
**__getUint8ClampedArrayView**(ptr: `number`): `Uint8ClampedArray`<br />
88+
**__getInt16ArrayView**(ptr: `number`): `Int16Array`<br />
89+
**__getUint16ArrayView**(ptr: `number`): `Uint16Array`<br />
90+
**__getInt32ArrayView**(ptr: `number`): `Int32Array`<br />
91+
**__getUint32ArrayView**(ptr: `number`): `Uint32Array`<br />
92+
**__getInt64ArrayView**(ptr: `number`): `BigInt64Array`<br />
93+
**__getUint64ArrayView**(ptr: `number`): `BigUint64Array`<br />
94+
**__getFloat32ArrayView**(ptr: `number`): `Float32Array`<br />
95+
**__getFloat64ArrayView**(ptr: `number`): `Float64Array`
9096

9197
* **__retain**(ptr: `number`): `number`<br />
9298
Retains a reference to a managed object externally, making sure that it doesn't become collected prematurely. Returns the pointer.
@@ -107,6 +113,16 @@ Besides demangling classes exported from your entry file to a handy object struc
107113
module.__release(ptr);
108114
```
109115

116+
* **__allocArray**(id: `number`, values: `number[]`): `number`<br />
117+
Allocates a new array in the module's memory and returns a reference (pointer) to it.
118+
Automatically retains interior pointers. The `id` is the unique runtime id of the respective array class. If you are using `Int32Array` for example, the best way to know the id is an `export const INT32ARRAY_ID = idof<Int32Array>()`. When done with the array, make sure to release it.
119+
120+
```ts
121+
var ptr = module.__retain(module.__allocArray(module.INT32ARRAY, [1, 2, 3]));
122+
...
123+
module.__release(ptr);
124+
```
125+
110126
* **__instanceof**(ptr: `number`, baseId: `number`): `boolean`<br />
111127
Tests whether an object is an instance of the class represented by the specified base id.
112128

lib/loader/index.d.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,66 +22,66 @@ interface ASUtil {
2222
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
2323
__allocArray(id: number, values: ArrayLike<number>): number;
2424

25-
/** Reads (copies) the value of a string from the module's memory. */
25+
/** Copies a string's value from the module's memory. */
2626
__getString(ptr: number): string;
27-
/** Reads (copies) the values of an array from the module's memory. */
28-
__getArray(ptr: number): number[];
29-
/** Gets a view on the values of an array in the module's memory. */
30-
__getArrayView(ptr: number): ArrayBufferView;
27+
/** Copies an ArrayBuffer's value from the module's memory. */
28+
__getArrayBuffer(ptr: number): ArrayBuffer;
3129

32-
/** Reads (copies) the values of Int8Array from the module's memory. */
30+
/** Copies an array's values from the module's memory. Infers the array type from RTTI. */
31+
__getArray(ptr: number): number[];
32+
/** Copies an Int8Array's values from the module's memory. */
3333
__getInt8Array(ptr: number): Int8Array;
34-
/** Reads (copies) the values of Uint8Array from the module's memory. */
34+
/** Copies an Uint8Array's values from the module's memory. */
3535
__getUint8Array(ptr: number): Uint8Array;
36-
/** Reads (copies) the values of Uint8Array from the module's memory. */
36+
/** Copies an Uint8ClampedArray's values from the module's memory. */
3737
__getUint8ClampedArray(ptr: number): Uint8ClampedArray;
38-
/** Reads (copies) the values of Int16Array from the module's memory. */
38+
/** Copies an Int16Array's values from the module's memory. */
3939
__getInt16Array(ptr: number): Int16Array;
40-
/** Reads (copies) the values of Uint16Array from the module's memory. */
40+
/** Copies an Uint16Array's values from the module's memory. */
4141
__getUint16Array(ptr: number): Uint16Array;
42-
/** Reads (copies) the values of Int32Array from the module's memory. */
42+
/** Copies an Int32Array's values from the module's memory. */
4343
__getInt32Array(ptr: number): Int32Array;
44-
/** Reads (copies) the values of Uint32Array from the module's memory. */
44+
/** Copies an Uint32Array's values from the module's memory. */
4545
__getUint32Array(ptr: number): Uint32Array;
46-
/** Reads (copies) the values of Int32Array from the module's memory. */
46+
/** Copies an Int32Array's values from the module's memory. */
4747
__getInt64Array?(ptr: number): BigInt64Array;
48-
/** Reads (copies) the values of Uint32Array from the module's memory. */
48+
/** Copies an Uint32Array's values from the module's memory. */
4949
__getUint64Array?(ptr: number): BigUint64Array;
50-
/** Reads (copies) the values of Float32Array from the module's memory. */
50+
/** Copies a Float32Array's values from the module's memory. */
5151
__getFloat32Array(ptr: number): Float32Array;
52-
/** Reads (copies) the values of Float64Array from the module's memory. */
52+
/** Copies a Float64Array's values from the module's memory. */
5353
__getFloat64Array(ptr: number): Float64Array;
5454

55-
/** Reads the values of Int8Array from the module's memory. */
55+
/** Gets a live view on an array's values in the module's memory. Infers the array type from RTTI. */
56+
__getArrayView(ptr: number): ArrayBufferView;
57+
/** Gets a live view on an Int8Array's values in the module's memory. */
5658
__getInt8ArrayView(ptr: number): Int8Array;
57-
/** Reads the values of Uint8Array from the module's memory. */
59+
/** Gets a live view on an Uint8Array's values in the module's memory. */
5860
__getUint8ArrayView(ptr: number): Uint8Array;
59-
/** Reads the values of Uint8Array from the module's memory. */
61+
/** Gets a live view on an Uint8ClampedArray's values in the module's memory. */
6062
__getUint8ClampedArrayView(ptr: number): Uint8ClampedArray;
61-
/** Reads the values of Int16Array from the module's memory. */
63+
/** Gets a live view on an Int16Array's values in the module's memory. */
6264
__getInt16ArrayView(ptr: number): Int16Array;
63-
/** Reads the values of Uint16Array from the module's memory. */
65+
/** Gets a live view on an Uint16Array's values in the module's memory. */
6466
__getUint16ArrayView(ptr: number): Uint16Array;
65-
/** Reads the values of Int32Array from the module's memory. */
67+
/** Gets a live view on an Int32Array's values in the module's memory. */
6668
__getInt32ArrayView(ptr: number): Int32Array;
67-
/** Reads the values of Uint32Array from the module's memory. */
69+
/** Gets a live view on an Uint32Array's values in the module's memory. */
6870
__getUint32ArrayView(ptr: number): Uint32Array;
69-
/** Reads the values of Int32Array from the module's memory. */
71+
/** Gets a live view on an Int32Array's values in the module's memory. */
7072
__getInt64ArrayView?(ptr: number): BigInt64Array;
71-
/** Reads the values of Uint32Array from the module's memory. */
73+
/** Gets a live view on an Uint32Array's values in the module's memory. */
7274
__getUint64ArrayView?(ptr: number): BigUint64Array;
73-
/** Reads the values of Float32Array from the module's memory. */
75+
/** Gets a live view on a Float32Array's values in the module's memory. */
7476
__getFloat32ArrayView(ptr: number): Float32Array;
75-
/** Reads the values of Float64Array from the module's memory. */
77+
/** Gets a live view on a Float64Array's values in the module's memory. */
7678
__getFloat64ArrayView(ptr: number): Float64Array;
7779

78-
/** Reads (copies) the data of an ArrayBuffer from the module's memory. */
79-
__getArrayBuffer(ptr: number): ArrayBuffer;
8080
/** Retains a reference to a managed object externally, making sure that it doesn't become collected prematurely. Returns the pointer. */
8181
__retain(ptr: number): number;
8282
/** Releases a previously retained reference to a managed object, allowing the runtime to collect it once its reference count reaches zero. */
8383
__release(ptr: number): void;
84-
/** Resets base pointer to initial offset. Uses only for stub runtime. */
84+
/** Forcefully resets the heap to its initial offset, effectively clearing dynamic memory. Stub runtime only. */
8585
__reset?(): void;
8686
/** Allocates an instance of the class represented by the specified id. */
8787
__alloc(size: number, id: number): number;

lib/loader/index.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function postInstantiate(baseModule, instance) {
180180

181181
baseModule.__allocArray = __allocArray;
182182

183-
/** Gets a view on the values of an array in the module's memory. */
183+
/** Gets a live view on an array's values in the module's memory. Infers the array type from RTTI. */
184184
function __getArrayView(arr) {
185185
const U32 = new Uint32Array(memory.buffer);
186186
const id = U32[arr + ID_OFFSET >>> 2];
@@ -197,7 +197,7 @@ function postInstantiate(baseModule, instance) {
197197

198198
baseModule.__getArrayView = __getArrayView;
199199

200-
/** Reads (copies) the values of an array from the module's memory. */
200+
/** Copies an array's values from the module's memory. Infers the array type from RTTI. */
201201
function __getArray(arr) {
202202
const input = __getArrayView(arr);
203203
const len = input.length;
@@ -208,7 +208,7 @@ function postInstantiate(baseModule, instance) {
208208

209209
baseModule.__getArray = __getArray;
210210

211-
/** Reads (copies) the data of an ArrayBuffer from the module's memory. */
211+
/** Copies an ArrayBuffer's value from the module's memory. */
212212
function __getArrayBuffer(ptr) {
213213
const buffer = memory.buffer;
214214
const length = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2];
@@ -217,50 +217,41 @@ function postInstantiate(baseModule, instance) {
217217

218218
baseModule.__getArrayBuffer = __getArrayBuffer;
219219

220+
/** Copies a typed array's values from the module's memory. */
221+
function getTypedArray(Type, alignLog2, ptr) {
222+
return new Type(getTypedArrayView(Type, alignLog2, ptr));
223+
}
224+
225+
/** Gets a live view on a typed array's values in the module's memory. */
220226
function getTypedArrayView(Type, alignLog2, ptr) {
221227
const buffer = memory.buffer;
222228
const U32 = new Uint32Array(buffer);
223229
const bufPtr = U32[ptr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
224230
return new Type(buffer, bufPtr, U32[bufPtr + SIZE_OFFSET >>> 2] >>> alignLog2);
225231
}
226232

227-
function getTypedArray(Type, alignLog2, ptr) {
228-
return new Type(getTypedArrayView(Type, alignLog2, ptr));
229-
}
230-
231-
/** Gets a view on the values of a known-to-be Int8Array in the module's memory. */
232233
baseModule.__getInt8Array = getTypedArray.bind(null, Int8Array, 0);
233234
baseModule.__getInt8ArrayView = getTypedArrayView.bind(null, Int8Array, 0);
234-
/** Gets a view on the values of a known-to-be Uint8Array in the module's memory. */
235235
baseModule.__getUint8Array = getTypedArray.bind(null, Uint8Array, 0);
236236
baseModule.__getUint8ArrayView = getTypedArrayView.bind(null, Uint8Array, 0);
237-
/** Gets a view on the values of a known-to-be Uint8ClampedArray in the module's memory. */
238237
baseModule.__getUint8ClampedArray = getTypedArray.bind(null, Uint8ClampedArray, 0);
239238
baseModule.__getUint8ClampedArrayView = getTypedArrayView.bind(null, Uint8ClampedArray, 0);
240-
/** Gets a view on the values of a known-to-be Int16Array in the module's memory. */
241239
baseModule.__getInt16Array = getTypedArray.bind(null, Int16Array, 1);
242240
baseModule.__getInt16ArrayView = getTypedArrayView.bind(null, Int16Array, 1);
243-
/** Gets a view on the values of a known-to-be Uint16Array in the module's memory. */
244241
baseModule.__getUint16Array = getTypedArray.bind(null, Uint16Array, 1);
245242
baseModule.__getUint16ArrayView = getTypedArrayView.bind(null, Uint16Array, 1);
246-
/** Gets a view on the values of a known-to-be Int32Array in the module's memory. */
247243
baseModule.__getInt32Array = getTypedArray.bind(null, Int32Array, 2);
248244
baseModule.__getInt32ArrayView = getTypedArrayView.bind(null, Int32Array, 2);
249-
/** Gets a view on the values of a known-to-be Uint32Array in the module's memory. */
250245
baseModule.__getUint32Array = getTypedArray.bind(null, Uint32Array, 2);
251246
baseModule.__getUint32ArrayView = getTypedArrayView.bind(null, Uint32Array, 2);
252247
if (BIGINT) {
253-
/** Gets a view on the values of a known-to-be-Int64Array in the module's memory. */
254248
baseModule.__getInt64Array = getTypedArray.bind(null, BigInt64Array, 3);
255249
baseModule.__getInt64ArrayView = getTypedArrayView.bind(null, BigInt64Array, 3);
256-
/** Gets a view on the values of a known-to-be-Uint64Array in the module's memory. */
257250
baseModule.__getUint64Array = getTypedArray.bind(null, BigUint64Array, 3);
258251
baseModule.__getUint64ArrayView = getTypedArrayView.bind(null, BigUint64Array, 3);
259252
}
260-
/** Gets a view on the values of a known-to-be Float32Array in the module's memory. */
261253
baseModule.__getFloat32Array = getTypedArray.bind(null, Float32Array, 2);
262254
baseModule.__getFloat32ArrayView = getTypedArrayView.bind(null, Float32Array, 2);
263-
/** Gets a view on the values of a known-to-be Float64Array in the module's memory. */
264255
baseModule.__getFloat64Array = getTypedArray.bind(null, Float64Array, 3);
265256
baseModule.__getFloat64ArrayView = getTypedArrayView.bind(null, Float64Array, 3);
266257

@@ -286,9 +277,9 @@ function postInstantiate(baseModule, instance) {
286277
}
287278

288279
/** Wraps a WebAssembly function while also taking care of variable arguments. */
289-
function wrapFunction(fn, setargc) {
280+
function wrapFunction(fn, argumentsLength) {
290281
var wrap = (...args) => {
291-
setargc(args.length);
282+
if (argumentsLength) argumentsLength.value = args.length;
292283
return fn(...args);
293284
}
294285
wrap.original = fn;
@@ -351,7 +342,7 @@ exports.instantiateStreaming = instantiateStreaming;
351342
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
352343
function demangle(exports, baseModule) {
353344
var module = baseModule ? Object.create(baseModule) : {};
354-
var setargc = exports["__setargc"] || function() {};
345+
var argumentsLength = exports["__argumentsLength"];
355346
function hasOwnProperty(elem, prop) {
356347
return Object.prototype.hasOwnProperty.call(elem, prop);
357348
}
@@ -401,11 +392,11 @@ function demangle(exports, baseModule) {
401392
}
402393
} else {
403394
if (name === 'constructor') {
404-
curr[name] = wrapFunction(elem, setargc);
395+
curr[name] = wrapFunction(elem, argumentsLength);
405396
} else { // for methods
406397
Object.defineProperty(curr, name, {
407398
value: function (...args) {
408-
setargc(args.length);
399+
if (argumentsLength) argumentsLength.value = args.length;
409400
return elem(this[THIS], ...args);
410401
}
411402
});
@@ -421,7 +412,7 @@ function demangle(exports, baseModule) {
421412
});
422413
}
423414
} else if (typeof elem === "function") {
424-
curr[name] = wrapFunction(elem, setargc);
415+
curr[name] = wrapFunction(elem, argumentsLength);
425416
} else {
426417
curr[name] = elem;
427418
}

lib/loader/tests/build/untouched.wasm

120 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)