logo
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! # Declarative Records
//!
//! Each declarative Environment Record is associated with an ECMAScript program scope containing variable,
//! `constant`, `let`, `class`, `module`, `import`, and/or function declarations.
//! A declarative Environment Record binds the set of identifiers defined by the declarations contained within its scope.
//! More info:  [ECMA-262 sec-declarative-environment-records](https://tc39.es/ecma262/#sec-declarative-environment-records)

use crate::{
    environment::{
        environment_record_trait::EnvironmentRecordTrait,
        lexical_environment::{Environment, EnvironmentType},
    },
    gc::{Finalize, Trace},
    object::JsObject,
    BoaProfiler, Context, JsResult, JsValue,
};
use gc::{Gc, GcCell};
use rustc_hash::FxHashMap;

/// Declarative Bindings have a few properties for book keeping purposes, such as mutability (const vs let).
/// Can it be deleted? and strict mode.
///
/// So we need to create a struct to hold these values.
/// From this point onwards, a binding is referring to one of these structures.
#[derive(Trace, Finalize, Debug, Clone)]
pub struct DeclarativeEnvironmentRecordBinding {
    pub value: Option<JsValue>,
    pub can_delete: bool,
    pub mutable: bool,
    pub strict: bool,
}

/// A declarative Environment Record binds the set of identifiers defined by the
/// declarations contained within its scope.
#[derive(Debug, Trace, Finalize, Clone)]
pub struct DeclarativeEnvironmentRecord {
    pub env_rec: GcCell<FxHashMap<Box<str>, DeclarativeEnvironmentRecordBinding>>,
    pub outer_env: Option<Environment>,
}

impl DeclarativeEnvironmentRecord {
    pub fn new(env: Option<Environment>) -> DeclarativeEnvironmentRecord {
        let _timer = BoaProfiler::global().start_event("new_declarative_environment", "env");
        DeclarativeEnvironmentRecord {
            env_rec: GcCell::new(FxHashMap::default()),
            outer_env: env,
        }
    }
}

impl EnvironmentRecordTrait for DeclarativeEnvironmentRecord {
    /// `9.1.1.1.1 HasBinding ( N )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-hasbinding-n
    fn has_binding(&self, name: &str, _context: &mut Context) -> JsResult<bool> {
        // 1. If envRec has a binding for the name that is the value of N, return true.
        // 2. Return false.
        Ok(self.env_rec.borrow().contains_key(name))
    }

    /// `9.1.1.1.2 CreateMutableBinding ( N, D )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-createmutablebinding-n-d
    fn create_mutable_binding(
        &self,
        name: &str,
        deletion: bool,
        allow_name_reuse: bool,
        _context: &mut Context,
    ) -> JsResult<()> {
        // 1. Assert: envRec does not already have a binding for N.
        if !allow_name_reuse {
            assert!(
                !self.env_rec.borrow().contains_key(name),
                "Identifier {} has already been declared",
                name
            );
        }

        // 2. Create a mutable binding in envRec for N and record that it is uninitialized.
        //    If D is true, record that the newly created binding may be deleted by a subsequent DeleteBinding call.
        self.env_rec.borrow_mut().insert(
            name.into(),
            DeclarativeEnvironmentRecordBinding {
                value: None,
                can_delete: deletion,
                mutable: true,
                strict: false,
            },
        );

        // 3. Return NormalCompletion(empty).
        Ok(())
    }

    /// `9.1.1.1.3 CreateImmutableBinding ( N, S )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-createimmutablebinding-n-s
    fn create_immutable_binding(
        &self,
        name: &str,
        strict: bool,
        _context: &mut Context,
    ) -> JsResult<()> {
        // 1. Assert: envRec does not already have a binding for N.
        assert!(
            !self.env_rec.borrow().contains_key(name),
            "Identifier {} has already been declared",
            name
        );

        // 2. Create an immutable binding in envRec for N and record that it is uninitialized.
        //    If S is true, record that the newly created binding is a strict binding.
        self.env_rec.borrow_mut().insert(
            name.into(),
            DeclarativeEnvironmentRecordBinding {
                value: None,
                can_delete: true,
                mutable: false,
                strict,
            },
        );

        // 3. Return NormalCompletion(empty).
        Ok(())
    }

    /// `9.1.1.1.4 InitializeBinding ( N, V )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
    fn initialize_binding(
        &self,
        name: &str,
        value: JsValue,
        _context: &mut Context,
    ) -> JsResult<()> {
        if let Some(ref mut record) = self.env_rec.borrow_mut().get_mut(name) {
            if record.value.is_none() {
                // 2. Set the bound value for N in envRec to V.
                // 3. Record that the binding for N in envRec has been initialized.
                record.value = Some(value);

                // 4. Return NormalCompletion(empty).
                return Ok(());
            }
        }

        // 1. Assert: envRec must have an uninitialized binding for N.
        panic!("record must have binding for {}", name);
    }

