rquickjs-core 0.15.0

High level bindings to the QuickJS JavaScript engine
Documentation
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
//! Exceptions thrown by queued jobs.
//!
//! QuickJS keeps a failing job's exception in a runtime-wide slot that the next
//! job can overwrite or consume. Every drain therefore catches the exception
//! immediately and keeps a copy in the runtime. [`JobError`] is a `Send` token
//! for that copy; [`JobError::into_value`] clones it out with a context from
//! the same runtime.

use alloc::collections::VecDeque;
use core::{
    fmt,
    mem::MaybeUninit,
    result::Result as StdResult,
    sync::atomic::{AtomicU64, Ordering},
};

use super::opaque::RuntimeOpaque;
use crate::{qjs, Ctx, Error, Persistent, Result, Value};

/// Process-wide ids so a token can never resolve to another runtime's error.
static NEXT_JOB_ERROR_ID: AtomicU64 = AtomicU64::new(0);

/// Retain enough failures that concurrently driven contexts can each keep one.
/// The oldest is evicted once the queue is full, so memory stays bounded.
pub(crate) const MAX_JOB_ERRORS: usize = 32;

/// Bounded FIFO of failed-job exceptions.
///
/// Every access happens while the runtime lock is held, so this needs no lock
/// of its own.
pub(crate) struct JobErrors {
    entries: VecDeque<Entry>,
}

impl Default for JobErrors {
    fn default() -> Self {
        Self::with_capacity(MAX_JOB_ERRORS)
    }
}

impl JobErrors {
    fn with_capacity(capacity: usize) -> Self {
        Self {
            entries: VecDeque::with_capacity(capacity),
        }
    }

    /// Push a failure, returning an evicted entry for the caller to drop after
    /// the borrow of `self` ends (a finalizer may re-enter `Opaque`).
    pub(crate) fn push(&mut self, entry: Entry) -> Option<Entry> {
        let evicted = if self.entries.len() == MAX_JOB_ERRORS {
            self.entries.pop_front()
        } else {
            None
        };
        self.entries.push_back(entry);
        evicted
    }

    /// Clone the exception with the given id out of the queue.
    pub(crate) fn get<'js>(&self, id: u64, ctx: &Ctx<'js>) -> Result<Value<'js>> {
        let entry = self
            .entries
            .iter()
            .find(|e| e.id == id)
            .ok_or(Error::JobErrorUnavailable)?;
        entry.value.clone().restore(ctx)
    }

    /// Move every entry out before `JS_FreeRuntime`.
    pub(crate) fn take_all(&mut self) -> VecDeque<Entry> {
        core::mem::take(&mut self.entries)
    }
}

/// A failed job's exception, kept inside its runtime.
///
/// Returned by [`Ctx::drain_jobs`](crate::Ctx::drain_jobs) and, with the
/// `"futures"` feature, by `AsyncContext::async_with`, `AsyncRuntime::idle`
/// and `AsyncRuntime::drive`.
///
/// The token carries no JavaScript value and is `Send`: the exception stays in
/// its runtime until it is evicted by an older-first FIFO (holding up to
/// [`MAX_JOB_ERRORS`](MAX_JOB_ERRORS) failures) or the runtime is dropped.
/// The value is cloned on extraction, so a token can be used more than once.
#[derive(Clone)]
pub struct JobError {
    pub(crate) id: u64,
}

impl fmt::Debug for JobError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JobError")
            .field("id", &self.id)
            .finish_non_exhaustive()
    }
}

impl fmt::Display for JobError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        "a queued job threw an exception".fmt(f)
    }
}

impl core::error::Error for JobError {}

impl JobError {
    /// Clone the exception out of the runtime.
    ///
    /// `ctx` must belong to the runtime the job ran in — not necessarily the
    /// same context. Ids are process-wide unique, so a context from another
    /// runtime cannot resolve to a different exception; [`Error::JobErrorUnavailable`]
    /// is returned instead. It is also returned if the failure was evicted
    /// from the bounded FIFO, or the runtime was dropped.
    pub fn into_value<'js>(&self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
        // SAFETY: a `Ctx` is only usable while the runtime lock is held, which
        // is what guards the runtime-owned queue.
        unsafe { ctx.get_runtime_opaque().take_job_error(self.id, ctx) }
    }
}

/// One retained exception.
pub(crate) struct Entry {
    pub(crate) id: u64,
    // SAFETY: `Persistent` erases the runtime borrow to `'static`. The entry is
    // dropped in `RuntimeOpaque::clear` or on eviction, before `JS_FreeRuntime`,
    // and `restore` rebinds a clone to a live `Ctx` from the same runtime.
    pub(crate) value: Persistent<Value<'static>>,
}

/// Execute one pending job, retaining the exception if it fails.
///
/// # Safety
/// `rt` must point to a live runtime whose lock is held.
pub(crate) unsafe fn execute_pending_job(rt: *mut qjs::JSRuntime) -> StdResult<bool, JobError> {
    let mut ctx_ptr = MaybeUninit::<*mut qjs::JSContext>::uninit();
    match qjs::JS_ExecutePendingJob(rt, ctx_ptr.as_mut_ptr()) {
        0 => Ok(false),
        1 => Ok(true),
        _ => {
            // QuickJS always reports the failing context on its error path.
            // Guard anyway so library code never builds a `Ctx` from null.
            let ctx_ptr = ctx_ptr.assume_init();
            if ctx_ptr.is_null() {
                return Ok(true);
            }
            Err(failed_job(rt, ctx_ptr))
        }
    }
}

