Skip to content

Commit

Permalink
Instantiate wasm with asyncify
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed Oct 5, 2021
1 parent b7a3db8 commit ccbd560
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 40 deletions.
14 changes: 7 additions & 7 deletions lib/loader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>;
/** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */
__newArrayBuffer(buf: ArrayBuffer): number;
__newArrayBuffer(buf: ArrayBuffer): Promise<number>;
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
__newArray(id: number, values: ArrayLike<number>): number;
__newArray(id: number, values: ArrayLike<number>): Promise<number>;

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

/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */
Expand Down
21 changes: 13 additions & 8 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { instantiate as asyncInstantiate } from 'asyncify-wasm';

// Runtime header offsets
const ID_OFFSET = -8;
const SIZE_OFFSET = -4;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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 } });
};
Expand Down
3 changes: 3 additions & 0 deletions lib/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@
],
"devDependencies": {
"esm2umd": "^0.1.2"
},
"dependencies": {
"asyncify-wasm": "^1.2.1"
}
}
30 changes: 15 additions & 15 deletions lib/loader/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]: {
Expand Down
52 changes: 42 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
},
"dependencies": {
"asyncify-wasm": "^1.2.1",
"binaryen": "101.0.0-nightly.20210723",
"long": "^4.0.0",
"source-map-support": "^0.5.19",
Expand Down

0 comments on commit ccbd560

Please sign in to comment.