Skip to content

Commit

Permalink
Error on long inputs in JS in wasm demo
Browse files Browse the repository at this point in the history
Before providing an input longer than the buffer caused a "data provided does not contain a nul" error in Rust. These should be caught in JS using the result of TextEncoder.encodeInto
  • Loading branch information
ictrobot committed Sep 8, 2024
1 parent ef45a5b commit 2b06488
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions crates/aoc_wasm/web/aoc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ export class Aoc {
* @return {{success: true, part1: string, part2: string} | {success: false, error: string}}
*/
run(year, day, input, isExample = false, part1 = true, part2 = true) {
this.#write(input);

let success;
try {
this.#write(input);
success = this.#exports.run_puzzle(year, day, isExample, part1, part2);
} catch (e) {
this.#instance = new WebAssembly.Instance(this.#module);
Expand Down Expand Up @@ -156,6 +155,9 @@ export class Aoc {
#write(input) {
const buffer = this.#buffer("INPUT");
const result = new TextEncoder().encodeInto(input, buffer);
if (result.read < input.length || result.written === buffer.length) {
throw new Error("Input string is too long");
}
buffer[result.written] = 0;
}

Expand Down

0 comments on commit 2b06488

Please sign in to comment.