diff --git a/lib/loader/index.d.ts b/lib/loader/index.d.ts index ce2dc2ef3a..40977b5a19 100644 --- a/lib/loader/index.d.ts +++ b/lib/loader/index.d.ts @@ -85,20 +85,20 @@ export interface ASUtil { /** Tests whether a managed object is an instance of the class represented by the specified base id. */ __instanceof(ptr: number, baseId: number): boolean; /** Allocates a new string in the module's memory and returns a reference (pointer) to it. */ - __newString(str: string): number; + __newString(str: string): Promise; /** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */ - __newArrayBuffer(buf: ArrayBuffer): number; + __newArrayBuffer(buf: ArrayBuffer): Promise; /** Allocates a new array in the module's memory and returns a reference (pointer) to it. */ - __newArray(id: number, values: ArrayLike): number; + __newArray(id: number, values: ArrayLike): Promise; /** Allocates an instance of the class represented by the specified id. */ - __new(size: number, id: number): number; + __new(size: number, id: number): Promise; /** Pins a managed object externally, preventing it from becoming garbage collected. */ - __pin(ptr: number): number; + __pin(ptr: number): Promise; /** Unpins a managed object externally, allowing it to become garbage collected. */ - __unpin(ptr: number): void; + __unpin(ptr: number): Promise; /** Performs a full garbage collection cycle. */ - __collect(incremental?: boolean): void; + __collect(incremental?: boolean): Promise; } /** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */ diff --git a/lib/loader/index.js b/lib/loader/index.js index f0bfca90fa..d015390466 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -1,3 +1,5 @@ +import { instantiate as asyncInstantiate } from 'asyncify-wasm'; + // Runtime header offsets const ID_OFFSET = -8; const SIZE_OFFSET = -4; @@ -139,10 +141,10 @@ function postInstantiate(extendedExports, instance) { // } /** Allocates a new string in the module's memory and returns its pointer. */ - function __newString(str) { + async function __newString(str) { if (str == null) return 0; const length = str.length; - const ptr = __new(length << 1, STRING_ID); + const ptr = await __new(length << 1, STRING_ID); const U16 = new Uint16Array(memory.buffer); for (var i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i); return ptr; @@ -193,18 +195,18 @@ function postInstantiate(extendedExports, instance) { } /** Allocates a new array in the module's memory and returns its pointer. */ - function __newArray(id, values) { + async function __newArray(id, values) { const info = getArrayInfo(id); const align = getValueAlign(info); const length = values.length; - const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); + const buf = await __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); + await __pin(buf); + const arr = await __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id); + await __unpin(buf); const U32 = new Uint32Array(memory.buffer); U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = buf; U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf; @@ -338,7 +340,7 @@ export 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 instance = await asyncInstantiate(module, imports); const exports = postInstantiate(extended, instance); return { module, instance, exports }; } @@ -395,6 +397,9 @@ export function demangle(exports, extendedExports = {}) { ctor.prototype = { valueOf() { return this[THIS]; } }; + ctor.__new = async function(...args) { + return ctor.wrap(await ctor.prototype.constructor(0, ...args)); + }; ctor.wrap = function(thisValue) { return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } }); }; diff --git a/lib/loader/package.json b/lib/loader/package.json index 2092906ae5..f6d8dc4834 100644 --- a/lib/loader/package.json +++ b/lib/loader/package.json @@ -46,5 +46,8 @@ ], "devDependencies": { "esm2umd": "^0.1.2" + }, + "dependencies": { + "asyncify-wasm": "^1.2.1" } } diff --git a/lib/loader/umd/index.js b/lib/loader/umd/index.js index 44a6fe11a0..134cc953d8 100644 --- a/lib/loader/umd/index.js +++ b/lib/loader/umd/index.js @@ -10,6 +10,9 @@ var loader = (function(exports) { exports.instantiateStreaming = instantiateStreaming; exports.demangle = demangle; exports.default = void 0; + + var _asyncifyWasm = require("asyncify-wasm"); + // Runtime header offsets const ID_OFFSET = -8; const SIZE_OFFSET = -4; // Runtime ids @@ -171,12 +174,10 @@ var loader = (function(exports) { /** Allocates a new string in the module's memory and returns its pointer. */ - function __newString(str) { + async function __newString(str) { if (str == null) return 0; const length = str.length; - - const ptr = __new(length << 1, STRING_ID); - + const ptr = await __new(length << 1, STRING_ID); const U16 = new Uint16Array(memory.buffer); for (var i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i); @@ -230,24 +231,19 @@ var loader = (function(exports) { /** Allocates a new array in the module's memory and returns its pointer. */ - function __newArray(id, values) { + async function __newArray(id, values) { const info = getArrayInfo(id); const align = getValueAlign(info); const length = values.length; - - const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); - + const buf = await __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); - + await __pin(buf); + const arr = await __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id); + await __unpin(buf); const U32 = new Uint32Array(memory.buffer); U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = buf; U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf; @@ -377,7 +373,7 @@ var loader = (function(exports) { 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 instance = await (0, _asyncifyWasm.instantiate)(module, imports); const exports = postInstantiate(extended, instance); return { module, @@ -455,6 +451,10 @@ var loader = (function(exports) { }; + ctor.__new = async function (...args) { + return ctor.wrap(await ctor.prototype.constructor(0, ...args)); + }; + ctor.wrap = function (thisValue) { return Object.create(ctor.prototype, { [THIS]: {