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

Promise support #78

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions lib/asbind-instance/asbind-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class AsbindInstance {
this.unboundExports = {};
this.exports = {};
this.importObject = {};
this.asyncifyStorageSize = 8 * 1024;
}

getTypeId(typeName) {
Expand All @@ -62,6 +63,11 @@ export default class AsbindInstance {
}

_validate() {
this.isAsyncifyModule = Boolean(
WebAssembly.Module.exports(this.module).find(
({ name }) => name === "asyncify_start_unwind"
)
);
if (
!WebAssembly.Module.exports(this.module).find(exp => exp.name === "__new")
) {
Expand Down Expand Up @@ -136,5 +142,11 @@ export default class AsbindInstance {
descriptor
);
}

if (!this.isAsyncifyModule) {
// If this module wasn’t built with Ayncify, we mock
// the asyncify state function to return that we are in “normal” mode.
this.exports.asyncify_get_state = () => 0;
}
}
}
70 changes: 60 additions & 10 deletions lib/asbind-instance/bind-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export function bindImportFunction(
// Create a wrapper function that applies the correct converter function to arguments and
// return value respectively.
return function(...args) {
if (asbindInstance.exports.asyncify_get_state() === 2 /* Rewinding */) {
asbindInstance.loadedModule.exports.asyncify_stop_rewind();
asbindInstance.loadedModule.exports.__unpin(
asbindInstance.asyncifyState.ptr
);
return returnValueConverterFunction(
asbindInstance,
asbindInstance.asyncifyState.value,
importedFunctionDescriptor.returnType
);
}

if (args.length != argumentConverterFunctions.length) {
throw Error(
`Expected ${argumentConverterFunctions.length} arguments, got ${args.length}`
Expand All @@ -40,11 +52,35 @@ export function bindImportFunction(
)
);
const result = importedFunction(...newArgs);
return returnValueConverterFunction(
asbindInstance,
result,
importedFunctionDescriptor.returnType
if (!asbindInstance.isAsyncifyModule || !(result instanceof Promise)) {
return returnValueConverterFunction(
asbindInstance,
result,
importedFunctionDescriptor.returnType
);
}
asbindInstance.asyncifyState = {
ptr: asbindInstance.loadedModule.exports.__new(
asbindInstance.asyncifyStorageSize,
0
)
};
asbindInstance.loadedModule.exports.__pin(asbindInstance.asyncifyState.ptr);
const dv = new DataView(asbindInstance.loadedModule.exports.memory.buffer);
dv.setUint32(
asbindInstance.asyncifyState.ptr,
asbindInstance.asyncifyState.ptr + 8,
true
);
dv.setUint32(
asbindInstance.asyncifyState.ptr + 4,
asbindInstance.asyncifyState.ptr + asbindInstance.asyncifyStorageSize,
true
);
asbindInstance.loadedModule.exports.asyncify_start_unwind(
asbindInstance.asyncifyState.ptr
);
asbindInstance.asyncifyState.promise = result;
};
}

Expand Down Expand Up @@ -78,11 +114,25 @@ export function bindExportFunction(
exportedFunctionDescriptor.parameters[i]
)
);
const result = exportedFunction(...newArgs);
return returnValueConverterFunction(
asbindInstance,
result,
exportedFunctionDescriptor.returnType
);
return function f(...args) {
const result = exportedFunction(...args);
if (asbindInstance.exports.asyncify_get_state() === 0 /* Normal */) {
return returnValueConverterFunction(
asbindInstance,
result,
exportedFunctionDescriptor.returnType
);
}
asbindInstance.loadedModule.exports.asyncify_stop_unwind();
let localAsyncifyState = asbindInstance.asyncifyState;
return localAsyncifyState.promise.then(value => {
localAsyncifyState.value = value;
asbindInstance.asyncifyState = localAsyncifyState;
asbindInstance.loadedModule.exports.asyncify_start_rewind(
asbindInstance.asyncifyState.ptr
);
return f(...args);
});
}.bind(this)(...newArgs);
};
}
11 changes: 9 additions & 2 deletions test/test-runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { promisify } = require("util");
const fs = require("fs/promises");
const path = require("path");

const Express = require("express");
const Mocha = require("mocha");
Expand Down Expand Up @@ -32,7 +33,7 @@ async function compileAllAsc() {
const transformFile = require.resolve("../dist/transform.cjs.js");
for (const ascFile of ascFiles) {
console.log(`Compiling ${ascFile}...`);
await asc.main([
const params = [
"--runtime",
"stub",
"--exportRuntime",
Expand All @@ -41,7 +42,13 @@ async function compileAllAsc() {
"--binaryFile",
ascFile.replace(/\.ts$/, ".wasm"),
ascFile
]);
];
try {
const configFile = "./" + path.join(path.dirname(ascFile), "config.js");
const config = require(configFile);
await config?.params?.(params);
} catch (e) {}
await asc.main(params);
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/tests/string-promise/asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function swapAndPad(a: string, b: string): string {
return "!" + swappedConcat(a, b) + "!";
}

declare function swappedConcat(a: string, b: string): string;
5 changes: 5 additions & 0 deletions test/tests/string-promise/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
params(param) {
param.push("--runPasses", "asyncify");
}
};
12 changes: 12 additions & 0 deletions test/tests/string-promise/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("as-bind", function() {
it("should handle strings", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
async swappedConcat(a, b) {
return b + a;
}
}
});
assert((await instance.exports.swapAndPad("a", "b")) === "!ba!");
});
});