mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Auto merge of #25468 - gterzian:add_generic_to_frozen_array, r=Manishearth
Add generic to frozen array <!-- Please describe your changes on the following line: --> https://github.com/servo/servo/pull/25467#discussion_r364575071 Depends on https://github.com/servo/servo/pull/25467 --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #___ (GitHub issue number if applicable) <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because ___ <!-- 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:
commit
dbba9ea453
4 changed files with 36 additions and 13 deletions
|
@ -10,10 +10,8 @@ use crate::dom::bindings::codegen::PrototypeList::{MAX_PROTO_CHAIN_LENGTH, PROTO
|
|||
use crate::dom::bindings::conversions::{jsstring_to_str, private_from_proto_check};
|
||||
use crate::dom::bindings::error::throw_invalid_this;
|
||||
use crate::dom::bindings::inheritance::TopTypeId;
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::bindings::trace::trace_object;
|
||||
use crate::dom::messageport::MessagePort;
|
||||
use crate::dom::windowproxy;
|
||||
use crate::script_runtime::JSContext as SafeJSContext;
|
||||
use js::conversions::{jsstr_to_string, ToJSValConvertible};
|
||||
|
@ -126,12 +124,9 @@ impl Clone for DOMJSClass {
|
|||
unsafe impl Sync for DOMJSClass {}
|
||||
|
||||
/// Returns a JSVal representing a frozen array of ports
|
||||
pub fn message_ports_to_frozen_array(
|
||||
message_ports: &[DomRoot<MessagePort>],
|
||||
cx: SafeJSContext,
|
||||
) -> JSVal {
|
||||
pub fn to_frozen_array<T: ToJSValConvertible>(convertibles: &[T], cx: SafeJSContext) -> JSVal {
|
||||
rooted!(in(*cx) let mut ports = UndefinedValue());
|
||||
unsafe { message_ports.to_jsval(*cx, ports.handle_mut()) };
|
||||
unsafe { convertibles.to_jsval(*cx, ports.handle_mut()) };
|
||||
|
||||
rooted!(in(*cx) let obj = ports.to_object());
|
||||
unsafe { JS_FreezeObject(*cx, RawHandleObject::from(obj.handle())) };
|
||||
|
|
|
@ -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;
|
||||
|
@ -10,7 +11,7 @@ use crate::dom::bindings::reflector::reflect_dom_object;
|
|||
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::bindings::trace::RootedTraceableBox;
|
||||
use crate::dom::bindings::utils::message_ports_to_frozen_array;
|
||||
use crate::dom::bindings::utils::to_frozen_array;
|
||||
use crate::dom::event::Event;
|
||||
use crate::dom::eventtarget::EventTarget;
|
||||
use crate::dom::extendableevent::ExtendableEvent;
|
||||
|
@ -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();
|
||||
message_ports_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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ use crate::dom::bindings::reflector::reflect_dom_object;
|
|||
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::bindings::trace::RootedTraceableBox;
|
||||
use crate::dom::bindings::utils::message_ports_to_frozen_array;
|
||||
use crate::dom::bindings::utils::to_frozen_array;
|
||||
use crate::dom::event::Event;
|
||||
use crate::dom::eventtarget::EventTarget;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
|
@ -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();
|
||||
message_ports_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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue