LibWeb: Move JS::Promise <-> WebIDL conversion into IDL

This change also removes as much direct use of JS::Promise in LibWeb
as possible. When specs refer to `Promise<T>` they should be assumed
to be referring to the WebIDL Promise type, not the JS::Promise type.

The one exception is the HostPromiseRejectionTracker hook on the JS
VM. This facility and its associated sets and events are intended to
expose the exact opaque object handles that were rejected to author
code. This is not possible with the WebIDL Promise type, so we have
to use JS::Promise or JS::Object to hold onto the promises.

It also exposes which specs need some updates in the area of
promises. WebDriver stands out in this regard. WebAudio could use
some more cross-references to WebIDL as well to clarify things.
This commit is contained in:
Andrew Kaster 2024-10-25 12:38:19 -06:00 committed by Andrew Kaster
commit 2c3531ab78
Notes: github-actions[bot] 2024-10-25 20:05:22 +00:00
61 changed files with 323 additions and 306 deletions

View file

@ -19,12 +19,14 @@
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/AbstractMachine/Validator.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/WebAssembly/Instance.h>
#include <LibWeb/WebAssembly/Memory.h>
#include <LibWeb/WebAssembly/Module.h>
#include <LibWeb/WebAssembly/Table.h>
#include <LibWeb/WebAssembly/WebAssembly.h>
#include <LibWeb/WebIDL/Buffers.h>
#include <LibWeb/WebIDL/Promise.h>
namespace Web::WebAssembly {
@ -80,26 +82,25 @@ bool validate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
}
// https://webassembly.github.io/spec/js-api/#dom-webassembly-compile
WebIDL::ExceptionOr<JS::Value> compile(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
{
auto& realm = *vm.current_realm();
// FIXME: This shouldn't block!
auto compiled_module_or_error = Detail::parse_module(vm, bytes->raw_object());
auto promise = JS::Promise::create(realm);
auto promise = WebIDL::create_promise(realm);
if (compiled_module_or_error.is_error()) {
promise->reject(*compiled_module_or_error.release_error().value());
WebIDL::reject_promise(realm, promise, compiled_module_or_error.error_value());
} else {
auto module_object = vm.heap().allocate<Module>(realm, realm, compiled_module_or_error.release_value());
promise->fulfill(module_object);
WebIDL::resolve_promise(realm, promise, module_object);
}
return promise;
}
// https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate
WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object)
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object)
{
// FIXME: Implement the importObject parameter.
(void)import_object;
@ -108,10 +109,10 @@ WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, JS::Handle<WebIDL::Buffer
// FIXME: This shouldn't block!
auto compiled_module_or_error = Detail::parse_module(vm, bytes->raw_object());
auto promise = JS::Promise::create(realm);
auto promise = WebIDL::create_promise(realm);
if (compiled_module_or_error.is_error()) {
promise->reject(*compiled_module_or_error.release_error().value());
WebIDL::reject_promise(realm, promise, compiled_module_or_error.error_value());
return promise;
}
@ -119,7 +120,7 @@ WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, JS::Handle<WebIDL::Buffer
auto result = Detail::instantiate_module(vm, compiled_module->module);
if (result.is_error()) {
promise->reject(*result.release_error().value());
WebIDL::reject_promise(realm, promise, result.error_value());
} else {
auto module_object = vm.heap().allocate<Module>(realm, realm, move(compiled_module));
auto instance_object = vm.heap().allocate<Instance>(realm, realm, result.release_value());
@ -127,29 +128,30 @@ WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, JS::Handle<WebIDL::Buffer
auto object = JS::Object::create(realm, nullptr);
object->define_direct_property("module", module_object, JS::default_attributes);
object->define_direct_property("instance", instance_object, JS::default_attributes);
promise->fulfill(object);
WebIDL::resolve_promise(realm, promise, object);
}
return promise;
}
// https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate-moduleobject-importobject
WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object)
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM& vm, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object)
{
// FIXME: Implement the importObject parameter.
(void)import_object;
auto& realm = *vm.current_realm();
auto promise = JS::Promise::create(realm);
auto promise = WebIDL::create_promise(realm);
// FIXME: This shouldn't block!
auto const& compiled_module = module_object.compiled_module();
auto result = Detail::instantiate_module(vm, compiled_module->module);
if (result.is_error()) {
promise->reject(*result.release_error().value());
WebIDL::reject_promise(realm, promise, result.error_value());
} else {
auto instance_object = vm.heap().allocate<Instance>(realm, realm, result.release_value());
promise->fulfill(instance_object);
WebIDL::resolve_promise(realm, promise, instance_object);
}
return promise;