mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 02:17: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:
parent
5398dcc55e
commit
f3117d46dc
165 changed files with 892 additions and 900 deletions
|
@ -35,7 +35,7 @@ TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
|
|||
{
|
||||
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");
|
||||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakSet");
|
||||
auto* weak_set = static_cast<JS::WeakSet*>(object);
|
||||
return JS::Value(weak_set->values().size());
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
|
|||
{
|
||||
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");
|
||||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakMap");
|
||||
auto* weak_map = static_cast<JS::WeakMap*>(object);
|
||||
return JS::Value(weak_map->values().size());
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage)
|
|||
{
|
||||
auto argument = vm.argument(0);
|
||||
if (!argument.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects());
|
||||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAString, argument.to_string_without_side_effects());
|
||||
|
||||
auto& variable_name = argument.as_string();
|
||||
|
||||
|
@ -62,14 +62,14 @@ TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage)
|
|||
return execution_context->lexical_environment != nullptr;
|
||||
});
|
||||
if (!outer_environment.has_value())
|
||||
return vm.throw_completion<JS::ReferenceError>(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string());
|
||||
return vm.throw_completion<JS::ReferenceError>(JS::ErrorType::UnknownIdentifier, variable_name.string());
|
||||
|
||||
auto reference = TRY(vm.resolve_binding(variable_name.string(), outer_environment.value()->lexical_environment));
|
||||
|
||||
auto value = TRY(reference.get_value(global_object));
|
||||
|
||||
if (!can_be_held_weakly(value))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::CannotBeHeldWeakly, String::formatted("Variable with name {}", variable_name.string()));
|
||||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::CannotBeHeldWeakly, String::formatted("Variable with name {}", variable_name.string()));
|
||||
|
||||
vm.heap().uproot_cell(&value.as_cell());
|
||||
TRY(reference.delete_(global_object));
|
||||
|
@ -81,7 +81,7 @@ TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer)
|
|||
{
|
||||
auto array_buffer = vm.argument(0);
|
||||
if (!array_buffer.is_object() || !is<JS::ArrayBuffer>(array_buffer.as_object()))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
|
||||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
|
||||
|
||||
auto& array_buffer_object = static_cast<JS::ArrayBuffer&>(array_buffer.as_object());
|
||||
TRY(JS::detach_array_buffer(global_object, array_buffer_object, vm.argument(1)));
|
||||
|
|
|
@ -18,17 +18,17 @@ TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
|
|||
auto filename = TRY(vm.argument(0).to_string(global_object));
|
||||
auto file = Core::Stream::File::open(filename, Core::Stream::OpenMode::Read);
|
||||
if (file.is_error())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, strerror(file.error().code()));
|
||||
return vm.throw_completion<JS::TypeError>(strerror(file.error().code()));
|
||||
|
||||
auto file_size = file.value()->size();
|
||||
if (file_size.is_error())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, strerror(file_size.error().code()));
|
||||
return vm.throw_completion<JS::TypeError>(strerror(file_size.error().code()));
|
||||
|
||||
auto* array = TRY(JS::Uint8Array::create(realm, file_size.value()));
|
||||
|
||||
auto read = file.value()->read(array->data());
|
||||
if (read.is_error())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, strerror(read.error().code()));
|
||||
return vm.throw_completion<JS::TypeError>(strerror(read.error().code()));
|
||||
|
||||
return JS::Value(array);
|
||||
}
|
||||
|
@ -57,10 +57,10 @@ public:
|
|||
linker.link(spec_test_namespace());
|
||||
auto link_result = linker.finish();
|
||||
if (link_result.is_error())
|
||||
return vm.throw_completion<JS::TypeError>(realm.global_object(), "Link failed");
|
||||
return vm.throw_completion<JS::TypeError>("Link failed");
|
||||
auto result = machine().instantiate(*instance->m_module, link_result.release_value());
|
||||
if (result.is_error())
|
||||
return vm.throw_completion<JS::TypeError>(realm.global_object(), result.release_error().error);
|
||||
return vm.throw_completion<JS::TypeError>(result.release_error().error);
|
||||
instance->m_module_instance = result.release_value();
|
||||
return instance;
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
|
|||
auto& realm = *global_object.associated_realm();
|
||||
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");
|
||||
return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module");
|
||||
auto& array = static_cast<JS::Uint8Array&>(*object);
|
||||
InputMemoryStream stream { array.data() };
|
||||
ScopeGuard handle_stream_error {
|
||||
|
@ -113,10 +113,10 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
|
|||
};
|
||||
auto result = Wasm::Module::parse(stream);
|
||||
if (result.is_error())
|
||||
return vm.throw_completion<JS::SyntaxError>(global_object, Wasm::parse_error_to_string(result.error()));
|
||||
return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_string(result.error()));
|
||||
|
||||
if (stream.handle_any_error())
|
||||
return vm.throw_completion<JS::SyntaxError>(global_object, "Binary stream contained errors");
|
||||
return vm.throw_completion<JS::SyntaxError>("Binary stream contained errors");
|
||||
|
||||
HashMap<Wasm::Linker::Name, Wasm::ExternValue> imports;
|
||||
auto import_value = vm.argument(1);
|
||||
|
@ -141,11 +141,11 @@ TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)
|
|||
{
|
||||
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");
|
||||
return vm.throw_completion<JS::TypeError>("Expected a TypedArray");
|
||||
auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs);
|
||||
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");
|
||||
return vm.throw_completion<JS::TypeError>("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());
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
|
|||
auto this_value = vm.this_value(global_object);
|
||||
auto* object = TRY(this_value.to_object(global_object));
|
||||
if (!is<WebAssemblyModule>(object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Not a WebAssemblyModule");
|
||||
return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule");
|
||||
auto instance = static_cast<WebAssemblyModule*>(object);
|
||||
for (auto& entry : instance->module_instance().exports()) {
|
||||
if (entry.name() == name) {
|
||||
|
@ -181,10 +181,10 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
|
|||
[&](const auto& ref) -> JS::Value { return JS::Value(static_cast<double>(ref.address.value())); });
|
||||
});
|
||||
}
|
||||
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("'{}' does not refer to a function or a global", name));
|
||||
return vm.throw_completion<JS::TypeError>(String::formatted("'{}' does not refer to a function or a global", name));
|
||||
}
|
||||
}
|
||||
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("'{}' could not be found", name));
|
||||
return vm.throw_completion<JS::TypeError>(String::formatted("'{}' could not be found", name));
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
|
||||
|
@ -193,16 +193,16 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
|
|||
Wasm::FunctionAddress function_address { address };
|
||||
auto function_instance = WebAssemblyModule::machine().store().get(function_address);
|
||||
if (!function_instance)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Invalid function address");
|
||||
return vm.throw_completion<JS::TypeError>("Invalid function address");
|
||||
|
||||
Wasm::FunctionType const* type { nullptr };
|
||||
function_instance->visit([&](auto& value) { type = &value.type(); });
|
||||
if (!type)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Invalid function found at given address");
|
||||
return vm.throw_completion<JS::TypeError>("Invalid function found at given address");
|
||||
|
||||
Vector<Wasm::Value> arguments;
|
||||
if (type->parameters().size() + 1 > vm.argument_count())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()));
|
||||
return vm.throw_completion<JS::TypeError>(String::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()));
|
||||
size_t index = 1;
|
||||
for (auto& param : type->parameters()) {
|
||||
auto argument = vm.argument(index++);
|
||||
|
@ -244,7 +244,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
|
|||
|
||||
auto result = WebAssemblyModule::machine().invoke(function_address, arguments);
|
||||
if (result.is_trap())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("Execution trapped: {}", result.trap().reason));
|
||||
return vm.throw_completion<JS::TypeError>(String::formatted("Execution trapped: {}", result.trap().reason));
|
||||
|
||||
if (result.values().is_empty())
|
||||
return JS::js_null();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue