diff --git a/Tests/LibJS/test-js.cpp b/Tests/LibJS/test-js.cpp index e83c1a548d0..5da868d4b14 100644 --- a/Tests/LibJS/test-js.cpp +++ b/Tests/LibJS/test-js.cpp @@ -18,7 +18,7 @@ TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0) TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource) { - auto source = TRY_OR_DISCARD(vm.argument(0).to_string(global_object)); + auto source = TRY(vm.argument(0).to_string(global_object)); auto parser = JS::Parser(JS::Lexer(source)); parser.parse_program(); return JS::Value(!parser.has_errors()); @@ -32,22 +32,18 @@ TESTJS_GLOBAL_FUNCTION(run_queued_promise_jobs, runQueuedPromiseJobs) TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize) { - auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object)); - if (!is(object)) { - vm.throw_exception(global_object, JS::ErrorType::NotAnObjectOfType, "WeakSet"); - return {}; - } + auto* object = TRY(vm.argument(0).to_object(global_object)); + if (!is(object)) + return vm.throw_completion(global_object, JS::ErrorType::NotAnObjectOfType, "WeakSet"); auto* weak_set = static_cast(object); return JS::Value(weak_set->values().size()); } TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize) { - auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object)); - if (!is(object)) { - vm.throw_exception(global_object, JS::ErrorType::NotAnObjectOfType, "WeakMap"); - return {}; - } + auto* object = TRY(vm.argument(0).to_object(global_object)); + if (!is(object)) + return vm.throw_completion(global_object, JS::ErrorType::NotAnObjectOfType, "WeakMap"); auto* weak_map = static_cast(object); return JS::Value(weak_map->values().size()); } @@ -55,10 +51,8 @@ TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize) TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage) { auto argument = vm.argument(0); - if (!argument.is_string()) { - vm.throw_exception(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects()); - return {}; - } + if (!argument.is_string()) + return vm.throw_completion(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects()); auto& variable_name = argument.as_string(); @@ -66,21 +60,17 @@ TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage) auto outer_environment = vm.execution_context_stack().last_matching([&](auto& execution_context) { return execution_context->lexical_environment != nullptr; }); - if (!outer_environment.has_value()) { - vm.throw_exception(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string()); - return {}; - } + if (!outer_environment.has_value()) + return vm.throw_completion(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string()); auto reference = vm.resolve_binding(variable_name.string(), outer_environment.value()->lexical_environment); auto value = reference.get_value(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return JS::throw_completion(exception->value()); - if (!value.is_object()) { - vm.throw_exception(global_object, JS::ErrorType::NotAnObject, String::formatted("Variable with name {}", variable_name.string())); - return {}; - } + if (!value.is_object()) + return vm.throw_completion(global_object, JS::ErrorType::NotAnObject, String::formatted("Variable with name {}", variable_name.string())); vm.heap().uproot_cell(&value.as_object()); reference.delete_(global_object); diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 067ee0258fe..e9243f8f4d8 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -13,16 +13,14 @@ TEST_ROOT("Userland/Libraries/LibWasm/Tests"); TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile) { - auto filename = TRY_OR_DISCARD(vm.argument(0).to_string(global_object)); + auto filename = TRY(vm.argument(0).to_string(global_object)); auto file = Core::File::open(filename, Core::OpenMode::ReadOnly); - if (file.is_error()) { - vm.throw_exception(global_object, file.error().string()); - return {}; - } + if (file.is_error()) + return vm.throw_completion(global_object, file.error().string()); auto contents = file.value()->read_all(); auto array = JS::Uint8Array::create(global_object, contents.size()); contents.span().copy_to(array->data()); - return array; + return JS::Value(array); } class WebAssemblyModule final : public JS::Object { @@ -93,11 +91,9 @@ HashMap WebAssemblyModule::s_spec_test_na TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) { - auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object)); - if (!is(object)) { - vm.throw_exception(global_object, "Expected a Uint8Array argument to parse_webassembly_module"); - return {}; - } + auto* object = TRY(vm.argument(0).to_object(global_object)); + if (!is(object)) + return vm.throw_completion(global_object, "Expected a Uint8Array argument to parse_webassembly_module"); auto& array = static_cast(*object); InputMemoryStream stream { array.data() }; ScopeGuard handle_stream_error { @@ -106,15 +102,11 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) } }; auto result = Wasm::Module::parse(stream); - if (result.is_error()) { - vm.throw_exception(global_object, Wasm::parse_error_to_string(result.error())); - return {}; - } + if (result.is_error()) + return vm.throw_completion(global_object, Wasm::parse_error_to_string(result.error())); - if (stream.handle_any_error()) { - vm.throw_exception(global_object, "Binary stream contained errors"); - return {}; - } + if (stream.handle_any_error()) + return vm.throw_completion(global_object, "Binary stream contained errors"); HashMap imports; auto import_value = vm.argument(1); @@ -132,22 +124,18 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) } } - return WebAssemblyModule::create(global_object, result.release_value(), imports); + return JS::Value(WebAssemblyModule::create(global_object, result.release_value(), imports)); } TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays) { - auto* lhs = TRY_OR_DISCARD(vm.argument(0).to_object(global_object)); - if (!is(lhs)) { - vm.throw_exception(global_object, "Expected a TypedArray"); - return {}; - } + auto* lhs = TRY(vm.argument(0).to_object(global_object)); + if (!is(lhs)) + return vm.throw_completion(global_object, "Expected a TypedArray"); auto& lhs_array = static_cast(*lhs); - auto* rhs = TRY_OR_DISCARD(vm.argument(1).to_object(global_object)); - if (!is(rhs)) { - vm.throw_exception(global_object, "Expected a TypedArray"); - return {}; - } + auto* rhs = TRY(vm.argument(1).to_object(global_object)); + if (!is(rhs)) + return vm.throw_completion(global_object, "Expected a TypedArray"); auto& rhs_array = static_cast(*rhs); return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer()); } diff --git a/Tests/LibWeb/test-web.cpp b/Tests/LibWeb/test-web.cpp index 893adab2626..0bd22a29edc 100644 --- a/Tests/LibWeb/test-web.cpp +++ b/Tests/LibWeb/test-web.cpp @@ -40,7 +40,7 @@ TESTJS_MAIN_HOOK() TESTJS_GLOBAL_FUNCTION(load_local_page, loadLocalPage) { - auto name = TRY_OR_DISCARD(vm.argument(0).to_string(global_object)); + auto name = TRY(vm.argument(0).to_string(global_object)); // Clear the hooks before_initial_load_hooks.clear(); @@ -59,8 +59,7 @@ TESTJS_GLOBAL_FUNCTION(after_initial_page_load, afterInitialPageLoad) auto function = vm.argument(0); if (!function.is_function()) { dbgln("afterInitialPageLoad argument is not a function"); - vm.throw_exception(global_object, JS::ErrorType::NotAnObjectOfType, "Function"); - return {}; + return vm.throw_completion(global_object, JS::ErrorType::NotAnObjectOfType, "Function"); } after_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) { @@ -74,8 +73,7 @@ TESTJS_GLOBAL_FUNCTION(before_initial_page_load, beforeInitialPageLoad) auto function = vm.argument(0); if (!function.is_function()) { dbgln("beforeInitialPageLoad argument is not a function"); - vm.throw_exception(global_object, JS::ErrorType::NotAnObjectOfType, "Function"); - return {}; + return vm.throw_completion(global_object, JS::ErrorType::NotAnObjectOfType, "Function"); } before_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) { diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunner.h b/Userland/Libraries/LibTest/JavaScriptTestRunner.h index 8bc93f63af2..10dfd622648 100644 --- a/Userland/Libraries/LibTest/JavaScriptTestRunner.h +++ b/Userland/Libraries/LibTest/JavaScriptTestRunner.h @@ -64,9 +64,9 @@ } __testjs_register_##fn {}; #define TESTJS_GLOBAL_FUNCTION(function, exposed_name, ...) \ - JS_DECLARE_OLD_NATIVE_FUNCTION(function); \ + JS_DECLARE_NATIVE_FUNCTION(function); \ __TESTJS_REGISTER_GLOBAL_FUNCTION(#exposed_name, function, ##__VA_ARGS__); \ - JS_DEFINE_OLD_NATIVE_FUNCTION(function) + JS_DEFINE_NATIVE_FUNCTION(function) #define TESTJS_MAIN_HOOK() \ struct __TestJS_main_hook { \ @@ -120,7 +120,7 @@ extern bool g_run_bytecode; extern bool g_dump_bytecode; extern String g_currently_running_test; struct FunctionWithLength { - JS::Value (*function)(JS::VM&, JS::GlobalObject&); + JS::ThrowCompletionOr (*function)(JS::VM&, JS::GlobalObject&); size_t length { 0 }; }; extern HashMap s_exposed_global_functions; @@ -191,7 +191,7 @@ inline void TestRunnerGlobalObject::initialize_global_object() Base::initialize_global_object(); define_direct_property("global", this, JS::Attribute::Enumerable); for (auto& entry : s_exposed_global_functions) { - define_old_native_function( + define_native_function( entry.key, [fn = entry.value.function](auto& vm, auto& global_object) { return fn(vm, global_object); },