Skip to content

Commit c82b31d

Browse files
committed
Ensure wasm examples custom sections aren't empty
The LLVM 20 upgrade (currently in beta) changes it so that empty custom sections are no longer included in the output
1 parent 74cae4c commit c82b31d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

crates/aoc_wasm/src/custom_sections.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
use aoc::all_puzzles;
44

55
const fn examples_len<P1, P2>(examples: &[(&'static str, Option<P1>, Option<P2>)]) -> usize {
6+
if examples.is_empty() {
7+
// Prevent empty custom sections which may be omitted from the wasm output
8+
return 1;
9+
}
10+
611
let mut length = 0;
712
let mut i = 0;
813
while i < examples.len() {
@@ -16,6 +21,10 @@ const fn examples_len<P1, P2>(examples: &[(&'static str, Option<P1>, Option<P2>)
1621
const fn examples_section<const N: usize, P1, P2>(
1722
examples: &[(&'static str, Option<P1>, Option<P2>)],
1823
) -> [u8; N] {
24+
if examples.is_empty() {
25+
return [0; N];
26+
}
27+
1928
let mut output = [0; N];
2029
let mut o = 0;
2130
let mut e = 0;
@@ -27,12 +36,13 @@ const fn examples_section<const N: usize, P1, P2>(
2736
let mut i = 0;
2837
let input = examples[e].0.as_bytes();
2938
while i < input.len() {
39+
assert!(input[i] != 0); // Check string doesn't contain any null bytes
3040
output[o] = input[i];
3141
i += 1;
3242
o += 1;
3343
}
3444

35-
o += 1; // null byte
45+
o += 1; // Terminating null byte
3646

3747
e += 1;
3848
}

crates/aoc_wasm/web/aoc.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export class Aoc {
8282
const decoder = new TextDecoder();
8383

8484
let view = new Uint8Array(section);
85+
if (view.length === 1 && view[0] === 0) {
86+
return [];
87+
}
88+
8589
while (view.length > 0) {
8690
const end = view.indexOf(0);
8791
result.push({

0 commit comments

Comments
 (0)