LibWeb/HTML: Port Window.structuredClone() to IDL

This commit is contained in:
Linus Groh 2023-03-07 18:21:49 +00:00
parent a31c561a5a
commit 129ab02470
Notes: sideshowbarker 2024-07-17 03:27:40 +09:00
8 changed files with 42 additions and 25 deletions

View file

@ -136,6 +136,9 @@ CppType idl_type_name_to_cpp_type(Type const& type, Interface const& interface)
if (type.name() == "any" || type.name() == "undefined")
return { .name = "JS::Value", .sequence_storage_type = SequenceStorageType::MarkedVector };
if (type.name() == "object")
return { .name = "JS::Handle<JS::Object>", .sequence_storage_type = SequenceStorageType::Vector };
if (type.name() == "BufferSource")
return { .name = "JS::Handle<JS::Object>", .sequence_storage_type = SequenceStorageType::MarkedVector };
@ -591,6 +594,10 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
@js_name@@js_suffix@ = new_promise;
}
auto @cpp_name@ = JS::make_handle(&static_cast<JS::Promise&>(@js_name@@js_suffix@.as_object()));
)~~~");
} else if (parameter.type->name() == "object") {
scoped_generator.append(R"~~~(
auto @cpp_name@ = JS::make_handle(TRY(@js_name@@js_suffix@.to_object(vm)));
)~~~");
} else if (parameter.type->name() == "BufferSource") {
scoped_generator.append(R"~~~(

View file

@ -17,6 +17,11 @@ namespace Web::HTML {
E(onmessage, HTML::EventNames::message) \
E(onmessageerror, HTML::EventNames::messageerror)
// https://html.spec.whatwg.org/multipage/web-messaging.html#structuredserializeoptions
struct StructuredSerializeOptions {
Vector<JS::Handle<JS::Object>> transfer;
};
// https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
class MessagePort final : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(MessagePort, DOM::EventTarget);

View file

@ -4,12 +4,15 @@
// https://html.spec.whatwg.org/multipage/web-messaging.html#messageport
[Exposed=(Window,Worker,AudioWorklet), Transferable]
interface MessagePort : EventTarget {
undefined postMessage(any message);
undefined start();
undefined close();
// event handlers
attribute EventHandler onmessage;
attribute EventHandler onmessageerror;
};
dictionary StructuredSerializeOptions {
sequence<object> transfer = [];
};

View file

@ -46,7 +46,6 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Storage.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/Timer.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
@ -742,13 +741,6 @@ JS::NonnullGCPtr<HTML::Storage> Window::session_storage()
return *storage;
}
// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
WebIDL::ExceptionOr<JS::Value> Window::structured_clone_impl(JS::VM& vm, JS::Value message)
{
auto serialized = TRY(structured_serialize(vm, message));
return MUST(structured_deserialize(vm, serialized, *vm.current_realm(), {}));
}
// https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
bool Window::has_transient_activation() const
{
@ -893,8 +885,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_function(realm, "queueMicrotask", queue_microtask, 1, attr);
define_native_function(realm, "structuredClone", structured_clone, 1, attr);
define_direct_property("CSS", MUST_OR_THROW_OOM(heap().allocate<Bindings::CSSNamespace>(realm, realm)), 0);
// FIXME: Implement codegen for readonly properties with [PutForwards]
@ -1608,12 +1598,4 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter)
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(Window::structured_clone)
{
auto* impl = TRY(impl_from(vm));
return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] {
return impl->structured_clone_impl(vm, vm.argument(0));
}));
}
}

View file

@ -61,6 +61,7 @@ public:
using WindowOrWorkerGlobalScopeMixin::atob;
using WindowOrWorkerGlobalScopeMixin::btoa;
using WindowOrWorkerGlobalScopeMixin::fetch;
using WindowOrWorkerGlobalScopeMixin::structured_clone;
// ^DOM::EventTarget
virtual bool dispatch_event(DOM::Event&) override;
@ -115,8 +116,6 @@ public:
JS::NonnullGCPtr<HTML::Storage> local_storage();
JS::NonnullGCPtr<HTML::Storage> session_storage();
WebIDL::ExceptionOr<JS::Value> structured_clone_impl(JS::VM& vm, JS::Value);
void start_an_idle_period();
AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }
@ -251,8 +250,6 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(location_setter);
JS_DECLARE_NATIVE_FUNCTION(structured_clone);
JS_DECLARE_NATIVE_FUNCTION(set_interval);
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
JS_DECLARE_NATIVE_FUNCTION(clear_interval);

View file

@ -12,6 +12,8 @@
#include <LibTextCodec/Decoder.h>
#include <LibWeb/Fetch/FetchMethod.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
#include <LibWeb/Infra/Base64.h>
#include <LibWeb/WebIDL/DOMException.h>
@ -84,6 +86,24 @@ WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::atob(String const& d
return TRY_OR_THROW_OOM(vm, decoder->to_utf8(decoded_data.value()));
}
// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
WebIDL::ExceptionOr<JS::Value> WindowOrWorkerGlobalScopeMixin::structured_clone(JS::Value value, StructuredSerializeOptions const& options) const
{
auto& vm = this_impl().vm();
(void)options;
// 1. Let serialized be ? StructuredSerializeWithTransfer(value, options["transfer"]).
// FIXME: Use WithTransfer variant of the AO
auto serialized = TRY(structured_serialize(vm, value));
// 2. Let deserializeRecord be ? StructuredDeserializeWithTransfer(serialized, this's relevant realm).
// FIXME: Use WithTransfer variant of the AO
auto deserialized = TRY(structured_deserialize(vm, serialized, relevant_realm(this_impl()), {}));
// 3. Return deserializeRecord.[[Deserialized]].
return deserialized;
}
JS::NonnullGCPtr<JS::Promise> WindowOrWorkerGlobalScopeMixin::fetch(Fetch::RequestInfo const& input, Fetch::RequestInit const& init) const
{
auto& vm = this_impl().vm();

View file

@ -10,6 +10,7 @@
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Fetch/Request.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/MessagePort.h>
namespace Web::HTML {
@ -27,6 +28,7 @@ public:
bool cross_origin_isolated() const;
WebIDL::ExceptionOr<String> btoa(String const& data) const;
WebIDL::ExceptionOr<String> atob(String const& data) const;
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Value, StructuredSerializeOptions const&) const;
JS::NonnullGCPtr<JS::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
};

View file

@ -1,5 +1,6 @@
#import <Fetch/Request.idl>
#import <Fetch/Response.idl>
#import <HTML/MessagePort.idl>
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
interface mixin WindowOrWorkerGlobalScope {
@ -27,7 +28,7 @@ interface mixin WindowOrWorkerGlobalScope {
// FIXME: Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, long sx, long sy, long sw, long sh, optional ImageBitmapOptions options = {});
// structured cloning
// FIXME: any structuredClone(any value, optional StructuredSerializeOptions options = {});
any structuredClone(any value, optional StructuredSerializeOptions options = {});
// https://fetch.spec.whatwg.org/#fetch-method
[NewObject] Promise<Response> fetch(RequestInfo input, optional RequestInit init = {});