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
use gc::Gc;
use crate::{
environment::{
declarative_environment_record::DeclarativeEnvironmentRecord,
environment_record_trait::EnvironmentRecordTrait,
lexical_environment::{Environment, EnvironmentType, VariableScope},
},
gc::{empty_trace, Finalize, Trace},
object::JsObject,
Context, JsResult, JsValue,
};
#[derive(Copy, Finalize, Debug, Clone)]
pub enum BindingStatus {
Lexical,
Initialized,
Uninitialized,
}
unsafe impl Trace for BindingStatus {
empty_trace!();
}
#[derive(Debug, Trace, Finalize, Clone)]
pub struct FunctionEnvironmentRecord {
pub declarative_record: DeclarativeEnvironmentRecord,
pub this_value: JsValue,
pub this_binding_status: BindingStatus,
pub function: JsObject,
pub home_object: JsValue,
pub new_target: JsValue,
}
impl FunctionEnvironmentRecord {
pub fn new(
f: JsObject,
this: Option<JsValue>,
outer: Option<Environment>,
binding_status: BindingStatus,
new_target: JsValue,
context: &mut Context,
) -> JsResult<FunctionEnvironmentRecord> {
let mut func_env = FunctionEnvironmentRecord {
declarative_record: DeclarativeEnvironmentRecord::new(outer), function: f,
this_binding_status: binding_status,
home_object: JsValue::undefined(),
new_target,
this_value: JsValue::undefined(),
};
if let Some(v) = this {
func_env.bind_this_value(v, context)?;
}
Ok(func_env)
}
pub fn bind_this_value(&mut self, value: JsValue, context: &mut Context) -> JsResult<JsValue> {
match self.this_binding_status {
BindingStatus::Lexical => {
panic!("Cannot bind to an arrow function!");
}
BindingStatus::Initialized => {
context.throw_reference_error("Cannot bind to an initialized function!")
}
BindingStatus::Uninitialized => {
self.this_value = value.clone();
self.this_binding_status = BindingStatus::Initialized;
Ok(value)
}
}
}
pub fn get_super_base(&self) -> JsValue {
let home = &self.home_object;
if home.is_undefined() {
JsValue::undefined()
} else {
assert!(home.is_object());
home.as_object()
.expect("home_object must be an Object")
.prototype_instance()
}
}
}
impl EnvironmentRecordTrait for FunctionEnvironmentRecord {
fn has_binding(&self, name: &str, context: &mut Context) -> JsResult<bool> {
self.declarative_record.has_binding(name, context)
}
fn create_mutable_binding(
&self,
name: &str,
deletion: bool,
allow_name_reuse: bool,
context: &mut Context,
) -> JsResult<()> {
self.declarative_record
.create_mutable_binding(name, deletion, allow_name_reuse, context)
}
fn create_immutable_binding(
&self,
name: &str,
strict: bool,
context: &mut Context,
) -> JsResult<()> {
self.declarative_record
.create_immutable_binding(name, strict, context)
}
fn initialize_binding(
&self,
name: &str,
value: JsValue,
context: &mut Context,
) -> JsResult<()> {
self.declarative_record
.initialize_binding(name, value, context)
}
fn set_mutable_binding(
&self,
name: &str,
value: JsValue,
strict: bool,
context: &mut Context,
) -> JsResult<()> {
self.declarative_record
.set_mutable_binding(name, value, strict, context)
}
fn get_binding_value(
&self,
name: &str,
strict: bool,
context: &mut Context,
) -> JsResult<JsValue> {
self.declarative_record
.get_binding_value(name, strict, context)
}
fn delete_binding(&self, name: &str, context: &mut Context) -> JsResult<bool> {
self.declarative_record.delete_binding(name, context)
}
fn has_this_binding(&self) -> bool {
!matches!(self.this_binding_status, BindingStatus::Lexical)
}
fn has_super_binding(&self) -> bool {
if let BindingStatus::Lexical = self.this_binding_status {
false
} else {
!self.home_object.is_undefined()
}
}
fn get_this_binding(&self, context: &mut Context) -> JsResult<JsValue> {
match self.this_binding_status {
BindingStatus::Lexical => {
panic!("There is no this for a lexical function record");
}
BindingStatus::Uninitialized => {
context.throw_reference_error("Uninitialized binding for this function")
}
BindingStatus::Initialized => Ok(self.this_value.clone()),
}
}
fn with_base_object(&self) -> Option<JsObject> {
None
}
fn get_outer_environment_ref(&self) -> Option<&Environment> {
self.declarative_record.get_outer_environment_ref()
}
fn set_outer_environment(&mut self, env: Environment) {
self.declarative_record.set_outer_environment(env)
}
fn get_environment_type(&self) -> EnvironmentType {
EnvironmentType::Function
}
fn recursive_create_mutable_binding(
&self,
name: &str,
deletion: bool,
_scope: VariableScope,
context: &mut Context,
) -> JsResult<()> {
self.create_mutable_binding(name, deletion, false, context)
}
fn recursive_create_immutable_binding(
&self,
name: &str,
deletion: bool,
_scope: VariableScope,
context: &mut Context,
) -> JsResult<()> {
self.create_immutable_binding(name, deletion, context)
}
}
impl From<FunctionEnvironmentRecord> for Environment {
fn from(env: FunctionEnvironmentRecord) -> Environment {
Gc::new(Box::new(env))
}
}