Skip to content

Commit

Permalink
feat(extensions): BroadcastChannel WPT conformance
Browse files Browse the repository at this point in the history
Still failing two tests. To be continued.
  • Loading branch information
bnoordhuis committed May 8, 2021
1 parent b7d86e4 commit f4ccb55
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
23 changes: 19 additions & 4 deletions extensions/broadcast_channel/01_broadcast_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
((window) => {
const core = window.Deno.core;
const webidl = window.__bootstrap.webidl;
const { setTarget } = window.__bootstrap.event;

const handlerSymbol = Symbol("eventHandlers");
function makeWrappedHandler(handler) {
Expand All @@ -21,7 +22,10 @@
// HTML specification section 8.1.5.1
Object.defineProperty(emitter, `on${name}`, {
get() {
return this[handlerSymbol]?.get(name)?.handler;
// TODO(bnoordhuis) The "BroadcastChannel should have an onmessage
// event" WPT test expects that .onmessage !== undefined. Returning
// null makes it pass but is perhaps not exactly in the spirit.
return this[handlerSymbol]?.get(name)?.handler ?? null;
},
set(value) {
if (!this[handlerSymbol]) {
Expand Down Expand Up @@ -81,14 +85,23 @@
throw new DOMException("Already closed", "InvalidStateError");
}

const prefix = "Failed to execute 'postMessage' on 'BroadcastChannel'";
webidl.requiredArguments(arguments.length, 1, { prefix });

if (typeof message === "function" || typeof message === "symbol") {
throw new DOMException("Uncloneable value", "DataCloneError");
}

core.opAsync("op_broadcast_send", this[_rid], core.serialize(message));
}

close() {
webidl.assertBranded(this, BroadcastChannel);

const closed = this[_closed];
this[_closed] = true;
core.close(this[_rid]);

if (!closed) core.close(this[_rid]);
}

async #eventLoop() {
Expand All @@ -102,11 +115,13 @@
return;
}

const location = window.location;

const event = new MessageEvent("message", {
data: core.deserialize(new Uint8Array(message)),
origin: window.location,
origin: location.protocol + "//" + location.host,
});
event.target = this;
setTarget(event, this);

this.dispatchEvent(event);
}
Expand Down
11 changes: 5 additions & 6 deletions extensions/broadcast_channel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,12 @@ pub async fn op_broadcast_next_event<B: BroadcastChannel + 'static>(
rid: ResourceId,
_bufs: Option<ZeroCopyBuf>,
) -> Result<Option<Vec<u8>>, AnyError> {
let resource = state
.borrow()
.resource_table
.get::<B::Resource>(rid)
.ok_or_else(bad_resource_id)?;
let resource = state.borrow().resource_table.get::<B::Resource>(rid);

B::next_event(&resource).await
match resource {
Some(resource) => B::next_event(&resource).await,
None => Ok(None),
}
}

pub fn init<B: BroadcastChannel + 'static>() -> Extension {
Expand Down
5 changes: 5 additions & 0 deletions extensions/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,10 @@
}

class MessageEvent extends Event {
get source() {
return null;
}

constructor(type, eventInitDict) {
super(type, {
bubbles: eventInitDict?.bubbles ?? false,
Expand Down Expand Up @@ -1208,5 +1212,6 @@
};
window.__bootstrap.event = {
setIsTrusted,
setTarget,
};
})(this);
29 changes: 20 additions & 9 deletions tools/wpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,21 +506,32 @@ function discoverTestsToRun(
continue;
}

for (
const [path, options] of entry.slice(
1,
) as ManifestTestVariation[]
) {
if (!path) continue;
const url = new URL(path, "http://web-platform.test:8000");
if (!url.pathname.endsWith(".any.html")) continue;
if (key.endsWith(".html")) {
const url = new URL(sourcePath, "http://web-platform.test:8000");
testsToRun.push({
sourcePath,
path: url.pathname + url.search,
url,
options,
options: {},
expectation,
});
} else {
for (
const [path, options] of entry.slice(
1,
) as ManifestTestVariation[]
) {
if (!path) continue;
const url = new URL(path, "http://web-platform.test:8000");
if (!url.pathname.endsWith(".any.html")) continue;
testsToRun.push({
sourcePath,
path: url.pathname + url.search,
url,
options,
expectation,
});
}
}
} else {
walk(entry, expectation, sourcePath);
Expand Down
6 changes: 6 additions & 0 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,12 @@
}
}
},
"webmessaging": {
"broadcastchannel": {
"basics.html": true,
"interface.html": true
}
},
"xhr": {
"formdata": {
"append.any.js": true,
Expand Down

0 comments on commit f4ccb55

Please sign in to comment.