Path: blob/main/crates/environ/src/ref_bits.rs
1691 views
//! Definitions for bits in the in-memory / in-table representation of references.12/// An "initialized bit" in a funcref table.3///4/// We lazily initialize tables of funcrefs, and this mechanism5/// requires us to interpret zero as "uninitialized", triggering a6/// slowpath on table read to possibly initialize the element. (This7/// has to be *zero* because that is the only value we can cheaply8/// initialize, e.g. with newly mmap'd memory.)9///10/// However, the user can also store a null reference into a table. We11/// have to interpret this as "actually null", and not "lazily12/// initialize to the original funcref that this slot had".13///14/// To do so, we rewrite nulls into the "initialized null" value. Note15/// that this should *only exist inside the table*: whenever we load a16/// value out of a table, we immediately mask off the low bit that17/// contains the initialized-null flag. Conversely, when we store into18/// a table, we have to translate a true null into an "initialized19/// null".20///21/// We can generalize a bit in order to simply the table-set logic: we22/// can set the LSB of *all* explicitly stored values to 1 in order to23/// note that they are indeed explicitly stored. We then mask off this24/// bit every time we load.25///26/// Note that we take care to set this bit and mask it off when27/// accessing tables directly in fastpaths in generated code as well.28pub const FUNCREF_INIT_BIT: usize = 1;2930/// The mask we apply to all refs loaded from funcref tables.31///32/// This allows us to use the LSB as an "initialized flag" (see below)33/// to distinguish from an uninitialized element in a34/// lazily-initialized funcref table.35pub const FUNCREF_MASK: usize = !FUNCREF_INIT_BIT;363738