diff options
author | networkException <git@nwex.de> | 2022-10-02 21:18:33 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-10-06 16:41:36 +0200 |
commit | fb6de442c6f407aabcb0afaeea758a3d35062e3e (patch) | |
tree | fb8c35a4e650d660ac0cc9784a4f6f9e1efc786a /Userland | |
parent | cfa0c9bf9f9477a9ac6102773d96209caa0c3832 (diff) | |
download | serenity-fb6de442c6f407aabcb0afaeea758a3d35062e3e.zip |
LibJS: Add [[HostDefined]] field to Modules
This patch adds the [[HostDefined]] field defined in
https://tc39.es/ecma262/#table-module-record-fields to module records.
Co-authored-by: davidot <davidot@serenityos.org>
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/CyclicModule.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/CyclicModule.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Module.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Module.h | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/SourceTextModule.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/SourceTextModule.h | 4 |
6 files changed, 21 insertions, 13 deletions
diff --git a/Userland/Libraries/LibJS/CyclicModule.cpp b/Userland/Libraries/LibJS/CyclicModule.cpp index 658889ab90..41be2749f7 100644 --- a/Userland/Libraries/LibJS/CyclicModule.cpp +++ b/Userland/Libraries/LibJS/CyclicModule.cpp @@ -11,8 +11,8 @@ namespace JS { -CyclicModule::CyclicModule(Realm& realm, StringView filename, bool has_top_level_await, Vector<ModuleRequest> requested_modules) - : Module(realm, filename) +CyclicModule::CyclicModule(Realm& realm, StringView filename, bool has_top_level_await, Vector<ModuleRequest> requested_modules, Script::HostDefined* host_defined) + : Module(realm, filename, host_defined) , m_requested_modules(move(requested_modules)) , m_has_top_level_await(has_top_level_await) { diff --git a/Userland/Libraries/LibJS/CyclicModule.h b/Userland/Libraries/LibJS/CyclicModule.h index b610a678fc..d267dc2b73 100644 --- a/Userland/Libraries/LibJS/CyclicModule.h +++ b/Userland/Libraries/LibJS/CyclicModule.h @@ -34,7 +34,7 @@ public: virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) override; protected: - CyclicModule(Realm& realm, StringView filename, bool has_top_level_await, Vector<ModuleRequest> requested_modules); + CyclicModule(Realm& realm, StringView filename, bool has_top_level_await, Vector<ModuleRequest> requested_modules, Script::HostDefined* host_defined); virtual void visit_edges(Cell::Visitor&) override; diff --git a/Userland/Libraries/LibJS/Module.cpp b/Userland/Libraries/LibJS/Module.cpp index abf506a905..0baabada4e 100644 --- a/Userland/Libraries/LibJS/Module.cpp +++ b/Userland/Libraries/LibJS/Module.cpp @@ -11,8 +11,9 @@ namespace JS { -Module::Module(Realm& realm, String filename) +Module::Module(Realm& realm, String filename, Script::HostDefined* host_defined) : m_realm(realm) + , m_host_defined(host_defined) , m_filename(move(filename)) { } @@ -25,6 +26,8 @@ void Module::visit_edges(Cell::Visitor& visitor) visitor.visit(m_realm); visitor.visit(m_environment); visitor.visit(m_namespace); + if (m_host_defined) + m_host_defined->visit_host_defined_self(visitor); } // 16.2.1.5.1.1 InnerModuleLinking ( module, stack, index ), https://tc39.es/ecma262/#sec-InnerModuleLinking diff --git a/Userland/Libraries/LibJS/Module.h b/Userland/Libraries/LibJS/Module.h index bb2400cda1..b7adcc6697 100644 --- a/Userland/Libraries/LibJS/Module.h +++ b/Userland/Libraries/LibJS/Module.h @@ -11,6 +11,7 @@ #include <LibJS/Heap/GCPtr.h> #include <LibJS/Runtime/Environment.h> #include <LibJS/Runtime/Realm.h> +#include <LibJS/Script.h> namespace JS { @@ -68,6 +69,8 @@ public: Environment* environment() { return m_environment; } + Script::HostDefined* host_defined() const { return m_host_defined; } + ThrowCompletionOr<Object*> get_module_namespace(VM& vm); virtual ThrowCompletionOr<void> link(VM& vm) = 0; @@ -80,7 +83,7 @@ public: virtual ThrowCompletionOr<u32> inner_module_evaluation(VM& vm, Vector<Module*>& stack, u32 index); protected: - Module(Realm&, String filename); + Module(Realm&, String filename, Script::HostDefined* host_defined = nullptr); virtual void visit_edges(Cell::Visitor&) override; @@ -97,9 +100,10 @@ private: // destroy the VM but keep the modules this should not happen. Because VM // stores modules with a RefPtr we cannot just store the VM as that leads to // cycles. - GCPtr<Realm> m_realm; // [[Realm]] - GCPtr<Environment> m_environment; // [[Environment]] - GCPtr<Object> m_namespace; // [[Namespace]] + GCPtr<Realm> m_realm; // [[Realm]] + GCPtr<Environment> m_environment; // [[Environment]] + GCPtr<Object> m_namespace; // [[Namespace]] + Script::HostDefined* m_host_defined { nullptr }; // [[HostDefined]] // Needed for potential lookups of modules. String m_filename; diff --git a/Userland/Libraries/LibJS/SourceTextModule.cpp b/Userland/Libraries/LibJS/SourceTextModule.cpp index 3f2f1e9359..621c9d48b4 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.cpp +++ b/Userland/Libraries/LibJS/SourceTextModule.cpp @@ -98,11 +98,11 @@ static Vector<ModuleRequest> module_requests(Program& program, Vector<String> co return requested_modules_in_source_order; } -SourceTextModule::SourceTextModule(Realm& realm, StringView filename, bool has_top_level_await, NonnullRefPtr<Program> body, Vector<ModuleRequest> requested_modules, +SourceTextModule::SourceTextModule(Realm& realm, StringView filename, Script::HostDefined* host_defined, bool has_top_level_await, NonnullRefPtr<Program> body, Vector<ModuleRequest> requested_modules, Vector<ImportEntry> import_entries, Vector<ExportEntry> local_export_entries, Vector<ExportEntry> indirect_export_entries, Vector<ExportEntry> star_export_entries, RefPtr<ExportStatement> default_export) - : CyclicModule(realm, filename, has_top_level_await, move(requested_modules)) + : CyclicModule(realm, filename, has_top_level_await, move(requested_modules), host_defined) , m_ecmascript_code(move(body)) , m_execution_context(realm.heap()) , m_import_entries(move(import_entries)) @@ -120,7 +120,7 @@ void SourceTextModule::visit_edges(Cell::Visitor& visitor) } // 16.2.1.6.1 ParseModule ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parsemodule -Result<NonnullGCPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule::parse(StringView source_text, Realm& realm, StringView filename) +Result<NonnullGCPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule::parse(StringView source_text, Realm& realm, StringView filename, Script::HostDefined* host_defined) { // 1. Let body be ParseText(sourceText, Module). auto parser = Parser(Lexer(source_text, filename), Program::Type::Module); @@ -248,6 +248,7 @@ Result<NonnullGCPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule:: return NonnullGCPtr(*realm.heap().allocate_without_realm<SourceTextModule>( realm, filename, + host_defined, async, move(body), move(requested_modules), diff --git a/Userland/Libraries/LibJS/SourceTextModule.h b/Userland/Libraries/LibJS/SourceTextModule.h index 2979b06d4a..756a9e81db 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.h +++ b/Userland/Libraries/LibJS/SourceTextModule.h @@ -22,7 +22,7 @@ public: using ImportEntry = ImportStatement::ImportEntry; using ExportEntry = ExportStatement::ExportEntry; - static Result<NonnullGCPtr<SourceTextModule>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {}); + static Result<NonnullGCPtr<SourceTextModule>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {}, Script::HostDefined* host_defined = nullptr); Program const& parse_node() const { return *m_ecmascript_code; } @@ -37,7 +37,7 @@ protected: virtual ThrowCompletionOr<void> execute_module(VM& vm, GCPtr<PromiseCapability> capability) override; private: - SourceTextModule(Realm&, StringView filename, bool has_top_level_await, NonnullRefPtr<Program> body, Vector<ModuleRequest> requested_modules, + SourceTextModule(Realm&, StringView filename, Script::HostDefined* host_defined, bool has_top_level_await, NonnullRefPtr<Program> body, Vector<ModuleRequest> requested_modules, Vector<ImportEntry> import_entries, Vector<ExportEntry> local_export_entries, Vector<ExportEntry> indirect_export_entries, Vector<ExportEntry> star_export_entries, RefPtr<ExportStatement> default_export); |