mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
LibJS: Convert test-js/test-web/test-wasm to ThrowCompletionOr
This commit is contained in:
parent
40eb3a39d4
commit
44555eb50a
Notes:
sideshowbarker
2024-07-18 02:47:59 +09:00
Author: https://github.com/IdanHo
Commit: 44555eb50a
Pull-request: https://github.com/SerenityOS/serenity/pull/10543
Reviewed-by: https://github.com/linusg
4 changed files with 40 additions and 64 deletions
|
@ -18,7 +18,7 @@ TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
|
||||||
|
|
||||||
TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
|
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));
|
auto parser = JS::Parser(JS::Lexer(source));
|
||||||
parser.parse_program();
|
parser.parse_program();
|
||||||
return JS::Value(!parser.has_errors());
|
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)
|
TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
|
||||||
{
|
{
|
||||||
auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
|
auto* object = TRY(vm.argument(0).to_object(global_object));
|
||||||
if (!is<JS::WeakSet>(object)) {
|
if (!is<JS::WeakSet>(object))
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakSet");
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakSet");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto* weak_set = static_cast<JS::WeakSet*>(object);
|
auto* weak_set = static_cast<JS::WeakSet*>(object);
|
||||||
return JS::Value(weak_set->values().size());
|
return JS::Value(weak_set->values().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
|
TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
|
||||||
{
|
{
|
||||||
auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
|
auto* object = TRY(vm.argument(0).to_object(global_object));
|
||||||
if (!is<JS::WeakMap>(object)) {
|
if (!is<JS::WeakMap>(object))
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakMap");
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WeakMap");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto* weak_map = static_cast<JS::WeakMap*>(object);
|
auto* weak_map = static_cast<JS::WeakMap*>(object);
|
||||||
return JS::Value(weak_map->values().size());
|
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)
|
TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage)
|
||||||
{
|
{
|
||||||
auto argument = vm.argument(0);
|
auto argument = vm.argument(0);
|
||||||
if (!argument.is_string()) {
|
if (!argument.is_string())
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects());
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects());
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& variable_name = argument.as_string();
|
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) {
|
auto outer_environment = vm.execution_context_stack().last_matching([&](auto& execution_context) {
|
||||||
return execution_context->lexical_environment != nullptr;
|
return execution_context->lexical_environment != nullptr;
|
||||||
});
|
});
|
||||||
if (!outer_environment.has_value()) {
|
if (!outer_environment.has_value())
|
||||||
vm.throw_exception<JS::ReferenceError>(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string());
|
return vm.throw_completion<JS::ReferenceError>(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string());
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto reference = vm.resolve_binding(variable_name.string(), outer_environment.value()->lexical_environment);
|
auto reference = vm.resolve_binding(variable_name.string(), outer_environment.value()->lexical_environment);
|
||||||
|
|
||||||
auto value = reference.get_value(global_object);
|
auto value = reference.get_value(global_object);
|
||||||
if (vm.exception())
|
if (auto* exception = vm.exception())
|
||||||
return {};
|
return JS::throw_completion(exception->value());
|
||||||
|
|
||||||
if (!value.is_object()) {
|
if (!value.is_object())
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObject, String::formatted("Variable with name {}", variable_name.string()));
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObject, String::formatted("Variable with name {}", variable_name.string()));
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
vm.heap().uproot_cell(&value.as_object());
|
vm.heap().uproot_cell(&value.as_object());
|
||||||
reference.delete_(global_object);
|
reference.delete_(global_object);
|
||||||
|
|
|
@ -13,16 +13,14 @@ TEST_ROOT("Userland/Libraries/LibWasm/Tests");
|
||||||
|
|
||||||
TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
|
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);
|
auto file = Core::File::open(filename, Core::OpenMode::ReadOnly);
|
||||||
if (file.is_error()) {
|
if (file.is_error())
|
||||||
vm.throw_exception<JS::TypeError>(global_object, file.error().string());
|
return vm.throw_completion<JS::TypeError>(global_object, file.error().string());
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto contents = file.value()->read_all();
|
auto contents = file.value()->read_all();
|
||||||
auto array = JS::Uint8Array::create(global_object, contents.size());
|
auto array = JS::Uint8Array::create(global_object, contents.size());
|
||||||
contents.span().copy_to(array->data());
|
contents.span().copy_to(array->data());
|
||||||
return array;
|
return JS::Value(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
class WebAssemblyModule final : public JS::Object {
|
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)
|
TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
|
||||||
{
|
{
|
||||||
auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
|
auto* object = TRY(vm.argument(0).to_object(global_object));
|
||||||
if (!is<JS::Uint8Array>(object)) {
|
if (!is<JS::Uint8Array>(object))
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
|
return vm.throw_completion<JS::TypeError>(global_object, "Expected a Uint8Array argument to parse_webassembly_module");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto& array = static_cast<JS::Uint8Array&>(*object);
|
auto& array = static_cast<JS::Uint8Array&>(*object);
|
||||||
InputMemoryStream stream { array.data() };
|
InputMemoryStream stream { array.data() };
|
||||||
ScopeGuard handle_stream_error {
|
ScopeGuard handle_stream_error {
|
||||||
|
@ -106,15 +102,11 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
auto result = Wasm::Module::parse(stream);
|
auto result = Wasm::Module::parse(stream);
|
||||||
if (result.is_error()) {
|
if (result.is_error())
|
||||||
vm.throw_exception<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
|
return vm.throw_completion<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream.handle_any_error()) {
|
if (stream.handle_any_error())
|
||||||
vm.throw_exception<JS::SyntaxError>(global_object, "Binary stream contained errors");
|
return vm.throw_completion<JS::SyntaxError>(global_object, "Binary stream contained errors");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
|
HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
|
||||||
auto import_value = vm.argument(1);
|
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)
|
TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
|
||||||
{
|
{
|
||||||
auto* lhs = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
|
auto* lhs = TRY(vm.argument(0).to_object(global_object));
|
||||||
if (!is<JS::TypedArrayBase>(lhs)) {
|
if (!is<JS::TypedArrayBase>(lhs))
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
|
return vm.throw_completion<JS::TypeError>(global_object, "Expected a TypedArray");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
|
auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
|
||||||
auto* rhs = TRY_OR_DISCARD(vm.argument(1).to_object(global_object));
|
auto* rhs = TRY(vm.argument(1).to_object(global_object));
|
||||||
if (!is<JS::TypedArrayBase>(rhs)) {
|
if (!is<JS::TypedArrayBase>(rhs))
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "Expected a TypedArray");
|
return vm.throw_completion<JS::TypeError>(global_object, "Expected a TypedArray");
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
|
auto& rhs_array = static_cast<JS::TypedArrayBase&>(*rhs);
|
||||||
return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
|
return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer());
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ TESTJS_MAIN_HOOK()
|
||||||
|
|
||||||
TESTJS_GLOBAL_FUNCTION(load_local_page, loadLocalPage)
|
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
|
// Clear the hooks
|
||||||
before_initial_load_hooks.clear();
|
before_initial_load_hooks.clear();
|
||||||
|
@ -59,8 +59,7 @@ TESTJS_GLOBAL_FUNCTION(after_initial_page_load, afterInitialPageLoad)
|
||||||
auto function = vm.argument(0);
|
auto function = vm.argument(0);
|
||||||
if (!function.is_function()) {
|
if (!function.is_function()) {
|
||||||
dbgln("afterInitialPageLoad argument is not a function");
|
dbgln("afterInitialPageLoad argument is not a function");
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
after_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
|
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);
|
auto function = vm.argument(0);
|
||||||
if (!function.is_function()) {
|
if (!function.is_function()) {
|
||||||
dbgln("beforeInitialPageLoad argument is not a function");
|
dbgln("beforeInitialPageLoad argument is not a function");
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
before_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
|
before_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
|
||||||
|
|
|
@ -64,9 +64,9 @@
|
||||||
} __testjs_register_##fn {};
|
} __testjs_register_##fn {};
|
||||||
|
|
||||||
#define TESTJS_GLOBAL_FUNCTION(function, exposed_name, ...) \
|
#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__); \
|
__TESTJS_REGISTER_GLOBAL_FUNCTION(#exposed_name, function, ##__VA_ARGS__); \
|
||||||
JS_DEFINE_OLD_NATIVE_FUNCTION(function)
|
JS_DEFINE_NATIVE_FUNCTION(function)
|
||||||
|
|
||||||
#define TESTJS_MAIN_HOOK() \
|
#define TESTJS_MAIN_HOOK() \
|
||||||
struct __TestJS_main_hook { \
|
struct __TestJS_main_hook { \
|
||||||
|
@ -120,7 +120,7 @@ extern bool g_run_bytecode;
|
||||||
extern bool g_dump_bytecode;
|
extern bool g_dump_bytecode;
|
||||||
extern String g_currently_running_test;
|
extern String g_currently_running_test;
|
||||||
struct FunctionWithLength {
|
struct FunctionWithLength {
|
||||||
JS::Value (*function)(JS::VM&, JS::GlobalObject&);
|
JS::ThrowCompletionOr<JS::Value> (*function)(JS::VM&, JS::GlobalObject&);
|
||||||
size_t length { 0 };
|
size_t length { 0 };
|
||||||
};
|
};
|
||||||
extern HashMap<String, FunctionWithLength> s_exposed_global_functions;
|
extern HashMap<String, FunctionWithLength> s_exposed_global_functions;
|
||||||
|
@ -191,7 +191,7 @@ inline void TestRunnerGlobalObject::initialize_global_object()
|
||||||
Base::initialize_global_object();
|
Base::initialize_global_object();
|
||||||
define_direct_property("global", this, JS::Attribute::Enumerable);
|
define_direct_property("global", this, JS::Attribute::Enumerable);
|
||||||
for (auto& entry : s_exposed_global_functions) {
|
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) {
|
entry.key, [fn = entry.value.function](auto& vm, auto& global_object) {
|
||||||
return fn(vm, global_object);
|
return fn(vm, global_object);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue