-
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathexecutor.rs
More file actions
162 lines (140 loc) · 5.53 KB
/
executor.rs
File metadata and controls
162 lines (140 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{
cell::RefCell,
collections::{BTreeMap, VecDeque},
mem,
rc::Rc,
};
use boa_engine::{
Context, JsResult,
context::time::JsInstant,
job::{GenericJob, Job, JobExecutor, NativeAsyncJob, PromiseJob, TimeoutJob},
};
use futures_concurrency::future::FutureGroup;
use futures_lite::{StreamExt, future};
use crate::{logger::SharedExternalPrinterLogger, uncaught_job_error};
pub(crate) struct Executor {
promise_jobs: RefCell<VecDeque<PromiseJob>>,
async_jobs: RefCell<VecDeque<NativeAsyncJob>>,
timeout_jobs: RefCell<BTreeMap<JsInstant, Vec<TimeoutJob>>>,
generic_jobs: RefCell<VecDeque<GenericJob>>,
finalization_registry_jobs: RefCell<VecDeque<NativeAsyncJob>>,
printer: SharedExternalPrinterLogger,
}
impl Executor {
pub(crate) fn new(printer: SharedExternalPrinterLogger) -> Self {
Self {
promise_jobs: RefCell::default(),
async_jobs: RefCell::default(),
timeout_jobs: RefCell::default(),
generic_jobs: RefCell::default(),
finalization_registry_jobs: RefCell::default(),
printer,
}
}
pub(crate) fn clear(&self) {
self.promise_jobs.borrow_mut().clear();
self.async_jobs.borrow_mut().clear();
self.timeout_jobs.borrow_mut().clear();
self.generic_jobs.borrow_mut().clear();
}
fn is_empty(&self) -> bool {
self.promise_jobs.borrow().is_empty()
&& self.async_jobs.borrow().is_empty()
&& self.timeout_jobs.borrow().is_empty()
&& self.generic_jobs.borrow().is_empty()
}
fn drain_timeout_jobs(&self, context: &mut Context) {
let now = context.clock().now();
let mut timeouts_borrow = self.timeout_jobs.borrow_mut();
let mut jobs_to_keep = timeouts_borrow.split_off(&now);
jobs_to_keep.retain(|_, jobs| {
jobs.retain(|job| !job.is_cancelled());
!jobs.is_empty()
});
let jobs_to_run = mem::replace(&mut *timeouts_borrow, jobs_to_keep);
drop(timeouts_borrow);
for jobs in jobs_to_run.into_values() {
for job in jobs {
if !job.is_cancelled()
&& let Err(e) = job.call(context)
{
self.printer.print(uncaught_job_error(&e));
}
}
}
}
fn drain_generic_jobs(&self, context: &mut Context) {
let job = self.generic_jobs.borrow_mut().pop_front();
if let Some(generic) = job
&& let Err(err) = generic.call(context)
{
self.printer.print(uncaught_job_error(&err));
}
}
}
impl JobExecutor for Executor {
fn enqueue_job(self: Rc<Self>, job: Job, context: &mut Context) {
match job {
Job::PromiseJob(job) => self.promise_jobs.borrow_mut().push_back(job),
Job::AsyncJob(job) => self.async_jobs.borrow_mut().push_back(job),
Job::TimeoutJob(job) => {
let now = context.clock().now();
self.timeout_jobs
.borrow_mut()
.entry(now + job.timeout())
.or_default()
.push(job);
}
Job::GenericJob(job) => self.generic_jobs.borrow_mut().push_back(job),
Job::FinalizationRegistryCleanupJob(job) => {
self.finalization_registry_jobs.borrow_mut().push_back(job);
}
job => self.printer.print(format!("unsupported job type {job:?}")),
}
}
fn run_jobs(self: Rc<Self>, context: &mut Context) -> JsResult<()> {
future::block_on(self.run_jobs_async(&RefCell::new(context)))
}
async fn run_jobs_async(self: Rc<Self>, context: &RefCell<&mut Context>) -> JsResult<()> {
let mut group = FutureGroup::new();
let mut fr_group = FutureGroup::new();
loop {
for job in mem::take(&mut *self.async_jobs.borrow_mut()) {
group.insert(job.call(context));
}
for job in mem::take(&mut *self.finalization_registry_jobs.borrow_mut()) {
fr_group.insert(job.call(context));
}
if let Some(Err(e)) = future::poll_once(group.next()).await.flatten() {
self.printer.print(uncaught_job_error(&e));
}
// We particularly want to make this check here such that the
// event loop is cancelled almost immediately after the channel with
// the reader gets closed.
if self.is_empty() && group.is_empty() {
// Run finalizers with a lower priority than every other type of
// job.
if let Some(Err(e)) = future::poll_once(fr_group.next()).await.flatten() {
self.printer.print(uncaught_job_error(&e));
}
// Finalizers could enqueue new jobs, so we cannot just exit.
if self.is_empty() {
return Ok(());
}
}
{
let context = &mut context.borrow_mut();
self.drain_timeout_jobs(context);
self.drain_generic_jobs(context);
let jobs = mem::take(&mut *self.promise_jobs.borrow_mut());
for job in jobs {
if let Err(e) = job.call(context) {
self.printer.print(uncaught_job_error(&e));
}
}
}
context.borrow_mut().clear_kept_objects();
future::yield_now().await;
}
}
}