
This is just another workaround, but it should be much more reliable than Interpreter::realm(), especially when allocating NativeFunctions and ECMAScriptFunctionObjects: we're guaranteed to have a GlobalObject at that point, and it likely was set as the GlobalObject of a Realm and can lead us back to it. We're however not guaranteed that the VM can give us an Interpreter, which is why functions in LibWeb can be a bit crashy at the moment. We use a WeakPtr<Realm> to properly handle the unlikely case where the Realm goes away after associating a GlobalObject to it. We'll always need _something_ of this sort if we want to support OrdinaryFunctionCreate and CreateBuiltinFunction without the explicit realm argument while no JS is running, because they want to use the current Realm Record (always in the first and as fallback in the second case).
40 lines
1.2 KiB
C++
40 lines
1.2 KiB
C++
/*
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
|
namespace JS {
|
|
|
|
// 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<GlobalEnvironment>(global_object, global_object, *this_value);
|
|
|
|
// 7. Return realmRec.
|
|
}
|
|
|
|
void Realm::visit_edges(Visitor& visitor)
|
|
{
|
|
visitor.visit(m_global_object);
|
|
visitor.visit(m_global_environment);
|
|
}
|
|
|
|
}
|