From a7c040a39c4f993f4b22881fcdbaa55d89857e1d Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Mon, 18 Sep 2023 10:09:45 -0400 Subject: [PATCH 1/5] feat: Improve error handling around `grain run` --- cli/bin/grainrun.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cli/bin/grainrun.js b/cli/bin/grainrun.js index 8d0dfd519e..9c9800f4f1 100644 --- a/cli/bin/grainrun.js +++ b/cli/bin/grainrun.js @@ -31,7 +31,14 @@ const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; async function run(filename) { try { - const wasm = await WebAssembly.compile(await readFile(filename)); + const wasm = await WebAssembly.compile(await readFile(filename)).catch((err) => { + if (filename.endsWith(".gr")) { + console.log(`Grain run is for compiled wasm files, to compile a grain file use grain ${filename}`); + } else { + console.log("Invalid wasm file detected"); + } + process.exit(); + }); const instance = await WebAssembly.instantiate(wasm, importObject); wasi.start(instance); From fac07250552b00765cc17cdf16d6bbdd2758d2c4 Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Mon, 18 Sep 2023 11:27:02 -0400 Subject: [PATCH 2/5] chore: run formatting --- cli/bin/grainrun.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cli/bin/grainrun.js b/cli/bin/grainrun.js index 9c9800f4f1..04d4867896 100644 --- a/cli/bin/grainrun.js +++ b/cli/bin/grainrun.js @@ -31,14 +31,18 @@ const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; async function run(filename) { try { - const wasm = await WebAssembly.compile(await readFile(filename)).catch((err) => { - if (filename.endsWith(".gr")) { - console.log(`Grain run is for compiled wasm files, to compile a grain file use grain ${filename}`); - } else { - console.log("Invalid wasm file detected"); + const wasm = await WebAssembly.compile(await readFile(filename)).catch( + (err) => { + if (filename.endsWith(".gr")) { + console.log( + `Grain run is for compiled wasm files, to compile a grain file use grain ${filename}` + ); + } else { + console.log("Invalid wasm file detected"); + } + process.exit(); } - process.exit(); - }); + ); const instance = await WebAssembly.instantiate(wasm, importObject); wasi.start(instance); From 3a559124e8aae2c79bb561fdbf25a1b803bffb9f Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Tue, 3 Oct 2023 14:12:22 -0400 Subject: [PATCH 3/5] feat: Switch to regular `try/catch` --- cli/bin/grainrun.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/cli/bin/grainrun.js b/cli/bin/grainrun.js index 04d4867896..6c043f661f 100644 --- a/cli/bin/grainrun.js +++ b/cli/bin/grainrun.js @@ -31,23 +31,21 @@ const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; async function run(filename) { try { - const wasm = await WebAssembly.compile(await readFile(filename)).catch( - (err) => { - if (filename.endsWith(".gr")) { - console.log( - `Grain run is for compiled wasm files, to compile a grain file use grain ${filename}` - ); - } else { - console.log("Invalid wasm file detected"); - } - process.exit(); - } - ); + const wasm = await WebAssembly.compile(await readFile(filename)) const instance = await WebAssembly.instantiate(wasm, importObject); - wasi.start(instance); } catch (err) { - console.error(err.stack); + if (err instanceof WebAssembly.CompileError) { + if (filename.endsWith(".gr")) { + console.log( + `Grain run is for compiled wasm files, to compile a grain file use grain ${filename}` + ); + } else { + console.log("Invalid wasm file detected"); + } + } else { + console.error(err.stack); + } process.exit(1); } } From 50eac4fac624a2c13a95f9eacde4b778c79cfd44 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 8 Oct 2023 16:37:21 -0700 Subject: [PATCH 4/5] more changes --- cli/bin/grainrun.js | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/cli/bin/grainrun.js b/cli/bin/grainrun.js index 6c043f661f..4bb5970ebd 100644 --- a/cli/bin/grainrun.js +++ b/cli/bin/grainrun.js @@ -30,24 +30,40 @@ const wasi = new WASI({ const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; async function run(filename) { + let bytes; try { - const wasm = await WebAssembly.compile(await readFile(filename)) - const instance = await WebAssembly.instantiate(wasm, importObject); - wasi.start(instance); + bytes = await readFile(filename); + } catch (err) { + console.error(`Unable to read file: ${filename}`); + process.exit(1); + } + + let wasm; + try { + wasm = await WebAssembly.compile(bytes); } catch (err) { - if (err instanceof WebAssembly.CompileError) { - if (filename.endsWith(".gr")) { - console.log( - `Grain run is for compiled wasm files, to compile a grain file use grain ${filename}` - ); - } else { - console.log("Invalid wasm file detected"); - } + if (filename.endsWith(".gr")) { + console.error( + `The \`grain run\` command is used on compiled \`.wasm\` files.` + ); + console.error( + `To compile and run your \`.gr\` file, use \`grain ${filename}\`` + ); } else { - console.error(err.stack); + console.error(`Unable to compile WASM module.`); + console.error(err.statck); } process.exit(1); } + + try { + const instance = await WebAssembly.instantiate(wasm, importObject); + wasi.start(instance); + } catch (err) { + console.error(`Unable to instantiate WASM module.`); + console.error(err.stack); + process.exit(1); + } } run(argv[2]); From e086ba8b4d6f06f3e2b588f3116591f4a899364d Mon Sep 17 00:00:00 2001 From: Oscar Spencer Date: Mon, 9 Oct 2023 08:34:05 -0500 Subject: [PATCH 5/5] Use full "WebAssembly" name in error messages --- cli/bin/grainrun.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/bin/grainrun.js b/cli/bin/grainrun.js index 4bb5970ebd..0ed5a6b66d 100644 --- a/cli/bin/grainrun.js +++ b/cli/bin/grainrun.js @@ -50,7 +50,7 @@ async function run(filename) { `To compile and run your \`.gr\` file, use \`grain ${filename}\`` ); } else { - console.error(`Unable to compile WASM module.`); + console.error(`Unable to compile WebAssembly module.`); console.error(err.statck); } process.exit(1); @@ -60,7 +60,7 @@ async function run(filename) { const instance = await WebAssembly.instantiate(wasm, importObject); wasi.start(instance); } catch (err) { - console.error(`Unable to instantiate WASM module.`); + console.error(`Unable to instantiate WebAssembly module.`); console.error(err.stack); process.exit(1); }