2021-09-09 16:02:31 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
|
2021-09-09 16:02:31 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
2024-11-14 15:01:23 +00:00
|
|
|
#include <LibGC/Ptr.h>
|
|
|
|
#include <LibGC/Root.h>
|
2022-11-23 11:42:35 +00:00
|
|
|
#include <LibJS/ParserError.h>
|
2021-09-12 11:02:20 +00:00
|
|
|
#include <LibJS/Runtime/Realm.h>
|
2021-09-09 16:02:31 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
// 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records
|
2022-09-05 12:31:25 +00:00
|
|
|
class Script final : public Cell {
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_CELL(Script, Cell);
|
|
|
|
GC_DECLARE_ALLOCATOR(Script);
|
2022-09-05 12:31:25 +00:00
|
|
|
|
2021-09-09 16:02:31 +00:00
|
|
|
public:
|
2022-02-07 17:25:39 +00:00
|
|
|
struct HostDefined {
|
|
|
|
virtual ~HostDefined() = default;
|
2022-09-05 12:32:33 +00:00
|
|
|
|
|
|
|
virtual void visit_host_defined_self(Cell::Visitor&) = 0;
|
2022-02-07 17:25:39 +00:00
|
|
|
};
|
|
|
|
|
2022-09-05 12:31:25 +00:00
|
|
|
virtual ~Script() override;
|
2024-11-14 15:01:23 +00:00
|
|
|
static Result<GC::Ref<Script>, Vector<ParserError>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr, size_t line_number_offset = 1);
|
2021-09-09 16:02:31 +00:00
|
|
|
|
2022-09-05 12:31:25 +00:00
|
|
|
Realm& realm() { return *m_realm; }
|
2021-09-09 17:02:40 +00:00
|
|
|
Program const& parse_node() const { return *m_parse_node; }
|
2023-12-03 13:21:21 +00:00
|
|
|
Vector<ModuleWithSpecifier>& loaded_modules() { return m_loaded_modules; }
|
2023-10-28 21:56:15 +00:00
|
|
|
Vector<ModuleWithSpecifier> const& loaded_modules() const { return m_loaded_modules; }
|
2021-09-09 16:02:31 +00:00
|
|
|
|
2022-10-02 21:16:19 +00:00
|
|
|
HostDefined* host_defined() const { return m_host_defined; }
|
2022-01-18 18:21:42 +00:00
|
|
|
StringView filename() const { return m_filename; }
|
|
|
|
|
2021-09-09 16:02:31 +00:00
|
|
|
private:
|
2022-02-07 17:25:39 +00:00
|
|
|
Script(Realm&, StringView filename, NonnullRefPtr<Program>, HostDefined* = nullptr);
|
2021-09-14 18:30:04 +00:00
|
|
|
|
2022-09-05 12:31:25 +00:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Realm> m_realm; // [[Realm]]
|
2023-10-28 21:56:15 +00:00
|
|
|
NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]]
|
|
|
|
Vector<ModuleWithSpecifier> m_loaded_modules; // [[LoadedModules]]
|
2022-01-18 18:21:42 +00:00
|
|
|
|
|
|
|
// Needed for potential lookups of modules.
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString m_filename;
|
2022-02-07 17:25:39 +00:00
|
|
|
HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
|
2021-09-09 16:02:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|