Auto merge of #25276 - kunalmohan:25192-initMessageEvent, r=jdm

Implement MessageEvent.InitMessageEvent

<!-- Please describe your changes on the following line: -->
InitMessageEvent had to be implemented as required by wpt. For this few keys of struct `MessageEvent` are now wrapped inside DomRefCell wrapper.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] These changes fix #25192  (GitHub issue number if applicable)

<!-- Either: -->
- [X] There are tests for these changes

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2019-12-18 17:01:36 -05:00 committed by GitHub
commit f183d66217
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 39 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods; use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::MessageEventBinding; use crate::dom::bindings::codegen::Bindings::MessageEventBinding;
use crate::dom::bindings::codegen::Bindings::MessageEventBinding::MessageEventMethods; use crate::dom::bindings::codegen::Bindings::MessageEventBinding::MessageEventMethods;
@ -56,10 +57,10 @@ pub struct MessageEvent {
event: Event, event: Event,
#[ignore_malloc_size_of = "mozjs"] #[ignore_malloc_size_of = "mozjs"]
data: Heap<JSVal>, data: Heap<JSVal>,
origin: DOMString, origin: DomRefCell<DOMString>,
source: Option<SrcObject>, source: DomRefCell<Option<SrcObject>>,
lastEventId: DOMString, lastEventId: DomRefCell<DOMString>,
ports: Vec<DomRoot<MessagePort>>, ports: DomRefCell<Vec<DomRoot<MessagePort>>>,
} }
impl MessageEvent { impl MessageEvent {
@ -85,10 +86,10 @@ impl MessageEvent {
let ev = Box::new(MessageEvent { let ev = Box::new(MessageEvent {
event: Event::new_inherited(), event: Event::new_inherited(),
data: Heap::default(), data: Heap::default(),
source: source.map(|source| source.into()), source: DomRefCell::new(source.map(|source| source.into())),
origin, origin: DomRefCell::new(origin),
lastEventId, lastEventId: DomRefCell::new(lastEventId),
ports, ports: DomRefCell::new(ports),
}); });
let ev = reflect_dom_object(ev, global, MessageEventBinding::Wrap); let ev = reflect_dom_object(ev, global, MessageEventBinding::Wrap);
ev.data.set(data.get()); ev.data.set(data.get());
@ -187,12 +188,12 @@ impl MessageEventMethods for MessageEvent {
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-origin> /// <https://html.spec.whatwg.org/multipage/#dom-messageevent-origin>
fn Origin(&self) -> DOMString { fn Origin(&self) -> DOMString {
self.origin.clone() self.origin.borrow().clone()
} }
// https://html.spec.whatwg.org/multipage/#dom-messageevent-source // https://html.spec.whatwg.org/multipage/#dom-messageevent-source
fn GetSource(&self) -> Option<WindowProxyOrMessagePortOrServiceWorker> { fn GetSource(&self) -> Option<WindowProxyOrMessagePortOrServiceWorker> {
match &self.source { match &*self.source.borrow() {
Some(SrcObject::WindowProxy(i)) => Some( Some(SrcObject::WindowProxy(i)) => Some(
WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(&*i)), WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(&*i)),
), ),
@ -208,7 +209,7 @@ impl MessageEventMethods for MessageEvent {
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-lasteventid> /// <https://html.spec.whatwg.org/multipage/#dom-messageevent-lasteventid>
fn LastEventId(&self) -> DOMString { fn LastEventId(&self) -> DOMString {
self.lastEventId.clone() self.lastEventId.borrow().clone()
} }
/// <https://dom.spec.whatwg.org/#dom-event-istrusted> /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
@ -218,6 +219,28 @@ impl MessageEventMethods for MessageEvent {
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-ports> /// <https://html.spec.whatwg.org/multipage/#dom-messageevent-ports>
fn Ports(&self, cx: JSContext) -> JSVal { fn Ports(&self, cx: JSContext) -> JSVal {
message_ports_to_frozen_array(self.ports.as_slice(), cx) message_ports_to_frozen_array(self.ports.borrow().as_slice(), cx)
}
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-initmessageevent>
fn InitMessageEvent(
&self,
_cx: JSContext,
type_: DOMString,
bubbles: bool,
cancelable: bool,
data: HandleValue,
origin: DOMString,
lastEventId: DOMString,
source: Option<WindowProxyOrMessagePortOrServiceWorker>,
ports: Vec<DomRoot<MessagePort>>,
) {
self.data.set(data.get());
*self.origin.borrow_mut() = origin.clone();
*self.source.borrow_mut() = source.as_ref().map(|source| source.into());
*self.lastEventId.borrow_mut() = lastEventId.clone();
*self.ports.borrow_mut() = ports;
self.event
.init_event(Atom::from(type_), bubbles, cancelable);
} }
} }

View file

@ -11,6 +11,17 @@ interface MessageEvent : Event {
readonly attribute DOMString lastEventId; readonly attribute DOMString lastEventId;
readonly attribute MessageEventSource? source; readonly attribute MessageEventSource? source;
readonly attribute /*FrozenArray<MessagePort>*/any ports; readonly attribute /*FrozenArray<MessagePort>*/any ports;
void initMessageEvent(
DOMString type,
optional boolean bubbles = false,
optional boolean cancelable = false,
optional any data = null,
optional DOMString origin = "",
optional DOMString lastEventId = "",
optional MessageEventSource? source = null,
optional sequence<MessagePort> ports = []
);
}; };
dictionary MessageEventInit : EventInit { dictionary MessageEventInit : EventInit {

View file

@ -245,9 +245,6 @@
[SVGElement interface: attribute onchange] [SVGElement interface: attribute onchange]
expected: FAIL expected: FAIL
[MessageEvent interface: calling initMessageEvent(DOMString, boolean, boolean, any, USVString, DOMString, MessageEventSource, [object Object\]) on new MessageEvent("message", { data: 5 }) with too few arguments must throw TypeError]
expected: FAIL
[OffscreenCanvasRenderingContext2D interface: operation bezierCurveTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double)] [OffscreenCanvasRenderingContext2D interface: operation bezierCurveTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double)]
expected: FAIL expected: FAIL
@ -431,9 +428,6 @@
[BarProp interface: attribute visible] [BarProp interface: attribute visible]
expected: FAIL expected: FAIL
[MessageEvent interface: operation initMessageEvent(DOMString, boolean, boolean, any, USVString, DOMString, MessageEventSource, [object Object\])]
expected: FAIL
[DragEvent interface object name] [DragEvent interface object name]
expected: FAIL expected: FAIL
@ -551,9 +545,6 @@
[SVGElement interface: attribute onsuspend] [SVGElement interface: attribute onsuspend]
expected: FAIL expected: FAIL
[MessageEvent interface: new MessageEvent("message", { data: 5 }) must inherit property "initMessageEvent(DOMString, boolean, boolean, any, USVString, DOMString, MessageEventSource, [object Object\])" with the proper type]
expected: FAIL
[DragEvent interface: existence and properties of interface prototype object] [DragEvent interface: existence and properties of interface prototype object]
expected: FAIL expected: FAIL

View file

@ -92,9 +92,6 @@
[OffscreenCanvasRenderingContext2D interface: operation beginPath()] [OffscreenCanvasRenderingContext2D interface: operation beginPath()]
expected: FAIL expected: FAIL
[MessageEvent interface: calling initMessageEvent(DOMString, boolean, boolean, any, USVString, DOMString, MessageEventSource, [object Object\]) on new MessageEvent("message", { data: 5 }) with too few arguments must throw TypeError]
expected: FAIL
[OffscreenCanvasRenderingContext2D interface: attribute lineWidth] [OffscreenCanvasRenderingContext2D interface: attribute lineWidth]
expected: FAIL expected: FAIL
@ -152,9 +149,6 @@
[Path2D interface object name] [Path2D interface object name]
expected: FAIL expected: FAIL
[MessageEvent interface: operation initMessageEvent(DOMString, boolean, boolean, any, USVString, DOMString, MessageEventSource, [object Object\])]
expected: FAIL
[OffscreenCanvasRenderingContext2D interface: operation quadraticCurveTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double)] [OffscreenCanvasRenderingContext2D interface: operation quadraticCurveTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double)]
expected: FAIL expected: FAIL
@ -209,9 +203,6 @@
[Path2D interface: operation quadraticCurveTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double)] [Path2D interface: operation quadraticCurveTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double)]
expected: FAIL expected: FAIL
[MessageEvent interface: new MessageEvent("message", { data: 5 }) must inherit property "initMessageEvent(DOMString, boolean, boolean, any, USVString, DOMString, MessageEventSource, [object Object\])" with the proper type]
expected: FAIL
[OffscreenCanvasRenderingContext2D interface: attribute lineCap] [OffscreenCanvasRenderingContext2D interface: attribute lineCap]
expected: FAIL expected: FAIL

View file

@ -1,23 +1,14 @@
[messageevent-constructor.https.html] [messageevent-constructor.https.html]
type: testharness type: testharness
[Default event values]
expected: FAIL
[ports attribute should be a FrozenArray] [ports attribute should be a FrozenArray]
expected: FAIL expected: FAIL
[initMessageEvent operation]
expected: FAIL
[All parameters to initMessageEvent should be mandatory] [All parameters to initMessageEvent should be mandatory]
expected: FAIL expected: FAIL
[Passing ServiceWorker for source member] [Passing ServiceWorker for source member]
expected: FAIL expected: FAIL
[initMessageEvent operation default parameter values]
expected: FAIL
[MessageEvent constructor] [MessageEvent constructor]
expected: FAIL expected: FAIL