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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use smallvec::SmallVec;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::time::{Duration, Instant};
/// Unique identifier for timers
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct TimerId(pub(crate) u64);
/// Type alias for fetch response data: (status, status_text, headers, body_data)
type FetchResponseData = (u16, String, Vec<(String, String)>, Vec<u8>);
struct Timer {
callback: v8::Global<v8::Function>,
fire_at: Instant,
interval: Option<Duration>,
}
/// Pending timer to be added
pub(crate) enum PendingTimer {
Timeout {
id: TimerId,
callback: v8::Global<v8::Function>,
delay_ms: u64,
},
Interval {
id: TimerId,
callback: v8::Global<v8::Function>,
interval_ms: u64,
},
}
pub(crate) struct EventLoop {
timers: BTreeMap<TimerId, Timer>,
timer_queue: BTreeMap<Instant, Vec<TimerId>>,
timers_to_clear: Rc<RefCell<Vec<TimerId>>>,
timers_to_add: Rc<RefCell<Vec<PendingTimer>>>,
pending_fetches: Rc<RefCell<Vec<crate::isolate_state::FetchRequest>>>,
active_dgram_sockets:
Rc<RefCell<rustc_hash::FxHashMap<u64, crate::isolate_state::ActiveDgramSocket>>>,
}
impl EventLoop {
pub(crate) fn new(
timers_to_clear: Rc<RefCell<Vec<TimerId>>>,
timers_to_add: Rc<RefCell<Vec<PendingTimer>>>,
_next_timer_id: Rc<RefCell<u64>>,
pending_fetches: Rc<RefCell<Vec<crate::isolate_state::FetchRequest>>>,
active_dgram_sockets: Rc<
RefCell<rustc_hash::FxHashMap<u64, crate::isolate_state::ActiveDgramSocket>>,
>,
) -> Self {
Self {
timers: BTreeMap::new(),
timer_queue: BTreeMap::new(),
timers_to_clear,
timers_to_add,
pending_fetches,
active_dgram_sockets,
}
}
/// Process pending timers that were queued for addition
#[inline]
fn add_pending_timers(&mut self) {
let mut pending_borrow = self.timers_to_add.borrow_mut();
if pending_borrow.is_empty() {
return;
}
let pending: SmallVec<[PendingTimer; 8]> = pending_borrow.drain(..).collect();
drop(pending_borrow);
for pending_timer in pending {
match pending_timer {
PendingTimer::Timeout {
id,
callback,
delay_ms,
} => {
let fire_at = Instant::now() + Duration::from_millis(delay_ms);
self.timers.insert(
id,
Timer {
callback,
fire_at,
interval: None,
},
);
self.timer_queue.entry(fire_at).or_default().push(id);
}
PendingTimer::Interval {
id,
callback,
interval_ms,
} => {
let interval = Duration::from_millis(interval_ms);
let fire_at = Instant::now() + interval;
self.timers.insert(
id,
Timer {
callback,
fire_at,
interval: Some(interval),
},
);
self.timer_queue.entry(fire_at).or_default().push(id);
}
}
}
}
/// Actually clear the marked timers
#[inline]
fn clear_marked_timers(&mut self) {
let mut to_clear_borrow = self.timers_to_clear.borrow_mut();
if to_clear_borrow.is_empty() {
return;
}
let to_clear: SmallVec<[TimerId; 8]> = to_clear_borrow.drain(..).collect();
drop(to_clear_borrow);
for id in to_clear {
if let Some(timer) = self.timers.remove(&id) {
// Remove from timer queue
if let Some(timers) = self.timer_queue.get_mut(&timer.fire_at) {
timers.retain(|&tid| tid != id);
if timers.is_empty() {
self.timer_queue.remove(&timer.fire_at);
}
}
}
}
}
/// Check if there are any pending timers, fetch requests, or active dgram sockets
pub(crate) fn has_pending_timers(&self) -> bool {
!self.timers.is_empty()
|| !self.pending_fetches.borrow().is_empty()
|| self.has_ref_dgram_sockets()
}
/// Check if there are any dgram sockets that are keeping the event loop alive
fn has_ref_dgram_sockets(&self) -> bool {
self.active_dgram_sockets
.borrow()
.values()
.any(|socket| socket.is_ref)
}
/// Get the next timer fire time
fn next_fire_time(&self) -> Option<Instant> {
self.timer_queue.keys().next().copied()
}
/// Process timers that are ready to fire
/// Returns the callbacks that should be executed
#[inline]
fn collect_ready_timers(&mut self) -> SmallVec<[(TimerId, v8::Global<v8::Function>, bool); 8]> {
let now = Instant::now();
let mut ready_callbacks = SmallVec::new();
// Collect all timers that should fire
let ready_times: SmallVec<[Instant; 8]> = self
.timer_queue
.keys()
.copied()
.take_while(|&time| time <= now)
.collect();
for fire_time in ready_times {
if let Some(timer_ids) = self.timer_queue.remove(&fire_time) {
for timer_id in timer_ids {
if let Some(timer) = self.timers.get(&timer_id) {
let is_interval = timer.interval.is_some();
ready_callbacks.push((timer_id, timer.callback.clone(), is_interval));
}
}
}
}
ready_callbacks
}
/// Reschedule an interval timer
#[inline]
fn reschedule_interval(&mut self, id: TimerId) {
if let Some(timer) = self.timers.get_mut(&id)
&& let Some(interval) = timer.interval
{
let new_fire_at = Instant::now() + interval;
timer.fire_at = new_fire_at;
self.timer_queue.entry(new_fire_at).or_default().push(id);
}
}
/// Poll dgram sockets for incoming data and invoke callbacks
#[inline]
fn poll_dgram_sockets(&self, scope: &mut v8::PinScope) {
let sockets_borrow = self.active_dgram_sockets.borrow();
if sockets_borrow.is_empty() {
return;
}
// Collect socket IDs to poll (we'll re-validate each one before use)
let socket_ids: SmallVec<[u64; 8]> = sockets_borrow.keys().copied().collect();
drop(sockets_borrow);
// Reuse buffer across all sockets (64KB max UDP packet size)
let mut buf = vec![0u8; 65536];
for socket_id in socket_ids {
// Re-borrow and validate the socket is still registered
// This prevents use-after-free if the socket was closed during callback execution
let socket_data = {
let sockets_borrow = self.active_dgram_sockets.borrow();
sockets_borrow
.get(&socket_id)
.map(|s| (s.socket_ptr, s.callback.clone()))
};
let Some((socket_ptr, callback)) = socket_data else {
continue;
};
if socket_ptr.is_null() {
continue;
}
// SAFETY: We only access the socket if it's still registered in active_dgram_sockets.
// The JS layer ensures unregister is called before close, and we re-validate
// registration before each access.
let socket = unsafe { &*socket_ptr };
// Try to receive data (non-blocking)
match socket.recv_from(&mut buf) {
Ok((size, addr)) => {
// Create Uint8Array with the received data
let data_slice = buf[..size].to_vec();
let backing_store =
v8::ArrayBuffer::new_backing_store_from_vec(data_slice).make_shared();
let array_buffer = v8::ArrayBuffer::with_backing_store(scope, &backing_store);
let uint8_array = v8::Uint8Array::new(scope, array_buffer, 0, size).unwrap();
// Create rinfo object
let rinfo = v8::Object::new(scope);
let address_key = v8::String::new(scope, "address").unwrap();
let address_value = v8::String::new(scope, &addr.ip().to_string()).unwrap();
rinfo.set(scope, address_key.into(), address_value.into());
let family_key = v8::String::new(scope, "family").unwrap();
let family_value = if addr.is_ipv4() {
v8::String::new(scope, "IPv4").unwrap()
} else {
v8::String::new(scope, "IPv6").unwrap()
};
rinfo.set(scope, family_key.into(), family_value.into());
let port_key = v8::String::new(scope, "port").unwrap();
let port_value = v8::Number::new(scope, addr.port() as f64);
rinfo.set(scope, port_key.into(), port_value.into());
let size_key = v8::String::new(scope, "size").unwrap();
let size_value = v8::Number::new(scope, size as f64);
rinfo.set(scope, size_key.into(), size_value.into());
// Call the callback with (data, rinfo)
let callback_local = v8::Local::new(scope, &callback);
let recv = v8::undefined(scope).into();
let _ = callback_local.call(scope, recv, &[uint8_array.into(), rinfo.into()]);
}
Err(e) => {
// For non-blocking sockets, WouldBlock means no data available - this is normal
if e.kind() != std::io::ErrorKind::WouldBlock {
// Log other errors but don't throw - the socket might still be usable
eprintln!("dgram recv error: {}", e);
}
}
}
}
}
/// Process pending fetch requests
#[inline]
fn process_fetches(&mut self, scope: &mut v8::PinScope) {
let mut fetches_borrow = self.pending_fetches.borrow_mut();
if fetches_borrow.is_empty() {
return;
}
let fetches: SmallVec<[crate::isolate_state::FetchRequest; 4]> =
fetches_borrow.drain(..).collect();
drop(fetches_borrow);
// Get the HTTP agent, next_stream_id, and header pool from isolate state
let isolate: &mut v8::Isolate = scope;
let state = crate::IsolateState::get(isolate);
let agent = state.borrow().http_agent.clone();
let next_stream_id_ref = state.borrow().next_stream_id.clone();
let header_pool = state.borrow().header_vec_pool.clone();
for fetch_request in fetches {
// Execute the HTTP request and get the response (but don't read body yet)
let result = Self::execute_fetch_streaming(
&agent,
&fetch_request.url,
&fetch_request.method,
&fetch_request.headers,
fetch_request.body.as_deref(),
);
// Return the request headers to the pool (cleared for reuse)
let mut headers = fetch_request.headers;
headers.clear();
header_pool.put(headers);
// Resolve the promise with the result
let resolver = v8::Local::new(scope, &fetch_request.resolver);
match result {
Ok((status, status_text, response_headers, body_data)) => {
// Allocate a stream ID for this fetch
let stream_id = {
let mut next_id = next_stream_id_ref.borrow_mut();
let id = *next_id;
*next_id += 1;
id
};
// Store the body data for streaming
let streaming_fetch = crate::isolate_state::StreamingFetch {
stream_id,
body_data,
offset: 0,
};
{
let isolate: &mut v8::Isolate = scope;
let state = crate::IsolateState::get(isolate);
let streaming_fetches = state.borrow().streaming_fetches.clone();
streaming_fetches
.borrow_mut()
.insert(stream_id, streaming_fetch);
}
// Create response object
let obj = v8::Object::new(scope);
// Get or create cached string keys
let isolate: &mut v8::Isolate = scope;
let state = crate::IsolateState::get(isolate);
let cache = state.borrow().string_cache.clone();
let mut cache_borrow = cache.borrow_mut();
// Set streamId
let stream_id_key = v8::String::new(scope, "streamId").unwrap();
let stream_id_value = v8::Number::new(scope, stream_id as f64);
obj.set(scope, stream_id_key.into(), stream_id_value.into());
// Set status (using cached string)
let status_key =
crate::get_or_create_cached_string!(scope, cache_borrow, status, "status");
let status_value = v8::Integer::new(scope, status as i32);
obj.set(scope, status_key.into(), status_value.into());
// Set statusText (using cached string)
let status_text_key = crate::get_or_create_cached_string!(
scope,
cache_borrow,
status_text,
"statusText"
);
let status_text_value = v8::String::new(scope, &status_text).unwrap();
obj.set(scope, status_text_key.into(), status_text_value.into());
// Set headers (using cached string)
let headers_key = crate::get_or_create_cached_string!(
scope,
cache_borrow,
headers,
"headers"
);
drop(cache_borrow);
let headers_len = response_headers.len() as i32;
let headers_array = v8::Array::new(scope, headers_len);
for (i, (key, value)) in response_headers.iter().enumerate() {
let entry = v8::Array::new(scope, 2);
let key_str = v8::String::new(scope, key).unwrap();
let value_str = v8::String::new(scope, value).unwrap();
entry.set_index(scope, 0, key_str.into());
entry.set_index(scope, 1, value_str.into());
headers_array.set_index(scope, i as u32, entry.into());
}
obj.set(scope, headers_key.into(), headers_array.into());
// Return response headers to pool (cleared for reuse)
let mut resp_headers = response_headers;
resp_headers.clear();
header_pool.put(resp_headers);
let _ = resolver.resolve(scope, obj.into());
}
Err(err) => {
let error_msg = v8::String::new(scope, &err).unwrap();
let error = v8::Exception::error(scope, error_msg);
let _ = resolver.reject(scope, error);
}
}
}
}
/// Execute an HTTP request using ureq (streaming version)
/// Returns (status, status_text, headers, body_data)
fn execute_fetch_streaming(
agent: &ureq::Agent,
url: &str,
method: &str,
headers: &[(String, String)],
body: Option<&str>,
) -> Result<FetchResponseData, String> {
// Build and execute the request based on method
let response = match method {
"GET" => {
let mut req = agent.get(url);
for (key, value) in headers {
req = req.header(key, value);
}
req.call()
}
"HEAD" => {
let mut req = agent.head(url);
for (key, value) in headers {
req = req.header(key, value);
}
req.call()
}
"DELETE" => {
let mut req = agent.delete(url);
for (key, value) in headers {
req = req.header(key, value);
}
req.call()
}
"POST" => {
let mut req = agent.post(url);
for (key, value) in headers {
req = req.header(key, value);
}
req.send(body.unwrap_or(""))
}
"PUT" => {
let mut req = agent.put(url);
for (key, value) in headers {
req = req.header(key, value);
}
req.send(body.unwrap_or(""))
}
"PATCH" => {
let mut req = agent.patch(url);
for (key, value) in headers {
req = req.header(key, value);
}
req.send(body.unwrap_or(""))
}
_ => return Err(format!("Unsupported HTTP method: {}", method)),
};
match response {
Ok(mut response) => {
let status_code = response.status();
let status = status_code.as_u16();
let status_text = status_code
.canonical_reason()
.unwrap_or("Unknown")
.to_string();
// Get headers - ureq 3.x uses http crate's HeaderMap
let headers_map = response.headers();
let mut response_headers = Vec::with_capacity(headers_map.len());
for (name, value) in headers_map {
if let Ok(value_str) = value.to_str() {
response_headers.push((name.as_str().to_string(), value_str.to_string()));
}
}
// Read the body into a vector
match response.body_mut().read_to_vec() {
Ok(body_data) => Ok((status, status_text, response_headers, body_data)),
Err(e) => Err(format!("Failed to read response body: {}", e)),
}
}
Err(err) => Err(format!("Network error: {}", err)),
}
}
/// Run the event loop until there are no more pending operations
pub(crate) fn run(&mut self, scope: &mut v8::PinScope) {
// First, add any pending timers
self.add_pending_timers();
while self.has_pending_timers() {
// Process pending fetch requests
self.process_fetches(scope);
// Poll dgram sockets for incoming data
self.poll_dgram_sockets(scope);
// Process all microtasks
scope.perform_microtask_checkpoint();
// Check again if we have pending operations after processing fetches
if !self.has_pending_timers() {
break;
}
// Determine sleep duration
let has_dgram_sockets = self.has_ref_dgram_sockets();
let sleep_duration = if has_dgram_sockets {
// When dgram sockets are active, use a short poll interval
// This allows us to check for incoming UDP data frequently
Some(Duration::from_millis(10))
} else if let Some(next_time) = self.next_fire_time() {
let now = Instant::now();
if next_time > now {
Some(next_time - now)
} else {
None
}
} else {
None
};
if let Some(duration) = sleep_duration {
std::thread::sleep(duration);
}
// Collect and execute ready timers
let ready_timers = self.collect_ready_timers();
for (timer_id, callback, is_interval) in ready_timers {
let callback_local = v8::Local::new(scope, &callback);
let recv = v8::undefined(scope).into();
let _ = callback_local.call(scope, recv, &[]);
if is_interval {
// Reschedule interval timers
self.reschedule_interval(timer_id);
} else {
// Remove one-shot timers
self.timers.remove(&timer_id);
}
}
// Clear any timers that were marked for clearing during callbacks
self.clear_marked_timers();
// Add any timers that were queued during callbacks
self.add_pending_timers();
}
// Final microtask checkpoint
scope.perform_microtask_checkpoint();
}
/// Process ready timers without blocking (suitable for REPL)
/// This method executes timers that are ready to fire and returns immediately
pub(crate) fn tick(&mut self, scope: &mut v8::PinScope) {
// Add any pending timers
self.add_pending_timers();
// Process pending fetch requests
self.process_fetches(scope);
// Poll dgram sockets for incoming data
self.poll_dgram_sockets(scope);
// Process all microtasks
scope.perform_microtask_checkpoint();
// Collect and execute ready timers (without sleeping)
let ready_timers = self.collect_ready_timers();
for (timer_id, callback, is_interval) in ready_timers {
let callback_local = v8::Local::new(scope, &callback);
let recv = v8::undefined(scope).into();
let _ = callback_local.call(scope, recv, &[]);
if is_interval {
// Reschedule interval timers
self.reschedule_interval(timer_id);
} else {
// Remove one-shot timers
self.timers.remove(&timer_id);
}
}
// Clear any timers that were marked for clearing during callbacks
self.clear_marked_timers();
// Add any timers that were queued during callbacks
self.add_pending_timers();
// Final microtask checkpoint
scope.perform_microtask_checkpoint();
}
}
impl Default for EventLoop {
fn default() -> Self {
Self::new(
Rc::new(RefCell::new(Vec::new())),
Rc::new(RefCell::new(Vec::new())),
Rc::new(RefCell::new(1)),
Rc::new(RefCell::new(Vec::new())),
Rc::new(RefCell::new(rustc_hash::FxHashMap::default())),
)
}
}
/// Get the event loop from the isolate state
pub(crate) fn get_event_loop(scope: &mut v8::PinScope) -> Rc<RefCell<EventLoop>> {
let isolate: &mut v8::Isolate = scope;
crate::IsolateState::get(isolate)
.borrow()
.event_loop
.clone()
}