LibJS: Port test262-runner to use Error and TRY a bit more

This commit is contained in:
Timothy Flynn 2024-12-12 12:46:14 -05:00 committed by Tim Flynn
commit 1b4c45b9eb
Notes: github-actions[bot] 2024-12-13 13:27:48 +00:00

View file

@ -6,9 +6,9 @@
*/ */
#include <AK/ByteString.h> #include <AK/ByteString.h>
#include <AK/Error.h>
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/Result.h>
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
@ -54,7 +54,7 @@ struct TestError {
using ScriptOrModuleProgram = Variant<GC::Ref<JS::Script>, GC::Ref<JS::SourceTextModule>>; using ScriptOrModuleProgram = Variant<GC::Ref<JS::Script>, GC::Ref<JS::SourceTextModule>>;
template<typename ScriptType> template<typename ScriptType>
static Result<ScriptOrModuleProgram, TestError> parse_program(JS::Realm& realm, StringView source, StringView filepath) static ErrorOr<ScriptOrModuleProgram, TestError> parse_program(JS::Realm& realm, StringView source, StringView filepath)
{ {
auto script_or_error = ScriptType::parse(source, realm, filepath); auto script_or_error = ScriptType::parse(source, realm, filepath);
if (script_or_error.is_error()) { if (script_or_error.is_error()) {
@ -68,7 +68,7 @@ static Result<ScriptOrModuleProgram, TestError> parse_program(JS::Realm& realm,
return ScriptOrModuleProgram { script_or_error.release_value() }; return ScriptOrModuleProgram { script_or_error.release_value() };
} }
static Result<ScriptOrModuleProgram, TestError> parse_program(JS::Realm& realm, StringView source, StringView filepath, JS::Program::Type program_type) static ErrorOr<ScriptOrModuleProgram, TestError> parse_program(JS::Realm& realm, StringView source, StringView filepath, JS::Program::Type program_type)
{ {
if (program_type == JS::Program::Type::Script) if (program_type == JS::Program::Type::Script)
return parse_program<JS::Script>(realm, source, filepath); return parse_program<JS::Script>(realm, source, filepath);
@ -76,7 +76,7 @@ static Result<ScriptOrModuleProgram, TestError> parse_program(JS::Realm& realm,
} }
template<typename InterpreterT> template<typename InterpreterT>
static Result<void, TestError> run_program(InterpreterT& interpreter, ScriptOrModuleProgram& program) static ErrorOr<void, TestError> run_program(InterpreterT& interpreter, ScriptOrModuleProgram& program)
{ {
auto result = program.visit( auto result = program.visit(
[&](auto& visitor) { [&](auto& visitor) {
@ -115,7 +115,7 @@ static Result<void, TestError> run_program(InterpreterT& interpreter, ScriptOrMo
static HashMap<ByteString, ByteString> s_cached_harness_files; static HashMap<ByteString, ByteString> s_cached_harness_files;
static Result<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); auto cache = s_cached_harness_files.find(harness_file);
if (cache == s_cached_harness_files.end()) { if (cache == s_cached_harness_files.end()) {
@ -147,7 +147,7 @@ static Result<StringView, TestError> read_harness_file(StringView harness_file)
return cache->value.view(); return cache->value.view();
} }
static Result<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)
{ {
auto source_or_error = read_harness_file(harness_file); auto source_or_error = read_harness_file(harness_file);
if (source_or_error.is_error()) if (source_or_error.is_error())
@ -193,7 +193,7 @@ struct TestMetadata {
StringView type; StringView type;
}; };
static Result<void, TestError> run_test(StringView source, StringView filepath, TestMetadata const& metadata) static ErrorOr<void, TestError> run_test(StringView source, StringView filepath, TestMetadata const& metadata)
{ {
if (s_parse_only || (metadata.is_negative && metadata.phase == NegativePhase::ParseOrEarly && metadata.program_type != JS::Program::Type::Module)) { if (s_parse_only || (metadata.is_negative && metadata.phase == NegativePhase::ParseOrEarly && metadata.program_type != JS::Program::Type::Module)) {
// Creating the vm and interpreter is heavy so we just parse directly here. // Creating the vm and interpreter is heavy so we just parse directly here.
@ -218,6 +218,7 @@ static Result<void, TestError> run_test(StringView source, StringView filepath,
GC::Ptr<JS::Realm> realm; GC::Ptr<JS::Realm> realm;
GC::Ptr<JS::Test262::GlobalObject> global_object; GC::Ptr<JS::Test262::GlobalObject> global_object;
auto root_execution_context = MUST(JS::Realm::initialize_host_defined_realm( auto root_execution_context = MUST(JS::Realm::initialize_host_defined_realm(
*vm, *vm,
[&](JS::Realm& realm_) -> JS::GlobalObject* { [&](JS::Realm& realm_) -> JS::GlobalObject* {
@ -227,17 +228,12 @@ static Result<void, TestError> run_test(StringView source, StringView filepath,
}, },
nullptr)); nullptr));
auto program_or_error = parse_program(*realm, source, filepath, metadata.program_type); auto program = TRY(parse_program(*realm, source, filepath, metadata.program_type));
if (program_or_error.is_error())
return program_or_error.release_error();
for (auto& harness_file : metadata.harness_files) { for (auto harness_file : metadata.harness_files) {
auto harness_program_or_error = parse_harness_files(*realm, harness_file); ScriptOrModuleProgram harness_program { TRY(parse_harness_files(*realm, harness_file)) };
if (harness_program_or_error.is_error())
return harness_program_or_error.release_error(); if (auto result = run_program(vm->bytecode_interpreter(), harness_program); result.is_error()) {
ScriptOrModuleProgram harness_program { harness_program_or_error.release_value() };
auto result = run_program(vm->bytecode_interpreter(), harness_program);
if (result.is_error()) {
return TestError { return TestError {
NegativePhase::Harness, NegativePhase::Harness,
result.error().type, result.error().type,
@ -247,10 +243,10 @@ static Result<void, TestError> run_test(StringView source, StringView filepath,
} }
} }
return run_program(vm->bytecode_interpreter(), program_or_error.value()); return run_program(vm->bytecode_interpreter(), program);
} }
static Result<TestMetadata, ByteString> extract_metadata(StringView source) static ErrorOr<TestMetadata, ByteString> extract_metadata(StringView source)
{ {
auto lines = source.lines(); auto lines = source.lines();
@ -397,7 +393,7 @@ static Result<TestMetadata, ByteString> extract_metadata(StringView source)
return failed_message; return failed_message;
} }
static bool verify_test(Result<void, TestError>& result, TestMetadata const& metadata, JsonObject& output) static bool verify_test(ErrorOr<void, TestError>& result, TestMetadata const& metadata, JsonObject& output)
{ {
if (result.is_error()) { if (result.is_error()) {
if (result.error().phase == NegativePhase::Harness) { if (result.error().phase == NegativePhase::Harness) {