    /// `9.1.1.1.5 SetMutableBinding ( N, V, S )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
    #[allow(clippy::else_if_without_else)]
    fn set_mutable_binding(
        &self,
        name: &str,
        value: JsValue,
        mut strict: bool,
        context: &mut Context,
    ) -> JsResult<()> {
        // 1. If envRec does not have a binding for N, then
        if self.env_rec.borrow().get(name).is_none() {
            // a. If S is true, throw a ReferenceError exception.
            if strict {
                return Err(context.construct_reference_error(format!("{} not found", name)));
            }

            // b. Perform envRec.CreateMutableBinding(N, true).
            self.create_mutable_binding(name, true, false, context)?;
            // c. Perform envRec.InitializeBinding(N, V).
            self.initialize_binding(name, value, context)?;

            // d. Return NormalCompletion(empty).
            return Ok(());
        }

        let (binding_strict, binding_value_is_none, binding_mutable) = {
            let env_rec = self.env_rec.borrow();
            let binding = env_rec.get(name).unwrap();
            (binding.strict, binding.value.is_none(), binding.mutable)
        };

        // 2. If the binding for N in envRec is a strict binding, set S to true.
        if binding_strict {
            strict = true;
        }

        // 3. If the binding for N in envRec has not yet been initialized, throw a ReferenceError exception.
        if binding_value_is_none {
            return Err(
                context.construct_reference_error(format!("{} has not been initialized", name))
            );
        // 4. Else if the binding for N in envRec is a mutable binding, change its bound value to V.
        } else if binding_mutable {
            let mut env_rec = self.env_rec.borrow_mut();
            let binding = env_rec.get_mut(name).unwrap();
            binding.value = Some(value);
        // 5. Else,
        // a. Assert: This is an attempt to change the value of an immutable binding.
        // b. If S is true, throw a TypeError exception.
        } else if strict {
            return Err(context
                .construct_type_error(format!("Cannot mutate an immutable binding {}", name)));
        }

        // 6. Return NormalCompletion(empty).
        Ok(())
    }

    /// `9.1.1.1.6 GetBindingValue ( N, S )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
    fn get_binding_value(
        &self,
        name: &str,
        _strict: bool,
        context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Assert: envRec has a binding for N.
        // 2. If the binding for N in envRec is an uninitialized binding, throw a ReferenceError exception.
        // 3. Return the value currently bound to N in envRec.
        if let Some(binding) = self.env_rec.borrow().get(name) {
            if let Some(ref val) = binding.value {
                Ok(val.clone())
            } else {
                context.throw_reference_error(format!("{} is an uninitialized binding", name))
            }
        } else {
            panic!("Cannot get binding value for {}", name);
        }
    }

    /// `9.1.1.1.7 DeleteBinding ( N )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
    fn delete_binding(&self, name: &str, _context: &mut Context) -> JsResult<bool> {
        // 1. Assert: envRec has a binding for the name that is the value of N.
        // 2. If the binding for N in envRec cannot be deleted, return false.
        // 3. Remove the binding for N from envRec.
        // 4. Return true.
        match self.env_rec.borrow().get(name) {
            Some(binding) => {
                if binding.can_delete {
                    self.env_rec.borrow_mut().remove(name);
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            None => panic!("env_rec has no binding for {}", name),
        }
    }

    /// `9.1.1.1.8 HasThisBinding ( )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-hasthisbinding
    fn has_this_binding(&self) -> bool {
        // 1. Return false.
        false
    }

    fn get_this_binding(&self, _context: &mut Context) -> JsResult<JsValue> {
        Ok(JsValue::undefined())
    }

    /// `9.1.1.1.9 HasSuperBinding ( )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-hassuperbinding
    fn has_super_binding(&self) -> bool {
        // 1. Return false.
        false
    }

    /// `9.1.1.1.10 WithBaseObject ( )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-declarative-environment-records-withbaseobject
    fn with_base_object(&self) -> Option<JsObject> {
        None
    }

    fn get_outer_environment_ref(&self) -> Option<&Environment> {
        self.outer_env.as_ref()
    }

    fn set_outer_environment(&mut self, env: Environment) {
        self.outer_env = Some(env);
    }

    fn get_environment_type(&self) -> EnvironmentType {
        EnvironmentType::Declarative
    }
}

impl From<DeclarativeEnvironmentRecord> for Environment {
    fn from(env: DeclarativeEnvironmentRecord) -> Environment {
        Gc::new(Box::new(env))
    }
}