boa/
realm.rs

1//! Conceptually, a realm consists of a set of intrinsic objects, an ECMAScript global environment,
2//! all of the ECMAScript code that is loaded within the scope of that global environment,
3//! and other associated state and resources.
4//!
5//! A realm is represented in this implementation as a Realm struct with the fields specified from the spec.
6
7use crate::{
8    environment::{
9        global_environment_record::GlobalEnvironmentRecord, lexical_environment::LexicalEnvironment,
10    },
11    object::{JsObject, Object, ObjectData},
12    BoaProfiler,
13};
14use gc::Gc;
15
16/// Representation of a Realm.
17///
18/// In the specification these are called Realm Records.
19#[derive(Debug)]
20pub struct Realm {
21    pub global_object: JsObject,
22    pub global_env: Gc<GlobalEnvironmentRecord>,
23    pub environment: LexicalEnvironment,
24}
25
26impl Realm {
27    #[allow(clippy::field_reassign_with_default)]
28    pub fn create() -> Self {
29        let _timer = BoaProfiler::global().start_event("Realm::create", "realm");
30        // Create brand new global object
31        // Global has no prototype to pass None to new_obj
32        let mut global = Object::default();
33
34        // Allow identification of the global object easily
35        global.data = ObjectData::global();
36
37        let gc_global = JsObject::new(global);
38
39        // We need to clone the global here because its referenced from separate places (only pointer is cloned)
40        let global_env = GlobalEnvironmentRecord::new(gc_global.clone(), gc_global.clone());
41
42        Self {
43            global_object: gc_global.clone(),
44            global_env: Gc::new(global_env),
45            environment: LexicalEnvironment::new(gc_global),
46        }
47    }
48}