bun_runtime 0.1.6

Bao runtime integration — JS engine + Bun API + event loop
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
// @trace REQ-ENG-007
use ::std::ptr::NonNull;

use mozjs::jsapi::*;
use mozjs::jsval::{Int32Value, JSVal, ObjectValue, UndefinedValue};
use mozjs::rooted;
use mozjs::rust::wrappers2 as w2;

use crate::require::cache_builtin;

pub fn install(cx: &mut mozjs::context::JSContext) {
    rooted!(&in(cx) let timers_mod = unsafe { w2::JS_NewPlainObject(cx) });
    if timers_mod.get().is_null() {
        return;
    }

    unsafe {
        w2::JS_DefineFunction(
            cx,
            timers_mod.handle(),
            c"setTimeout".as_ptr(),
            Some(timers_set_timeout),
            2,
            0,
        );
        w2::JS_DefineFunction(
            cx,
            timers_mod.handle(),
            c"clearTimeout".as_ptr(),
            Some(timers_clear_timeout),
            1,
            0,
        );
        w2::JS_DefineFunction(
            cx,
            timers_mod.handle(),
            c"setInterval".as_ptr(),
            Some(timers_set_interval),
            2,
            0,
        );
        w2::JS_DefineFunction(
            cx,
            timers_mod.handle(),
            c"clearInterval".as_ptr(),
            Some(timers_clear_interval),
            1,
            0,
        );
        w2::JS_DefineFunction(
            cx,
            timers_mod.handle(),
            c"setImmediate".as_ptr(),
            Some(timers_set_immediate),
            1,
            0,
        );
        w2::JS_DefineFunction(
            cx,
            timers_mod.handle(),
            c"clearImmediate".as_ptr(),
            Some(timers_clear_immediate),
            1,
            0,
        );

        rooted!(&in(cx) let promises_obj = w2::JS_NewPlainObject(cx));
        if !promises_obj.get().is_null() {
            w2::JS_DefineFunction(
                cx,
                promises_obj.handle(),
                c"setTimeout".as_ptr(),
                Some(timers_promises_set_timeout),
                1,
                0,
            );
            w2::JS_DefineFunction(
                cx,
                promises_obj.handle(),
                c"setImmediate".as_ptr(),
                Some(timers_promises_set_immediate),
                0,
                0,
            );
            w2::JS_DefineFunction(
                cx,
                promises_obj.handle(),
                c"setInterval".as_ptr(),
                Some(timers_promises_set_interval),
                1,
                0,
            );

            rooted!(&in(cx) let scheduler_obj = w2::JS_NewPlainObject(cx));
            if !scheduler_obj.get().is_null() {
                w2::JS_DefineFunction(
                    cx,
                    scheduler_obj.handle(),
                    c"wait".as_ptr(),
                    Some(timers_promises_set_timeout),
                    1,
                    0,
                );
                w2::JS_DefineFunction(
                    cx,
                    scheduler_obj.handle(),
                    c"yield".as_ptr(),
                    Some(timers_promises_set_immediate),
                    0,
                    0,
                );
                rooted!(&in(cx) let sched_val = ObjectValue(scheduler_obj.get()));
                JS_DefineProperty(
                    cx.raw_cx(),
                    promises_obj.handle().into(),
                    c"scheduler".as_ptr(),
                    sched_val.handle().into(),
                    JSPROP_ENUMERATE as u32,
                );
            }

            rooted!(&in(cx) let prom_val = ObjectValue(promises_obj.get()));
            JS_DefineProperty(
                cx.raw_cx(),
                timers_mod.handle().into(),
                c"promises".as_ptr(),
                prom_val.handle().into(),
                JSPROP_ENUMERATE as u32,
            );

            cache_builtin(cx, "timers/promises", promises_obj.get());

            stamp_promisify_customs(cx, promises_obj.get());
        }
    }

    cache_builtin(cx, "timers", timers_mod.get());
}

