Navigation Menu

Skip to content

Commit

Permalink
Introduce rejectionhandled event
Browse files Browse the repository at this point in the history
  • Loading branch information
CYBAI committed Nov 13, 2018
1 parent b1a2b6b commit 23a4d64
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -65,6 +65,7 @@ radio
range
readystatechange
reftest-wait
rejectionhandled
reset
resize
right
Expand Down
2 changes: 1 addition & 1 deletion components/malloc_size_of/Cargo.toml
Expand Up @@ -32,7 +32,7 @@ hashglobe = { path = "../hashglobe" }
hyper = { version = "0.12", optional = true }
hyper_serde = { version = "0.9", optional = true }
keyboard-types = {version = "0.4.3", optional = true}
mozjs = { version = "0.9.3", optional = true }
mozjs = { version = "0.9.4", optional = true }
selectors = { path = "../selectors" }
serde = { version = "1.0.27", optional = true }
serde_bytes = { version = "0.10", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion components/script/Cargo.toml
Expand Up @@ -63,7 +63,7 @@ hyper_serde = "0.9"
image = "0.19"
ipc-channel = "0.11"
itertools = "0.7.6"
js = {package = "mozjs", version = "0.9.3"}
js = {package = "mozjs", version = "0.9.4"}
jstraceable_derive = {path = "../jstraceable_derive"}
keyboard-types = "0.4.3"
lazy_static = "1"
Expand Down
45 changes: 33 additions & 12 deletions components/script/script_runtime.rs
Expand Up @@ -41,6 +41,7 @@ use js::jsapi::{SetBuildIdOp, SetEnqueuePromiseJobCallback, SetPromiseRejectionT
use js::panic::wrap_panic;
use js::rust::wrappers::{GetPromiseIsHandled, GetPromiseResult};
use js::rust::Handle;
use js::rust::IntoHandle;
use js::rust::Runtime as RustRuntime;
use malloc_size_of::MallocSizeOfOps;
use msg::constellation_msg::PipelineId;
Expand Down Expand Up @@ -152,7 +153,7 @@ unsafe extern "C" fn enqueue_job(
)
}

#[allow(unsafe_code)]
#[allow(unsafe_code, unrooted_must_root)]
/// https://html.spec.whatwg.org/multipage/#the-hostpromiserejectiontracker-implementation
unsafe extern "C" fn promise_rejection_tracker(
cx: *mut JSContext,
Expand Down Expand Up @@ -190,14 +191,38 @@ unsafe extern "C" fn promise_rejection_tracker(
.borrow()
.contains(&Heap::boxed(promise.get()))
{
global.add_consumed_rejection(promise);
return;
}

// Step 5-3.
global.remove_consumed_rejection(promise);

// TODO: Step 5-4 - Queue a task to fire `rejectionhandled` event
let target = Trusted::new(global.upcast::<EventTarget>());
let promise = Promise::new_with_js_promise(Handle::from_raw(promise), cx);
let trusted_promise = TrustedPromise::new(promise.clone());

// Step 5-4.
global.dom_manipulation_task_source().queue(
task!(rejection_handled_event: move || {
let target = target.root();
let cx = target.global().get_cx();
let root_promise = trusted_promise.root();

rooted!(in(cx) let reason = GetPromiseResult(root_promise.reflector().get_jsobject()));

let event = PromiseRejectionEvent::new(
&target.global(),
atom!("rejectionhandled"),
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable,
root_promise,
reason.handle()
);

event.upcast::<Event>().fire(&target);
}),
global.upcast(),
).unwrap();
},
};
}),
Expand Down Expand Up @@ -254,7 +279,7 @@ pub fn notify_about_rejected_promises(global: &GlobalScope) {
atom!("unhandledrejection"),
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable,
promise,
promise.clone(),
reason.handle()
);

Expand All @@ -265,19 +290,15 @@ pub fn notify_about_rejected_promises(global: &GlobalScope) {
// TODO: The promise rejection is not handled; we need to add it back to the list.
}

// TODO: Step 4-4 - If [[PromiseIsHandled]] is false, add promise to consumed_rejections
// Step 4-4.
if !promise_is_handled {
target.global().add_consumed_rejection(promise.reflector().get_jsobject().into_handle());
}
}
}),
global.upcast(),
).unwrap();
}

if global.get_consumed_rejections().borrow().len() > 0 {
// FIXME(cybai): Implement `rejectionhandled` event instead of clearing the whole
// consumed rejections
// https://html.spec.whatwg.org/multipage/#the-hostpromiserejectiontracker-implementation
global.get_consumed_rejections().borrow_mut().clear();
}
}
}

Expand Down

0 comments on commit 23a4d64

Please sign in to comment.