1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:37:34 +00:00

LibJS: Convert NativeFunction::{call,construct}() to ThrowCompletionOr

Both at the same time because many of them call construct() in call()
and I'm not keen on adding a bunch of temporary plumbing to turn
exceptions into throw completions.
Also changes the return value of construct() to Object* instead of Value
as it always needs to return an object; allowing an arbitrary Value is a
massive foot gun.
This commit is contained in:
Linus Groh 2021-10-20 21:16:30 +01:00
parent 0881f8160f
commit 5832de62fe
99 changed files with 597 additions and 669 deletions

View file

@ -23,27 +23,23 @@ WebAssemblyInstanceConstructor::~WebAssemblyInstanceConstructor()
{
}
JS::Value WebAssemblyInstanceConstructor::call()
JS::ThrowCompletionOr<JS::Value> WebAssemblyInstanceConstructor::call()
{
vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
return {};
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
}
JS::Value WebAssemblyInstanceConstructor::construct(FunctionObject&)
JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
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 {};
}
auto* module_argument = TRY(vm.argument(0).to_object(global_object));
if (!is<WebAssemblyModuleObject>(module_argument))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
auto result = WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object);
if (result.is_error()) {
vm.throw_exception(global_object, result.release_error());
return {};
vm.throw_exception(global_object, result.error());
return JS::throw_completion(result.error());
}
return heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.value());
}

View file

@ -18,8 +18,8 @@ public:
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyInstanceConstructor() override;
virtual JS::Value call() override;
virtual JS::Value construct(JS::FunctionObject& new_target) override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;
virtual JS::ThrowCompletionOr<JS::Object*> construct(JS::FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }

View file

@ -21,38 +21,33 @@ WebAssemblyMemoryConstructor::~WebAssemblyMemoryConstructor()
{
}
JS::Value WebAssemblyMemoryConstructor::call()
JS::ThrowCompletionOr<JS::Value> WebAssemblyMemoryConstructor::call()
{
vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Memory");
return {};
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Memory");
}
JS::Value WebAssemblyMemoryConstructor::construct(FunctionObject&)
JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
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"));
auto descriptor = TRY(vm.argument(0).to_object(global_object));
auto initial_value = TRY(descriptor->get("initial"));
auto maximum_value = TRY(descriptor->get("maximum"));
if (initial_value.is_empty()) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Number");
return {};
}
if (initial_value.is_empty())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Number");
auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object));
auto initial = TRY(initial_value.to_u32(global_object));
Optional<u32> maximum;
if (!maximum_value.is_empty())
maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object));
maximum = TRY(maximum_value.to_u32(global_object));
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::MemoryType { Wasm::Limits { initial, maximum } });
if (!address.has_value()) {
vm.throw_exception<JS::TypeError>(global_object, "Wasm Memory allocation failed");
return {};
}
if (!address.has_value())
return vm.throw_completion<JS::TypeError>(global_object, "Wasm Memory allocation failed");
return vm.heap().allocate<WebAssemblyMemoryObject>(global_object, global_object, *address);
}

View file

@ -18,8 +18,8 @@ public:
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyMemoryConstructor() override;
virtual JS::Value call() override;
virtual JS::Value construct(JS::FunctionObject& new_target) override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;
virtual JS::ThrowCompletionOr<JS::Object*> construct(JS::FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }

View file

@ -23,22 +23,21 @@ WebAssemblyModuleConstructor::~WebAssemblyModuleConstructor()
{
}
JS::Value WebAssemblyModuleConstructor::call()
JS::ThrowCompletionOr<JS::Value> WebAssemblyModuleConstructor::call()
{
vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Module");
return {};
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Module");
}
JS::Value WebAssemblyModuleConstructor::construct(FunctionObject&)
JS::ThrowCompletionOr<JS::Object*> WebAssemblyModuleConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto* buffer_object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
auto* buffer_object = TRY(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());
return {};
return JS::throw_completion(result.error());
}
return heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value());

View file

@ -18,8 +18,8 @@ public:
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyModuleConstructor() override;
virtual JS::Value call() override;
virtual JS::Value construct(JS::FunctionObject& new_target) override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;
virtual JS::ThrowCompletionOr<JS::Object*> construct(JS::FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }

View file

@ -23,23 +23,20 @@ WebAssemblyTableConstructor::~WebAssemblyTableConstructor()
{
}
JS::Value WebAssemblyTableConstructor::call()
JS::ThrowCompletionOr<JS::Value> WebAssemblyTableConstructor::call()
{
vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Table");
return {};
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Table");
}
JS::Value WebAssemblyTableConstructor::construct(FunctionObject&)
JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
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());
return {};
}
auto descriptor = TRY(vm.argument(0).to_object(global_object));
auto element_value = TRY(descriptor->get("element"));
if (!element_value.is_string())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element_value.to_string_without_side_effects());
auto& element = element_value.as_string().string();
Optional<Wasm::ValueType> reference_type;
@ -48,27 +45,23 @@ JS::Value WebAssemblyTableConstructor::construct(FunctionObject&)
else if (element == "externref"sv)
reference_type = Wasm::ValueType(Wasm::ValueType::ExternReference);
if (!reference_type.has_value()) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element);
return {};
}
if (!reference_type.has_value())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element);
auto initial_value = TRY_OR_DISCARD(descriptor->get("initial"));
auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum"));
auto initial_value = TRY(descriptor->get("initial"));
auto maximum_value = TRY(descriptor->get("maximum"));
auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object));
auto initial = TRY(initial_value.to_u32(global_object));
Optional<u32> maximum;
if (!maximum_value.is_undefined())
maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object));
maximum = TRY(maximum_value.to_u32(global_object));
if (maximum.has_value() && maximum.value() < initial) {
vm.throw_exception<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
return {};
}
if (maximum.has_value() && maximum.value() < initial)
return vm.throw_completion<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
auto value_value = TRY_OR_DISCARD(descriptor->get("value"));
auto value_value = TRY(descriptor->get("value"));
auto reference_value = [&]() -> Optional<Wasm::Value> {
if (value_value.is_undefined())
return Wasm::Value(*reference_type, 0ull);
@ -76,16 +69,14 @@ JS::Value WebAssemblyTableConstructor::construct(FunctionObject&)
return to_webassembly_value(value_value, *reference_type, global_object);
}();
if (!reference_value.has_value())
return {};
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
auto& reference = reference_value->value().get<Wasm::Reference>();
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
if (!address.has_value()) {
vm.throw_exception<JS::TypeError>(global_object, "Wasm Table allocation failed");
return {};
}
if (!address.has_value())
return vm.throw_completion<JS::TypeError>(global_object, "Wasm Table allocation failed");
auto& table = *WebAssemblyObject::s_abstract_machine.store().get(*address);
for (auto& element : table.elements())

View file

@ -18,8 +18,8 @@ public:
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyTableConstructor() override;
virtual JS::Value call() override;
virtual JS::Value construct(JS::FunctionObject& new_target) override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;
virtual JS::ThrowCompletionOr<JS::Object*> construct(JS::FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }