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
use crate::environment::environment_record_trait::EnvironmentRecordTrait;
use crate::environment::lexical_environment::{Environment, EnvironmentType};
use crate::js::object::Property;
use crate::js::value::{Value, ValueData};
use gc::Gc;
#[derive(Trace, Finalize, Clone)]
pub struct ObjectEnvironmentRecord {
pub bindings: Value,
pub with_environment: bool,
pub outer_env: Option<Environment>,
}
impl EnvironmentRecordTrait for ObjectEnvironmentRecord {
fn has_binding(&self, name: &String) -> bool {
if !self.bindings.has_field(name.to_string()) {
return false;
}
if !self.with_environment {
return true;
}
true
}
fn create_mutable_binding(&mut self, name: String, deletion: bool) {
let bindings = &mut self.bindings;
let uninitialized = Gc::new(ValueData::Undefined);
let mut prop = Property::new(uninitialized);
prop.enumerable = true;
prop.writable = true;
prop.configurable = deletion;
bindings.set_prop(name, prop);
}
fn create_immutable_binding(&mut self, _name: String, _strict: bool) {
unimplemented!()
}
fn initialize_binding(&mut self, name: String, value: Value) {
debug_assert!(self.has_binding(&name));
return self.set_mutable_binding(name, value, false);
}
fn set_mutable_binding(&mut self, name: String, value: Value, strict: bool) {
debug_assert!(value.is_object() || value.is_function());
let bindings = &mut self.bindings;
bindings.update_prop(name, Some(value.clone()), None, None, Some(strict));
}
fn get_binding_value(&self, name: String, strict: bool) -> Value {
if self.bindings.has_field(name.clone()) {
return self.bindings.get_field(name);
}
if !strict {
return Gc::new(ValueData::Undefined);
}
Gc::new(ValueData::Undefined)
}
fn delete_binding(&mut self, name: String) -> bool {
self.bindings.remove_prop(&name);
true
}
fn has_this_binding(&self) -> bool {
false
}
fn has_super_binding(&self) -> bool {
false
}
fn with_base_object(&self) -> Value {
if self.with_environment {
return self.bindings.clone();
}
Gc::new(ValueData::Undefined)
}
fn get_outer_environment(&self) -> Option<Environment> {
match &self.outer_env {
Some(outer) => Some(outer.clone()),
None => None,
}
}
fn set_outer_environment(&mut self, env: Environment) {
self.outer_env = Some(env);
}
fn get_environment_type(&self) -> EnvironmentType {
return EnvironmentType::Function;
}
fn get_global_object(&self) -> Option<Value> {
match &self.outer_env {
Some(outer) => outer.borrow().get_global_object(),
None => None,
}
}
}