@@ -10,7 +10,7 @@ use std::{
1010 collections:: VecDeque ,
1111 sync:: { atomic:: AtomicBool , mpsc} ,
1212 thread,
13- time:: Duration ,
13+ time:: { Duration , Instant } ,
1414} ;
1515
1616use nova_vm:: ecmascript:: { HostHooks , Job } ;
@@ -19,7 +19,7 @@ use crate::{ChildToHostMessage, HostToChildMessage};
1919
2020pub struct CliChildHooks {
2121 promise_job_queue : RefCell < VecDeque < Job > > ,
22- macrotask_queue : RefCell < Vec < Job > > ,
22+ macrotask_queue : RefCell < Vec < ( Option < Instant > , Job ) > > ,
2323 pub ( crate ) receiver : mpsc:: Receiver < HostToChildMessage > ,
2424 pub ( crate ) host_sender : mpsc:: SyncSender < ChildToHostMessage > ,
2525 ready_to_leave : AtomicBool ,
@@ -78,9 +78,11 @@ impl CliChildHooks {
7878 let mut counter = 0u8 ;
7979 while !off_thread_job_queue. is_empty ( ) {
8080 counter = counter. wrapping_add ( 1 ) ;
81- for ( i, job) in off_thread_job_queue. iter ( ) . enumerate ( ) {
82- if job. is_finished ( ) {
83- let job = off_thread_job_queue. swap_remove ( i) ;
81+ let now = Instant :: now ( ) ;
82+ for ( i, ( deadline, job) ) in off_thread_job_queue. iter ( ) . enumerate ( ) {
83+ let deadline_reached = deadline. is_none_or ( |d| now >= d) ;
84+ if deadline_reached && job. is_finished ( ) {
85+ let ( _, job) = off_thread_job_queue. swap_remove ( i) ;
8486 return Some ( job) ;
8587 }
8688 }
@@ -96,14 +98,19 @@ impl CliChildHooks {
9698
9799impl HostHooks for CliChildHooks {
98100 fn enqueue_generic_job ( & self , job : Job ) {
99- self . macrotask_queue . borrow_mut ( ) . push ( job) ;
101+ self . macrotask_queue . borrow_mut ( ) . push ( ( None , job) ) ;
100102 }
101103
102104 fn enqueue_promise_job ( & self , job : Job ) {
103105 self . promise_job_queue . borrow_mut ( ) . push_back ( job) ;
104106 }
105107
106- fn enqueue_timeout_job ( & self , _timeout_job : Job , _milliseconds : u64 ) { }
108+ fn enqueue_timeout_job ( & self , timeout_job : Job , milliseconds : u64 ) {
109+ let deadline = Instant :: now ( ) + Duration :: from_millis ( milliseconds) ;
110+ self . macrotask_queue
111+ . borrow_mut ( )
112+ . push ( ( Some ( deadline) , timeout_job) ) ;
113+ }
107114
108115 fn get_host_data ( & self ) -> & dyn std:: any:: Any {
109116 self
0 commit comments