Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MessagePort and MessageChannel #16622

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Accept transfer argument for StructuredCloneData::write

  • Loading branch information
KiChjang committed Jun 9, 2019
commit 74bcb9d9c627ed8fc8b6336905b5556ca8727607
@@ -255,7 +255,11 @@ pub enum StructuredCloneData {
impl StructuredCloneData {
// TODO: should this be unsafe?
/// Writes a structured clone. Returns a `DataClone` error if that fails.
pub fn write(cx: *mut JSContext, message: HandleValue) -> Fallible<StructuredCloneData> {
pub fn write(
cx: *mut JSContext,
message: HandleValue,
transfer: HandleValue,
) -> Fallible<StructuredCloneData> {
unsafe {
let scbuf = NewJSAutoStructuredCloneBuffer(
StructuredCloneScope::DifferentProcess,
@@ -274,7 +278,7 @@ impl StructuredCloneData {
policy,
&STRUCTURED_CLONE_CALLBACKS,
ptr::null_mut(),
HandleValue::undefined(),
transfer,
);
if !result {
JS_ClearPendingException(cx);
@@ -560,7 +560,8 @@ impl DedicatedWorkerGlobalScopeMethods for DedicatedWorkerGlobalScope {
#[allow(unsafe_code)]
// https://html.spec.whatwg.org/multipage/#dom-dedicatedworkerglobalscope-postmessage
unsafe fn PostMessage(&self, cx: *mut JSContext, message: HandleValue) -> ErrorResult {
let data = StructuredCloneData::write(cx, message)?;
rooted!(in(cx) let transfer = UndefinedValue());
let data = StructuredCloneData::write(cx, message, transfer.handle())?;
let worker = self.worker.borrow().as_ref().unwrap().clone();
let pipeline_id = self.upcast::<GlobalScope>().pipeline_id();
let task = Box::new(task!(post_worker_message: move || {
@@ -154,7 +154,8 @@ impl DissimilarOriginWindowMethods for DissimilarOriginWindow {

// Step 1-2, 6-8.
// TODO(#12717): Should implement the `transfer` argument.
let data = StructuredCloneData::write(cx, message)?;
rooted!(in(cx) let transfer = UndefinedValue());
let data = StructuredCloneData::write(cx, message, transfer.handle())?;

This comment has been minimized.

Copy link
@gterzian

gterzian Jun 26, 2018

Member

could the TODO on line 160 now be removed?

This comment has been minimized.

Copy link
@KiChjang

KiChjang Jul 21, 2018

Author Member

I don't think so, because the transfer argument is undefined. The proper way to do this would be to actually obtain them from the arguments passed to window.postMessage.

This comment has been minimized.

Copy link
@gterzian

gterzian Jul 22, 2018

Member

Ok, I see


// Step 9.
self.post_message(origin, data);
@@ -184,7 +184,8 @@ impl History {
// TODO: Step 4

// Step 5
let serialized_data = StructuredCloneData::write(cx, data)?.move_to_arraybuffer();
rooted!(in(cx) let transfer = UndefinedValue());
let serialized_data = StructuredCloneData::write(cx, data, transfer.handle())?.move_to_arraybuffer();

let new_url: ServoUrl = match url {
// Step 6
@@ -19,6 +19,7 @@ use crate::dom::globalscope::GlobalScope;
use crate::task::TaskOnce;
use dom_struct::dom_struct;
use js::jsapi::JSContext;
use js::jsval::UndefinedValue;
use js::rust::HandleValue;
use script_traits::{DOMMessage, ScriptMsg};
use servo_url::ServoUrl;
@@ -98,7 +99,8 @@ impl ServiceWorkerMethods for ServiceWorker {
return Err(Error::InvalidState);
}
// Step 7
let data = StructuredCloneData::write(cx, message)?;
rooted!(in(cx) let transfer = UndefinedValue());
let data = StructuredCloneData::write(cx, message, transfer.handle())?;
let msg_vec = DOMMessage(data.move_to_arraybuffer());
let _ = self
.global()
@@ -877,7 +877,8 @@ impl WindowMethods for Window {

// Step 1-2, 6-8.
// TODO(#12717): Should implement the `transfer` argument.
let data = StructuredCloneData::write(cx, message)?;
rooted!(in(cx) let transfer = UndefinedValue());
let data = StructuredCloneData::write(cx, message, transfer.handle())?;

// Step 9.
self.post_message(origin, &*source.window_proxy(), data);
@@ -160,7 +160,8 @@ impl WorkerMethods for Worker {
#[allow(unsafe_code)]
// https://html.spec.whatwg.org/multipage/#dom-worker-postmessage
unsafe fn PostMessage(&self, cx: *mut JSContext, message: HandleValue) -> ErrorResult {
let data = StructuredCloneData::write(cx, message)?;
rooted!(in(cx) let transfer = UndefinedValue());
let data = StructuredCloneData::write(cx, message, transfer.handle())?;
let address = Trusted::new(self);

// NOTE: step 9 of https://html.spec.whatwg.org/multipage/#dom-messageport-postmessage
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.