Skip to content
Permalink
Browse files

cache frozen array of ports

  • Loading branch information
gterzian committed Jan 11, 2020
1 parent e768ca1 commit 03396011282d527c0fc5e530925877902199e053
@@ -2,6 +2,7 @@
* 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/. */

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding;
use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding::ExtendableMessageEventMethods;
use crate::dom::bindings::error::Fallible;
@@ -32,6 +33,8 @@ pub struct ExtendableMessageEvent {
origin: DOMString,
lastEventId: DOMString,
ports: Vec<Dom<MessagePort>>,
#[ignore_malloc_size_of = "mozjs"]
frozen_ports: DomRefCell<Option<Heap<JSVal>>>,
}

impl ExtendableMessageEvent {
@@ -49,6 +52,7 @@ impl ExtendableMessageEvent {
.into_iter()
.map(|port| Dom::from_ref(&*port))
.collect(),
frozen_ports: DomRefCell::new(None),
}
}

@@ -141,11 +145,22 @@ impl ExtendableMessageEventMethods for ExtendableMessageEvent {

/// https://w3c.github.io/ServiceWorker/#extendablemessage-event-ports
fn Ports(&self, cx: JSContext) -> JSVal {
if let Some(ports) = &*self.frozen_ports.borrow() {
return ports.get();
}

let ports: Vec<DomRoot<MessagePort>> = self
.ports
.iter()
.map(|port| DomRoot::from_ref(&**port))
.collect();
to_frozen_array(ports.as_slice(), cx)
let frozen_ports = to_frozen_array(ports.as_slice(), cx);

// Cache the Js value.
let heap_val = Heap::default();
heap_val.set(frozen_ports);
*self.frozen_ports.borrow_mut() = Some(heap_val);

frozen_ports
}
}
@@ -61,6 +61,8 @@ pub struct MessageEvent {
source: DomRefCell<Option<SrcObject>>,
lastEventId: DomRefCell<DOMString>,
ports: DomRefCell<Vec<Dom<MessagePort>>>,
#[ignore_malloc_size_of = "mozjs"]
frozen_ports: DomRefCell<Option<Heap<JSVal>>>,
}

impl MessageEvent {
@@ -82,6 +84,7 @@ impl MessageEvent {
.map(|port| Dom::from_ref(&*port))
.collect(),
),
frozen_ports: DomRefCell::new(None),
}
}

@@ -238,13 +241,24 @@ impl MessageEventMethods for MessageEvent {

/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-ports>
fn Ports(&self, cx: JSContext) -> JSVal {
if let Some(ports) = &*self.frozen_ports.borrow() {
return ports.get();
}

let ports: Vec<DomRoot<MessagePort>> = self
.ports
.borrow()
.iter()
.map(|port| DomRoot::from_ref(&**port))
.collect();
to_frozen_array(ports.as_slice(), cx)
let frozen_ports = to_frozen_array(ports.as_slice(), cx);

// Cache the Js value.
let heap_val = Heap::default();
heap_val.set(frozen_ports);
*self.frozen_ports.borrow_mut() = Some(heap_val);

frozen_ports
}

/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-initmessageevent>
@@ -268,6 +282,7 @@ impl MessageEventMethods for MessageEvent {
.into_iter()
.map(|port| Dom::from_ref(&*port))
.collect();
*self.frozen_ports.borrow_mut() = None;
self.event
.init_event(Atom::from(type_), bubbles, cancelable);
}
@@ -1,7 +1,5 @@
[messageevent-constructor.https.html]
type: testharness
[ports attribute should be a FrozenArray]
expected: FAIL

[All parameters to initMessageEvent should be mandatory]
expected: FAIL

0 comments on commit 0339601

Please sign in to comment.
You can’t perform that action at this time.