mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-21 08:41:35 +00:00
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.
This commit is contained in:
@ -2641,16 +2641,23 @@ impl<'a> Context<'a> {
|
|||||||
|
|
||||||
Intrinsic::InitAnyrefTable => {
|
Intrinsic::InitAnyrefTable => {
|
||||||
self.expose_anyref_table();
|
self.expose_anyref_table();
|
||||||
String::from(
|
|
||||||
|
// Grow the table to insert our initial values, and then also
|
||||||
|
// set the 0th slot to `undefined` since that's what we've
|
||||||
|
// historically used for our ABI which is that the index of 0
|
||||||
|
// returns `undefined` for types like `None` going out.
|
||||||
|
let mut base = format!(
|
||||||
"
|
"
|
||||||
const table = wasm.__wbg_anyref_table;
|
const table = wasm.__wbg_anyref_table;
|
||||||
const offset = table.grow(4);
|
const offset = table.grow({});
|
||||||
table.set(offset + 0, undefined);
|
table.set(0, undefined);
|
||||||
table.set(offset + 1, null);
|
|
||||||
table.set(offset + 2, true);
|
|
||||||
table.set(offset + 3, false);
|
|
||||||
",
|
",
|
||||||
)
|
INITIAL_HEAP_VALUES.len(),
|
||||||
|
);
|
||||||
|
for (i, value) in INITIAL_HEAP_VALUES.iter().enumerate() {
|
||||||
|
base.push_str(&format!("table.set(offset + {}, {});\n", i, value));
|
||||||
|
}
|
||||||
|
base
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(expr)
|
Ok(expr)
|
||||||
|
Reference in New Issue
Block a user