1
//! A read of a global (plan 0014 PERSIST): a cell whose flag is not 1 throws a
2
//! catchable `$nomi_error` at run time. Flag 0 is `UNBOUND-VARIABLE`, as
3
//! reading an unbound special in Common Lisp; `LOST_FLAG` is a cell that starts
4
//! with an earlier unit's value no constant holds, unreadable until a store.
5

            
6
use wasm_encoder::BlockType;
7

            
8
use super::CompileContext;
9
use crate::compiler::emit::FunctionEmitter;
10
use crate::error::{Error, Result};
11

            
12
/// The flag of a cell, and the interned code and message of the condition a
13
/// read of it throws while the flag is not 1.
14
#[derive(Clone, Copy, Debug)]
15
pub(crate) struct UnboundCell {
16
    flag: u32,
17
    code: (u32, i32),
18
    message: (u32, i32),
19
}
20

            
21
/// The flag of a cell that starts with an earlier unit's value no constant
22
/// holds: bound, but unreadable until a store sets the flag to 1.
23
pub(crate) const LOST_FLAG: i32 = 2;
24

            
25
const UNBOUND: &str = "UNBOUND-VARIABLE";
26
const LOST: &str = "VALUE-LOST";
27

            
28
impl CompileContext {
29
    /// Records that the cell in global `idx`, named `name`, starts unbound
30
    /// with the bound flag `flag`.
31
150282
    pub(crate) fn note_unbound_cell(&mut self, idx: u32, flag: u32, name: &str) -> Result<()> {
32
150282
        let message = format!("the variable {name} is unbound");
33
150282
        self.note_read_check(idx, flag, UNBOUND, &message)
34
150282
    }
35

            
36
    /// Records that the cell in global `idx`, named `name`, starts with an
37
    /// earlier unit's value of type `type_name` no constant holds.
38
5826
    pub(crate) fn note_lost_cell(
39
5826
        &mut self,
40
5826
        idx: u32,
41
5826
        flag: u32,
42
5826
        name: &str,
43
5826
        type_name: &str,
44
5826
    ) -> Result<()> {
45
5826
        let message = format!("{name}: its {type_name} value does not survive the form");
46
5826
        self.note_read_check(idx, flag, LOST, &message)
47
5826
    }
48

            
49
156108
    fn note_read_check(&mut self, idx: u32, flag: u32, code: &str, message: &str) -> Result<()> {
50
        let cell = UnboundCell {
51
156108
            flag,
52
156108
            code: self.intern(code)?,
53
156108
            message: self.intern(message)?,
54
        };
55
156108
        self.read_checks.insert(idx, cell);
56
156108
        Ok(())
57
156108
    }
58

            
59
312216
    fn intern(&mut self, text: &str) -> Result<(u32, i32)> {
60
312216
        let len = i32::try_from(text.len())
61
            .map_err(|_| Error::Compile(format!("{text}: too long for a data segment")))?;
62
312216
        Ok((self.add_data(text.as_bytes())?, len))
63
312216
    }
64

            
65
    /// Pushes global `idx`, throwing first when it is a cell that is not bound
66
    /// to a readable value.
67
361975
    pub(crate) fn emit_global_read(&mut self, emit: &mut FunctionEmitter, idx: u32) -> Result<()> {
68
361975
        if let Some(cell) = self.read_checks.get(&idx).copied() {
69
289191
            emit.global_get(cell.flag);
70
289191
            emit.i32_const(1);
71
289191
            emit.i32_ne();
72
289191
            emit.if_block(BlockType::Empty);
73
289191
            for (data, len) in [cell.code, cell.message] {
74
578382
                emit.i32_const(0);
75
578382
                emit.i32_const(len);
76
578382
                emit.array_new_data(self.ids.ty_i8_array, data);
77
            }
78
289191
            emit.ref_null_any();
79
289191
            emit.struct_new(self.condition_type_idx());
80
289191
            emit.throw(self.nomi_error_tag()?);
81
289191
            emit.block_end()?;
82
72784
        }
83
361975
        emit.global_get(idx);
84
361975
        Ok(())
85
361975
    }
86
}