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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use std::ops::ControlFlow;
use ahash::AHashSet;
use crate::{
ecmascript::{
Agent, DeclarativeEnvironment, DeclarativeEnvironmentRecord, ExceptionType,
GlobalEnvironment, InternalMethods, JsResult, Object, ObjectEnvironment,
ObjectEnvironmentRecord, PropertyDescriptor, PropertyKey, PropertyLookupCache, SetResult,
String, TryError, TryResult, Value, define_property_or_throw, has_own_property,
is_extensible, js_result_into_try, set, try_has_own_property,
},
engine::{Bindable, GcScope, NoGcScope, Scopable},
heap::{ArenaAccess, ArenaAccessMut, CompactionLists, HeapMarkAndSweep, WorkQueues},
};
use super::TryHasBindingContinue;
/// ### [9.1.1.4 Global Environment Records](https://tc39.es/ecma262/#sec-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 19), properties of the global object, and for all
/// top-level declarations (8.2.9, 8.2.11) that occur within a Script.
#[derive(Debug, Clone)]
pub(crate) struct GlobalEnvironmentRecord {
/// ### \[\[ObjectRecord\]\]
///
/// Binding object is the global object. It contains global built-in
/// bindings as well as FunctionDeclaration, GeneratorDeclaration,
/// AsyncFunctionDeclaration, AsyncGeneratorDeclaration, and
/// VariableDeclaration bindings in global code for the associated realm.
object_record: ObjectEnvironment<'static>,
/// ### \[\[GlobalThisValue\]\]
///
/// The value returned by this in global scope. Hosts may provide any
/// ECMAScript Object value.
global_this_value: Object<'static>,
/// ### \[\[DeclarativeRecord\]\]
///
/// Contains bindings for all declarations in global code for the
/// associated realm code except for FunctionDeclaration,
/// GeneratorDeclaration, AsyncFunctionDeclaration,
/// AsyncGeneratorDeclaration, and VariableDeclaration bindings.
declarative_record: DeclarativeEnvironment<'static>,
/// ### \[\[VarNames\]\]
///
/// The string names bound by FunctionDeclaration, GeneratorDeclaration,
/// AsyncFunctionDeclaration, AsyncGeneratorDeclaration, and
/// VariableDeclaration declarations in global code for the associated
/// realm.
// TODO: Use the Heap to set this.
var_names: AHashSet<String<'static>>,
}
impl HeapMarkAndSweep for GlobalEnvironmentRecord {
fn mark_values(&self, queues: &mut WorkQueues) {
let Self {
object_record,
global_this_value,
declarative_record,
var_names,
} = self;
declarative_record.mark_values(queues);
global_this_value.mark_values(queues);
object_record.mark_values(queues);
for ele in var_names {
ele.mark_values(queues);
}
}
fn sweep_values(&mut self, compactions: &CompactionLists) {
let Self {
object_record,
global_this_value,
declarative_record,
var_names,
} = self;
declarative_record.sweep_values(compactions);
global_this_value.sweep_values(compactions);
object_record.sweep_values(compactions);
for key in var_names.clone() {
let mut new_key = key;
new_key.sweep_values(compactions);
if key != new_key {
self.var_names.remove(&key);
self.var_names.insert(new_key);
}
}
}
}
/// ### [9.1.2.5 NewGlobalEnvironment ( G, thisValue )](https://tc39.es/ecma262/#sec-newglobalenvironment)
///
/// The abstract operation NewGlobalEnvironment takes arguments G (an
/// Object) and thisValue (an Object) and returns a Global Environment
/// Record.
pub(crate) fn new_global_environment<'a>(
agent: &mut Agent,
global: Object,
this_value: Object,
gc: NoGcScope<'a, '_>,
) -> GlobalEnvironment<'a> {
// 1. Let objRec be NewObjectEnvironment(G, false, null).
let obj_rec = ObjectEnvironmentRecord::new(global, false, None);
// 2. Let dclRec be NewDeclarativeEnvironment(null).
let dcl_rec = DeclarativeEnvironmentRecord::new(None);
agent.heap.alloc_counter += core::mem::size_of::<Option<ObjectEnvironmentRecord>>()
+ core::mem::size_of::<Option<DeclarativeEnvironmentRecord>>();
let (object_record, declarative_record) = agent
.heap
.environments
.push_object_environment(obj_rec, dcl_rec, gc);
// 3. Let env be a new Global Environment Record.
agent.heap.alloc_counter += core::mem::size_of::<Option<GlobalEnvironmentRecord>>();
agent.heap.environments.push_global_environment(
GlobalEnvironmentRecord {
// 4. Set env.[[ObjectRecord]] to objRec.
object_record: object_record.unbind(),
// 5. Set env.[[GlobalThisValue]] to thisValue.
global_this_value: this_value.unbind(),
// 6. Set env.[[DeclarativeRecord]] to dclRec.
declarative_record: declarative_record.unbind(),
// 7. Set env.[[VarNames]] to a new empty List.
var_names: AHashSet::default(),
// 8. Set env.[[OuterEnv]] to null.
// NOTE: We do not expose an outer environment, so this is implicit.
},
gc,
)
// 9. Return env.
}
impl<'e> GlobalEnvironment<'e> {
pub(crate) fn get_binding_object(self, agent: &Agent) -> Object<'e> {
self.get(agent).object_record.get_binding_object(agent)
}
/// ### Try [9.1.1.4.1 HasBinding ( N )](https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n)
///
/// The HasBinding concrete method of a Global Environment Record envRec
/// takes argument N (a String) and returns either a normal completion
/// containing a Boolean or a throw completion. It determines if the
/// argument identifier is one of the identifiers bound by the record.
pub(crate) fn try_has_binding<'gc>(
self,
agent: &mut Agent,
name: String,
cache: Option<PropertyLookupCache>,
gc: NoGcScope<'gc, '_>,
) -> ControlFlow<TryError<'gc>, TryHasBindingContinue<'gc>> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
// 2. If ! DclRec.HasBinding(N) is true, return true.
if env_rec.declarative_record.has_binding(agent, name) {
return TryHasBindingContinue::Result(true).into();
}
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 4. Return ? ObjRec.HasBinding(N).
obj_rec.try_has_binding(agent, name, cache, gc)
}
/// ### [9.1.1.4.1 HasBinding ( N )](https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n)
///
/// The HasBinding concrete method of a Global Environment Record envRec
/// takes argument N (a String) and returns either a normal completion
/// containing a Boolean or a throw completion. It determines if the
/// argument identifier is one of the identifiers bound by the record.
pub(crate) fn has_binding<'a>(
self,
agent: &mut Agent,
name: String,
gc: GcScope<'a, '_>,
) -> JsResult<'a, bool> {
let env = self.bind(gc.nogc());
let name = name.bind(gc.nogc());
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
// 2. If ! DclRec.HasBinding(N) is true, return true.
if env_rec.declarative_record.has_binding(agent, name) {
return Ok(true);
}
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 4. Return ? ObjRec.HasBinding(N).
obj_rec.has_binding(agent, name.unbind(), gc)
}
/// ### [9.1.1.4.2 CreateMutableBinding ( N, D )](https://tc39.es/ecma262/#sec-global-environment-records-createmutablebinding-n-d)
///
/// The CreateMutableBinding concrete method of a Global Environment Record
/// envRec takes arguments N (a String) and D (a Boolean) and returns
/// either a normal completion containing UNUSED or a throw completion. It
/// creates a new mutable binding for the name N that is uninitialized. The
/// binding is created in the associated DeclarativeRecord. A binding for N
/// must not already exist in the DeclarativeRecord. If D is true, the new
/// binding is marked as being subject to deletion.
pub(crate) fn create_mutable_binding<'a>(
self,
agent: &mut Agent,
name: String,
is_deletable: bool,
gc: NoGcScope<'a, '_>,
) -> JsResult<'a, ()> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, throw a TypeError exception.
if dcl_rec.has_binding(agent, name) {
let error_message = format!(
"Redeclaration of global binding '{}'.",
name.to_string_lossy_(agent)
);
Err(agent.throw_exception(ExceptionType::TypeError, error_message, gc))
} else {
// 3. Return ! DclRec.CreateMutableBinding(N, D).
dcl_rec.create_mutable_binding(agent, name, is_deletable);
Ok(())
}
}
/// ### [9.1.1.4.3 CreateImmutableBinding ( N, S )](https://tc39.es/ecma262/#sec-global-environment-records-createimmutablebinding-n-s)
///
/// The CreateImmutableBinding concrete method of a Global Environment
/// Record envRec takes arguments N (a String) and S (a Boolean) and
/// returns either a normal completion containing UNUSED or a throw
/// completion. It creates a new immutable binding for the name N that is
/// uninitialized. A binding must not already exist in this Environment
/// Record for N. If S is true, the new binding is marked as a strict
/// binding.
pub(crate) fn create_immutable_binding<'a>(
self,
agent: &mut Agent,
name: String,
is_strict: bool,
gc: NoGcScope<'a, '_>,
) -> JsResult<'a, ()> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, throw a TypeError exception.
if dcl_rec.has_binding(agent, name) {
let error_message = format!(
"Redeclaration of global binding '{}'.",
name.to_string_lossy_(agent)
);
Err(agent.throw_exception(ExceptionType::TypeError, error_message, gc))
} else {
// 3. Return ! DclRec.CreateImmutableBinding(N, S).
dcl_rec.create_immutable_binding(agent, name, is_strict);
Ok(())
}
}
/// ### [9.1.1.4.4 InitializeBinding ( N, V )](https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v)
///
/// The InitializeBinding concrete method of a Global Environment Record
/// envRec takes arguments N (a String) and V (an ECMAScript language
/// value) and returns either a normal completion containing UNUSED or a
/// throw completion. It is used to set the bound value of the current
/// binding of the identifier whose name is N to the value V. An
/// uninitialized binding for N must already exist.
pub(crate) fn try_initialize_binding<'gc>(
self,
agent: &mut Agent,
name: String,
cache: Option<PropertyLookupCache>,
value: Value,
gc: NoGcScope<'gc, '_>,
) -> TryResult<'gc, SetResult<'gc>> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, name) {
// a. Return ! DclRec.InitializeBinding(N, V).
dcl_rec.initialize_binding(agent, name, value);
SetResult::Done.into()
} else {
// 3. Assert: If the binding exists, it must be in the Object Environment Record.
// 4. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 5. Return ? ObjRec.InitializeBinding(N, V).
obj_rec.try_initialize_binding(agent, name, cache, value, gc)
}
}
/// ### [9.1.1.4.4 InitializeBinding ( N, V )](https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v)
///
/// The InitializeBinding concrete method of a Global Environment Record
/// envRec takes arguments N (a String) and V (an ECMAScript language
/// value) and returns either a normal completion containing UNUSED or a
/// throw completion. It is used to set the bound value of the current
/// binding of the identifier whose name is N to the value V. An
/// uninitialized binding for N must already exist.
pub(crate) fn initialize_binding<'a>(
self,
agent: &mut Agent,
name: String,
cache: Option<PropertyLookupCache>,
value: Value,
gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let nogc = gc.nogc();
let env = self.bind(nogc);
let name = name.bind(nogc);
let cache = cache.bind(nogc);
let value = value.bind(nogc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, name) {
// a. Return ! DclRec.InitializeBinding(N, V).
dcl_rec.initialize_binding(agent, name, value);
Ok(())
} else {
// 3. Assert: If the binding exists, it must be in the Object Environment Record.
// 4. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 5. Return ? ObjRec.InitializeBinding(N, V).
obj_rec.initialize_binding(agent, name.unbind(), cache.unbind(), value.unbind(), gc)
}
}
/// ### Try [9.1.1.4.5 SetMutableBinding ( N, V, S )](https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s)
///
/// The SetMutableBinding concrete method of a Global Environment Record
/// envRec takes arguments N (a String), V (an ECMAScript language value),
/// and S (a Boolean) and returns either a normal completion containing
/// UNUSED or a throw completion. It attempts to change the bound value of
/// the current binding of the identifier whose name is N to the value V.
/// If the binding is an immutable binding and S is true, a TypeError is
/// thrown. A property named N normally already exists but if it does not
/// or is not currently writable, error handling is determined by S.
pub(crate) fn try_set_mutable_binding<'gc>(
self,
agent: &mut Agent,
name: String,
cache: Option<PropertyLookupCache>,
value: Value,
is_strict: bool,
gc: NoGcScope<'gc, '_>,
) -> TryResult<'gc, SetResult<'gc>> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, name) {
// a. Return ? DclRec.SetMutableBinding(N, V, S).
js_result_into_try(
dcl_rec
.set_mutable_binding(agent, name, value, is_strict, gc)
.map(|_| SetResult::Done),
)
} else {
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 4. Return ? ObjRec.SetMutableBinding(N, V, S).
obj_rec.try_set_mutable_binding(agent, name, cache, value, is_strict, gc)
}
}
/// ### [9.1.1.4.5 SetMutableBinding ( N, V, S )](https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s)
///
/// The SetMutableBinding concrete method of a Global Environment Record
/// envRec takes arguments N (a String), V (an ECMAScript language value),
/// and S (a Boolean) and returns either a normal completion containing
/// UNUSED or a throw completion. It attempts to change the bound value of
/// the current binding of the identifier whose name is N to the value V.
/// If the binding is an immutable binding and S is true, a TypeError is
/// thrown. A property named N normally already exists but if it does not
/// or is not currently writable, error handling is determined by S.
pub(crate) fn set_mutable_binding<'a>(
self,
agent: &mut Agent,
name: String,
cache: Option<PropertyLookupCache>,
value: Value,
is_strict: bool,
gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let nogc = gc.nogc();
let env = self.bind(nogc);
let name = name.bind(nogc);
let cache = cache.bind(nogc);
let value = value.bind(nogc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, name) {
// a. Return ? DclRec.SetMutableBinding(N, V, S).
dcl_rec.set_mutable_binding(
agent,
name.unbind(),
value.unbind(),
is_strict,
gc.into_nogc(),
)
} else {
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 4. Return ? ObjRec.SetMutableBinding(N, V, S).
obj_rec.set_mutable_binding(
agent,
name.unbind(),
cache.unbind(),
value.unbind(),
is_strict,
gc,
)
}
}
/// ### Try [9.1.1.4.6 GetBindingValue ( N, S )](https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s)
///
/// The GetBindingValue concrete method of a Global Environment Record
/// envRec takes arguments N (a String) and S (a Boolean) and returns
/// either a normal completion containing an ECMAScript language value or a
/// throw completion. It returns the value of its bound identifier whose
/// name is N. If the binding is an uninitialized binding throw a
/// ReferenceError exception. A property named N normally already exists
/// but if it does not or is not currently writable, error handling is
/// determined by S.
pub(crate) fn try_get_binding_value(
self,
agent: &mut Agent,
n: String,
cache: Option<PropertyLookupCache>,
s: bool,
gc: NoGcScope<'e, '_>,
) -> TryResult<'e, Value<'e>> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, n) {
// a. Return ? DclRec.GetBindingValue(N, S).
js_result_into_try(dcl_rec.get_binding_value(agent, n, s, gc))
} else {
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 4. Return ? ObjRec.GetBindingValue(N, S).
obj_rec.try_get_binding_value(agent, n, cache, s, gc)
}
}
/// ### [9.1.1.4.6 GetBindingValue ( N, S )](https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s)
///
/// The GetBindingValue concrete method of a Global Environment Record
/// envRec takes arguments N (a String) and S (a Boolean) and returns
/// either a normal completion containing an ECMAScript language value or a
/// throw completion. It returns the value of its bound identifier whose
/// name is N. If the binding is an uninitialized binding throw a
/// ReferenceError exception. A property named N normally already exists
/// but if it does not or is not currently writable, error handling is
/// determined by S.
pub(crate) fn get_binding_value<'a>(
self,
agent: &mut Agent,
n: String,
s: bool,
gc: GcScope<'a, '_>,
) -> JsResult<'a, Value<'a>> {
let env = self.bind(gc.nogc());
let n = n.bind(gc.nogc());
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, n) {
// a. Return ? DclRec.GetBindingValue(N, S).
dcl_rec.get_binding_value(agent, n.unbind(), s, gc.into_nogc())
} else {
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record;
// 4. Return ? ObjRec.GetBindingValue(N, S).
obj_rec.get_binding_value(agent, n.unbind(), s, gc)
}
}
/// ### Try [9.1.1.4.7 DeleteBinding ( N )](https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n)
///
/// The DeleteBinding concrete method of a Global Environment Record envRec
/// takes argument N (a String) and returns either a normal completion
/// containing a Boolean or a throw completion. It can only delete bindings
/// that have been explicitly designated as being subject to deletion.
pub(crate) fn try_delete_binding<'a>(
self,
agent: &mut Agent,
name: String,
gc: NoGcScope<'a, '_>,
) -> TryResult<'a, bool> {
let env = self.bind(gc);
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, name) {
// a. Return ! DclRec.DeleteBinding(N).
return TryResult::Continue(dcl_rec.delete_binding(agent, name));
}
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc);
// 4. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
// 5. Let existingProp be ? HasOwnProperty(globalObject, N).
let n = PropertyKey::from(name);
let existing_prop = try_has_own_property(agent, global_object, n, None, gc)?;
// 6. If existingProp is true, then
if existing_prop {
// a. Let status be ? ObjRec.DeleteBinding(N).
let status = obj_rec.try_delete_binding(agent, name, gc)?;
// b. If status is true and envRec.[[VarNames]] contains N, then
if status {
let env_rec = env.get_mut(agent);
if env_rec.var_names.contains(&name) {
// i. Remove N from envRec.[[VarNames]].
env_rec.var_names.remove(&name.unbind());
}
}
// c. Return status.
TryResult::Continue(status)
} else {
// 7. Return true.
TryResult::Continue(true)
}
}
/// ### [9.1.1.4.7 DeleteBinding ( N )](https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n)
///
/// The DeleteBinding concrete method of a Global Environment Record envRec
/// takes argument N (a String) and returns either a normal completion
/// containing a Boolean or a throw completion. It can only delete bindings
/// that have been explicitly designated as being subject to deletion.
pub(crate) fn delete_binding<'a>(
self,
agent: &mut Agent,
name: String,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, bool> {
let env = self.bind(gc.nogc());
let name = name.bind(gc.nogc());
let env_rec = &env.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. If ! DclRec.HasBinding(N) is true, then
if dcl_rec.has_binding(agent, name) {
// a. Return ! DclRec.DeleteBinding(N).
return Ok(dcl_rec.delete_binding(agent, name));
}
// 3. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc.nogc());
// 4. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
// 5. Let existingProp be ? HasOwnProperty(globalObject, N).
let n = PropertyKey::from(name);
let scoped_name = name.scope(agent, gc.nogc());
let env = env.scope(agent, gc.nogc());
let obj_rec = obj_rec.scope(agent, gc.nogc());
let existing_prop =
has_own_property(agent, global_object.unbind(), n.unbind(), gc.reborrow()).unbind()?;
// SAFETY: obj_rec not shared.
let obj_rec = unsafe { obj_rec.take(agent).bind(gc.nogc()) };
// 6. If existingProp is true, then
if existing_prop {
// a. Let status be ? ObjRec.DeleteBinding(N).
let status = obj_rec
.unbind()
.delete_binding(agent, scoped_name.get(agent), gc.reborrow())
.unbind()?;
let env = unsafe { env.take(agent) }.bind(gc.nogc());
// b. If status is true and envRec.[[VarNames]] contains N, then
if status {
let name = scoped_name.get(agent);
let env_rec = env.get_mut(agent);
if env_rec.var_names.contains(&name) {
// i. Remove N from envRec.[[VarNames]].
env_rec.var_names.remove(&name);
}
}
// c. Return status.
Ok(status)
} else {
// 7. Return true.
Ok(true)
}
}
/// ### [9.1.1.4.11 GetThisBinding ( )](https://tc39.es/ecma262/#sec-global-environment-records-getthisbinding)
///
/// The GetThisBinding concrete method of a Global Environment Record
/// envRec takes no arguments and returns a normal completion containing an
/// Object.
pub(crate) fn get_this_binding(self, agent: &Agent) -> Object<'e> {
// 1. Return envRec.[[GlobalThisValue]].
self.get(agent).global_this_value
}
/// ### [9.1.1.4.12 HasVarDeclaration ( N )](https://tc39.es/ecma262/#sec-hasvardeclaration)
///
/// The HasVarDeclaration concrete method of a Global Environment Record
/// envRec takes argument N (a String) and returns a Boolean. It determines
/// if the argument identifier has a binding in this record that was
/// created
/// using a VariableStatement or a FunctionDeclaration.
pub(crate) fn has_var_declaration(self, agent: &Agent, name: String) -> bool {
let env_rec = &self.get(agent);
// 1. Let varDeclaredNames be envRec.[[VarNames]].
let var_declared_names = &env_rec.var_names;
// 2. If varDeclaredNames contains N, return true.
// 3. Return false.
var_declared_names.contains(&name)
}
/// ### [9.1.1.4.13 HasLexicalDeclaration ( N )](https://tc39.es/ecma262/#sec-haslexicaldeclaration)
///
/// The HasLexicalDeclaration concrete method of a Global Environment
/// Record envRec takes argument N (a String) and returns a Boolean. It
/// determines if the argument identifier has a binding in this record that
/// was created using a lexical declaration such as a LexicalDeclaration or
/// a ClassDeclaration.
pub(crate) fn has_lexical_declaration(self, agent: &Agent, name: String) -> bool {
let env_rec = &self.get(agent);
// 1. Let DclRec be envRec.[[DeclarativeRecord]].
let dcl_rec = env_rec.declarative_record;
// 2. Return ! DclRec.HasBinding(N).
dcl_rec.has_binding(agent, name)
}
/// ### [9.1.1.4.14 HasRestrictedGlobalProperty ( N )](https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty)
///
/// The HasRestrictedGlobalProperty concrete method of a Global Environment
/// Record envRec takes argument N (a String) and returns either a normal
/// completion containing a Boolean or a throw completion. It determines if
/// the argument identifier is the name of a property of the global object
/// that must not be shadowed by a global lexical binding.
pub(crate) fn has_restricted_global_property<'a>(
self,
agent: &mut Agent,
name: String,
gc: GcScope<'a, '_>,
) -> JsResult<'a, bool> {
let env = self.bind(gc.nogc());
let name = name.bind(gc.nogc());
let env_rec = &env.get(agent);
// 1. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc.nogc());
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
let n = PropertyKey::from(name);
let existing_prop =
global_object
.unbind()
.internal_get_own_property(agent, n.unbind(), gc)?;
let Some(existing_prop) = existing_prop else {
// 4. If existingProp is undefined, return false.
return Ok(false);
};
// 5. If existingProp.[[Configurable]] is true, return false.
// 6. Return true.
Ok(existing_prop.configurable != Some(true))
}
/// ### [9.1.1.4.15 CanDeclareGlobalVar ( N )](https://tc39.es/ecma262/#sec-candeclareglobalvar)
///
/// The CanDeclareGlobalVar concrete method of a Global Environment Record
/// envRec takes argument N (a String) and returns either a normal
/// completion containing a Boolean or a throw completion. It determines if
/// a corresponding CreateGlobalVarBinding call would succeed if called for
/// the same argument N. Redundant var declarations and var declarations
/// for pre-existing global object properties are allowed.
pub(crate) fn can_declare_global_var<'a>(
self,
agent: &mut Agent,
name: String,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, bool> {
let env = self.bind(gc.nogc());
let name = name.bind(gc.nogc());
let env_rec = &env.get(agent);
// 1. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc.nogc());
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
let scoped_global_object = global_object.scope(agent, gc.nogc());
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
let n = PropertyKey::from(name);
let has_property =
has_own_property(agent, global_object.unbind(), n.unbind(), gc.reborrow()).unbind()?;
// 4. If hasProperty is true, return true.
if has_property {
Ok(true)
} else {
// 5. Return ? IsExtensible(globalObject).
is_extensible(agent, scoped_global_object.get(agent), gc)
}
}
/// ### [9.1.1.4.16 CanDeclareGlobalFunction ( N )](https://tc39.es/ecma262/#sec-candeclareglobalfunction)
///
/// The CanDeclareGlobalFunction concrete method of a Global Environment
/// Record envRec takes argument N (a String) and returns either a normal
/// completion containing a Boolean or a throw completion. It determines if
/// a corresponding CreateGlobalFunctionBinding call would succeed if
/// called for the same argument N.
pub(crate) fn can_declare_global_function<'a>(
self,
agent: &mut Agent,
name: String,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, bool> {
let name = name.bind(gc.nogc());
let env = self.bind(gc.nogc());
let env_rec = &env.get(agent);
// 1. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc.nogc());
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
let scoped_global_object = global_object.scope(agent, gc.nogc());
let n = PropertyKey::from(name);
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
let existing_prop = global_object
.unbind()
.internal_get_own_property(agent, n.unbind(), gc.reborrow())
.unbind()?
.bind(gc.nogc());
// 4. If existingProp is undefined, return ? IsExtensible(globalObject).
let Some(existing_prop) = existing_prop else {
return is_extensible(agent, scoped_global_object.get(agent), gc);
};
// 5. If existingProp.[[Configurable]] is true, return true.
if existing_prop.configurable == Some(true)
|| existing_prop.is_data_descriptor()
&& existing_prop.writable == Some(true)
&& existing_prop.enumerable == Some(true)
{
// 6. If IsDataDescriptor(existingProp) is true and existingProp has attribute values { [[Writable]]: true, [[Enumerable]]: true }, true.
Ok(true)
} else {
// 7. Return false.
Ok(false)
}
}
/// ### [9.1.1.4.17 CreateGlobalVarBinding ( N, D )](https://tc39.es/ecma262/#sec-createglobalvarbinding)
///
/// The CreateGlobalVarBinding concrete method of a Global Environment
/// Record envRec takes arguments N (a String) and D (a Boolean) and
/// returns either a normal completion containing UNUSED or a throw
/// completion. It creates and initializes a mutable binding in the
/// associated Object Environment Record and records the bound name in the
/// associated \[\[VarNames]] List. If a binding already exists, it is
/// reused and assumed to be initialized.
pub(crate) fn create_global_var_binding<'a>(
self,
agent: &mut Agent,
name: String,
cache: PropertyLookupCache,
is_deletable: bool,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let nogc = gc.nogc();
let env = self.bind(nogc);
let name = name.bind(nogc);
let cache = cache.bind(nogc);
let env_rec = &env.get(agent);
// 1. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc.nogc());
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
let scoped_global_object = global_object.scope(agent, nogc);
let n = PropertyKey::from(name);
let name = name.scope(agent, nogc);
let env = env.scope(agent, nogc);
let cache = cache.scope(agent, nogc);
let obj_rec = obj_rec.scope(agent, nogc);
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
let has_property =
has_own_property(agent, global_object.unbind(), n.unbind(), gc.reborrow()).unbind()?;
// 4. Let extensible be ? IsExtensible(globalObject).
let extensible =
is_extensible(agent, scoped_global_object.get(agent), gc.reborrow()).unwrap();
// 5. If hasProperty is false and extensible is true, then
if !has_property && extensible {
// a. Perform ? ObjRec.CreateMutableBinding(N, D).
obj_rec
.get(agent)
.create_mutable_binding(agent, name.get(agent), is_deletable, gc.reborrow())
.unbind()?
.bind(gc.nogc());
// b. Perform ? ObjRec.InitializeBinding(N, undefined).
// SAFETY: obj_rec is not shared.
unsafe { obj_rec.take(agent) }.initialize_binding(
agent,
name.get(agent),
Some(cache.get(agent)),
Value::Undefined,
gc,
)?;
} else {
// SAFETY: obj_rec is not shared
let _ = unsafe { obj_rec.take(agent) };
}
// SAFETY: cache is not shared.
let _ = unsafe { cache.take(agent) };
// SAFETY: env is not shared.
let env = unsafe { env.take(agent) };
// SAFETY: name is not shared.
let name = unsafe { name.take(agent) };
// 6. If envRec.[[VarNames]] does not contain N, then
// a. Append N to envRec.[[VarNames]].
env.get_mut(agent).var_names.insert(name);
// 7. Return UNUSED.
Ok(())
}
/// ### [9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D )](https://tc39.es/ecma262/#sec-createglobalfunctionbinding)
///
/// The CreateGlobalFunctionBinding concrete method of a Global Environment
/// Record envRec takes arguments N (a String), V (an ECMAScript language
/// value), and D (a Boolean) and returns either a normal completion
/// containing UNUSED or a throw completion. It creates and initializes a
/// mutable binding in the associated Object Environment Record and records
/// the bound name in the associated [[VarNames]] List. If a binding
/// already exists, it is replaced.
pub(crate) fn create_global_function_binding<'a>(
self,
agent: &mut Agent,
name: String,
value: Value,
d: bool,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let nogc = gc.nogc();
let env = self.bind(nogc);
let name = name.bind(nogc);
let value = value.scope(agent, nogc);
let env_rec = &env.get(agent);
// 1. Let ObjRec be envRec.[[ObjectRecord]].
let obj_rec = env_rec.object_record.bind(gc.nogc());
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = obj_rec.get_binding_object(agent);
let scoped_global_object = global_object.scope(agent, nogc);
let n = PropertyKey::from(name);
let scoped_n = n.scope(agent, nogc);
let env = env.scope(agent, nogc);
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
let existing_prop = global_object
.unbind()
.internal_get_own_property(agent, n.unbind(), gc.reborrow())
.unbind()?
.bind(gc.nogc());
// 4. If existingProp is undefined or existingProp.[[Configurable]] is true, then
let desc = if existing_prop.is_none() || existing_prop.unwrap().configurable == Some(true) {
// a. Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }.
PropertyDescriptor {
value: Some(value.get(agent)),
writable: Some(true),
get: None,
set: None,
enumerable: Some(true),
configurable: Some(d),
}
} else {
// 5. Else,
// a. Let desc be the PropertyDescriptor { [[Value]]: V }.
PropertyDescriptor {
value: Some(value.get(agent)),
writable: None,
get: None,
set: None,
enumerable: None,
configurable: None,
}
};
// 6. Perform ? DefinePropertyOrThrow(globalObject, N, desc).
define_property_or_throw(
agent,
scoped_global_object.get(agent),
scoped_n.get(agent),
desc,
gc.reborrow(),
)
.unbind()?;
// 7. Perform ? Set(globalObject, N, V, false).
set(
agent,
scoped_global_object.get(agent),
scoped_n.get(agent),
value.get(agent),
false,
gc,
)?;
// SAFETY: env is not shared.
let env = unsafe { env.take(agent) };
// SAFETY: scoped_n is not shared.
let name = unsafe { scoped_n.take(agent) };
// 8. If envRec.[[VarNames]] does not contain N, then
// a. Append N to envRec.[[VarNames]].
// SAFETY: Name of a global function cannot be a numeric string.
let n = unsafe { String::try_from(name.into_value_unchecked()).unwrap() };
env.get_mut(agent).var_names.insert(n);
// 9. Return UNUSED.
Ok(())
// NOTE
// Global function declarations are always represented as own
// properties of the global object. If possible, an existing own
// property is reconfigured to have a standard set of attribute values.
// Step 7 is equivalent to what calling the InitializeBinding concrete
// method would do and if globalObject is a Proxy will produce the same
// sequence of Proxy trap calls.
}
}
impl HeapMarkAndSweep for GlobalEnvironment<'static> {
fn mark_values(&self, queues: &mut WorkQueues) {
queues.global_environments.push(*self);
}
fn sweep_values(&mut self, compactions: &CompactionLists) {
compactions.global_environments.shift_index(&mut self.0);
}
}