forked from boa-dev/boa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
1317 lines (1162 loc) · 43.3 KB
/
mod.rs
File metadata and controls
1317 lines (1162 loc) · 43.3 KB
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! The ECMAScript context.
use std::{cell::Cell, path::Path, rc::Rc};
use boa_ast::StatementList;
use boa_interner::Interner;
use boa_parser::source::ReadChar;
pub use hooks::{DefaultHooks, HostHooks};
#[cfg(feature = "intl")]
pub use icu::IcuError;
use intrinsics::Intrinsics;
#[cfg(any(feature = "temporal", feature = "intl"))]
use temporal_rs::provider::TimeZoneProvider;
#[cfg(any(feature = "temporal", feature = "intl"))]
use timezone_provider::experimental_tzif::ZeroCompiledTzdbProvider;
use crate::job::Job;
use crate::js_error;
use crate::module::DynModuleLoader;
use crate::vm::{CodeBlock, RuntimeLimits, create_function_object_fast};
use crate::{
HostDefined, JsNativeError, JsResult, JsString, JsValue, NativeObject, Source, builtins,
class::{Class, ClassBuilder},
job::{JobExecutor, SimpleJobExecutor},
js_string,
module::{IdleModuleLoader, ModuleLoader, SimpleModuleLoader},
native_function::NativeFunction,
object::{FunctionObjectBuilder, JsObject, shape::RootShape},
optimizer::{Optimizer, OptimizerOptions, OptimizerStatistics},
property::{Attribute, PropertyDescriptor, PropertyKey},
realm::Realm,
script::Script,
vm::{ActiveRunnable, CallFrame, Vm},
};
use self::intrinsics::StandardConstructor;
pub mod time;
use crate::context::time::StdClock;
pub use time::Clock;
mod hooks;
#[cfg(feature = "intl")]
pub(crate) mod icu;
pub mod intrinsics;
thread_local! {
static CANNOT_BLOCK_COUNTER: Cell<u64> = const { Cell::new(0) };
}
/// ECMAScript context. It is the primary way to interact with the runtime.
///
/// `Context`s constructed in a thread share the same runtime, therefore it
/// is possible to share objects from one context to another context, but they
/// have to be in the same thread.
///
/// # Examples
///
/// ## Execute Function of Script File
///
/// ```rust
/// use boa_engine::{
/// Context, Source, js_string,
/// object::ObjectInitializer,
/// property::{Attribute, PropertyDescriptor},
/// };
///
/// let script = r#"
/// function test(arg1) {
/// if(arg1 != null) {
/// return arg1.x;
/// }
/// return 112233;
/// }
/// "#;
///
/// let mut context = Context::default();
///
/// // Populate the script definition to the context.
/// context.eval(Source::from_bytes(script)).unwrap();
///
/// // Create an object that can be used in eval calls.
/// let arg = ObjectInitializer::new(&mut context)
/// .property(js_string!("x"), 12, Attribute::READONLY)
/// .build();
/// context
/// .register_global_property(js_string!("arg"), arg, Attribute::all())
/// .expect("property shouldn't exist");
///
/// let value = context.eval(Source::from_bytes("test(arg)")).unwrap();
///
/// assert_eq!(value.as_number(), Some(12.0))
/// ```
pub struct Context {
/// String interner in the context.
interner: Interner,
/// Execute in strict mode,
strict: bool,
/// Number of instructions remaining before a forced exit
#[cfg(feature = "fuzz")]
pub(crate) instructions_remaining: usize,
pub(crate) vm: Vm,
pub(crate) kept_alive: Vec<JsObject>,
can_block: bool,
#[cfg(any(feature = "temporal", feature = "intl"))]
timezone_provider: Box<dyn TimeZoneProvider>,
/// Intl data provider.
#[cfg(feature = "intl")]
intl_provider: icu::IntlProvider,
host_hooks: Rc<dyn HostHooks>,
clock: Rc<dyn Clock>,
job_executor: Rc<dyn JobExecutor>,
module_loader: Rc<dyn DynModuleLoader>,
optimizer_options: OptimizerOptions,
root_shape: RootShape,
/// Unique identifier for each parser instance used during the context lifetime.
parser_identifier: u32,
data: HostDefined,
}
impl std::fmt::Debug for Context {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut debug = f.debug_struct("Context");
debug
.field("realm", &self.vm.frame().realm)
.field("interner", &self.interner)
.field("vm", &self.vm)
.field("strict", &self.strict)
.field("job_executor", &"JobExecutor")
.field("hooks", &"HostHooks")
.field("clock", &"Clock")
.field("module_loader", &"ModuleLoader")
.field("optimizer_options", &self.optimizer_options);
#[cfg(feature = "intl")]
debug.field("intl_provider", &self.intl_provider);
// TODO: Support TimeZoneProvider debug names
#[cfg(feature = "temporal")]
debug.field("timezone_provider", &"TimeZoneProvider");
debug.finish_non_exhaustive()
}
}
impl Drop for Context {
fn drop(&mut self) {
if !self.can_block {
CANNOT_BLOCK_COUNTER.set(CANNOT_BLOCK_COUNTER.get() - 1);
}
}
}
impl Default for Context {
fn default() -> Self {
ContextBuilder::default()
.build()
.expect("Building the default context should not fail")
}
}
// ==== Public API ====
impl Context {
/// Create a new [`ContextBuilder`] to specify the [`Interner`] and/or
/// the icu data provider.
#[must_use]
pub fn builder() -> ContextBuilder {
ContextBuilder::default()
}
/// Evaluates the given source by compiling down to bytecode, then interpreting the
/// bytecode into a value.
///
/// # Examples
/// ```
/// # use boa_engine::{Context, Source};
/// let mut context = Context::default();
///
/// let source = Source::from_bytes("1 + 3");
/// let value = context.eval(source).unwrap();
///
/// assert!(value.is_number());
/// assert_eq!(value.as_number().unwrap(), 4.0);
/// ```
///
/// Note that this won't run any scheduled promise jobs; you need to call [`Context::run_jobs`]
/// on the context or [`JobExecutor::run_jobs`] on the provided queue to run them.
#[allow(clippy::unit_arg, dropping_copy_types)]
pub fn eval<R: ReadChar>(&mut self, src: Source<'_, R>) -> JsResult<JsValue> {
Script::parse(src, None, self)?.evaluate(self)
}
/// Applies optimizations to the [`StatementList`] inplace.
pub fn optimize_statement_list(
&mut self,
statement_list: &mut StatementList,
) -> OptimizerStatistics {
let mut optimizer = Optimizer::new(self);
optimizer.apply(statement_list)
}
/// Register a global property.
///
/// It will return an error if the property is already defined.
///
/// # Example
/// ```
/// use boa_engine::{
/// Context, js_string,
/// object::ObjectInitializer,
/// property::{Attribute, PropertyDescriptor},
/// };
///
/// let mut context = Context::default();
///
/// context
/// .register_global_property(
/// js_string!("myPrimitiveProperty"),
/// 10,
/// Attribute::all(),
/// )
/// .expect("property shouldn't exist");
///
/// let object = ObjectInitializer::new(&mut context)
/// .property(js_string!("x"), 0, Attribute::all())
/// .property(js_string!("y"), 1, Attribute::all())
/// .build();
/// context
/// .register_global_property(
/// js_string!("myObjectProperty"),
/// object,
/// Attribute::all(),
/// )
/// .expect("property shouldn't exist");
/// ```
pub fn register_global_property<K, V>(
&mut self,
key: K,
value: V,
attribute: Attribute,
) -> JsResult<()>
where
K: Into<PropertyKey>,
V: Into<JsValue>,
{
self.global_object().define_property_or_throw(
key,
PropertyDescriptor::builder()
.value(value)
.writable(attribute.writable())
.enumerable(attribute.enumerable())
.configurable(attribute.configurable()),
self,
)?;
Ok(())
}
/// Register a global native callable.
///
/// The function will be both `constructable` (call with `new <name>()`) and `callable` (call
/// with `<name>()`).
///
/// The function will be bound to the global object with `writable`, `non-enumerable`
/// and `configurable` attributes. The same as when you create a function in JavaScript.
///
/// # Note
///
/// If you wish to only create the function object without binding it to the global object, you
/// can use the [`FunctionObjectBuilder`] API.
pub fn register_global_callable(
&mut self,
name: JsString,
length: usize,
body: NativeFunction,
) -> JsResult<()> {
let function = FunctionObjectBuilder::new(self.realm(), body)
.name(name.clone())
.length(length)
.constructor(true)
.build();
self.global_object().define_property_or_throw(
name,
PropertyDescriptor::builder()
.value(function)
.writable(true)
.enumerable(false)
.configurable(true),
self,
)?;
Ok(())
}
/// Register a global native function that is not a constructor.
///
/// The function will be bound to the global object with `writable`, `non-enumerable`
/// and `configurable` attributes. The same as when you create a function in JavaScript.
///
/// # Note
///
/// The difference to [`Context::register_global_callable`] is, that the function will not be
/// `constructable`. Usage of the function as a constructor will produce a `TypeError`.
pub fn register_global_builtin_callable(
&mut self,
name: JsString,
length: usize,
body: NativeFunction,
) -> JsResult<()> {
let function = FunctionObjectBuilder::new(self.realm(), body)
.name(name.clone())
.length(length)
.constructor(false)
.build();
self.global_object().define_property_or_throw(
name,
PropertyDescriptor::builder()
.value(function)
.writable(true)
.enumerable(false)
.configurable(true),
self,
)?;
Ok(())
}
/// Registers a global class `C` in the currently active realm.
///
/// Errors if the class has already been registered.
///
/// # Example
/// ```ignore
/// #[derive(Debug, Trace, Finalize)]
/// struct MyClass;
///
/// impl Class for MyClass {
/// // ...
/// }
///
/// context.register_global_class::<MyClass>()?;
/// ```
pub fn register_global_class<C: Class>(&mut self) -> JsResult<()> {
if self.realm().has_class::<C>() {
return Err(JsNativeError::typ()
.with_message("cannot register a class twice")
.into());
}
let mut class_builder = ClassBuilder::new::<C>(self);
C::init(&mut class_builder)?;
let class = class_builder.build();
let property = PropertyDescriptor::builder()
.value(class.constructor())
.writable(C::ATTRIBUTES.writable())
.enumerable(C::ATTRIBUTES.enumerable())
.configurable(C::ATTRIBUTES.configurable());
self.global_object()
.define_property_or_throw(js_string!(C::NAME), property, self)?;
self.realm().register_class::<C>(class);
Ok(())
}
/// Removes the global class `C` from the currently active realm, returning the constructor
/// and prototype of the class if `C` was registered.
///
/// # Note
///
/// This makes the constructor return an error on further calls, but note that this won't protect
/// static properties from being accessed within variables that stored the constructor before being
/// unregistered. If you need that functionality, you can use a static accessor that first checks
/// if the class is registered ([`Context::has_global_class`]) before returning the static value.
///
/// # Example
/// ```ignore
/// #[derive(Debug, Trace, Finalize)]
/// struct MyClass;
///
/// impl Class for MyClass {
/// // ...
/// }
///
/// context.register_global_class::<MyClass>()?;
/// // ... code
/// context.unregister_global_class::<MyClass>()?;
/// ```
pub fn unregister_global_class<C: Class>(&mut self) -> JsResult<Option<StandardConstructor>> {
self.global_object()
.delete_property_or_throw(js_string!(C::NAME), self)?;
Ok(self.realm().unregister_class::<C>())
}
/// Checks if the currently active realm has the global class `C` registered.
#[must_use]
pub fn has_global_class<C: Class>(&self) -> bool {
self.realm().has_class::<C>()
}
/// Gets the constructor and prototype of the global class `C` if the currently active realm has
/// that class registered.
#[must_use]
pub fn get_global_class<C: Class>(&self) -> Option<StandardConstructor> {
self.realm().get_class::<C>()
}
/// Gets the string interner.
#[inline]
#[must_use]
pub const fn interner(&self) -> &Interner {
&self.interner
}
/// Gets a mutable reference to the string interner.
#[inline]
pub fn interner_mut(&mut self) -> &mut Interner {
&mut self.interner
}
/// Returns the global object.
#[inline]
#[must_use]
pub fn global_object(&self) -> JsObject {
self.vm.frame().realm.global_object().clone()
}
/// Returns the currently active intrinsic constructors and objects.
#[inline]
#[must_use]
pub fn intrinsics(&self) -> &Intrinsics {
self.vm.frame().realm.intrinsics()
}
/// Returns the amount of remaining instructions to be executed
#[cfg(feature = "fuzz")]
#[inline]
#[must_use]
pub fn instructions_remaining(&self) -> usize {
self.instructions_remaining
}
/// Returns the currently active realm.
#[inline]
#[must_use]
pub fn realm(&self) -> &Realm {
&self.vm.frame().realm
}
/// Set the value of trace on the context
#[cfg(feature = "trace")]
#[inline]
pub fn set_trace(&mut self, trace: bool) {
self.vm.trace = trace;
}
/// Get optimizer options.
#[inline]
#[must_use]
pub const fn optimizer_options(&self) -> OptimizerOptions {
self.optimizer_options
}
/// Enable or disable optimizations
#[inline]
pub fn set_optimizer_options(&mut self, optimizer_options: OptimizerOptions) {
self.optimizer_options = optimizer_options;
}
/// Changes the strictness mode of the context.
#[inline]
pub fn strict(&mut self, strict: bool) {
self.strict = strict;
}
/// Enqueues a [`Job`] on the [`JobExecutor`].
#[inline]
pub fn enqueue_job(&mut self, job: Job) {
self.job_executor().enqueue_job(job, self);
}
/// Runs all the jobs with the provided job executor.
#[inline]
pub fn run_jobs(&mut self) -> JsResult<()> {
self.job_executor().run_jobs(self)
}
/// Abstract operation [`ClearKeptObjects`][clear].
///
/// Clears all objects maintained alive by calls to the [`AddToKeptObjects`][add] abstract
/// operation, used within the [`WeakRef`][weak] constructor.
///
/// [clear]: https://tc39.es/ecma262/multipage/executable-code-and-execution-contexts.html#sec-clear-kept-objects
/// [add]: https://tc39.es/ecma262/multipage/executable-code-and-execution-contexts.html#sec-addtokeptobjects
/// [weak]: https://tc39.es/ecma262/multipage/managing-memory.html#sec-weak-ref-objects
#[inline]
pub fn clear_kept_objects(&mut self) {
self.kept_alive.clear();
}
/// Retrieves the current stack trace of the context.
///
/// The stack trace is returned ordered with the most recent frames first.
#[inline]
pub fn stack_trace(&self) -> impl Iterator<Item = &CallFrame> {
// The first frame is always a dummy frame (see `Vm` implementation for more details),
// so skip the dummy frame and return the reversed list so that the most recent frames are first.
self.vm.frames.iter().skip(1).rev()
}
/// Replaces the currently active realm with `realm`, and returns the old realm.
#[inline]
pub fn enter_realm(&mut self, realm: Realm) -> Realm {
std::mem::replace(&mut self.vm.frame_mut().realm, realm)
}
/// Create a new Realm with the default global bindings.
pub fn create_realm(&mut self) -> JsResult<Realm> {
let realm = Realm::create(self.host_hooks.as_ref(), &self.root_shape)?;
let old_realm = self.enter_realm(realm);
builtins::set_default_global_bindings(self)?;
Ok(self.enter_realm(old_realm))
}
/// Get the [`RootShape`].
#[inline]
#[must_use]
pub const fn root_shape(&self) -> &RootShape {
&self.root_shape
}
/// Gets the host hooks.
#[inline]
#[must_use]
pub fn host_hooks(&self) -> Rc<dyn HostHooks> {
self.host_hooks.clone()
}
/// Gets the internal clock.
#[inline]
#[must_use]
pub fn clock(&self) -> &dyn Clock {
self.clock.as_ref()
}
/// Gets the current job executor, or `None` if the current job executor
/// is not a `T`.
#[inline]
#[must_use]
pub fn downcast_job_executor<T: 'static>(&self) -> Option<Rc<T>> {
Rc::downcast(self.job_executor.clone()).ok()
}
/// Gets the current module loader, or `None` if the current module loader
/// is not a `T`.
#[must_use]
pub fn downcast_module_loader<T: 'static>(&self) -> Option<Rc<T>> {
Rc::downcast(self.module_loader.clone()).ok()
}
/// Get the [`RuntimeLimits`].
#[inline]
#[must_use]
pub const fn runtime_limits(&self) -> RuntimeLimits {
self.vm.runtime_limits
}
/// Set the [`RuntimeLimits`].
#[inline]
pub fn set_runtime_limits(&mut self, runtime_limits: RuntimeLimits) {
self.vm.runtime_limits = runtime_limits;
}
/// Get a mutable reference to the [`RuntimeLimits`].
#[inline]
pub fn runtime_limits_mut(&mut self) -> &mut RuntimeLimits {
&mut self.vm.runtime_limits
}
/// Returns `true` if this context can be suspended by an `Atomics.wait` call.
#[inline]
#[must_use]
pub fn can_block(&self) -> bool {
self.can_block
}
/// Insert a type into the context-specific [`HostDefined`] field.
#[inline]
pub fn insert_data<T: NativeObject>(&mut self, value: T) -> Option<Box<T>> {
self.data.insert(value)
}
/// Check if the context-specific [`HostDefined`] has type T.
#[inline]
#[must_use]
pub fn has_data<T: NativeObject>(&self) -> bool {
self.data.has::<T>()
}
/// Remove type T from the context-specific [`HostDefined`], if it exists.
#[inline]
pub fn remove_data<T: NativeObject>(&mut self) -> Option<Box<T>> {
self.data.remove::<T>()
}
/// Get type T from the context-specific [`HostDefined`], if it exists.
#[inline]
#[must_use]
pub fn get_data<T: NativeObject>(&self) -> Option<&T> {
self.data.get::<T>()
}
}
// ==== Private API ====
impl Context {
/// Gets the current job executor.
pub(crate) fn job_executor(&self) -> Rc<dyn JobExecutor> {
self.job_executor.clone()
}
/// Gets the current module loader.
pub(crate) fn module_loader(&self) -> Rc<dyn DynModuleLoader> {
self.module_loader.clone()
}
/// Swaps the currently active realm with `realm`.
pub(crate) fn swap_realm(&mut self, realm: &mut Realm) {
std::mem::swap(&mut self.vm.frame_mut().realm, realm);
}
/// Increment and get the parser identifier.
pub(crate) fn next_parser_identifier(&mut self) -> u32 {
self.parser_identifier += 1;
self.parser_identifier
}
/// `CanDeclareGlobalFunction ( N )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-candeclareglobalfunction
pub(crate) fn can_declare_global_function(&mut self, name: &JsString) -> JsResult<bool> {
// 1. Let ObjRec be envRec.[[ObjectRecord]].
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = self.realm().global_object().clone();
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
let name = name.clone().into();
let existing_prop = global_object.__get_own_property__(&name, &mut self.into())?;
// 4. If existingProp is undefined, return ? IsExtensible(globalObject).
let Some(existing_prop) = existing_prop else {
return global_object.is_extensible(self);
};
// 5. If existingProp.[[Configurable]] is true, return true.
if existing_prop.configurable() == Some(true) {
return Ok(true);
}
// 6. If IsDataDescriptor(existingProp) is true and existingProp has attribute values { [[Writable]]: true, [[Enumerable]]: true }, return true.
if existing_prop.is_data_descriptor()
&& existing_prop.writable() == Some(true)
&& existing_prop.enumerable() == Some(true)
{
return Ok(true);
}
// 7. Return false.
Ok(false)
}
/// `CanDeclareGlobalVar ( N )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-candeclareglobalvar
pub(crate) fn can_declare_global_var(&mut self, name: &JsString) -> JsResult<bool> {
// 1. Let ObjRec be envRec.[[ObjectRecord]].
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = self.realm().global_object().clone();
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
let has_property = global_object.has_own_property(name.clone(), self)?;
// 4. If hasProperty is true, return true.
if has_property {
return Ok(true);
}
// 5. Return ? IsExtensible(globalObject).
global_object.is_extensible(self)
}
/// `CreateGlobalVarBinding ( N, D )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-createglobalvarbinding
pub(crate) fn create_global_var_binding(
&mut self,
name: JsString,
configurable: bool,
) -> JsResult<()> {
// 1. Let ObjRec be envRec.[[ObjectRecord]].
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = self.realm().global_object().clone();
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
let has_property = global_object.has_own_property(name.clone(), self)?;
// 4. Let extensible be ? IsExtensible(globalObject).
let extensible = global_object.is_extensible(self)?;
// 5. If hasProperty is false and extensible is true, then
if !has_property && extensible {
// a. Perform ? ObjRec.CreateMutableBinding(N, D).
// b. Perform ? ObjRec.InitializeBinding(N, undefined).
global_object.define_property_or_throw(
name,
PropertyDescriptor::builder()
.value(JsValue::undefined())
.writable(true)
.enumerable(true)
.configurable(configurable)
.build(),
self,
)?;
}
// 6. If envRec.[[VarNames]] does not contain N, then
// a. Append N to envRec.[[VarNames]].
// 7. Return unused.
Ok(())
}
/// `CreateGlobalFunctionBinding ( N, V, D )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-createglobalfunctionbinding
pub(crate) fn create_global_function_binding(
&mut self,
name: JsString,
function: JsObject,
configurable: bool,
) -> JsResult<()> {
// 1. Let ObjRec be envRec.[[ObjectRecord]].
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = self.realm().global_object().clone();
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
let existing_prop =
global_object.__get_own_property__(&name.clone().into(), &mut self.into())?;
// 4. If existingProp is undefined or existingProp.[[Configurable]] is true, then
let desc = if existing_prop.is_none()
|| existing_prop.and_then(|p| p.configurable()) == Some(true)
{
// a. Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }.
PropertyDescriptor::builder()
.value(function.clone())
.writable(true)
.enumerable(true)
.configurable(configurable)
.build()
}
// 5. Else,
else {
// a. Let desc be the PropertyDescriptor { [[Value]]: V }.
PropertyDescriptor::builder()
.value(function.clone())
.build()
};
// 6. Perform ? DefinePropertyOrThrow(globalObject, N, desc).
global_object.define_property_or_throw(name.clone(), desc, self)?;
// 7. Perform ? Set(globalObject, N, V, false).
global_object.set(name, function, false, self)?;
// 8. If envRec.[[VarNames]] does not contain N, then
// a. Append N to envRec.[[VarNames]].
// 9. Return unused.
Ok(())
}
/// `HasRestrictedGlobalProperty ( N )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
pub(crate) fn has_restricted_global_property(&mut self, name: &JsString) -> JsResult<bool> {
// 1. Let ObjRec be envRec.[[ObjectRecord]].
// 2. Let globalObject be ObjRec.[[BindingObject]].
let global_object = self.realm().global_object().clone();
// 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
let name = name.clone().into();
let existing_prop = global_object.__get_own_property__(&name, &mut self.into())?;
// 4. If existingProp is undefined, return false.
let Some(existing_prop) = existing_prop else {
return Ok(false);
};
// 5. If existingProp.[[Configurable]] is true, return false.
if existing_prop.configurable() == Some(true) {
return Ok(false);
}
// 6. Return true.
Ok(true)
}
/// Returns `true` if this context is in strict mode.
pub(crate) const fn is_strict(&self) -> bool {
self.strict
}
/// `9.4.1 GetActiveScriptOrModule ( )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-getactivescriptormodule
#[must_use]
pub fn get_active_script_or_module(&self) -> Option<ActiveRunnable> {
// 1. If the execution context stack is empty, return null.
// 2. Let ec be the topmost execution context on the execution context stack whose ScriptOrModule component is not null.
// 3. If no such execution context exists, return null. Otherwise, return ec's ScriptOrModule.
if let Some(active_runnable) = &self.vm.frame().active_runnable {
return Some(active_runnable.clone());
}
self.vm
.frames
.iter()
.rev()
.find_map(|frame| frame.active_runnable.clone())
}
/// Get `active function object`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#active-function-object
pub(crate) fn active_function_object(&self) -> Option<JsObject> {
if self.vm.native_active_function.is_some() {
return self.vm.native_active_function.clone();
}
self.vm.stack.get_function(self.vm.frame())
}
/// Creates all globals required to evaluate `codeblock`.
///
/// This is the common path of the instantiations:
/// - `EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict )`
/// - `GlobalDeclarationInstantiation ( script, env )`
fn create_globals(
&mut self,
codeblock: &CodeBlock,
configurable_globals: bool,
) -> JsResult<()> {
// 8. For each element d of varDeclarations, in reverse List order, do
// a. If d is not either a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
// i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
// ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
// iii. Let fn be the sole element of the BoundNames of d.
for fun in codeblock.global_fns.iter().rev() {
// ...
// 1. Let fnDefinable be ? env.CanDeclareGlobalFunction(fn).
// 2. If fnDefinable is false, throw a TypeError exception.
let name = codeblock.constant_string(fun.name_index as usize);
if !self.can_declare_global_function(&name)? {
return Err(js_error!(TypeError: "cannot declare global function"));
}
}
// 10. For each element d of varDeclarations, do
for global_var in &codeblock.global_vars {
// ...
// a. Let vnDefinable be ? env.CanDeclareGlobalVar(vn).
// b. If vnDefinable is false, throw a TypeError exception.
let name = codeblock.constant_string(*global_var as usize);
if !self.can_declare_global_var(&name)? {
return Err(js_error!(TypeError: "cannot declare global variable"));
}
}
// 16. For each Parse Node f of functionsToInitialize, do
for fun in &codeblock.global_fns {
// ...
// c. Perform ? env.CreateGlobalFunctionBinding(fn, fo, false).
let function = create_function_object_fast(
codeblock.constant_function(fun.function_index as usize),
self,
);
let name = codeblock.constant_string(fun.name_index as usize);
self.create_global_function_binding(name, function, configurable_globals)?;
}
// 17. For each String vn of declaredVarNames, do
for global_declared_var in &codeblock.global_vars {
// a. Perform ? env.CreateGlobalVarBinding(vn, false).
let name = codeblock.constant_string(*global_declared_var as usize);
self.create_global_var_binding(name, configurable_globals)?;
}
Ok(())
}
/// `GlobalDeclarationInstantiation ( script, env )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-globaldeclarationinstantiation
pub(crate) fn global_declaration_instantiation(
&mut self,
codeblock: &CodeBlock,
) -> JsResult<()> {
// 3. For each element name of lexNames, do
for global_lex in &codeblock.global_lexs {
// c. Let hasRestrictedGlobal be ? env.HasRestrictedGlobalProperty(name).
// d. If hasRestrictedGlobal is true, throw a SyntaxError exception.
let name = codeblock.constant_string(*global_lex as usize);
if self.has_restricted_global_property(&name)? {
return Err(
js_error!(SyntaxError: "cannot redefine non-configurable global property"),
);
}
}
self.create_globals(codeblock, false)
}
/// `EvalDeclarationInstantiation ( body, varEnv, lexEnv, privateEnv, strict )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-evaldeclarationinstantiation
pub(crate) fn eval_declaration_instantiation(&mut self, codeblock: &CodeBlock) -> JsResult<()> {
self.create_globals(codeblock, true)
}
}
impl Context {
/// Creates a `ContextCleanupGuard` that executes some cleanup after being dropped.
pub(crate) fn guard<F>(&mut self, cleanup: F) -> ContextCleanupGuard<'_, F>
where
F: FnOnce(&mut Context) + 'static,
{
ContextCleanupGuard::new(self, cleanup)
}
/// Get the Intl data provider.
#[cfg(feature = "intl")]
pub(crate) const fn intl_provider(&self) -> &icu::IntlProvider {
&self.intl_provider
}
/// Get the Time Zone Provider
#[cfg(feature = "temporal")]
pub(crate) fn timezone_provider(&self) -> &dyn TimeZoneProvider {
self.timezone_provider.as_ref()
}
}
/// Builder for the [`Context`] type.
///
/// This builder allows custom initialization of the [`Interner`] within
/// the context.
#[derive(Default)]