Skip to content

Commit

Permalink
Use safe JSContext in MicrotaskQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
marmeladema committed Aug 8, 2019
1 parent b18fa8b commit 6d444af
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 39 deletions.
18 changes: 6 additions & 12 deletions components/script/dom/globalscope.rs
Expand Up @@ -677,23 +677,17 @@ impl GlobalScope {
}

/// Perform a microtask checkpoint.
#[allow(unsafe_code)]
pub fn perform_a_microtask_checkpoint(&self) {
unsafe {
self.microtask_queue.checkpoint(
*self.get_cx(),
|_| Some(DomRoot::from_ref(self)),
vec![DomRoot::from_ref(self)],
);
}
self.microtask_queue.checkpoint(
self.get_cx(),
|_| Some(DomRoot::from_ref(self)),
vec![DomRoot::from_ref(self)],
);
}

/// Enqueue a microtask for subsequent execution.
#[allow(unsafe_code)]
pub fn enqueue_microtask(&self, job: Microtask) {
unsafe {
self.microtask_queue.enqueue(job, *self.get_cx());
}
self.microtask_queue.enqueue(job, self.get_cx());
}

/// Create a new sender/receiver pair that can be used to implement an on-demand
Expand Down
14 changes: 7 additions & 7 deletions components/script/microtask.rs
Expand Up @@ -14,9 +14,9 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlimageelement::ImageElementMicrotask;
use crate::dom::htmlmediaelement::MediaElementMicrotask;
use crate::dom::mutationobserver::MutationObserver;
use crate::script_runtime::notify_about_rejected_promises;
use crate::script_runtime::{notify_about_rejected_promises, JSContext};
use crate::script_thread::ScriptThread;
use js::jsapi::{JSContext, JobQueueIsEmpty, JobQueueMayNotBeEmpty};
use js::jsapi::{JobQueueIsEmpty, JobQueueMayNotBeEmpty};
use msg::constellation_msg::PipelineId;
use std::cell::Cell;
use std::mem;
Expand Down Expand Up @@ -56,17 +56,17 @@ impl MicrotaskQueue {
/// Add a new microtask to this queue. It will be invoked as part of the next
/// microtask checkpoint.
#[allow(unsafe_code)]
pub unsafe fn enqueue(&self, job: Microtask, cx: *mut JSContext) {
pub fn enqueue(&self, job: Microtask, cx: JSContext) {
self.microtask_queue.borrow_mut().push(job);
JobQueueMayNotBeEmpty(cx);
unsafe { JobQueueMayNotBeEmpty(*cx) };
}

/// <https://html.spec.whatwg.org/multipage/#perform-a-microtask-checkpoint>
/// Perform a microtask checkpoint, executing all queued microtasks until the queue is empty.
#[allow(unsafe_code)]
pub unsafe fn checkpoint<F>(
pub fn checkpoint<F>(
&self,
cx: *mut JSContext,
cx: JSContext,
target_provider: F,
globalscopes: Vec<DomRoot<GlobalScope>>,
) where
Expand All @@ -86,7 +86,7 @@ impl MicrotaskQueue {

for (idx, job) in pending_queue.iter().enumerate() {
if idx == pending_queue.len() - 1 && self.microtask_queue.borrow().is_empty() {
JobQueueIsEmpty(cx);
unsafe { JobQueueIsEmpty(*cx) };
}

match *job {
Expand Down
3 changes: 2 additions & 1 deletion components/script/script_runtime.rs
Expand Up @@ -172,14 +172,15 @@ unsafe extern "C" fn enqueue_promise_job(
_allocation_site: HandleObject,
incumbent_global: HandleObject,
) -> bool {
let cx = JSContext::from_ptr(cx);
wrap_panic(
AssertUnwindSafe(|| {
let microtask_queue = &*(extra as *const MicrotaskQueue);
let global = GlobalScope::from_object(incumbent_global.get());
let pipeline = global.pipeline_id();
microtask_queue.enqueue(
Microtask::Promise(EnqueuedPromiseCallback {
callback: PromiseJobCallback::new(JSContext::from_ptr(cx), job.get()),
callback: PromiseJobCallback::new(cx, job.get()),
pipeline,
}),
cx,
Expand Down
31 changes: 12 additions & 19 deletions components/script/script_thread.rs
Expand Up @@ -910,16 +910,13 @@ impl ScriptThread {
}

// https://html.spec.whatwg.org/multipage/#await-a-stable-state
#[allow(unsafe_code)]
pub fn await_stable_state(task: Microtask) {
SCRIPT_THREAD_ROOT.with(|root| {
if let Some(script_thread) = root.get() {
unsafe {
let script_thread = &*script_thread;
script_thread
.microtask_queue
.enqueue(task, *script_thread.get_cx());
}
let script_thread = unsafe { &*script_thread };
script_thread
.microtask_queue
.enqueue(task, script_thread.get_cx());
}
});
}
Expand Down Expand Up @@ -3776,17 +3773,15 @@ impl ScriptThread {
}
}

#[allow(unsafe_code)]
pub fn enqueue_microtask(job: Microtask) {
SCRIPT_THREAD_ROOT.with(|root| unsafe {
let script_thread = &*root.get().unwrap();
SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() };
script_thread
.microtask_queue
.enqueue(job, *script_thread.get_cx());
.enqueue(job, script_thread.get_cx());
});
}

#[allow(unsafe_code)]
fn perform_a_microtask_checkpoint(&self) {
let globals = self
.documents
Expand All @@ -3795,13 +3790,11 @@ impl ScriptThread {
.map(|(_id, document)| document.global())
.collect();

unsafe {
self.microtask_queue.checkpoint(
*self.get_cx(),
|id| self.documents.borrow().find_global(id),
globals,
)
}
self.microtask_queue.checkpoint(
self.get_cx(),
|id| self.documents.borrow().find_global(id),
globals,
)
}
}

Expand Down

0 comments on commit 6d444af

Please sign in to comment.