summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-10-20 01:58:04 +0300
committerLinus Groh <mail@linusgroh.de>2021-10-20 12:27:19 +0100
commit44555eb50a51606073f37e0d7faa94afa4e8eab0 (patch)
tree3562977b6453da6a15d228a2e61a38910d487f85 /Tests
parent40eb3a39d41fe0c3242b586da9cf7e4ce2fcbf13 (diff)
downloadserenity-44555eb50a51606073f37e0d7faa94afa4e8eab0.zip
LibJS: Convert test-js/test-web/test-wasm to ThrowCompletionOr
Diffstat (limited to 'Tests')
-rw-r--r--Tests/LibJS/test-js.cpp40
-rw-r--r--Tests/LibWasm/test-wasm.cpp48
-rw-r--r--Tests/LibWeb/test-web.cpp8
3 files changed, 36 insertions, 60 deletions
diff --git a/Tests/LibJS/test-js.cpp b/Tests/LibJS/test-js.cpp
index e83c1a548d..5da868d4b1 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<JS::WeakSet>(object)) {
- vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakSet");
- return {};
- }
+ auto* object = TRY(vm.argument(0).to_object(global_object));
+ if (!is<JS::WeakSet>(object))
+ return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakSet");
auto* weak_set = static_cast<JS::WeakSet*>(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<JS::WeakMap>(object)) {
- vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakMap");
- return {};
- }
+ auto* object = TRY(vm.argument(0).to_object(global_object));
+ if (!is<JS::WeakMap>(object))
+ return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakMap");
auto* weak_map = static_cast<JS::WeakMap*>(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<JS::TypeError>(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects());
- return {};
- }
+ if (!argument.is_string())
+ return vm.throw_completion<JS::TypeError>(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<JS::ReferenceError>(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string());
- return {};
- }
+ if (!outer_environment.has_value())
+ return vm.throw_completion<JS::ReferenceError>(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<JS::TypeError>(global_object, JS::ErrorType::NotAnObject, String::formatted("Variable with name {}", variable_name.string()));
- return {};
- }
+ if (!value.is_object())
+ return vm.throw_completion<JS::TypeError>(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 067ee0258f..e9243f8f4d 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<JS::TypeError>(global_object, file.error().string());
- return {};
- }
+ if (file.is_error())
+ return vm.throw_completion<JS::TypeError>(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<Wasm::Linker::Name, Wasm::ExternValue> 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<JS::Uint8Array>(object)) {
- vm.throw_exception<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
- return {};
- }
+ auto* object = TRY(vm.argument(0).to_object(global_object));
+ if (!is<JS::Uint8Array>(object))
+ return vm.throw_completion<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
auto& array = static_cast<JS::Uint8Array&>(*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<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
- return {};
- }
+ if (result.is_error())
+ return vm.throw_completion<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
- if (stream.handle_any_error()) {
- vm.throw_exception<JS::SyntaxError>(global_object, "Binary stream contained errors");
- return {};
- }
+ if (stream.handle_any_error())
+ return vm.throw_completion<JS::SyntaxError>(global_object, "Binary stream contained errors");
HashMap<Wasm::Linker::Name, Wasm::ExternValue> 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<JS::TypedArrayBase>(lhs)) {
- vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
- return {};
- }
+ auto* lhs = TRY(vm.argument(0).to_object(global_object));
+ if (!is<JS::TypedArrayBase>(lhs))
+ return vm.throw_completion<JS::TypeError>(global_object, "Expected a TypedArray");
auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
- auto* rhs = TRY_OR_DISCARD(vm.argument(1).to_object(global_object));
- if (!is<JS::TypedArrayBase>(rhs)) {
- vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
- return {};
- }
+ auto* rhs = TRY(vm.argument(1).to_object(global_object));
+ if (!is<JS::TypedArrayBase>(rhs))
+ return vm.throw_completion<JS::TypeError>(global_object, "Expected a TypedArray");
auto& rhs_array = static_cast<JS::TypedArrayBase&>(*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 893adab262..0bd22a29ed 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<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
- return {};
+ return vm.throw_completion<JS::TypeError>(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<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
- return {};
+ return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
}
before_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {