1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:47:45 +00:00

LibJS: Convert to_object() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-12 19:24:57 +01:00
parent 9eb065a1f6
commit 52976bfac6
45 changed files with 239 additions and 490 deletions

View file

@ -33,11 +33,7 @@ JS::Value WebAssemblyInstanceConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto module_argument = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
auto* module_argument = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
if (!is<WebAssemblyModuleObject>(module_argument)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
return {};

View file

@ -19,9 +19,7 @@ void WebAssemblyInstancePrototype::initialize(JS::GlobalObject& global_object)
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyInstancePrototype::exports_getter)
{
auto this_value = vm.this_value(global_object);
auto this_object = this_value.to_object(global_object);
if (vm.exception())
return {};
auto* this_object = TRY_OR_DISCARD(this_value.to_object(global_object));
if (!is<WebAssemblyInstanceObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Instance");
return {};

View file

@ -32,10 +32,7 @@ JS::Value WebAssemblyMemoryConstructor::construct(FunctionObject&)
auto& vm = this->vm();
auto& global_object = this->global_object();
auto descriptor = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
auto descriptor = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
auto initial_value = TRY_OR_DISCARD(descriptor->get("initial"));
auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum"));

View file

@ -22,8 +22,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
auto page_count = vm.argument(0).to_u32(global_object);
if (vm.exception())
return {};
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object || !is<WebAssemblyMemoryObject>(this_object)) {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
return {};
}
@ -44,8 +44,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::buffer_getter)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object || !is<WebAssemblyMemoryObject>(this_object)) {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
return {};
}

View file

@ -34,10 +34,7 @@ JS::Value WebAssemblyModuleConstructor::construct(FunctionObject&)
auto& vm = this->vm();
auto& global_object = this->global_object();
auto buffer_object = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
auto* buffer_object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
auto result = parse_module(global_object, buffer_object);
if (result.is_error()) {
vm.throw_exception(global_object, result.error());

View file

@ -130,17 +130,19 @@ Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Obje
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile)
{
// FIXME: This shouldn't block!
auto buffer = vm.argument(0).to_object(global_object);
auto buffer_or_error = vm.argument(0).to_object(global_object);
JS::Value rejection_value;
if (vm.exception()) {
rejection_value = vm.exception()->value();
if (buffer_or_error.is_error()) {
rejection_value = buffer_or_error.throw_completion().value();
vm.clear_exception();
vm.stop_unwind();
}
auto promise = JS::Promise::create(global_object);
if (!rejection_value.is_empty()) {
promise->reject(rejection_value);
return promise;
}
auto* buffer = buffer_or_error.release_value();
auto result = parse_module(global_object, buffer);
if (result.is_error())
promise->reject(result.error());
@ -155,11 +157,13 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
auto import_argument = vm.argument(1);
if (!import_argument.is_undefined()) {
[[maybe_unused]] auto import_object = import_argument.to_object(global_object);
if (auto exception = vm.exception()) {
auto import_object_or_error = import_argument.to_object(global_object);
if (import_object_or_error.is_error()) {
vm.clear_exception();
return exception->value();
vm.stop_unwind();
return import_object_or_error.throw_completion().value();
}
[[maybe_unused]] auto* import_object = import_object_or_error.release_value();
dbgln("Trying to resolve stuff because import object was specified");
for (const Wasm::Linker::Name& import_name : linker.unresolved_imports()) {
@ -168,10 +172,10 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
if (value_or_error.is_error())
break;
auto value = value_or_error.release_value();
auto object = value.to_object(global_object);
if (vm.exception())
auto object_or_error = value.to_object(global_object);
if (object_or_error.is_error())
break;
auto* object = object_or_error.release_value();
auto import_or_error = object->get(import_name.name);
if (import_or_error.is_error())
break;
@ -307,22 +311,17 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
{
// FIXME: This shouldn't block!
auto buffer = vm.argument(0).to_object(global_object);
auto buffer_or_error = vm.argument(0).to_object(global_object);
auto promise = JS::Promise::create(global_object);
bool should_return_module = false;
auto take_exception_and_reject_if_needed = [&] {
if (vm.exception()) {
auto rejection_value = vm.exception()->value();
vm.clear_exception();
promise->reject(rejection_value);
return true;
}
return false;
};
if (take_exception_and_reject_if_needed())
if (buffer_or_error.is_error()) {
auto rejection_value = buffer_or_error.throw_completion().value();
vm.clear_exception();
vm.stop_unwind();
promise->reject(rejection_value);
return promise;
}
auto* buffer = buffer_or_error.release_value();
const Wasm::Module* module { nullptr };
if (is<JS::ArrayBuffer>(buffer) || is<JS::TypedArrayBase>(buffer)) {

View file

@ -34,10 +34,7 @@ JS::Value WebAssemblyTableConstructor::construct(FunctionObject&)
auto& vm = this->vm();
auto& global_object = this->global_object();
auto descriptor = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
auto descriptor = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
auto element_value = TRY_OR_DISCARD(descriptor->get("element"));
if (!element_value.is_string()) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element_value.to_string_without_side_effects());

View file

@ -24,8 +24,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
auto delta = vm.argument(0).to_u32(global_object);
if (vm.exception())
return {};
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object || !is<WebAssemblyTableObject>(this_object)) {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
@ -63,8 +63,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
if (vm.exception())
return {};
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object || !is<WebAssemblyTableObject>(this_object)) {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
@ -93,8 +93,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
if (vm.exception())
return {};
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object || !is<WebAssemblyTableObject>(this_object)) {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
@ -128,8 +128,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::length_getter)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object || !is<WebAssemblyTableObject>(this_object)) {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}