diff options
author | davidot <davidot@serenityos.org> | 2022-01-18 19:21:42 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-22 01:21:18 +0000 |
commit | 12c2f30c548cbd170fdcfe6e04a5c074bdc6fa46 (patch) | |
tree | 34a90fe646f05bdd4dbfadba1140fa78635ef929 | |
parent | 57c5a59cab1742f8dd9c66895b164665fd6d79de (diff) | |
download | serenity-12c2f30c548cbd170fdcfe6e04a5c074bdc6fa46.zip |
LibJS: Add filename tracking to Script and Module
This will allow us to resolve modules dynamically loaded from a script.
-rw-r--r-- | Userland/Libraries/LibJS/Module.cpp | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Module.h | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Script.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Script.h | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/SourceTextModule.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/SourceTextModule.h | 2 |
6 files changed, 22 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Module.cpp b/Userland/Libraries/LibJS/Module.cpp index e99a54e18c..38cf4ed746 100644 --- a/Userland/Libraries/LibJS/Module.cpp +++ b/Userland/Libraries/LibJS/Module.cpp @@ -8,9 +8,10 @@ namespace JS { -Module::Module(Realm& realm) +Module::Module(Realm& realm, StringView filename) : m_vm(realm.vm()) , m_realm(make_handle(&realm)) + , m_filename(filename) { } diff --git a/Userland/Libraries/LibJS/Module.h b/Userland/Libraries/LibJS/Module.h index 8eb99d985b..ebb12ba3df 100644 --- a/Userland/Libraries/LibJS/Module.h +++ b/Userland/Libraries/LibJS/Module.h @@ -19,11 +19,13 @@ public: Realm& realm() { return *m_realm.cell(); } Realm const& realm() const { return *m_realm.cell(); } + StringView filename() const { return m_filename; } + Environment* environment() { return m_environment.cell(); } Object* namespace_() { return m_namespace.cell(); } protected: - explicit Module(Realm&); + explicit Module(Realm&, StringView filename); private: // Handles are not safe unless we keep the VM alive. @@ -32,6 +34,9 @@ private: Handle<Realm> m_realm; // [[Realm]] Handle<Environment> m_environment; // [[Environment]] Handle<Object> m_namespace; // [[Namespace]] + + // Needed for potential lookups of modules. + String m_filename; }; } diff --git a/Userland/Libraries/LibJS/Script.cpp b/Userland/Libraries/LibJS/Script.cpp index 158bff42cd..11423225c5 100644 --- a/Userland/Libraries/LibJS/Script.cpp +++ b/Userland/Libraries/LibJS/Script.cpp @@ -23,13 +23,14 @@ Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView so return parser.errors(); // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }. - return adopt_ref(*new Script(realm, move(body))); + return adopt_ref(*new Script(realm, filename, move(body))); } -Script::Script(Realm& realm, NonnullRefPtr<Program> parse_node) +Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node) : m_vm(realm.vm()) , m_realm(make_handle(&realm)) , m_parse_node(move(parse_node)) + , m_filename(filename) { } diff --git a/Userland/Libraries/LibJS/Script.h b/Userland/Libraries/LibJS/Script.h index c58ddf956f..782627e99f 100644 --- a/Userland/Libraries/LibJS/Script.h +++ b/Userland/Libraries/LibJS/Script.h @@ -24,14 +24,19 @@ public: Realm& realm() { return *m_realm.cell(); } Program const& parse_node() const { return *m_parse_node; } + StringView filename() const { return m_filename; } + private: - Script(Realm&, NonnullRefPtr<Program>); + Script(Realm&, StringView filename, NonnullRefPtr<Program>); // Handles are not safe unless we keep the VM alive. NonnullRefPtr<VM> m_vm; Handle<Realm> m_realm; // [[Realm]] NonnullRefPtr<Program> m_parse_node; // [[ECMAScriptCode]] + + // Needed for potential lookups of modules. + String m_filename; }; } diff --git a/Userland/Libraries/LibJS/SourceTextModule.cpp b/Userland/Libraries/LibJS/SourceTextModule.cpp index 1e6235f132..abc0698f5c 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.cpp +++ b/Userland/Libraries/LibJS/SourceTextModule.cpp @@ -9,7 +9,7 @@ namespace JS { // 16.2.1.6.1 ParseModule ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parsemodule -Result<NonnullRefPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule::parse(StringView source_text, Realm& realm, [[maybe_unused]] StringView filename) +Result<NonnullRefPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule::parse(StringView source_text, Realm& realm, StringView filename) { // 1. Let body be ParseText(sourceText, Module). auto parser = Parser(Lexer(source_text, filename), Program::Type::Module); @@ -20,11 +20,11 @@ Result<NonnullRefPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule: return parser.errors(); // FIXME: Implement the rest of ParseModule. - return adopt_ref(*new SourceTextModule(realm, move(body))); + return adopt_ref(*new SourceTextModule(realm, filename, move(body))); } -SourceTextModule::SourceTextModule(Realm& realm, NonnullRefPtr<Program> program) - : Module(realm) +SourceTextModule::SourceTextModule(Realm& realm, StringView filename, NonnullRefPtr<Program> program) + : Module(realm, filename) , m_ecmascript_code(move(program)) { } diff --git a/Userland/Libraries/LibJS/SourceTextModule.h b/Userland/Libraries/LibJS/SourceTextModule.h index c318e721f5..4ddf1f0da8 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.h +++ b/Userland/Libraries/LibJS/SourceTextModule.h @@ -22,7 +22,7 @@ public: Program const& parse_node() const { return *m_ecmascript_code; } private: - explicit SourceTextModule(Realm&, NonnullRefPtr<Program>); + explicit SourceTextModule(Realm&, StringView filename, NonnullRefPtr<Program>); NonnullRefPtr<Program> m_ecmascript_code; // [[ECMAScriptCode]] }; |