Skip to content

Commit d765b30

Browse files
committed
refactor: destructure wasm exports to improve readability
1 parent a984021 commit d765b30

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

actionlint.cjs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,18 @@ module.exports.createActionlint = async function createActionlint(loader) {
2121
// able to call the `runActionlint` function.
2222
go.run(wasm.instance);
2323

24-
if (!(wasm.instance.exports.memory instanceof WebAssembly.Memory)) {
25-
throw new Error("Could not get wasm memory");
26-
}
27-
const memory = wasm.instance.exports.memory;
28-
29-
if (!(wasm.instance.exports.WasmAlloc instanceof Function)) {
30-
throw new Error("Could not get wasm alloc function");
31-
}
32-
const wasmAlloc = wasm.instance.exports.WasmAlloc;
24+
const { memory, WasmAlloc, WasmFree, RunActionlint } = wasm.instance.exports;
3325

34-
if (!(wasm.instance.exports.WasmFree instanceof Function)) {
35-
throw new Error("Could not get wasm free function");
36-
}
37-
const wasmFree = wasm.instance.exports.WasmFree;
38-
39-
if (!(wasm.instance.exports.RunActionlint instanceof Function)) {
40-
throw new Error("Could not get wasm runActionLint function");
26+
if (
27+
!(memory instanceof WebAssembly.Memory) ||
28+
!(WasmAlloc instanceof Function) ||
29+
!(WasmFree instanceof Function) ||
30+
!(RunActionlint instanceof Function)
31+
) {
32+
throw new Error(
33+
"Invalid wasm exports. Expected memory, WasmAlloc, WasmFree, RunActionlint."
34+
);
4135
}
42-
const runActionlint = wasm.instance.exports.RunActionlint;
4336

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

53-
const workflowPointer = wasmAlloc(workflow.byteLength);
46+
const workflowPointer = WasmAlloc(workflow.byteLength);
5447
new Uint8Array(memory.buffer).set(workflow, workflowPointer);
5548

56-
const filePathPointer = wasmAlloc(filePath.byteLength);
49+
const filePathPointer = WasmAlloc(filePath.byteLength);
5750
new Uint8Array(memory.buffer).set(filePath, filePathPointer);
5851

59-
const resultPointer = runActionlint(
52+
const resultPointer = RunActionlint(
6053
workflowPointer,
6154
workflow.byteLength,
6255
workflow.byteLength,
@@ -65,8 +58,8 @@ module.exports.createActionlint = async function createActionlint(loader) {
6558
filePath.byteLength
6659
);
6760

68-
wasmFree(workflowPointer);
69-
wasmFree(filePathPointer);
61+
WasmFree(workflowPointer);
62+
WasmFree(filePathPointer);
7063

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

0 commit comments

Comments
 (0)