Skip to content
Permalink
Browse files

add a generic utility to convert a slice into a frozen array

  • Loading branch information
gterzian committed Jan 9, 2020
1 parent c506983 commit e768ca164336180dcb0b321888f5bb60bdc16b88
@@ -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())) };
@@ -10,7 +10,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;
@@ -146,6 +146,6 @@ impl ExtendableMessageEventMethods for ExtendableMessageEvent {
.iter()
.map(|port| DomRoot::from_ref(&**port))
.collect();
message_ports_to_frozen_array(ports.as_slice(), cx)
to_frozen_array(ports.as_slice(), cx)
}
}
@@ -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;
@@ -244,7 +244,7 @@ impl MessageEventMethods for MessageEvent {
.iter()
.map(|port| DomRoot::from_ref(&**port))
.collect();
message_ports_to_frozen_array(ports.as_slice(), cx)
to_frozen_array(ports.as_slice(), cx)
}

/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-initmessageevent>

0 comments on commit e768ca1

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