/// # Safety
/// `rt` must point to a live runtime whose lock is held; `ctx` is the context
/// `JS_ExecutePendingJob` reported for a failed job.
unsafe fn failed_job(rt: *mut qjs::JSRuntime, ctx: *mut qjs::JSContext) -> JobError {
    let opaque = RuntimeOpaque::from_runtime_ptr(rt);
    let panic = opaque.take_panic();

    // `JS_ExecutePendingJob` hands back a borrowed context; dup it for the guard.
    let ctx = Ctx::from_ptr(ctx);
    let exception = qjs::JS_GetException(ctx.as_ptr());

    // A panicking Rust callback throws a `JS_TAG_EXCEPTION` sentinel. JS cannot
    // fabricate that tag, so it is the only failure the stashed payload belongs
    // to; resume it here instead of turning it into a JavaScript value.
    let is_panic = qjs::JS_VALUE_GET_NORM_TAG(exception) == qjs::JS_TAG_EXCEPTION;
    match panic {
        Some(payload) if is_panic => crate::util::resume_unwind(payload),
        Some(_) => {} // JS caught the synthetic exception; don't resume it later.
        None => {}
    }

    let value = Value::from_js_value(ctx.clone(), exception);
    let id = NEXT_JOB_ERROR_ID.fetch_add(1, Ordering::Relaxed);
    unsafe { opaque.capture_job_error(id, &ctx, value) }
}

#[cfg(test)]
mod test {
    use super::JobError;
    use crate::{function::Func, Context, Ctx, Error, Promise, Runtime};

    fn message_of(ctx: &Ctx<'_>, err: &JobError) -> String {
        err.clone()
            .into_value(ctx)
            .unwrap()
            .as_object()
            .unwrap()
            .get::<_, String>("message")
            .unwrap()
    }

