Skip to content

Commit 6cc7e3d

Browse files
committed
Ensure the 0th slot of anyref table is undefined
This is currently required by our ABI for wasm-bindgen where `None` js values going out have an index of 0 and are intended to be `undefined`. This also refactors initialization a bit to be slightly more generic over the constants we already have defined in this module.
1 parent 5aee2f9 commit 6cc7e3d

File tree

1 file changed

+14
-7
lines changed
  • crates/cli-support/src/js

1 file changed

+14
-7
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,16 +2641,23 @@ impl<'a> Context<'a> {
26412641

26422642
Intrinsic::InitAnyrefTable => {
26432643
self.expose_anyref_table();
2644-
String::from(
2644+
2645+
// Grow the table to insert our initial values, and then also
2646+
// set the 0th slot to `undefined` since that's what we've
2647+
// historically used for our ABI which is that the index of 0
2648+
// returns `undefined` for types like `None` going out.
2649+
let mut base = format!(
26452650
"
26462651
const table = wasm.__wbg_anyref_table;
2647-
const offset = table.grow(4);
2648-
table.set(offset + 0, undefined);
2649-
table.set(offset + 1, null);
2650-
table.set(offset + 2, true);
2651-
table.set(offset + 3, false);
2652+
const offset = table.grow({});
2653+
table.set(0, undefined);
26522654
",
2653-
)
2655+
INITIAL_HEAP_VALUES.len(),
2656+
);
2657+
for (i, value) in INITIAL_HEAP_VALUES.iter().enumerate() {
2658+
base.push_str(&format!("table.set(offset + {}, {});\n", i, value));
2659+
}
2660+
base
26542661
}
26552662
};
26562663
Ok(expr)

0 commit comments

Comments
 (0)