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
//! # Global Environment Records
//!
//! A global Environment Record is used to represent the outer most scope that is shared by all
//! of the ECMAScript Script elements that are processed in a common realm.
//! A global Environment Record provides the bindings for built-in globals (clause 18),
//! properties of the global object, and for all top-level declarations (13.2.8, 13.2.10)
//! that occur within a Script.
//! More info:  https://tc39.github.io/ecma262/#sec-global-environment-records

use crate::environment::declerative_environment_record::DeclerativeEnvironmentRecord;
use crate::environment::environment_record_trait::EnvironmentRecordTrait;
use crate::environment::lexical_environment::{Environment, EnvironmentType};
use crate::environment::object_environment_record::ObjectEnvironmentRecord;
use crate::js::value::{Value, ValueData};
use gc::Gc;
use std::collections::HashSet;

#[derive(Trace, Finalize, Clone)]
pub struct GlobalEnvironmentRecord {
    pub object_record: Box<ObjectEnvironmentRecord>,
    pub global_this_binding: Value,
    pub declerative_record: Box<DeclerativeEnvironmentRecord>,
    pub var_names: HashSet<String>,
}

impl GlobalEnvironmentRecord {
    pub fn get_this_binding(&self) -> Value {
        return self.global_this_binding.clone();
    }

    pub fn has_var_decleration(&self, name: &String) -> bool {
        return self.var_names.contains(name);
    }

    pub fn has_lexical_decleration(&self, name: &String) -> bool {
        self.declerative_record.has_binding(name)
    }

    pub fn has_restricted_global_property(&self, name: &String) -> bool {
        let global_object = &self.object_record.bindings;
        let existing_prop = global_object.get_prop(name.clone());
        match existing_prop {
            Some(prop) => {
                if prop.value.is_undefined() || prop.configurable == true {
                    return false;
                }
                true
            }
            None => false,
        }
    }

    pub fn create_global_var_binding(&mut self, name: String, deletion: bool) {
        let obj_rec = &mut self.object_record;
        let global_object = &obj_rec.bindings;
        let has_property = global_object.has_field(name.clone());
        let extensible = global_object.is_extensible();
        if !has_property && extensible {
            obj_rec.create_mutable_binding(name.clone(), deletion);
            obj_rec.initialize_binding(name.clone(), Gc::new(ValueData::Undefined));
        }

        let var_declared_names = &mut self.var_names;
        if !var_declared_names.contains(&name) {
            var_declared_names.insert(name.clone());
        }
    }

    pub fn create_global_function_binding(&mut self, name: String, value: Value, deletion: bool) {
        let global_object = &mut self.object_record.bindings;
        let existing_prop = global_object.get_prop(name.clone());
        match existing_prop {
            Some(prop) => {
                if prop.value.is_undefined() || prop.configurable {
                    global_object.update_prop(
                        name,
                        Some(value),
                        Some(true),
                        Some(true),
                        Some(deletion),
                    );
                }
            }
            None => {
                global_object.update_prop(
                    name,
                    Some(value),
                    Some(true),
                    Some(true),
                    Some(deletion),
                );
            }
        }
    }
}

impl EnvironmentRecordTrait for GlobalEnvironmentRecord {
    fn has_binding(&self, name: &String) -> bool {
        if self.declerative_record.has_binding(name) {
            return true;
        }
        self.object_record.has_binding(name)
    }

    fn create_mutable_binding(&mut self, name: String, deletion: bool) {
        if self.declerative_record.has_binding(&name) {
            // TODO: change to exception
            panic!("Binding already exists!");
        }

        self.declerative_record
            .create_mutable_binding(name.clone(), deletion)
    }

    fn create_immutable_binding(&mut self, name: String, strict: bool) {
        if self.declerative_record.has_binding(&name) {
            // TODO: change to exception
            panic!("Binding already exists!");
        }
        self.declerative_record
            .create_immutable_binding(name.clone(), strict)
    }

    fn initialize_binding(&mut self, name: String, value: Value) {
        if self.declerative_record.has_binding(&name) {
            // TODO: assert binding is in the object environment record
            return self
                .declerative_record
                .initialize_binding(name.clone(), value);
        }

        panic!("Should not initialized binding without creating first.");
    }

    fn set_mutable_binding(&mut self, name: String, value: Value, strict: bool) {
        if self.declerative_record.has_binding(&name) {
            return self
                .declerative_record
                .set_mutable_binding(name, value, strict);
        }
        self.object_record.set_mutable_binding(name, value, strict)
    }

    fn get_binding_value(&self, name: String, strict: bool) -> Value {
        if self.declerative_record.has_binding(&name) {
            return self.declerative_record.get_binding_value(name, strict);
        }
        return self.object_record.get_binding_value(name, strict);
    }

    fn delete_binding(&mut self, name: String) -> bool {
        if self.declerative_record.has_binding(&name) {
            return self.declerative_record.delete_binding(name.clone());
        }

        let global: &Value = &self.object_record.bindings;
        if global.has_field(name.clone()) {
            let status = self.object_record.delete_binding(name.clone());
            if status {
                let var_names = &mut self.var_names;
                if var_names.contains(&name) {
                    var_names.remove(&name);
                    return status;
                }
            }
        }
        true
    }

    fn has_this_binding(&self) -> bool {
        true
    }

    fn has_super_binding(&self) -> bool {
        false
    }

    fn with_base_object(&self) -> Value {
        Gc::new(ValueData::Undefined)
    }

    fn get_outer_environment(&self) -> Option<Environment> {
        None
    }

    fn set_outer_environment(&mut self, _env: Environment) {
        unimplemented!()
    }

    fn get_environment_type(&self) -> EnvironmentType {
        return EnvironmentType::Global;
    }

    fn get_global_object(&self) -> Option<Value> {
        Some(self.global_this_binding.clone())
    }
}