mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-22 17:24:48 -05:00
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.
This commit is contained in:
parent
1b4c45b9eb
commit
40e7f46ac8
Notes:
github-actions[bot]
2024-12-13 13:27:43 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/40e7f46ac85 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2889
1 changed files with 25 additions and 27 deletions
|
@ -113,24 +113,27 @@ static ErrorOr<void, TestError> run_program(InterpreterT& interpreter, ScriptOrM
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
static HashMap<ByteString, ByteString> s_cached_harness_files;
|
|
||||||
|
|
||||||
static ErrorOr<StringView, TestError> read_harness_file(StringView harness_file)
|
static ErrorOr<StringView, TestError> read_harness_file(StringView harness_file)
|
||||||
{
|
{
|
||||||
auto cache = s_cached_harness_files.find(harness_file);
|
static HashMap<ByteString, ByteBuffer> s_cached_harness_files;
|
||||||
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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto contents_or_error = file_or_error.value()->read_until_eof();
|
auto cache = [&]() -> ErrorOr<StringView> {
|
||||||
if (contents_or_error.is_error()) {
|
if (auto it = s_cached_harness_files.find(harness_file); it != s_cached_harness_files.end())
|
||||||
|
return StringView { it->value };
|
||||||
|
|
||||||
|
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 {
|
return TestError {
|
||||||
NegativePhase::Harness,
|
NegativePhase::Harness,
|
||||||
"filesystem",
|
"filesystem",
|
||||||
|
@ -139,12 +142,7 @@ static ErrorOr<StringView, TestError> read_harness_file(StringView harness_file)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView contents_view = contents_or_error.value();
|
return cache.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());
|
|
||||||
}
|
|
||||||
return cache->value.view();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static ErrorOr<GC::Ref<JS::Script>, TestError> parse_harness_files(JS::Realm& realm, StringView harness_file)
|
static ErrorOr<GC::Ref<JS::Script>, TestError> parse_harness_files(JS::Realm& realm, StringView harness_file)
|
||||||
|
|
Loading…
Reference in a new issue