summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Realm.cpp
blob: 4f73ab44a397f0b8ef368a7a3439f5caa923f10b (plain)
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
/*
 * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>

namespace JS {

// 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm
Realm* Realm::create(VM& vm)
{
    return vm.heap().allocate_without_global_object<Realm>();
}

// 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
void Realm::set_global_object(GlobalObject& global_object, Object* this_value)
{
    // NOTE: Step 1 is not supported, the global object must be allocated elsewhere.
    // 2. Assert: Type(globalObj) is Object.

    // Non-standard
    global_object.set_associated_realm({}, *this);

    // 3. If thisValue is undefined, set thisValue to globalObj.
    if (!this_value)
        this_value = &global_object;

    // 4. Set realmRec.[[GlobalObject]] to globalObj.
    m_global_object = &global_object;

    // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
    // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
    m_global_environment = global_object.heap().allocate_without_global_object<GlobalEnvironment>(global_object, *this_value);

    // 7. Return realmRec.
}

void Realm::visit_edges(Visitor& visitor)
{
    visitor.visit(m_global_object);
    visitor.visit(m_global_environment);
}

}