summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus-emitter/src/array_emitter.rs
blob: b44137182ed2a8e8d2cb2343da8f3a5a2cf05e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::ast_emitter::AstEmitter;
use crate::emitter::EmitError;

/// Struct for emitting bytecode for an array element.
pub struct ArrayElementEmitter<'a, F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub state: &'a mut ArrayEmitterState,
    pub elem: F,
}

impl<'a, F> ArrayElementEmitter<'a, F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack] ARRAY INDEX?

        (self.elem)(emitter)?;
        //              [stack] ARRAY INDEX? ELEM

        match &mut self.state.0 {
            ArrayEmitterStateInternal::BeforeSpread { ref mut index } => {
                emitter.emit.init_elem_array(*index);
                //      [stack] ARRAY

                *index += 1;
            }
            ArrayEmitterStateInternal::AfterSpread => {
                emitter.emit.init_elem_inc();
                //      [stack] ARRAY INDEX+1
            }
        }

        //              [stack] ARRAY INDEX?

        Ok(())
    }
}

/// Struct for emitting bytecode for an array element with hole.
pub struct ArrayElisionEmitter<'a> {
    pub state: &'a mut ArrayEmitterState,
}

impl<'a> ArrayElisionEmitter<'a> {
    pub fn emit(self, emitter: &mut AstEmitter) {
        //              [stack] ARRAY INDEX?

        emitter.emit.hole();
        //              [stack] ARRAY INDEX? HOLE

        match &mut self.state.0 {
            ArrayEmitterStateInternal::BeforeSpread { ref mut index } => {
                emitter.emit.init_elem_array(*index);
                //      [stack] ARRAY

                *index += 1;
            }
            ArrayEmitterStateInternal::AfterSpread => {
                emitter.emit.init_elem_inc();
                //      [stack] ARRAY INDEX+1
            }
        }

        //              [stack] ARRAY INDEX?
    }
}

/// Struct for emitting bytecode for an array element with spread.
pub struct ArraySpreadEmitter<'a, F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub state: &'a mut ArrayEmitterState,
    pub elem: F,
}

impl<'a, F> ArraySpreadEmitter<'a, F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack] ARRAY INDEX?

        match self.state.0 {
            ArrayEmitterStateInternal::BeforeSpread { index } => {
                emitter.emit.numeric(index as f64);
            }
            _ => {}
        }
        self.state.0 = ArrayEmitterStateInternal::AfterSpread;

        //              [stack] ARRAY INDEX

        Err(EmitError::NotImplemented("TODO: spread element"))
    }
}

enum ArrayEmitterStateInternal {
    BeforeSpread { index: u32 },
    AfterSpread,
}

/// Opaque struct that can be created only by ArrayEmitter.
/// This guarantees that Array*Emitter structs cannot be used outside
/// of ArrayEmitter callback.
pub struct ArrayEmitterState(ArrayEmitterStateInternal);

impl ArrayEmitterState {
    fn new() -> Self {
        Self(ArrayEmitterStateInternal::BeforeSpread { index: 0 })
    }
}

pub enum ArrayElementKind {
    Normal,
    Elision,
    Spread,
}

/// Struct for emitting bytecode for an array expression.
pub struct ArrayEmitter<'a, ElemT, ElemKindF, ElemF>
where
    ElemKindF: Fn(&ElemT) -> ArrayElementKind,
    ElemF: Fn(&mut AstEmitter, &mut ArrayEmitterState, &'a ElemT) -> Result<(), EmitError>,
{
    pub elements: std::slice::Iter<'a, ElemT>,
    pub elem_kind: ElemKindF,
    pub elem: ElemF,
}

impl<'a, ElemT, ElemKindF, ElemF> ArrayEmitter<'a, ElemT, ElemKindF, ElemF>
where
    ElemKindF: Fn(&ElemT) -> ArrayElementKind,
    ElemF: Fn(&mut AstEmitter, &mut ArrayEmitterState, &'a ElemT) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack]

        // Initialize the array to its minimum possible length.
        let min_length = self
            .elements
            .clone()
            .map(|e| match (self.elem_kind)(e) {
                ArrayElementKind::Normal => 1,
                ArrayElementKind::Elision => 1,
                ArrayElementKind::Spread => 0,
            })
            .sum::<u32>();

        emitter.emit.new_array(min_length);
        //              [stack] ARRAY

        let mut state = ArrayEmitterState::new();
        for element in self.elements {
            (self.elem)(emitter, &mut state, element)?;
            //          [stack] ARRAY INDEX?
        }

        match state.0 {
            ArrayEmitterStateInternal::AfterSpread => {
                //      [stack] ARRAY INDEX

                emitter.emit.pop();
                //      [stack] ARRAY
            }
            _ => {}
        }

        //              [stack] ARRAY

        Ok(())
    }
}