From d765b30bea63074a3db770e8a66b00c48a6287bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brauer?= Date: Wed, 27 Apr 2022 07:55:40 +0000 Subject: [PATCH] refactor: destructure wasm exports to improve readability --- actionlint.cjs | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/actionlint.cjs b/actionlint.cjs index cff8b6f..2985569 100644 --- a/actionlint.cjs +++ b/actionlint.cjs @@ -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 @@ -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, @@ -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);