2021-09-14 20:51:16 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2021-2023, Andreas Kling <andreas@ladybird.org>
|
2022-01-18 19:29:17 +01:00
|
|
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
2021-09-14 20:51:16 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-01-18 19:29:17 +01:00
|
|
|
#include <LibJS/CyclicModule.h>
|
2025-07-19 13:49:30 -07:00
|
|
|
#include <LibJS/Export.h>
|
2021-09-14 20:51:16 +02:00
|
|
|
#include <LibJS/Forward.h>
|
2026-03-19 13:37:46 -05:00
|
|
|
#include <LibJS/ModuleEntry.h>
|
2022-11-23 11:41:19 +01:00
|
|
|
#include <LibJS/Runtime/ExecutionContext.h>
|
2021-09-14 20:51:16 +02:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2026-03-17 15:47:58 -06:00
|
|
|
namespace FFI {
|
|
|
|
|
|
|
|
|
|
struct ParsedProgram;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 20:51:16 +02:00
|
|
|
// 16.2.1.6 Source Text Module Records, https://tc39.es/ecma262/#sec-source-text-module-records
|
2025-07-19 13:49:30 -07:00
|
|
|
class JS_API SourceTextModule final : public CyclicModule {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(SourceTextModule, CyclicModule);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(SourceTextModule);
|
2022-09-05 14:31:25 +02:00
|
|
|
|
2021-09-14 20:51:16 +02:00
|
|
|
public:
|
2024-06-16 10:38:35 +02:00
|
|
|
virtual ~SourceTextModule() override;
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
static Result<GC::Ref<SourceTextModule>, Vector<ParserError>> parse(StringView source_text, Realm&, StringView filename = {}, Script::HostDefined* host_defined = nullptr);
|
2026-03-17 15:47:58 -06:00
|
|
|
static Result<GC::Ref<SourceTextModule>, Vector<ParserError>> parse_from_pre_parsed(FFI::ParsedProgram* parsed, NonnullRefPtr<SourceCode const> source_code, Realm&, Script::HostDefined* host_defined = nullptr);
|
2021-09-14 20:51:16 +02:00
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
virtual Vector<Utf16FlyString> get_exported_names(VM& vm, HashTable<Module const*>& export_star_set) override;
|
|
|
|
|
virtual ResolvedBinding resolve_export(VM& vm, Utf16FlyString const& export_name, Vector<ResolvedBinding> resolve_set = {}) override;
|
2022-01-18 19:29:17 +01:00
|
|
|
|
2022-09-05 14:31:25 +02:00
|
|
|
Object* import_meta() { return m_import_meta; }
|
2023-07-11 23:07:12 -04:00
|
|
|
void set_import_meta(Badge<VM>, Object* import_meta) { m_import_meta = import_meta; }
|
2022-01-18 19:29:17 +01:00
|
|
|
|
2026-02-23 11:50:46 +01:00
|
|
|
// Pre-computed module declaration instantiation data.
|
|
|
|
|
// These are extracted from the AST at construction time so that
|
|
|
|
|
// initialize_environment() can run without walking the AST.
|
|
|
|
|
struct FunctionToInitialize {
|
|
|
|
|
GC::Ref<SharedFunctionInstanceData> shared_data;
|
|
|
|
|
Utf16FlyString name;
|
|
|
|
|
};
|
|
|
|
|
struct LexicalBinding {
|
|
|
|
|
Utf16FlyString name;
|
|
|
|
|
bool is_constant { false };
|
|
|
|
|
i32 function_index { -1 }; // index into m_functions_to_initialize, -1 if not a function
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-18 19:29:17 +01:00
|
|
|
protected:
|
2022-05-02 20:54:39 +02:00
|
|
|
virtual ThrowCompletionOr<void> initialize_environment(VM& vm) override;
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual ThrowCompletionOr<void> execute_module(VM& vm, GC::Ptr<PromiseCapability> capability) override;
|
2022-01-18 19:29:17 +01:00
|
|
|
|
|
|
|
|
private:
|
2026-02-23 11:50:46 +01:00
|
|
|
SourceTextModule(Realm&, StringView filename, Script::HostDefined* host_defined, bool has_top_level_await, Vector<ModuleRequest> requested_modules, Vector<ImportEntry> import_entries, Vector<ExportEntry> local_export_entries, Vector<ExportEntry> indirect_export_entries, Vector<ExportEntry> star_export_entries, Optional<Utf16FlyString> default_export_binding_name, Vector<Utf16FlyString> var_declared_names, Vector<LexicalBinding> lexical_bindings, Vector<FunctionToInitialize> functions_to_initialize, GC::Ptr<Bytecode::Executable> executable, GC::Ptr<SharedFunctionInstanceData> tla_shared_data);
|
|
|
|
|
|
2022-09-05 14:31:25 +02:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
2023-11-27 16:45:45 +01:00
|
|
|
NonnullOwnPtr<ExecutionContext> m_execution_context; // [[Context]]
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Object> m_import_meta; // [[ImportMeta]]
|
2023-11-27 16:45:45 +01:00
|
|
|
Vector<ImportEntry> m_import_entries; // [[ImportEntries]]
|
|
|
|
|
Vector<ExportEntry> m_local_export_entries; // [[LocalExportEntries]]
|
|
|
|
|
Vector<ExportEntry> m_indirect_export_entries; // [[IndirectExportEntries]]
|
|
|
|
|
Vector<ExportEntry> m_star_export_entries; // [[StarExportEntries]]
|
2022-01-18 19:29:17 +01:00
|
|
|
|
2026-02-11 09:48:00 +01:00
|
|
|
Vector<Utf16FlyString> m_var_declared_names;
|
|
|
|
|
Vector<LexicalBinding> m_lexical_bindings;
|
|
|
|
|
Vector<FunctionToInitialize> m_functions_to_initialize;
|
|
|
|
|
Optional<Utf16FlyString> m_default_export_binding_name;
|
2026-02-11 09:49:52 +01:00
|
|
|
|
|
|
|
|
GC::Ptr<Bytecode::Executable> m_executable;
|
|
|
|
|
GC::Ptr<SharedFunctionInstanceData> m_tla_shared_data;
|
2021-09-14 20:51:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|