LibWeb+WebWorker: Use IPC mechanics for structured serialization

Our structured serialization implementation had its own bespoke encoder
and decoder to serialize JS values. It also used a u32 buffer under the
hood, which made using its structures a bit awkward. We had previously
worked around its data structures in transferable streams, which nested
transfers of MessagePort instances. We basically had to add hooks into
the MessagePort to route to the correct transfer receiving steps, and
we could not invoke the correct AOs directly as the spec dictates.

We now use IPC mechanics to encode and decode data. This works because,
although we are encoding JS values, we are only ultimately encoding
primitive and basic AK types. The resulting data structures actually
enforce that we implement transferable streams exactly as the spec is
worded (I had planned to do that in a separate commit, but the fallout
of this patch actually required that change).
This commit is contained in:
Timothy Flynn 2025-07-17 09:51:04 -04:00 committed by Tim Flynn
parent 7fad8c333d
commit 64abc6101d
Notes: github-actions[bot] 2025-07-18 14:10:44 +00:00
44 changed files with 765 additions and 970 deletions

View file

@ -89,7 +89,7 @@ void MessagePort::set_worker_event_target(GC::Ref<DOM::EventTarget> target)
}
// https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports:transfer-steps
WebIDL::ExceptionOr<void> MessagePort::transfer_steps(HTML::TransferDataHolder& data_holder)
WebIDL::ExceptionOr<void> MessagePort::transfer_steps(HTML::TransferDataEncoder& data_holder)
{
// 1. Set value's has been shipped flag to true.
m_has_been_shipped = true;
@ -102,23 +102,23 @@ WebIDL::ExceptionOr<void> MessagePort::transfer_steps(HTML::TransferDataHolder&
// 1. Set remotePort's has been shipped flag to true.
m_remote_port->m_has_been_shipped = true;
// 2. Set dataHolder.[[RemotePort]] to remotePort.
// TODO: Mach IPC
auto fd = MUST(m_transport->release_underlying_transport_for_transfer());
m_transport.clear();
data_holder.fds.append(IPC::File::adopt_fd(fd));
data_holder.data.append(IPC_FILE_TAG);
}
// 2. Set dataHolder.[[RemotePort]] to remotePort.
// TODO: Mach IPC
data_holder.encode(IPC_FILE_TAG);
data_holder.encode(IPC::File::adopt_fd(fd));
}
// 4. Otherwise, set dataHolder.[[RemotePort]] to null.
else {
data_holder.data.append(0);
data_holder.encode<u8>(0);
}
return {};
}
WebIDL::ExceptionOr<void> MessagePort::transfer_receiving_steps(HTML::TransferDataHolder& data_holder)
WebIDL::ExceptionOr<void> MessagePort::transfer_receiving_steps(HTML::TransferDataDecoder& data_holder)
{
// 1. Set value's has been shipped flag to true.
m_has_been_shipped = true;
@ -129,10 +129,9 @@ WebIDL::ExceptionOr<void> MessagePort::transfer_receiving_steps(HTML::TransferDa
// 3. If dataHolder.[[RemotePort]] is not null, then entangle dataHolder.[[RemotePort]] and value.
// (This will disentangle dataHolder.[[RemotePort]] from the original port that was transferred.)
auto fd_tag = data_holder.data.take_first();
if (fd_tag == IPC_FILE_TAG) {
if (auto fd_tag = data_holder.decode<u8>(); fd_tag == IPC_FILE_TAG) {
// TODO: Mach IPC
auto fd = data_holder.fds.take_first();
auto fd = data_holder.decode<IPC::File>();
m_transport = make<IPC::Transport>(MUST(Core::LocalSocket::adopt_fd(fd.take_fd())));
m_transport->set_up_read_hook([strong_this = GC::make_root(this)]() {