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

LibJS: Remove GlobalObject from VM::throw_completion()

This is a continuation of the previous five commits.

A first big step into the direction of no longer having to pass a realm
(or currently, a global object) trough layers upon layers of AOs!
Unlike the create() APIs we can safely assume that this is only ever
called when a running execution context and therefore current realm
exists. If not, you can always manually allocate the Error and put it in
a Completion :^)

In the spec, throw exceptions implicitly use the current realm's
intrinsics as well: https://tc39.es/ecma262/#sec-throw-an-exception
This commit is contained in:
Linus Groh 2022-08-16 20:33:17 +01:00
parent 5398dcc55e
commit f3117d46dc
165 changed files with 892 additions and 900 deletions

View file

@ -23,7 +23,7 @@ WebAssemblyInstanceConstructor::~WebAssemblyInstanceConstructor() = default;
JS::ThrowCompletionOr<JS::Value> WebAssemblyInstanceConstructor::call()
{
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
}
JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(FunctionObject&)
@ -34,7 +34,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(Fun
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");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
auto result = TRY(WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object));
return heap().allocate<WebAssemblyInstanceObject>(realm, realm, result);

View file

@ -21,7 +21,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyInstancePrototype::exports_getter)
auto this_value = vm.this_value(global_object);
auto* this_object = TRY(this_value.to_object(global_object));
if (!is<WebAssemblyInstanceObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Instance");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Instance");
auto object = static_cast<WebAssemblyInstanceObject*>(this_object);
return object->m_exports_object;
}

View file

@ -21,7 +21,7 @@ WebAssemblyMemoryConstructor::~WebAssemblyMemoryConstructor() = default;
JS::ThrowCompletionOr<JS::Value> WebAssemblyMemoryConstructor::call()
{
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Memory");
return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Memory");
}
JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(FunctionObject&)
@ -35,7 +35,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(Funct
auto maximum_value = TRY(descriptor->get("maximum"));
if (!initial_value.is_number())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Number");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Number");
u32 initial = TRY(initial_value.to_u32(global_object));
@ -46,10 +46,10 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(Funct
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::MemoryType { Wasm::Limits { initial, maximum } });
if (!address.has_value())
return vm.throw_completion<JS::TypeError>(global_object, "Wasm Memory allocation failed");
return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed");
if (!WebAssemblyObject::s_abstract_machine.store().get(*address)->grow(initial))
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("Wasm Memory grow failed: {}", initial));
return vm.throw_completion<JS::TypeError>(String::formatted("Wasm Memory grow failed: {}", initial));
return vm.heap().allocate<WebAssemblyMemoryObject>(realm, realm, *address);
}

View file

@ -22,7 +22,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
auto page_count = TRY(vm.argument(0).to_u32(global_object));
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
auto* memory_object = static_cast<WebAssemblyMemoryObject*>(this_object);
auto address = memory_object->address();
auto* memory = WebAssemblyObject::s_abstract_machine.store().get(address);
@ -31,7 +31,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
auto previous_size = memory->size() / Wasm::Constants::page_size;
if (!memory->grow(page_count * Wasm::Constants::page_size))
return vm.throw_completion<JS::TypeError>(global_object, "Memory.grow() grows past the stated limit of the memory instance");
return vm.throw_completion<JS::TypeError>("Memory.grow() grows past the stated limit of the memory instance");
return JS::Value(static_cast<u32>(previous_size));
}
@ -42,7 +42,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::buffer_getter)
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
auto* memory_object = static_cast<WebAssemblyMemoryObject*>(this_object);
auto address = memory_object->address();
auto* memory = WebAssemblyObject::s_abstract_machine.store().get(address);

View file

@ -23,7 +23,7 @@ WebAssemblyModuleConstructor::~WebAssemblyModuleConstructor() = default;
JS::ThrowCompletionOr<JS::Value> WebAssemblyModuleConstructor::call()
{
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Module");
return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Module");
}
JS::ThrowCompletionOr<JS::Object*> WebAssemblyModuleConstructor::construct(FunctionObject&)

View file

