LibWeb: Implement DedicatedWorkerGlobalScope postMessage(msg, transfer)

Unfortunately the added test (which passes locally) is skipped as it is
based off other Worker tests which are also skipped due to being flakey
in CI.
This commit is contained in:
Shannon Booth 2024-10-28 20:14:18 +13:00 committed by Andreas Kling
commit 755b63132b
Notes: github-actions[bot] 2024-10-28 22:33:18 +00:00
6 changed files with 43 additions and 2 deletions

View file

@ -15,3 +15,4 @@ Text/input/Worker/Worker-importScripts.html
Text/input/Worker/Worker-location.html Text/input/Worker/Worker-location.html
Text/input/Worker/Worker-module.html Text/input/Worker/Worker-module.html
Text/input/Worker/Worker-performance.html Text/input/Worker/Worker-performance.html
Text/input/Worker/Worker-postMessage-transfer.html

View file

@ -0,0 +1 @@
Message received from worker: Hello, world

View file

@ -0,0 +1,29 @@
<script src="../include.js"></script>
<script>
asyncTest((done) => {
const workerScript = `
self.onmessage = function(evt) {
const receivedBuffer = evt.data;
self.postMessage(receivedBuffer, [receivedBuffer]);
};
`;
const blob = new Blob([workerScript], { type: 'application/javascript' });
const workerScriptURL = URL.createObjectURL(blob);
const worker = new Worker(workerScriptURL);
worker.onmessage = function(evt) {
const bufTransferredBackFromWorker = evt.data;
const decoder = new TextDecoder();
println('Message received from worker: ' + decoder.decode(bufTransferredBackFromWorker));
done();
};
const encoder = new TextEncoder();
const message = encoder.encode("Hello, world");
const myBuf = message.buffer;
worker.postMessage(myBuf, [myBuf]);
});
</script>

View file

@ -48,6 +48,7 @@ void DedicatedWorkerGlobalScope::finalize()
WindowOrWorkerGlobalScopeMixin::finalize(); WindowOrWorkerGlobalScopeMixin::finalize();
} }
// https://html.spec.whatwg.org/multipage/workers.html#dom-dedicatedworkerglobalscope-postmessage-options
WebIDL::ExceptionOr<void> DedicatedWorkerGlobalScope::post_message(JS::Value message, StructuredSerializeOptions const& options) WebIDL::ExceptionOr<void> DedicatedWorkerGlobalScope::post_message(JS::Value message, StructuredSerializeOptions const& options)
{ {
// The postMessage(message, transfer) and postMessage(message, options) methods on DedicatedWorkerGlobalScope objects act as if, // The postMessage(message, transfer) and postMessage(message, options) methods on DedicatedWorkerGlobalScope objects act as if,
@ -56,6 +57,15 @@ WebIDL::ExceptionOr<void> DedicatedWorkerGlobalScope::post_message(JS::Value mes
return m_internal_port->post_message(message, options); return m_internal_port->post_message(message, options);
} }
// https://html.spec.whatwg.org/multipage/workers.html#dom-dedicatedworkerglobalscope-postmessage
WebIDL::ExceptionOr<void> DedicatedWorkerGlobalScope::post_message(JS::Value message, Vector<JS::Handle<JS::Object>> const& transfer)
{
// The postMessage(message, transfer) and postMessage(message, options) methods on DedicatedWorkerGlobalScope objects act as if,
// when invoked, it immediately invoked the respective postMessage(message, transfer) and postMessage(message, options)
// on the port, with the same arguments, and returned the same return value.
return m_internal_port->post_message(message, transfer);
}
#undef __ENUMERATE #undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name) \ #define __ENUMERATE(attribute_name, event_name) \
void DedicatedWorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \ void DedicatedWorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \

View file

@ -26,6 +26,7 @@ public:
virtual ~DedicatedWorkerGlobalScope() override; virtual ~DedicatedWorkerGlobalScope() override;
WebIDL::ExceptionOr<void> post_message(JS::Value message, StructuredSerializeOptions const&); WebIDL::ExceptionOr<void> post_message(JS::Value message, StructuredSerializeOptions const&);
WebIDL::ExceptionOr<void> post_message(JS::Value message, Vector<JS::Handle<JS::Object>> const& transfer);
void set_name(String name) { m_name = move(name); } void set_name(String name) { m_name = move(name); }
String name() const { return m_name; } String name() const { return m_name; }

View file

@ -5,8 +5,7 @@
interface DedicatedWorkerGlobalScope : WorkerGlobalScope { interface DedicatedWorkerGlobalScope : WorkerGlobalScope {
[Replaceable] readonly attribute DOMString name; [Replaceable] readonly attribute DOMString name;
// FIXME: IDL overload issue here undefined postMessage(any message, sequence<object> transfer);
// FIXME: undefined postMessage(any message, sequence<object> transfer);
undefined postMessage(any message, optional StructuredSerializeOptions options = {}); undefined postMessage(any message, optional StructuredSerializeOptions options = {});
undefined close(); undefined close();