summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-09-11 19:36:25 +0100
committerLinus Groh <mail@linusgroh.de>2021-09-12 11:10:20 +0100
commit2b8d5696abd595f64927bd756c76a0366f15d4b7 (patch)
tree21f8f153fcba6a5af8b5905dfefabff052dcce5c /Userland/Libraries/LibJS
parentd9c3bafcd9997ef49925af13dca2c9db3a13df44 (diff)
downloadserenity-2b8d5696abd595f64927bd756c76a0366f15d4b7.zip
LibJS: Allocate a Realm next to GlobalObject in Interpreter::create()
Also pass a Realm reference to the Bytecode::Interpreter constructor, just like we pass the GlobalObject.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Interpreter.cpp3
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Interpreter.h4
-rw-r--r--Userland/Libraries/LibJS/Interpreter.cpp13
-rw-r--r--Userland/Libraries/LibJS/Interpreter.h16
-rw-r--r--Userland/Libraries/LibJS/Runtime/Realm.cpp20
-rw-r--r--Userland/Libraries/LibJS/Runtime/Realm.h2
6 files changed, 51 insertions, 7 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
index 50d9f2a49b..9368f39af4 100644
--- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
@@ -22,9 +22,10 @@ Interpreter* Interpreter::current()
return s_current;
}
-Interpreter::Interpreter(GlobalObject& global_object)
+Interpreter::Interpreter(GlobalObject& global_object, Realm& realm)
: m_vm(global_object.vm())
, m_global_object(global_object)
+ , m_realm(realm)
{
VERIFY(!s_current);
s_current = this;
diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h
index 0dda59a99b..f8faea9029 100644
--- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h
+++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h
@@ -22,13 +22,14 @@ using RegisterWindow = Vector<Value>;
class Interpreter {
public:
- explicit Interpreter(GlobalObject&);
+ Interpreter(GlobalObject&, Realm&);
~Interpreter();
// FIXME: Remove this thing once we don't need it anymore!
static Interpreter* current();
GlobalObject& global_object() { return m_global_object; }
+ Realm& realm() { return m_realm; }
VM& vm() { return m_vm; }
Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr);
@@ -74,6 +75,7 @@ private:
VM& m_vm;
GlobalObject& m_global_object;
+ Realm& m_realm;
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
Optional<BasicBlock const*> m_pending_jump;
Value m_return_value;
diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp
index 41db16c75d..3436be652e 100644
--- a/Userland/Libraries/LibJS/Interpreter.cpp
+++ b/Userland/Libraries/LibJS/Interpreter.cpp
@@ -11,7 +11,6 @@
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
-#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/OrdinaryFunctionObject.h>
#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/Shape.h>
@@ -23,7 +22,7 @@ NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(Globa
{
DeferGC defer_gc(global_object.heap());
auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
- interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
+ interpreter->m_global_object = make_handle(&global_object);
return interpreter;
}
@@ -80,6 +79,16 @@ const GlobalObject& Interpreter::global_object() const
return static_cast<const GlobalObject&>(*m_global_object.cell());
}
+Realm& Interpreter::realm()
+{
+ return static_cast<Realm&>(*m_realm.cell());
+}
+
+const Realm& Interpreter::realm() const
+{
+ return static_cast<const Realm&>(*m_realm.cell());
+}
+
void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
{
ScopeGuard guard([&] {
diff --git a/Userland/Libraries/LibJS/Interpreter.h b/Userland/Libraries/LibJS/Interpreter.h
index 204cf041d5..cb75ab14ce 100644
--- a/Userland/Libraries/LibJS/Interpreter.h
+++ b/Userland/Libraries/LibJS/Interpreter.h
@@ -18,7 +18,9 @@
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/Exception.h>
+#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/MarkedValueList.h>
+#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
@@ -37,8 +39,12 @@ public:
DeferGC defer_gc(vm.heap());
auto interpreter = adopt_own(*new Interpreter(vm));
VM::InterpreterExecutionScope scope(*interpreter);
- interpreter->m_global_object = make_handle(static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...)));
- static_cast<GlobalObjectType*>(interpreter->m_global_object.cell())->initialize_global_object();
+ auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...));
+ auto* realm = Realm::create(vm);
+ realm->set_global_object(*global_object, global_object);
+ interpreter->m_global_object = make_handle(global_object);
+ interpreter->m_realm = make_handle(realm);
+ static_cast<GlobalObjectType*>(global_object)->initialize_global_object();
return interpreter;
}
@@ -51,6 +57,9 @@ public:
GlobalObject& global_object();
const GlobalObject& global_object() const;
+ Realm& realm();
+ Realm const& realm() const;
+
ALWAYS_INLINE VM& vm() { return *m_vm; }
ALWAYS_INLINE const VM& vm() const { return *m_vm; }
ALWAYS_INLINE Heap& heap() { return vm().heap(); }
@@ -91,7 +100,8 @@ private:
NonnullRefPtr<VM> m_vm;
- Handle<Object> m_global_object;
+ Handle<GlobalObject> m_global_object;
+ Handle<Realm> m_realm;
};
}
diff --git a/Userland/Libraries/LibJS/Runtime/Realm.cpp b/Userland/Libraries/LibJS/Runtime/Realm.cpp
index 7d832905f5..1082f0804f 100644
--- a/Userland/Libraries/LibJS/Runtime/Realm.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Realm.cpp
@@ -8,6 +8,26 @@
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.
+
+ // 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);
diff --git a/Userland/Libraries/LibJS/Runtime/Realm.h b/Userland/Libraries/LibJS/Runtime/Realm.h
index 56571729cf..bd4d25d5e2 100644
--- a/Userland/Libraries/LibJS/Runtime/Realm.h
+++ b/Userland/Libraries/LibJS/Runtime/Realm.h
@@ -23,6 +23,8 @@ public:
return vm.heap().allocate_without_global_object<Realm>();
}
+ void set_global_object(GlobalObject&, Object* this_value = nullptr);
+
[[nodiscard]] GlobalObject& global_object() const { return *m_global_object; }
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }