-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathindex.js
566 lines (441 loc) · 18.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// GENERATED FILE. DO NOT EDIT.
var loader = (function(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.instantiate = instantiate;
exports.instantiateSync = instantiateSync;
exports.instantiateStreaming = instantiateStreaming;
exports.demangle = demangle;
exports.default = void 0;
// Runtime header offsets
const ID_OFFSET = -8;
const SIZE_OFFSET = -4; // Runtime ids
const ARRAYBUFFER_ID = 0;
const STRING_ID = 1; // const ARRAYBUFFERVIEW_ID = 2;
// Runtime type information
const ARRAYBUFFERVIEW = 1 << 0;
const ARRAY = 1 << 1;
const STATICARRAY = 1 << 2; // const SET = 1 << 3;
// const MAP = 1 << 4;
const VAL_ALIGN_OFFSET = 6; // const VAL_ALIGN = 1 << VAL_ALIGN_OFFSET;
const VAL_SIGNED = 1 << 11;
const VAL_FLOAT = 1 << 12; // const VAL_NULLABLE = 1 << 13;
const VAL_MANAGED = 1 << 14; // const KEY_ALIGN_OFFSET = 15;
// const KEY_ALIGN = 1 << KEY_ALIGN_OFFSET;
// const KEY_SIGNED = 1 << 20;
// const KEY_FLOAT = 1 << 21;
// const KEY_NULLABLE = 1 << 22;
// const KEY_MANAGED = 1 << 23;
// Array(BufferView) layout
const ARRAYBUFFERVIEW_BUFFER_OFFSET = 0;
const ARRAYBUFFERVIEW_DATASTART_OFFSET = 4;
const ARRAYBUFFERVIEW_BYTELENGTH_OFFSET = 8;
const ARRAYBUFFERVIEW_SIZE = 12;
const ARRAY_LENGTH_OFFSET = 12;
const ARRAY_SIZE = 16;
const E_NO_EXPORT_TABLE = "Operation requires compiling with --exportTable";
const E_NO_EXPORT_RUNTIME = "Operation requires compiling with --exportRuntime";
const F_NO_EXPORT_RUNTIME = () => {
throw Error(E_NO_EXPORT_RUNTIME);
};
const BIGINT = typeof BigUint64Array !== "undefined";
const THIS = Symbol();
const STRING_SMALLSIZE = 192; // break-even point in V8
const STRING_CHUNKSIZE = 1024; // mitigate stack overflow
const utf16 = new TextDecoder("utf-16le", {
fatal: true
}); // != wtf16
/** polyfill for Object.hasOwn */
Object.hasOwn = Object.hasOwn || function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
/** Gets a string from memory. */
function getStringImpl(buffer, ptr) {
let len = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2] >>> 1;
const wtf16 = new Uint16Array(buffer, ptr, len);
if (len <= STRING_SMALLSIZE) return String.fromCharCode(...wtf16);
try {
return utf16.decode(wtf16);
} catch {
let str = "",
off = 0;
while (len - off > STRING_CHUNKSIZE) {
str += String.fromCharCode(...wtf16.subarray(off, off += STRING_CHUNKSIZE));
}
return str + String.fromCharCode(...wtf16.subarray(off));
}
}
/** Prepares the base module prior to instantiation. */
function preInstantiate(imports) {
const extendedExports = {};
function getString(memory, ptr) {
if (!memory) return "<yet unknown>";
return getStringImpl(memory.buffer, ptr);
} // add common imports used by stdlib for convenience
const env = imports.env = imports.env || {};
env.abort = env.abort || function abort(msg, file, line, colm) {
const memory = extendedExports.memory || env.memory; // prefer exported, otherwise try imported
throw Error(`abort: ${getString(memory, msg)} at ${getString(memory, file)}:${line}:${colm}`);
};
env.trace = env.trace || function trace(msg, n, ...args) {
const memory = extendedExports.memory || env.memory;
console.log(`trace: ${getString(memory, msg)}${n ? " " : ""}${args.slice(0, n).join(", ")}`);
};
env.seed = env.seed || Date.now;
imports.Math = imports.Math || Math;
imports.Date = imports.Date || Date;
return extendedExports;
}
/** Prepares the final module once instantiation is complete. */
function postInstantiate(extendedExports, instance) {
const exports = instance.exports;
const memory = exports.memory;
const table = exports.table;
const __new = exports.__new || F_NO_EXPORT_RUNTIME;
const __pin = exports.__pin || F_NO_EXPORT_RUNTIME;
const __unpin = exports.__unpin || F_NO_EXPORT_RUNTIME;
const __collect = exports.__collect || F_NO_EXPORT_RUNTIME;
const __rtti_base = exports.__rtti_base;
const getRttiCount = __rtti_base ? arr => arr[__rtti_base >>> 2] : F_NO_EXPORT_RUNTIME;
extendedExports.__new = __new;
extendedExports.__pin = __pin;
extendedExports.__unpin = __unpin;
extendedExports.__collect = __collect;
/** Gets the runtime type info for the given id. */
function getRttInfo(id) {
const U32 = new Uint32Array(memory.buffer);
if ((id >>>= 0) >= getRttiCount(U32)) throw Error(`invalid id: ${id}`);
return U32[(__rtti_base + 4 >>> 2) + (id << 1)];
}
/** Gets the runtime base id for the given id. */
function getRttBase(id) {
const U32 = new Uint32Array(memory.buffer);
if ((id >>>= 0) >= getRttiCount(U32)) throw Error(`invalid id: ${id}`);
return U32[(__rtti_base + 4 >>> 2) + (id << 1) + 1];
}
/** Gets and validate runtime type info for the given id for array like objects */
function getArrayInfo(id) {
const info = getRttInfo(id);
if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`);
return info;
}
/** Gets the runtime alignment of a collection's values. */
function getValueAlign(info) {
return 31 - Math.clz32(info >>> VAL_ALIGN_OFFSET & 31); // -1 if none
}
/** Gets the runtime alignment of a collection's keys. */
// function getKeyAlign(info) {
// return 31 - Math.clz32((info >>> KEY_ALIGN_OFFSET) & 31); // -1 if none
// }
/** Allocates a new string in the module's memory and returns its pointer. */
function __newString(str) {
if (str == null) return 0;
const length = str.length;
const ptr = __new(length << 1, STRING_ID);
const U16 = new Uint16Array(memory.buffer);
for (let i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i);
return ptr;
}
extendedExports.__newString = __newString;
/** Allocates a new ArrayBuffer in the module's memory and returns its pointer. */
function __newArrayBuffer(buf) {
if (buf == null) return 0;
const bufview = new Uint8Array(buf);
const ptr = __new(bufview.length, ARRAYBUFFER_ID);
const U8 = new Uint8Array(memory.buffer);
U8.set(bufview, ptr);
return ptr;
}
extendedExports.__newArrayBuffer = __newArrayBuffer;
/** Reads a string from the module's memory by its pointer. */
function __getString(ptr) {
if (!ptr) return null;
const buffer = memory.buffer;
const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error(`not a string: ${ptr}`);
return getStringImpl(buffer, ptr);
}
extendedExports.__getString = __getString;
/** Gets the view matching the specified alignment, signedness and floatness. */
function getView(alignLog2, signed, float) {
const buffer = memory.buffer;
if (float) {
switch (alignLog2) {
case 2:
return new Float32Array(buffer);
case 3:
return new Float64Array(buffer);
}
} else {
switch (alignLog2) {
case 0:
return new (signed ? Int8Array : Uint8Array)(buffer);
case 1:
return new (signed ? Int16Array : Uint16Array)(buffer);
case 2:
return new (signed ? Int32Array : Uint32Array)(buffer);
case 3:
return new (signed ? BigInt64Array : BigUint64Array)(buffer);
}
}
throw Error(`unsupported align: ${alignLog2}`);
}
/** Allocates a new array in the module's memory and returns its pointer. */
function __newArray(id, valuesOrCapacity = 0) {
const input = valuesOrCapacity;
const info = getArrayInfo(id);
const align = getValueAlign(info);
const isArrayLike = typeof input !== "number";
const length = isArrayLike ? input.length : input;
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
let result;
if (info & STATICARRAY) {
result = buf;
} else {
__pin(buf);
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
__unpin(buf);
const U32 = new Uint32Array(memory.buffer);
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_BYTELENGTH_OFFSET >>> 2] = length << align;
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
result = arr;
}
if (isArrayLike) {
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
const start = buf >>> align;
if (info & VAL_MANAGED) {
for (let i = 0; i < length; ++i) {
view[start + i] = input[i];
}
} else {
view.set(input, start);
}
}
return result;
}
extendedExports.__newArray = __newArray;
/** Gets a live view on an array's values in the module's memory. Infers the array type from RTTI. */
function __getArrayView(arr) {
const U32 = new Uint32Array(memory.buffer);
const id = U32[arr + ID_OFFSET >>> 2];
const info = getArrayInfo(id);
const align = getValueAlign(info);
let buf = info & STATICARRAY ? arr : U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = info & ARRAY ? U32[arr + ARRAY_LENGTH_OFFSET >>> 2] : U32[buf + SIZE_OFFSET >>> 2] >>> align;
return getView(align, info & VAL_SIGNED, info & VAL_FLOAT).subarray(buf >>>= align, buf + length);
}
extendedExports.__getArrayView = __getArrayView;
/** Copies an array's values from the module's memory. Infers the array type from RTTI. */
function __getArray(arr) {
const input = __getArrayView(arr);
const len = input.length;
const out = new Array(len);
for (let i = 0; i < len; i++) out[i] = input[i];
return out;
}
extendedExports.__getArray = __getArray;
/** Copies an ArrayBuffer's value from the module's memory. */
function __getArrayBuffer(ptr) {
const buffer = memory.buffer;
const length = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2];
return buffer.slice(ptr, ptr + length);
}
extendedExports.__getArrayBuffer = __getArrayBuffer;
/** Gets a function from poiner which contain table's index. */
function __getFunction(ptr) {
if (!table) throw Error(E_NO_EXPORT_TABLE);
const index = new Uint32Array(memory.buffer)[ptr >>> 2];
return table.get(index);
}
extendedExports.__getFunction = __getFunction;
/** Copies a typed array's values from the module's memory. */
function getTypedArray(Type, alignLog2, ptr) {
return new Type(getTypedArrayView(Type, alignLog2, ptr));
}
/** Gets a live view on a typed array's values in the module's memory. */
function getTypedArrayView(Type, alignLog2, ptr) {
const buffer = memory.buffer;
const U32 = new Uint32Array(buffer);
return new Type(buffer, U32[ptr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2], U32[ptr + ARRAYBUFFERVIEW_BYTELENGTH_OFFSET >>> 2] >>> alignLog2);
}
/** Attach a set of get TypedArray and View functions to the exports. */
function attachTypedArrayFunctions(ctor, name, align) {
extendedExports[`__get${name}`] = getTypedArray.bind(null, ctor, align);
extendedExports[`__get${name}View`] = getTypedArrayView.bind(null, ctor, align);
}
[Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array].forEach(ctor => {
attachTypedArrayFunctions(ctor, ctor.name, 31 - Math.clz32(ctor.BYTES_PER_ELEMENT));
});
if (BIGINT) {
[BigUint64Array, BigInt64Array].forEach(ctor => {
attachTypedArrayFunctions(ctor, ctor.name.slice(3), 3);
});
}
/** Tests whether an object is an instance of the class represented by the specified base id. */
function __instanceof(ptr, baseId) {
const U32 = new Uint32Array(memory.buffer);
let id = U32[ptr + ID_OFFSET >>> 2];
if (id <= getRttiCount(U32)) {
do {
if (id == baseId) return true;
id = getRttBase(id);
} while (id);
}
return false;
}
extendedExports.__instanceof = __instanceof; // Pull basic exports to extendedExports so code in preInstantiate can use them
extendedExports.memory = extendedExports.memory || memory;
extendedExports.table = extendedExports.table || table; // Demangle exports and provide the usual utility on the prototype
return demangle(exports, extendedExports);
}
function isResponse(src) {
return typeof Response !== "undefined" && src instanceof Response;
}
function isModule(src) {
return src instanceof WebAssembly.Module;
}
/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */
async function instantiate(source, imports = {}) {
if (isResponse(source = await source)) return instantiateStreaming(source, imports);
const module = isModule(source) ? source : await WebAssembly.compile(source);
const extended = preInstantiate(imports);
const instance = await WebAssembly.instantiate(module, imports);
const exports = postInstantiate(extended, instance);
return {
module,
instance,
exports
};
}
/** Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer. */
function instantiateSync(source, imports = {}) {
const module = isModule(source) ? source : new WebAssembly.Module(source);
const extended = preInstantiate(imports);
const instance = new WebAssembly.Instance(module, imports);
const exports = postInstantiate(extended, instance);
return {
module,
instance,
exports
};
}
/** Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`. */
async function instantiateStreaming(source, imports = {}) {
if (!WebAssembly.instantiateStreaming) {
return instantiate(isResponse(source = await source) ? source.arrayBuffer() : source, imports);
}
const extended = preInstantiate(imports);
const result = await WebAssembly.instantiateStreaming(source, imports);
const exports = postInstantiate(extended, result.instance);
return { ...result,
exports
};
}
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
function demangle(exports, extendedExports = {}) {
const setArgumentsLength = exports["__argumentsLength"] ? length => {
exports["__argumentsLength"].value = length;
} : exports["__setArgumentsLength"] || exports["__setargc"] || (() => {
/* nop */
});
for (let internalName of Object.keys(exports)) {
const elem = exports[internalName];
let parts = internalName.split(".");
let curr = extendedExports;
while (parts.length > 1) {
let part = parts.shift();
if (!Object.hasOwn(curr, part)) curr[part] = {};
curr = curr[part];
}
let name = parts[0];
let hash = name.indexOf("#");
if (hash >= 0) {
const className = name.substring(0, hash);
const classElem = curr[className];
if (typeof classElem === "undefined" || !classElem.prototype) {
const ctor = function (...args) {
return ctor.wrap(ctor.prototype.constructor(0, ...args));
};
ctor.prototype = {
valueOf() {
return this[THIS];
}
};
ctor.wrap = function (thisValue) {
return Object.create(ctor.prototype, {
[THIS]: {
value: thisValue,
writable: false
}
});
};
if (classElem) Object.getOwnPropertyNames(classElem).forEach(name => Object.defineProperty(ctor, name, Object.getOwnPropertyDescriptor(classElem, name)));
curr[className] = ctor;
}
name = name.substring(hash + 1);
curr = curr[className].prototype;
if (/^(get|set):/.test(name)) {
if (!Object.hasOwn(curr, name = name.substring(4))) {
let getter = exports[internalName.replace("set:", "get:")];
let setter = exports[internalName.replace("get:", "set:")];
Object.defineProperty(curr, name, {
get() {
return getter(this[THIS]);
},
set(value) {
setter(this[THIS], value);
},
enumerable: true
});
}
} else {
if (name === 'constructor') {
(curr[name] = function (...args) {
setArgumentsLength(args.length);
return elem(...args);
}).original = elem;
} else {
// instance method
(curr[name] = function (...args) {
// !
setArgumentsLength(args.length);
return elem(this[THIS], ...args);
}).original = elem;
}
}
} else {
if (/^(get|set):/.test(name)) {
if (!Object.hasOwn(curr, name = name.substring(4))) {
Object.defineProperty(curr, name, {
get: exports[internalName.replace("set:", "get:")],
set: exports[internalName.replace("get:", "set:")],
enumerable: true
});
}
} else if (typeof elem === "function" && elem !== setArgumentsLength) {
(curr[name] = (...args) => {
setArgumentsLength(args.length);
return elem(...args);
}).original = elem;
} else {
curr[name] = elem;
}
}
}
return extendedExports;
}
var _default = {
instantiate,
instantiateSync,
instantiateStreaming,
demangle
};
exports.default = _default;
return exports;
})({});
if (typeof define === 'function' && define.amd) define([], function() { return loader; });
else if (typeof module === 'object' && typeof exports==='object') module.exports = loader;