@ -131,7 +131,7 @@ JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::
auto& buffer = static_cast<JS::DataView&>(*buffer_object);
data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), buffer.byte_length());
} else {
return vm.throw_completion<JS::TypeError>(global_object, "Not a BufferSource");
return vm.throw_completion<JS::TypeError>("Not a BufferSource");
}
InputMemoryStream stream { data };
auto module_result = Wasm::Module::parse(stream);
@ -142,12 +142,12 @@ JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::
};
if (module_result.is_error()) {
// FIXME: Throw CompileError instead.
return vm.throw_completion<JS::TypeError>(global_object, Wasm::parse_error_to_string(module_result.error()));
return vm.throw_completion<JS::TypeError>(Wasm::parse_error_to_string(module_result.error()));
}
if (auto validation_result = WebAssemblyObject::s_abstract_machine.validate(module_result.value()); validation_result.is_error()) {
// FIXME: Throw CompileError instead.
return vm.throw_completion<JS::TypeError>(global_object, validation_result.error().error_string);
return vm.throw_completion<JS::TypeError>(validation_result.error().error_string);
}
WebAssemblyObject::s_compiled_modules.append(make<WebAssemblyObject::CompiledWebAssemblyModule>(module_result.release_value()));
@ -250,11 +250,11 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
if (import_.is_number() || import_.is_bigint()) {
if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) {
// FIXME: Throw a LinkError instead.
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a Number to a BigInteger");
return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a Number to a BigInteger");
}
if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) {
// FIXME: Throw a LinkError instead.
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a BigInteger to a Number");
return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a BigInteger to a Number");
}
auto cast_value = TRY(to_webassembly_value(global_object, import_, type.type()));
address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value);
@ -264,7 +264,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
// let globaladdr be v.[[Global]]
// FIXME: Throw a LinkError instead
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Invalid value for global type");
return vm.throw_completion<JS::TypeError>("LinkError: Invalid value for global type");
}
resolved_imports.set(import_name, Wasm::ExternValue { *address });
@ -273,7 +273,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
[&](Wasm::MemoryType const&) -> JS::ThrowCompletionOr<void> {
if (!import_.is_object() || !is<WebAssemblyMemoryObject>(import_.as_object())) {
// FIXME: Throw a LinkError instead
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Memory for a memory import");
return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Memory for a memory import");
}
auto address = static_cast<WebAssemblyMemoryObject const&>(import_.as_object()).address();
resolved_imports.set(import_name, Wasm::ExternValue { address });
@ -282,7 +282,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
[&](Wasm::TableType const&) -> JS::ThrowCompletionOr<void> {
if (!import_.is_object() || !is<WebAssemblyTableObject>(import_.as_object())) {
// FIXME: Throw a LinkError instead
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Table for a table import");
return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Table for a table import");
}
auto address = static_cast<WebAssemblyTableObject const&>(import_.as_object()).address();
resolved_imports.set(import_name, Wasm::ExternValue { address });
@ -291,7 +291,7 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
[&](auto const&) -> JS::ThrowCompletionOr<void> {
// FIXME: Implement these.
dbgln("Unimplemented import of non-function attempted");
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Not Implemented");
return vm.throw_completion<JS::TypeError>("LinkError: Not Implemented");
}));
}
}
@ -303,13 +303,13 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
StringBuilder builder;
builder.append("LinkError: Missing "sv);
builder.join(' ', link_result.error().missing_imports);
return vm.throw_completion<JS::TypeError>(global_object, builder.build());
return vm.throw_completion<JS::TypeError>(builder.build());
}
auto instance_result = s_abstract_machine.instantiate(module, link_result.release_value());
if (instance_result.is_error()) {
// FIXME: Throw a LinkError instead.
return vm.throw_completion<JS::TypeError>(global_object, instance_result.error().error);
return vm.throw_completion<JS::TypeError>(instance_result.error().error);
}
s_instantiated_modules.append(instance_result.release_value());
@ -431,7 +431,7 @@ JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::GlobalObject& global
}
}
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Exported function");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Exported function");
}
case Wasm::ValueType::ExternReference:
case Wasm::ValueType::NullExternReference:
@ -465,7 +465,7 @@ JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm
auto result = WebAssemblyObject::s_abstract_machine.invoke(address, move(values));
// FIXME: Use the convoluted mapping of errors defined in the spec.
if (result.is_trap())
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("Wasm execution trapped (WIP): {}", result.trap().reason));
return vm.throw_completion<JS::TypeError>(String::formatted("Wasm execution trapped (WIP): {}", result.trap().reason));
if (result.values().is_empty())
return JS::js_undefined();

View file

@ -23,7 +23,7 @@ WebAssemblyTableConstructor::~WebAssemblyTableConstructor() = default;
JS::ThrowCompletionOr<JS::Value> WebAssemblyTableConstructor::call()
{
return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Table");
return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Table");
}
JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(FunctionObject&)
@ -35,7 +35,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
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());
return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidHint, element_value.to_string_without_side_effects());
auto& element = element_value.as_string().string();
Optional<Wasm::ValueType> reference_type;
@ -45,7 +45,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
reference_type = Wasm::ValueType(Wasm::ValueType::ExternReference);
if (!reference_type.has_value())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::InvalidHint, element);
return vm.throw_completion<JS::TypeError>(JS::ErrorType::InvalidHint, element);
auto initial_value = TRY(descriptor->get("initial"));
auto maximum_value = TRY(descriptor->get("maximum"));
@ -58,7 +58,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
maximum = TRY(maximum_value.to_u32(global_object));
if (maximum.has_value() && maximum.value() < initial)
return vm.throw_completion<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
return vm.throw_completion<JS::RangeError>("maximum should be larger than or equal to initial");
auto value_value = TRY(descriptor->get("value"));
auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
@ -72,7 +72,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
if (!address.has_value())
return vm.throw_completion<JS::TypeError>(global_object, "Wasm Table allocation failed");
return vm.throw_completion<JS::TypeError>("Wasm Table allocation failed");
auto& table = *WebAssemblyObject::s_abstract_machine.store().get(*address);
for (auto& element : table.elements())

View file

@ -25,7 +25,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
@ -44,7 +44,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
auto& reference = reference_value.value().get<Wasm::Reference>();
if (!table->grow(delta, reference))
return vm.throw_completion<JS::RangeError>(global_object, "Failed to grow table");
return vm.throw_completion<JS::RangeError>("Failed to grow table");
return JS::Value(static_cast<u32>(initial_size));
}
@ -55,7 +55,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
@ -63,7 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
return JS::js_undefined();
if (table->elements().size() <= index)
return vm.throw_completion<JS::RangeError>(global_object, "Table element index out of range");
return vm.throw_completion<JS::RangeError>("Table element index out of range");
auto& ref = table->elements()[index];
if (!ref.has_value())
@ -79,7 +79,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
@ -87,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
return JS::js_undefined();
if (table->elements().size() <= index)
return vm.throw_completion<JS::RangeError>(global_object, "Table element index out of range");
return vm.throw_completion<JS::RangeError>("Table element index out of range");
auto value_value = vm.argument(1);
auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
@ -107,7 +107,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::length_getter)
{
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);