From 40e7f46ac8526ed5ad701bff36f2c5caaff7f75f Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 12 Dec 2024 12:55:33 -0500 Subject: [PATCH] LibJS: Simplify reading test262 harness files Make use of TRY semantics a bit more. And we don't need to store harness files as a ByteString - we can store the contents as the ByteBuffer that we receive from reading the file. --- Tests/LibJS/test262-runner.cpp | 52 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/Tests/LibJS/test262-runner.cpp b/Tests/LibJS/test262-runner.cpp index 0894263641a..7af0445b234 100644 --- a/Tests/LibJS/test262-runner.cpp +++ b/Tests/LibJS/test262-runner.cpp @@ -113,38 +113,36 @@ static ErrorOr run_program(InterpreterT& interpreter, ScriptOrM return {}; } -static HashMap s_cached_harness_files; - static ErrorOr read_harness_file(StringView harness_file) { - auto cache = s_cached_harness_files.find(harness_file); - if (cache == s_cached_harness_files.end()) { - auto file_or_error = Core::File::open(ByteString::formatted("{}{}", s_harness_file_directory, harness_file), Core::File::OpenMode::Read); - if (file_or_error.is_error()) { - return TestError { - NegativePhase::Harness, - "filesystem", - ByteString::formatted("Could not open file: {}", harness_file), - harness_file - }; - } + static HashMap s_cached_harness_files; - auto contents_or_error = file_or_error.value()->read_until_eof(); - if (contents_or_error.is_error()) { - return TestError { - NegativePhase::Harness, - "filesystem", - ByteString::formatted("Could not read file: {}", harness_file), - harness_file - }; - } + auto cache = [&]() -> ErrorOr { + if (auto it = s_cached_harness_files.find(harness_file); it != s_cached_harness_files.end()) + return StringView { it->value }; - StringView contents_view = contents_or_error.value(); - s_cached_harness_files.set(harness_file, contents_view.to_byte_string()); - cache = s_cached_harness_files.find(harness_file); - VERIFY(cache != s_cached_harness_files.end()); + auto path = ByteString::formatted("{}{}", s_harness_file_directory, harness_file); + auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read)); + + auto contents = TRY(file->read_until_eof()); + s_cached_harness_files.set(harness_file, move(contents)); + + auto it = s_cached_harness_files.find(harness_file); + VERIFY(it != s_cached_harness_files.end()); + + return StringView { it->value }; + }(); + + if (cache.is_error()) { + return TestError { + NegativePhase::Harness, + "filesystem", + ByteString::formatted("Could not read file: {}", harness_file), + harness_file + }; } - return cache->value.view(); + + return cache.value(); } static ErrorOr, TestError> parse_harness_files(JS::Realm& realm, StringView harness_file)