/// domain-check a1f2e22140 (own-idiom fix, wiring v3): value-stamp the
/// `nodejs.util.promisify.custom` symbol on THIS realm's global timer
/// functions so util.promisify(setTimeout) IS timers/promises.setTimeout —
/// identity, not a wrapper (Node wires the same custom in lib/timers.js).
///
/// Stamp points (idempotent value property — safe to run on every realm):
///   1. `install` above (install_node_apis phase, right after the promises
///      object is cached) — covers the realm that runs the node segment;
///   2. `timers::install_timer_globals` (install_web_apis phase, per-realm)
///      — pulls the cached promises singleton via `get_builtin`, covering
///      LATER realms whose web segment runs after an earlier realm already
///      populated the module cache (cache miss = first realm before the
///      node segment: skip silently, point 1 carries it).
/// The consumer chain: native setTimeout ← this stamp; the async_hooks
/// timer wrapper (which REPLACES the global functions at its install) ←
/// forwards the custom symbol through a live getter onto the wrapped
/// original (node_async_hooks `_wrapTimerConstructor`), so the stamp stays
/// visible through the replacement that previously sank it.
///
/// v3 hardening (vs the bare-identifier v2):
///   1. Stamp TARGET is explicit: the factory reads `g.setTimeout` BY NAME
///      off the global handed in as an argument — no bare-identifier lexical
///      lookup that could resolve against a different global or hit the
///      timers_mod-local same-name function (a different JSFunction object;
///      identity would never hold).
///   2. Symbol family: `Symbol.for(...)` constructed inside the factory JS —
///      the exact same registered-symbol family the util.promisify probe
///      reads (dns×7 green is this contract working end-to-end).
///   3. Value property (plain assignment), no getter.
/// Args are array-backed (install_module_global precedent) — a former
/// single-element `&rooted.get() as *const Value` pointed at a destroyed
/// rvalue temporary (UB: the factory could receive garbage for its
/// parameter and throw, silently un-stamping). Every failure path prints
/// one stderr line — a silent no-op is exactly how the earlier wirings died
/// undetected.
pub fn stamp_promisify_customs(cx: &mut mozjs::context::JSContext, promises_obj: *mut JSObject) {
    const STAMP_SRC: &str = r#"(function(g, p) {
  var custom = Symbol.for('nodejs.util.promisify.custom');
  g.setTimeout[custom] = p.setTimeout;
  g.setInterval[custom] = p.setInterval;
  g.setImmediate[custom] = p.setImmediate;
})"#;
    unsafe {
        let mut stamp_js = mozjs::rust::transform_str_to_source_text(STAMP_SRC);
        let mut factory_val = UndefinedValue();
        let factory_h = MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut factory_val,
        };
        let opts =
            mozjs::glue::NewCompileOptions(cx.raw_cx(), c"<timers_promisify_custom>".as_ptr(), 1);
        if opts.is_null() {
            eprintln!("[node_timers_module] promisify-custom wiring skipped: no compile options");
            return;
        }
        let evaluated = JS::Evaluate2(cx.raw_cx(), opts, &mut stamp_js, factory_h);
        libc::free(opts as *mut _);
        if !evaluated || !factory_val.is_object() {
            eprintln!("[node_timers_module] promisify-custom wiring factory evaluation failed");
            return;
        }
        let global = CurrentGlobalOrNull(cx.raw_cx());
        if global.is_null() {
            eprintln!("[node_timers_module] promisify-custom wiring skipped: null global");
            return;
        }
        rooted!(&in(cx) let global_root = global);
        rooted!(&in(cx) let factory_obj = factory_val.to_object());
        rooted!(&in(cx) let factory_call_val = ObjectValue(factory_obj.get()));
        // Array-backed args: the slice outlives the call.
        let elems = [ObjectValue(global_root.get()), ObjectValue(promises_obj)];
        let args = HandleValueArray {
            length_: 2,
            elements_: elems.as_ptr(),
        };
        let mut call_rval = UndefinedValue();
        let called = JS_CallFunctionValue(
            cx.raw_cx(),
            global_root.handle().into(),
            factory_call_val.handle().into(),
            &args,
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut call_rval,
            },
        );
        if !called {
            // Drain and surface the pending exception, then clear it —
            // install must not abort, but the failure must stay observable.
            let mut exn = UndefinedValue();
            JS_GetPendingException(
                cx.raw_cx(),
                MutableHandle::<Value> {
                    _phantom_0: ::std::marker::PhantomData,
                    ptr: &mut exn,
                },
            );
            JS_ClearPendingException(cx.raw_cx());
            eprintln!("[node_timers_module] promisify-custom wiring call failed");
        }
    }
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_set_timeout(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 || !(*args.get(0).ptr).is_object() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let callback_root = (*args.get(0).ptr).to_object());
    let delay = if argc > 1 {
        let v = *args.get(1).ptr;
        if v.is_int32() {
            v.to_int32().max(0) as u64
        } else if v.is_double() {
            v.to_double().max(0.0) as u64
        } else {
            0
        }
    } else {
        0
    };

    let id = crate::timers::schedule_raw(cx, callback_root.get(), delay, false, &[]);
    args.rval().set(Int32Value(id as i32));
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_clear_timeout(_cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc > 0 {
        let v = *args.get(0).ptr;
        if v.is_int32() {
            crate::timers::cancel_raw(v.to_int32() as u32);
        }
    }
    args.rval().set(UndefinedValue());
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_set_interval(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 || !(*args.get(0).ptr).is_object() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let callback_root = (*args.get(0).ptr).to_object());
    let delay = if argc > 1 {
        let v = *args.get(1).ptr;
        if v.is_int32() {
            v.to_int32().max(1) as u64
        } else if v.is_double() {
            v.to_double().max(1.0) as u64
        } else {
            1
        }
    } else {
        1
    };

    let id = crate::timers::schedule_raw(cx, callback_root.get(), delay, true, &[]);
    args.rval().set(Int32Value(id as i32));
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_clear_interval(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    timers_clear_timeout(cx, argc, vp)
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_set_immediate(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 || !(*args.get(0).ptr).is_object() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let callback_root = (*args.get(0).ptr).to_object());
    let id = crate::timers::schedule_raw(cx, callback_root.get(), 0, false, &[]);
    args.rval().set(Int32Value(id as i32));
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_clear_immediate(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    timers_clear_timeout(cx, argc, vp)
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_promises_set_timeout(
    cx: *mut JSContext,
    argc: u32,
    vp: *mut JSVal,
) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let delay = if argc > 0 {
        let v = *args.get(0).ptr;
        if v.is_int32() {
            v.to_int32().max(0) as u64
        } else if v.is_double() {
            v.to_double().max(0.0) as u64
        } else {
            0
        }
    } else {
        0
    };

    let resolve_src = format!(
        "new Promise(function(resolve) {{ setTimeout(resolve, {}) }})",
        delay
    );
    let mut rval = UndefinedValue();
    let opts = mozjs::glue::NewCompileOptions(cx, c"timers_promises".as_ptr(), 1);
    if !opts.is_null() {
        let mut src = mozjs::rust::transform_str_to_source_text(&resolve_src);
        JS::Evaluate2(
            cx,
            opts,
            &mut src,
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut rval,
            },
        );
        libc::free(opts as *mut _);
    }
    args.rval().set(rval);
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_promises_set_immediate(
    cx: *mut JSContext,
    _argc: u32,
    vp: *mut JSVal,
) -> bool {
    let args = CallArgs::from_vp(vp, _argc);
    let mut rval = UndefinedValue();
    let opts = mozjs::glue::NewCompileOptions(cx, c"timers_promises".as_ptr(), 1);
    if !opts.is_null() {
        let mut src = mozjs::rust::transform_str_to_source_text(
            "new Promise(function(resolve) { setImmediate(resolve) })",
        );
        JS::Evaluate2(
            cx,
            opts,
            &mut src,
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut rval,
            },
        );
        libc::free(opts as *mut _);
    }
    args.rval().set(rval);
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn timers_promises_set_interval(
    cx: *mut JSContext,
    argc: u32,
    vp: *mut JSVal,
) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let delay = if argc > 0 {
        let v = *args.get(0).ptr;
        if v.is_int32() {
            v.to_int32().max(1) as u64
        } else if v.is_double() {
            v.to_double().max(1.0) as u64
        } else {
            1
        }
    } else {
        1
    };

    let resolve_src = format!(
        "new Promise(function(resolve) {{ setInterval(resolve, {}) }})",
        delay
    );
    let mut rval = UndefinedValue();
    let opts = mozjs::glue::NewCompileOptions(cx, c"timers_promises".as_ptr(), 1);
    if !opts.is_null() {
        let mut src = mozjs::rust::transform_str_to_source_text(&resolve_src);
        JS::Evaluate2(
            cx,
            opts,
            &mut src,
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut rval,
            },
        );
        libc::free(opts as *mut _);
    }
    args.rval().set(rval);
    true
}