Skip to content

Commit 47b0415

Browse files
committed
fix(wasm): correct composed braid locals and helper expectations
1 parent 8149993 commit 47b0415

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

  • compiler/tangle-wasm/src

compiler/tangle-wasm/src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ impl WasmModule {
154154
}
155155

156156
/// Bump allocator for WASM linear memory.
157+
#[allow(dead_code)]
157158
struct BumpAllocator {
158159
next_offset: u32,
159160
capacity: u32,
160161
}
161162

163+
#[allow(dead_code)]
162164
impl BumpAllocator {
163165
fn new(initial_offset: u32, initial_pages: u32) -> Self {
164166
Self {
@@ -346,7 +348,8 @@ impl WasmBackend {
346348

347349
// Compile composed braids: sequential calls to generators
348350
for braid in &program.braids {
349-
let mut func_body = WasmFunc::new(vec![]);
351+
// local 0 stores the allocated strand pointer
352+
let mut func_body = WasmFunc::new(vec![(1, ValType::I32)]);
350353

351354
// Allocate strands first
352355
func_body.instruction(&Instruction::I32Const(braid.strand_count as i32));
@@ -848,17 +851,25 @@ mod tests {
848851
assert_eq!(&bytes[0..4], b"\0asm");
849852
// WASM version 1
850853
assert_eq!(bytes[4], 1);
851-
assert_eq!(module.functions.is_empty(), true);
854+
// Runtime helper exports are always emitted.
855+
assert_eq!(module.functions.len(), 3);
856+
assert_eq!(module.functions[0].name, "markov_type_i");
857+
assert_eq!(module.functions[1].name, "markov_type_ii");
858+
assert_eq!(module.functions[2].name, "braid_inverse");
852859
}
853860

854861
#[test]
855862
fn test_simple_generator_compiles() {
856863
let mut backend = WasmBackend::new();
857864
let module = backend.generate(&simple_program()).unwrap();
858-
assert_eq!(module.functions.len(), 1);
865+
// 1 generator + 3 runtime helpers
866+
assert_eq!(module.functions.len(), 4);
859867
assert_eq!(module.functions[0].name, "sigma1");
860868
assert_eq!(module.functions[0].params, vec![WasmType::I32]);
861869
assert_eq!(module.functions[0].result, None);
870+
assert_eq!(module.functions[1].name, "markov_type_i");
871+
assert_eq!(module.functions[2].name, "markov_type_ii");
872+
assert_eq!(module.functions[3].name, "braid_inverse");
862873
let bytes = module.to_bytes();
863874
assert_eq!(&bytes[0..4], b"\0asm");
864875
}
@@ -890,10 +901,13 @@ mod tests {
890901

891902
let mut backend = WasmBackend::new();
892903
let module = backend.generate(&program).unwrap();
893-
// 2 generators + 1 composed braid = 3 functions
894-
assert_eq!(module.functions.len(), 3);
904+
// 2 generators + 1 composed braid + 3 runtime helpers
905+
assert_eq!(module.functions.len(), 6);
895906
assert_eq!(module.functions[2].name, "trefoil");
896907
assert_eq!(module.functions[2].result, Some(WasmType::I32));
908+
assert_eq!(module.functions[3].name, "markov_type_i");
909+
assert_eq!(module.functions[4].name, "markov_type_ii");
910+
assert_eq!(module.functions[5].name, "braid_inverse");
897911
}
898912

899913
#[test]

0 commit comments

Comments
 (0)