From c922d21ecb64bf487b3331734e4e679bfe3fcd0b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 24 Nov 2022 18:01:52 +0100 Subject: [PATCH] LibJS: Unbreak test262-parser-tests after source range caching changes Turns out we have to be a little more lenient when dealing with the slightly hostile inputs in tests262-parser-tests. This fix papers over some problematic situations by returning a fallback range if we can't figure out exactly where an error occurred. I've added a FIXME about returning nicer values. --- Userland/Libraries/LibJS/SourceCode.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/SourceCode.cpp b/Userland/Libraries/LibJS/SourceCode.cpp index f0700a1c2d4..fbf9a56ac98 100644 --- a/Userland/Libraries/LibJS/SourceCode.cpp +++ b/Userland/Libraries/LibJS/SourceCode.cpp @@ -54,8 +54,9 @@ void SourceCode::compute_line_break_offsets() const SourceRange SourceCode::range_from_offsets(u32 start_offset, u32 end_offset) const { + // If the underlying code is an empty string, the range is 1,1 - 1,1 no matter what. if (m_code.is_empty()) - return { *this, {}, {} }; + return { *this, { .line = 1, .column = 1, .offset = 0 }, { .line = 1, .column = 1, .offset = 0 } }; if (!m_line_break_offsets.has_value()) compute_line_break_offsets(); @@ -70,8 +71,8 @@ SourceRange SourceCode::range_from_offsets(u32 start_offset, u32 end_offset) con nearest_preceding_line_break_offset = (*m_line_break_offsets)[nearest_line_break_index]; } - Position start; - Position end; + Optional start; + Optional end; size_t column = 1; @@ -80,7 +81,8 @@ SourceRange SourceCode::range_from_offsets(u32 start_offset, u32 end_offset) con Utf8View view(m_code.view()); for (auto it = view.iterator_at_byte_offset_without_validation(nearest_preceding_line_break_offset); it != view.end(); ++it) { - if (start_offset == view.byte_offset_of(it)) { + // If we're on or after the start offset, this is the start position. + if (!start.has_value() && view.byte_offset_of(it) >= start_offset) { start = Position { .line = line, .column = column, @@ -88,7 +90,8 @@ SourceRange SourceCode::range_from_offsets(u32 start_offset, u32 end_offset) con }; } - if (end_offset == view.byte_offset_of(it)) { + // If we're on or after the end offset, this is the end position. + if (!end.has_value() && view.byte_offset_of(it) >= end_offset) { end = Position { .line = line, .column = column, @@ -110,7 +113,12 @@ SourceRange SourceCode::range_from_offsets(u32 start_offset, u32 end_offset) con ++column; } - return SourceRange { *this, start, end }; + // If we didn't find both a start and end position, just return 1,1-1,1. + // FIXME: This is a hack. Find a way to return the nicest possible values here. + if (!start.has_value() || !end.has_value()) + return SourceRange { *this, { .line = 1, .column = 1 }, { .line = 1, .column = 1 } }; + + return SourceRange { *this, *start, *end }; } }