From 0c4befd811dbf426ce98f7781ef1fe68cc06cd59 Mon Sep 17 00:00:00 2001 From: davidot Date: Sun, 27 Nov 2022 01:15:37 +0100 Subject: [PATCH] LibJS: Use the source offset to sort imports in module Using source offsets directly means we don't have to synthesize any SourceRanges and circumvents that SourceRange returns invalid values for the last range of a file. --- Userland/Libraries/LibJS/AST.h | 1 + Userland/Libraries/LibJS/SourceTextModule.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index f01f6c1917..d08b78dbae 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -54,6 +54,7 @@ public: virtual void dump(int indent) const; [[nodiscard]] SourceRange source_range() const; + u32 start_offset() const { return m_start_offset; } void set_end_offset(Badge, u32 end_offset) { m_end_offset = end_offset; } diff --git a/Userland/Libraries/LibJS/SourceTextModule.cpp b/Userland/Libraries/LibJS/SourceTextModule.cpp index 5004c4b126..386e1f53f0 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.cpp +++ b/Userland/Libraries/LibJS/SourceTextModule.cpp @@ -47,27 +47,26 @@ static Vector module_requests(Program& program, Vector co // A List of all the ModuleSpecifier strings used by the module represented by this record to request the importation of a module. // Note: The List is source text occurrence ordered! struct RequestedModuleAndSourceIndex { - u64 source_index { 0 }; + u32 source_offset { 0 }; ModuleRequest* module_request { nullptr }; }; Vector requested_modules_with_indices; - for (auto& import_statement : program.imports()) { - requested_modules_with_indices.empend(import_statement.source_range().start.offset, &import_statement.module_request()); - } + for (auto& import_statement : program.imports()) + requested_modules_with_indices.empend(import_statement.start_offset(), &import_statement.module_request()); for (auto& export_statement : program.exports()) { for (auto& export_entry : export_statement.entries()) { if (!export_entry.is_module_request()) continue; - requested_modules_with_indices.empend(export_statement.source_range().start.offset, &export_statement.module_request()); + requested_modules_with_indices.empend(export_statement.start_offset(), &export_statement.module_request()); } } // Note: The List is source code occurrence ordered. https://tc39.es/proposal-import-assertions/#table-cyclic-module-fields quick_sort(requested_modules_with_indices, [&](RequestedModuleAndSourceIndex const& lhs, RequestedModuleAndSourceIndex const& rhs) { - return lhs.source_index < rhs.source_index; + return lhs.source_offset < rhs.source_offset; }); Vector requested_modules_in_source_order;