Skip to content

Commit ccd450f

Browse files
committed
Instantiate wasm with asyncify
1 parent b7a3db8 commit ccd450f

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

lib/loader/index.d.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,20 @@ export interface ASUtil {
8585
/** Tests whether a managed object is an instance of the class represented by the specified base id. */
8686
__instanceof(ptr: number, baseId: number): boolean;
8787
/** Allocates a new string in the module's memory and returns a reference (pointer) to it. */
88-
__newString(str: string): number;
88+
__newString(str: string): Promise<number>;
8989
/** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */
90-
__newArrayBuffer(buf: ArrayBuffer): number;
90+
__newArrayBuffer(buf: ArrayBuffer): Promise<number>;
9191
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
92-
__newArray(id: number, values: ArrayLike<number>): number;
92+
__newArray(id: number, values: ArrayLike<number>): Promise<number>;
9393

9494
/** Allocates an instance of the class represented by the specified id. */
95-
__new(size: number, id: number): number;
95+
__new(size: number, id: number): Promise<number>;
9696
/** Pins a managed object externally, preventing it from becoming garbage collected. */
97-
__pin(ptr: number): number;
97+
__pin(ptr: number): Promise<number>;
9898
/** Unpins a managed object externally, allowing it to become garbage collected. */
99-
__unpin(ptr: number): void;
99+
__unpin(ptr: number): Promise<void>;
100100
/** Performs a full garbage collection cycle. */
101-
__collect(incremental?: boolean): void;
101+
__collect(incremental?: boolean): Promise<void>;
102102
}
103103

104104
/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */

lib/loader/index.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { instantiate as asyncInstantiate } from 'asyncify-wasm';
2+
13
// Runtime header offsets
24
const ID_OFFSET = -8;
35
const SIZE_OFFSET = -4;
@@ -139,10 +141,10 @@ function postInstantiate(extendedExports, instance) {
139141
// }
140142

141143
/** Allocates a new string in the module's memory and returns its pointer. */
142-
function __newString(str) {
144+
async function __newString(str) {
143145
if (str == null) return 0;
144146
const length = str.length;
145-
const ptr = __new(length << 1, STRING_ID);
147+
const ptr = await __new(length << 1, STRING_ID);
146148
const U16 = new Uint16Array(memory.buffer);
147149
for (var i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i);
148150
return ptr;
@@ -193,18 +195,18 @@ function postInstantiate(extendedExports, instance) {
193195
}
194196

195197
/** Allocates a new array in the module's memory and returns its pointer. */
196-
function __newArray(id, values) {
198+
async function __newArray(id, values) {
197199
const info = getArrayInfo(id);
198200
const align = getValueAlign(info);
199201
const length = values.length;
200-
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
202+
const buf = await __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
201203
let result;
202204
if (info & STATICARRAY) {
203205
result = buf;
204206
} else {
205-
__pin(buf);
206-
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
207-
__unpin(buf);
207+
await __pin(buf);
208+
const arr = await __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
209+
await __unpin(buf);
208210
const U32 = new Uint32Array(memory.buffer);
209211
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = buf;
210212
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
@@ -338,7 +340,7 @@ export async function instantiate(source, imports = {}) {
338340
if (isResponse(source = await source)) return instantiateStreaming(source, imports);
339341
const module = isModule(source) ? source : await WebAssembly.compile(source);
340342
const extended = preInstantiate(imports);
341-
const instance = await WebAssembly.instantiate(module, imports);
343+
const instance = await asyncInstantiate(module, imports);
342344
const exports = postInstantiate(extended, instance);
343345
return { module, instance, exports };
344346
}
@@ -395,6 +397,9 @@ export function demangle(exports, extendedExports = {}) {
395397
ctor.prototype = {
396398
valueOf() { return this[THIS]; }
397399
};
400+
ctor.__new = async function(...args) {
401+
return ctor.wrap(await ctor.prototype.constructor(0, ...args));
402+
};
398403
ctor.wrap = function(thisValue) {
399404
return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } });
400405
};

lib/loader/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@
4646
],
4747
"devDependencies": {
4848
"esm2umd": "^0.1.2"
49+
},
50+
"dependencies": {
51+
"asyncify-wasm": "^1.2.1"
4952
}
5053
}

lib/loader/umd/index.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var loader = (function(exports) {
1010
exports.instantiateStreaming = instantiateStreaming;
1111
exports.demangle = demangle;
1212
exports.default = void 0;
13+
14+
var _asyncifyWasm = require("asyncify-wasm");
15+
1316
// Runtime header offsets
1417
const ID_OFFSET = -8;
1518
const SIZE_OFFSET = -4; // Runtime ids
@@ -171,12 +174,10 @@ var loader = (function(exports) {
171174
/** Allocates a new string in the module's memory and returns its pointer. */
172175

173176

174-
function __newString(str) {
177+
async function __newString(str) {
175178
if (str == null) return 0;
176179
const length = str.length;
177-
178-
const ptr = __new(length << 1, STRING_ID);
179-
180+
const ptr = await __new(length << 1, STRING_ID);
180181
const U16 = new Uint16Array(memory.buffer);
181182

182183
for (var i = 0, p = ptr >>> 1; i < length; ++i) U16[p + i] = str.charCodeAt(i);
@@ -230,24 +231,19 @@ var loader = (function(exports) {
230231
/** Allocates a new array in the module's memory and returns its pointer. */
231232

232233

233-
function __newArray(id, values) {
234+
async function __newArray(id, values) {
234235
const info = getArrayInfo(id);
235236
const align = getValueAlign(info);
236237
const length = values.length;
237-
238-
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
239-
238+
const buf = await __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
240239
let result;
241240

242241
if (info & STATICARRAY) {
243242
result = buf;
244243
} else {
245-
__pin(buf);
246-
247-
const arr = __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
248-
249-
__unpin(buf);
250-
244+
await __pin(buf);
245+
const arr = await __new(info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE, id);
246+
await __unpin(buf);
251247
const U32 = new Uint32Array(memory.buffer);
252248
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = buf;
253249
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
@@ -377,7 +373,7 @@ var loader = (function(exports) {
377373
if (isResponse(source = await source)) return instantiateStreaming(source, imports);
378374
const module = isModule(source) ? source : await WebAssembly.compile(source);
379375
const extended = preInstantiate(imports);
380-
const instance = await WebAssembly.instantiate(module, imports);
376+
const instance = await (0, _asyncifyWasm.instantiate)(module, imports);
381377
const exports = postInstantiate(extended, instance);
382378
return {
383379
module,
@@ -455,6 +451,10 @@ var loader = (function(exports) {
455451

456452
};
457453

454+
ctor.__new = async function (...args) {
455+
return ctor.wrap(await ctor.prototype.constructor(0, ...args));
456+
};
457+
458458
ctor.wrap = function (thisValue) {
459459
return Object.create(ctor.prototype, {
460460
[THIS]: {

0 commit comments

Comments
 (0)