    #[test]
    fn drain_returns_the_failed_microtask_exception() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('foobar'); })")
                .unwrap();
            let err = ctx.drain_jobs().unwrap_err();
            assert_eq!(message_of(&ctx, &err), "foobar");
        });
    }

    #[test]
    fn a_token_can_be_used_more_than_once() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('twice'); })")
                .unwrap();
            let err = ctx.drain_jobs().unwrap_err();
            assert_eq!(message_of(&ctx, &err), "twice");
            assert_eq!(message_of(&ctx, &err), "twice");
        });
    }

    #[test]
    fn multiple_failures_are_all_retained() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.eval::<(), _>(
                "queueMicrotask(() => { throw new Error('first'); });\
                 queueMicrotask(() => { throw new Error('second'); });",
            )
            .unwrap();

            let first = ctx.drain_jobs().unwrap_err();
            let second = ctx.drain_jobs().unwrap_err();
            assert_eq!(message_of(&ctx, &first), "first");
            assert_eq!(message_of(&ctx, &second), "second");
        });
    }

    #[test]
    fn a_live_handle_survives_later_drains() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('first'); })")
                .unwrap();
            let err = ctx.drain_jobs().unwrap_err();

            ctx.eval::<(), _>("queueMicrotask(() => { globalThis.ok = 1; })")
                .unwrap();
            ctx.drain_jobs().unwrap();

            assert_eq!(message_of(&ctx, &err), "first");
            assert_eq!(ctx.eval::<i32, _>("globalThis.ok").unwrap(), 1);
        });
    }

    #[cfg(feature = "parallel")]
    #[test]
    fn error_survives_a_drain_on_another_thread() {
        use std::thread;

        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('shared'); })")
                .unwrap();
        });
        let err = ctx.with(|ctx| ctx.drain_jobs()).unwrap_err();

        let ctx2 = ctx.clone();
        thread::scope(|scope| {
            scope.spawn(move || ctx2.with(|ctx| ctx.drain_jobs().unwrap()));
        });

        ctx.with(|ctx| assert_eq!(message_of(&ctx, &err), "shared"));
    }

    #[test]
    fn a_stale_pending_exception_is_not_a_job_error() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            let _ = ctx.eval::<(), _>("throw new Error('stale')");
            ctx.eval::<(), _>("queueMicrotask(() => { globalThis.ran = 1; })")
                .unwrap();
            ctx.drain_jobs().unwrap();
            assert_eq!(ctx.eval::<i32, _>("globalThis.ran").unwrap(), 1);
        });
    }

    #[test]
    fn into_value_works_from_another_context_of_the_same_runtime() {
        let rt = Runtime::new().unwrap();
        let err = {
            let ctx1 = Context::full(&rt).unwrap();
            ctx1.with(|ctx| {
                ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('shared'); })")
                    .unwrap();
                ctx.drain_jobs().unwrap_err()
            })
            // The failing context is dropped here; the retained value keeps it alive.
        };

        let ctx2 = Context::full(&rt).unwrap();
        ctx2.with(|ctx| {
            ctx.globals()
                .set("err", err.into_value(&ctx).unwrap())
                .unwrap();
            assert_eq!(
                ctx.eval::<std::string::String, _>("err.message").unwrap(),
                "shared"
            );
        });
    }

    #[test]
    fn a_stale_handle_cannot_resolve_in_a_new_runtime() {
        let err = {
            let rt1 = Runtime::new().unwrap();
            let ctx1 = Context::full(&rt1).unwrap();
            ctx1.with(|ctx| {
                ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('old'); })")
                    .unwrap();
                ctx.drain_jobs().unwrap_err()
            })
        };

        let rt2 = Runtime::new().unwrap();
        let ctx2 = Context::full(&rt2).unwrap();
        ctx2.with(|ctx| {
            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('new'); })")
                .unwrap();
            let _ = ctx.drain_jobs().unwrap_err();
            assert!(matches!(
                err.into_value(&ctx),
                Err(Error::JobErrorUnavailable)
            ));
        });
    }

    #[test]
    fn into_value_rejects_an_unrelated_runtime() {
        let rt1 = Runtime::new().unwrap();
        let ctx1 = Context::full(&rt1).unwrap();
        let err = ctx1.with(|ctx| {
            ctx.eval::<(), _>("queueMicrotask(() => { throw 3; })")
                .unwrap();
            ctx.drain_jobs().unwrap_err()
        });

        let rt2 = Runtime::new().unwrap();
        let ctx2 = Context::full(&rt2).unwrap();
        ctx2.with(|ctx| {
            assert!(matches!(
                err.into_value(&ctx),
                Err(Error::JobErrorUnavailable)
            ));
        });
    }

    #[test]
    fn oldest_errors_are_evicted_from_the_fifo() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            let mut errors = Vec::new();
            for i in 0..super::MAX_JOB_ERRORS {
                ctx.eval::<(), _>(format!(
                    "queueMicrotask(() => {{ throw new Error('e{i}'); }})"
                ))
                .unwrap();
                errors.push(ctx.drain_jobs().unwrap_err());
            }
            // All retained so far.
            assert_eq!(message_of(&ctx, &errors[0]), "e0");

            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('new'); })")
                .unwrap();
            let newest = ctx.drain_jobs().unwrap_err();

            // The oldest was evicted, the rest survived.
            assert!(matches!(
                errors[0].into_value(&ctx),
                Err(Error::JobErrorUnavailable)
            ));
            assert_eq!(message_of(&ctx, &errors[1]), "e1");
            assert_eq!(message_of(&ctx, &newest), "new");
        });
    }

    #[test]
    fn repeated_unread_errors_are_freed() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            for _ in 0..4 {
                ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('x'); })")
                    .unwrap();
                let _ = ctx.drain_jobs().unwrap_err();
            }
            ctx.drain_jobs().unwrap();
        });
    }

    #[test]
    fn an_unconsumed_handle_survives_runtime_teardown() {
        let err = {
            let rt = Runtime::new().unwrap();
            let ctx = Context::full(&rt).unwrap();
            ctx.with(|ctx| {
                ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('leak'); })")
                    .unwrap();
                ctx.drain_jobs().unwrap_err()
            })
        };
        let _ = err;
    }

    #[test]
    fn promise_finish_rethrows_a_failed_job() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            let promise: Promise = ctx.eval("new Promise(() => {})").unwrap();
            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('from job'); })")
                .unwrap();
            assert!(matches!(promise.finish::<()>(), Err(Error::Exception)));
            ctx.globals().set("err", ctx.catch()).unwrap();
            assert_eq!(ctx.eval::<String, _>("err.message").unwrap(), "from job");
        });
    }

    // wasm uses `panic=abort`, so `catch_unwind` cannot stash a panic payload
    // and `should_panic` tests are ignored by libtest there.
    #[cfg_attr(target_arch = "wasm32", ignore)]
    #[test]
    #[should_panic(expected = "boom")]
    fn a_panicking_job_is_resumed() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.globals()
                .set("boom", Func::from(|| -> () { panic!("boom") }))
                .unwrap();
            ctx.eval::<(), _>("queueMicrotask(boom)").unwrap();
            let _ = ctx.drain_jobs();
        });
    }

    #[cfg_attr(target_arch = "wasm32", ignore)]
    #[test]
    fn a_caught_panic_is_not_resumed_at_a_later_failure() {
        let rt = Runtime::new().unwrap();
        let ctx = Context::full(&rt).unwrap();
        ctx.with(|ctx| {
            ctx.globals()
                .set("boom", Func::from(|| -> () { panic!("boom") }))
                .unwrap();
            ctx.eval::<(), _>("queueMicrotask(() => { try { boom() } catch (e) {} })")
                .unwrap();
            ctx.drain_jobs().unwrap();

            ctx.eval::<(), _>("queueMicrotask(() => { throw new Error('later'); })")
                .unwrap();
            let err = ctx.drain_jobs().unwrap_err();
            assert_eq!(message_of(&ctx, &err), "later");
        });
    }
}