Skip to content

Commit

Permalink
refactor: destructure wasm exports to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Apr 27, 2022
1 parent a984021 commit d765b30
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions actionlint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,18 @@ module.exports.createActionlint = async function createActionlint(loader) {
// able to call the `runActionlint` function.
go.run(wasm.instance);

if (!(wasm.instance.exports.memory instanceof WebAssembly.Memory)) {
throw new Error("Could not get wasm memory");
}
const memory = wasm.instance.exports.memory;

if (!(wasm.instance.exports.WasmAlloc instanceof Function)) {
throw new Error("Could not get wasm alloc function");
}
const wasmAlloc = wasm.instance.exports.WasmAlloc;
const { memory, WasmAlloc, WasmFree, RunActionlint } = wasm.instance.exports;

if (!(wasm.instance.exports.WasmFree instanceof Function)) {
throw new Error("Could not get wasm free function");
}
const wasmFree = wasm.instance.exports.WasmFree;

if (!(wasm.instance.exports.RunActionlint instanceof Function)) {
throw new Error("Could not get wasm runActionLint function");
if (
!(memory instanceof WebAssembly.Memory) ||
!(WasmAlloc instanceof Function) ||
!(WasmFree instanceof Function) ||
!(RunActionlint instanceof Function)
) {
throw new Error(
"Invalid wasm exports. Expected memory, WasmAlloc, WasmFree, RunActionlint."
);
}
const runActionlint = wasm.instance.exports.RunActionlint;

/**
* @param {string} input
Expand All @@ -50,13 +43,13 @@ module.exports.createActionlint = async function createActionlint(loader) {
const workflow = encoder.encode(input);
const filePath = encoder.encode(path);

const workflowPointer = wasmAlloc(workflow.byteLength);
const workflowPointer = WasmAlloc(workflow.byteLength);
new Uint8Array(memory.buffer).set(workflow, workflowPointer);

const filePathPointer = wasmAlloc(filePath.byteLength);
const filePathPointer = WasmAlloc(filePath.byteLength);
new Uint8Array(memory.buffer).set(filePath, filePathPointer);

const resultPointer = runActionlint(
const resultPointer = RunActionlint(
workflowPointer,
workflow.byteLength,
workflow.byteLength,
Expand All @@ -65,8 +58,8 @@ module.exports.createActionlint = async function createActionlint(loader) {
filePath.byteLength
);

wasmFree(workflowPointer);
wasmFree(filePathPointer);
WasmFree(workflowPointer);
WasmFree(filePathPointer);

const result = new Uint8Array(memory.buffer).subarray(resultPointer);
const end = result.indexOf(0);
Expand Down

0 comments on commit d765b30

Please sign in to comment.