1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 16:18:12 +00:00

LibJS: Make Script and Module GC-allocated

This ensures that code currently in any active or saved execution stack
always stays alive.
This commit is contained in:
Andreas Kling 2022-09-05 14:31:25 +02:00
parent cb15132146
commit 00c8f07192
18 changed files with 145 additions and 89 deletions

View file

@ -7,8 +7,8 @@
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <LibJS/AST.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/Realm.h>
@ -16,18 +16,18 @@
namespace JS {
// 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records
class Script
: public RefCounted<Script>
, public Weakable<Script> {
class Script final : public Cell {
JS_CELL(Script, Cell);
public:
struct HostDefined {
virtual ~HostDefined() = default;
};
~Script() = default;
static Result<NonnullRefPtr<Script>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr, size_t line_number_offset = 1);
virtual ~Script() override;
static Result<NonnullGCPtr<Script>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr, size_t line_number_offset = 1);
Realm& realm() { return *m_realm.cell(); }
Realm& realm() { return *m_realm; }
Program const& parse_node() const { return *m_parse_node; }
HostDefined* host_defined() { return m_host_defined; }
@ -35,10 +35,10 @@ public:
private:
Script(Realm&, StringView filename, NonnullRefPtr<Program>, HostDefined* = nullptr);
// Handles are not safe unless we keep the VM alive.
NonnullRefPtr<VM> m_vm;
Handle<Realm> m_realm; // [[Realm]]
virtual void visit_edges(Cell::Visitor&) override;
GCPtr<Realm> m_realm; // [[Realm]]
NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]]
// Needed for potential lookups of modules.