2021-09-09 18:02:31 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
|
2021-09-09 18:02:31 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2026-02-11 01:45:55 +01:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
|
#include <AK/Utf16FlyString.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Ptr.h>
|
|
|
|
|
#include <LibGC/Root.h>
|
2025-07-19 13:49:30 -07:00
|
|
|
#include <LibJS/Export.h>
|
2026-02-11 01:42:41 +01:00
|
|
|
#include <LibJS/Forward.h>
|
2022-11-23 12:42:35 +01:00
|
|
|
#include <LibJS/ParserError.h>
|
2021-09-12 12:02:20 +01:00
|
|
|
#include <LibJS/Runtime/Realm.h>
|
2021-09-09 18:02:31 +02:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2026-02-23 11:50:46 +01:00
|
|
|
JS_API extern bool g_dump_ast;
|
|
|
|
|
JS_API extern bool g_dump_ast_use_color;
|
|
|
|
|
|
2026-03-17 15:47:58 -06:00
|
|
|
namespace FFI {
|
|
|
|
|
|
|
|
|
|
struct ParsedProgram;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 11:50:46 +01:00
|
|
|
namespace RustIntegration {
|
|
|
|
|
|
|
|
|
|
struct ScriptResult;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 18:02:31 +02:00
|
|
|
// 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records
|
2025-07-19 13:49:30 -07:00
|
|
|
class JS_API Script final : public Cell {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(Script, Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(Script);
|
2022-09-05 14:31:25 +02:00
|
|
|
|
2021-09-09 18:02:31 +02:00
|
|
|
public:
|
2022-02-07 18:25:39 +01:00
|
|
|
struct HostDefined {
|
|
|
|
|
virtual ~HostDefined() = default;
|
2022-09-05 14:32:33 +02:00
|
|
|
|
|
|
|
|
virtual void visit_host_defined_self(Cell::Visitor&) = 0;
|
2025-04-18 10:23:02 +02:00
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
|
|
|
|
|
virtual bool is_script() const { return false; }
|
|
|
|
|
virtual bool is_classic_script() const { return false; }
|
|
|
|
|
virtual bool is_module_script() const { return false; }
|
2022-02-07 18:25:39 +01:00
|
|
|
};
|
|
|
|
|
|
2022-09-05 14:31:25 +02:00
|
|
|
virtual ~Script() override;
|
2024-11-15 04:01:23 +13:00
|
|
|
static Result<GC::Ref<Script>, Vector<ParserError>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr, size_t line_number_offset = 1);
|
2026-03-17 15:47:58 -06:00
|
|
|
static Result<GC::Ref<Script>, Vector<ParserError>> create_from_parsed(FFI::ParsedProgram* parsed, NonnullRefPtr<SourceCode const> source_code, Realm&, HostDefined* = nullptr);
|
2021-09-09 18:02:31 +02:00
|
|
|
|
2022-09-05 14:31:25 +02:00
|
|
|
Realm& realm() { return *m_realm; }
|
2025-09-22 16:46:22 -07:00
|
|
|
Vector<LoadedModuleRequest>& loaded_modules() { return m_loaded_modules; }
|
|
|
|
|
Vector<LoadedModuleRequest> const& loaded_modules() const { return m_loaded_modules; }
|
2021-09-09 18:02:31 +02:00
|
|
|
|
2022-10-02 23:16:19 +02:00
|
|
|
HostDefined* host_defined() const { return m_host_defined; }
|
2025-08-30 08:18:47 +02:00
|
|
|
StringView filename() const LIFETIME_BOUND { return m_filename; }
|
2022-01-18 19:21:42 +01:00
|
|
|
|
2026-02-11 01:42:41 +01:00
|
|
|
Bytecode::Executable* cached_executable() const { return m_executable; }
|
|
|
|
|
|
2026-02-11 01:48:19 +01:00
|
|
|
ThrowCompletionOr<void> global_declaration_instantiation(VM&, GlobalEnvironment&);
|
|
|
|
|
|
2026-02-11 01:45:55 +01:00
|
|
|
// Pre-computed global declaration instantiation data.
|
|
|
|
|
// These are extracted from the AST at parse time so that GDI can run
|
|
|
|
|
// without needing to walk the AST.
|
|
|
|
|
struct FunctionToInitialize {
|
|
|
|
|
GC::Ref<SharedFunctionInstanceData> shared_data;
|
|
|
|
|
Utf16FlyString name;
|
|
|
|
|
};
|
|
|
|
|
struct LexicalBinding {
|
|
|
|
|
Utf16FlyString name;
|
|
|
|
|
bool is_constant { false };
|
|
|
|
|
};
|
2026-02-23 11:50:46 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Script(Realm&, StringView filename, RustIntegration::ScriptResult&&, HostDefined*);
|
|
|
|
|
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
|
|
|
|
GC::Ptr<Realm> m_realm; // [[Realm]]
|
|
|
|
|
Vector<LoadedModuleRequest> m_loaded_modules; // [[LoadedModules]]
|
|
|
|
|
|
|
|
|
|
mutable GC::Ptr<Bytecode::Executable> m_executable;
|
|
|
|
|
|
2026-02-11 01:45:55 +01:00
|
|
|
Vector<Utf16FlyString> m_lexical_names;
|
|
|
|
|
Vector<Utf16FlyString> m_var_names;
|
|
|
|
|
Vector<FunctionToInitialize> m_functions_to_initialize;
|
|
|
|
|
HashTable<Utf16FlyString> m_declared_function_names;
|
|
|
|
|
Vector<Utf16FlyString> m_var_scoped_names;
|
2026-02-23 11:50:46 +01:00
|
|
|
Vector<Utf16FlyString> m_annex_b_candidate_names;
|
2026-02-11 01:45:55 +01:00
|
|
|
Vector<LexicalBinding> m_lexical_bindings;
|
|
|
|
|
bool m_is_strict_mode { false };
|
|
|
|
|
|
2022-01-18 19:21:42 +01:00
|
|
|
// Needed for potential lookups of modules.
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_filename;
|
2022-02-07 18:25:39 +01:00
|
|
|
HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
|
2021-09-09 18:02:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|