Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate wasm with asyncify #1

Open
wants to merge 3 commits into
base: v0.19.10-asyncify
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/publish-gitea.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish npm package to gitea
on:
release:
types: [published]
jobs:
npm_publish:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16.x ]
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- name: Run yarn build
run: |
yarn build
- name: Configure git.vdb.to npm registry
run: |
npm config set registry https://git.vdb.to/api/packages/cerc-io/npm/
- name: Authenticate to git.vdb.to registry
run: |
npm config set -- '//git.vdb.to/api/packages/cerc-io/npm/:_authToken' "${{ secrets.GITEA_PUBLISH_TOKEN }}"
- name: npm publish
run: |
npm publish
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
Loading