bun_runtime 0.1.0

Bao runtime integration — JS engine + Bun API + event loop
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// @trace REQ-ENG-14 [api:Bun.password] — password hashing (argon2id/argon2i/argon2d/bcrypt).
//
// Bridges workspace `rust-argon2` and `bcrypt` crates to SpiderMonkey host
// functions. Bun.password is a namespace object with four methods:
//   - hash(password, options?)       → Promise<string>  (async)
//   - hashSync(password, options?)   → string           (sync)
//   - verify(password, hash, options?) → Promise<bool>  (async)
//   - verifySync(password, hash, options?) → bool       (sync)
//
// Async versions currently resolve synchronously (compute + resolve Promise
// immediately). They will become truly async once integrated with the event
// loop's microtask queue.

use ::std::borrow::Cow;
use ::std::ptr::{self, NonNull};

use mozjs::jsapi::*;
use mozjs::jsval::{BooleanValue, JSVal, ObjectValue, StringValue, UndefinedValue};
use mozjs::rooted;
use mozjs::rust::wrappers2::{JS_DefineFunction, JS_DefineProperty3, JS_NewPlainObject};

use bun_core::ZBox;

// ── Algorithm identifiers ──────────────────────────────────────────────

const ALGO_ARGON2ID: &str = "argon2id";
const ALGO_ARGON2I: &str = "argon2i";
const ALGO_ARGON2D: &str = "argon2d";
const ALGO_BCRYPT: &str = "bcrypt";

// ── Default parameters (matching Bun upstream) ─────────────────────────

/// argon2id defaults: time_cost=2, memory_cost=65536 KiB (64 MiB), parallelism=1
const ARGON2_DEFAULT_TIME_COST: u32 = 2;
const ARGON2_DEFAULT_MEMORY_COST: u32 = 65536; // KiB
const ARGON2_DEFAULT_PARALLELISM: u32 = 1;
const ARGON2_HASH_LENGTH: u32 = 32;
const ARGON2_SALT_LEN: usize = 32;

/// bcrypt default cost (log2 rounds)
const BCRYPT_DEFAULT_COST: u32 = 10;

// ── Verification safety limits (matching Bun upstream pwhash.rs) ───────

const MAX_VERIFY_TIME_COST: u32 = 1 << 16;
const MAX_VERIFY_MEMORY_COST: u32 = 1 << 22;
const MAX_VERIFY_PARALLELISM: u32 = 64;

// ── Install Bun.password namespace ─────────────────────────────────────

/// Install `Bun.password` namespace object on the given `bun_obj`.
///
/// # Safety
///
/// Caller must ensure `cx` is a valid JSContext pointer and `bun_obj` is a
/// valid handle to a JSObject.
pub unsafe fn install(
    cx: &mut mozjs::context::JSContext,
    bun_obj: mozjs::rust::Handle<*mut JSObject>,
) {
    rooted!(&in(cx) let pwd_obj = JS_NewPlainObject(cx));
    if pwd_obj.get().is_null() {
        return;
    }

    // Bun.password.hash(password, options?) → Promise<string>
    JS_DefineFunction(
        cx,
        pwd_obj.handle(),
        c"hash".as_ptr(),
        Some(pwd_hash),
        2,
        JSPROP_ENUMERATE as u32,
    );
    // Bun.password.hashSync(password, options?) → string
    JS_DefineFunction(
        cx,
        pwd_obj.handle(),
        c"hashSync".as_ptr(),
        Some(pwd_hash_sync),
        2,
        JSPROP_ENUMERATE as u32,
    );
    // Bun.password.verify(password, hash, options?) → Promise<bool>
    JS_DefineFunction(
        cx,
        pwd_obj.handle(),
        c"verify".as_ptr(),
        Some(pwd_verify),
        3,
        JSPROP_ENUMERATE as u32,
    );
    // Bun.password.verifySync(password, hash, options?) → bool
    JS_DefineFunction(
        cx,
        pwd_obj.handle(),
        c"verifySync".as_ptr(),
        Some(pwd_verify_sync),
        3,
        JSPROP_ENUMERATE as u32,
    );

    JS_DefineProperty3(
        cx,
        bun_obj,
        c"password".as_ptr(),
        pwd_obj.handle(),
        JSPROP_ENUMERATE as u32,
    );
}

// ── JS argument helpers ────────────────────────────────────────────────

/// Extract a JS string argument as Rust String. Returns None if not a string
/// or index out of bounds.
unsafe fn get_string_arg(
    cx: *mut JSContext,
    args: &CallArgs,
    index: u32,
    argc: u32,
) -> Option<String> {
    if index >= argc {
        return None;
    }
    let val = *args.get(index).ptr;
    if !val.is_string() {
        return None;
    }
    let ptr = val.to_string();
    NonNull::new(ptr).map(|nn| mozjs::conversions::unsafe_jsstr_to_string(cx, nn))
}

/// Algorithm and parameters parsed from the optional `options` argument.
struct HashOptions {
    algorithm: String,
    time_cost: u32,
    memory_cost: u32,
    parallelism: u32,
    cost: u32, // bcrypt cost (log2 rounds)
}

impl Default for HashOptions {
    fn default() -> Self {
        HashOptions {
            algorithm: ALGO_ARGON2ID.to_string(),
            time_cost: ARGON2_DEFAULT_TIME_COST,
            memory_cost: ARGON2_DEFAULT_MEMORY_COST,
            parallelism: ARGON2_DEFAULT_PARALLELISM,
            cost: BCRYPT_DEFAULT_COST,
        }
    }
}

/// Parse the optional second argument (options object or algorithm string).
unsafe fn parse_hash_options(cx: *mut JSContext, args: &CallArgs, argc: u32) -> HashOptions {
    let mut opts = HashOptions::default();

    if argc < 2 {
        return opts;
    }

    let val = *args.get(1).ptr;

    // Short form: Bun.password.hash("pw", "argon2id")
    if val.is_string() {
        let ptr = val.to_string();
        if let Some(nn) = NonNull::new(ptr) {
            opts.algorithm = mozjs::conversions::unsafe_jsstr_to_string(cx, nn);
        }
        return opts;
    }

    // Object form: Bun.password.hash("pw", { algorithm: "argon2id", ... })
    if val.is_object() {
        let obj = val.to_object();
        let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
        let cx_ref = &mut wrapped_cx;

        // algorithm
        if let Some(algo) = get_string_property(cx_ref, obj, "algorithm") {
            opts.algorithm = algo;
        }

        // timeCost
        if let Some(v) = get_uint_property(cx_ref, obj, "timeCost") {
            opts.time_cost = v;
        }

        // memoryCost
        if let Some(v) = get_uint_property(cx_ref, obj, "memoryCost") {
            opts.memory_cost = v;
        }

        // parallelism
        if let Some(v) = get_uint_property(cx_ref, obj, "parallelism") {
            opts.parallelism = v;
        }

        // cost (bcrypt)
        if let Some(v) = get_uint_property(cx_ref, obj, "cost") {
            opts.cost = v;
        }
    }

    opts
}

/// Parse verify options (third argument). Only `algorithm` is read;
/// if absent, the algorithm is auto-detected from the hash prefix.
unsafe fn parse_verify_options(cx: *mut JSContext, args: &CallArgs, argc: u32) -> Option<String> {
    if argc < 3 {
        return None;
    }
    let val = *args.get(2).ptr;

    if val.is_string() {
        let ptr = val.to_string();
        return NonNull::new(ptr).map(|nn| mozjs::conversions::unsafe_jsstr_to_string(cx, nn));
    }

    if val.is_object() {
        let obj = val.to_object();
        let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
        let cx_ref = &mut wrapped_cx;
        return get_string_property(cx_ref, obj, "algorithm");
    }

    None
}

/// Read a string property from a JS object.
/// Pattern matches node_fs.rs `get_hidden_int` / `define_*_prop` style.
unsafe fn get_string_property(
    cx: &mut mozjs::context::JSContext,
    obj: *mut JSObject,
    name: &str,
) -> Option<String> {
    let c_name = ZBox::from_bytes(name.as_bytes());
    rooted!(&in(cx) let obj_rooted = obj);
    let mut val = UndefinedValue();
    JS_GetProperty(
        cx.raw_cx(),
        obj_rooted.handle().into(),
        c_name.as_ptr(),
        MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut val,
        },
    );
    if !val.is_string() {
        return None;
    }
    let ptr = val.to_string();
    NonNull::new(ptr).map(|nn| mozjs::conversions::unsafe_jsstr_to_string(cx.raw_cx(), nn))
}

/// Read a u32 property from a JS object.
unsafe fn get_uint_property(
    cx: &mut mozjs::context::JSContext,
    obj: *mut JSObject,
    name: &str,
) -> Option<u32> {
    let c_name = ZBox::from_bytes(name.as_bytes());
    rooted!(&in(cx) let obj_rooted = obj);
    let mut val = UndefinedValue();
    JS_GetProperty(
        cx.raw_cx(),
        obj_rooted.handle().into(),
        c_name.as_ptr(),
        MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut val,
        },
    );
    if val.is_int32() {
        Some(val.to_int32() as u32)
    } else if val.is_double() {
        Some(val.to_double() as u32)
    } else {
        None
    }
}

/// Throw a JS Error with the given message.
unsafe fn throw_error(cx: *mut JSContext, msg: &str) {
    let c_msg = ZBox::from_bytes(msg.as_bytes());
    JS_ReportErrorUTF8(cx, c"%s".as_ptr(), c_msg.as_ptr());
}

/// Auto-detect algorithm from the hash string prefix.
fn detect_algorithm(hash: &str) -> &'static str {
    if hash.starts_with("$argon2") {
        if hash.starts_with("$argon2id$") {
            ALGO_ARGON2ID
        } else if hash.starts_with("$argon2i$") {
            ALGO_ARGON2I
        } else if hash.starts_with("$argon2d$") {
            ALGO_ARGON2D
        } else {
            ALGO_ARGON2ID
        }
    } else if hash.starts_with("$2") || hash.starts_with("$bcrypt$") {
        ALGO_BCRYPT
    } else {
        ALGO_ARGON2ID
    }
}

// ── Core hash/verify logic ─────────────────────────────────────────────

/// Compute a password hash. Returns the PHC-encoded (argon2) or modular-crypt
/// (bcrypt) hash string.
fn compute_hash(password: &str, opts: &HashOptions) -> ::std::result::Result<String, String> {
    match opts.algorithm.as_str() {
        ALGO_ARGON2ID | ALGO_ARGON2I | ALGO_ARGON2D => compute_argon2_hash(password, opts),
        ALGO_BCRYPT => compute_bcrypt_hash(password, opts),
        other => Err(format!("Unknown algorithm: {}", other)),
    }
}

fn compute_argon2_hash(
    password: &str,
    opts: &HashOptions,
) -> ::std::result::Result<String, String> {
    use argon2::{Config, ThreadMode, Variant, Version};

    let variant = match opts.algorithm.as_str() {
        ALGO_ARGON2ID => Variant::Argon2id,
        ALGO_ARGON2I => Variant::Argon2i,
        ALGO_ARGON2D => Variant::Argon2d,
        _ => Variant::Argon2id,
    };

    let config = Config {
        ad: &[],
        hash_length: ARGON2_HASH_LENGTH,
        lanes: opts.parallelism,
        mem_cost: opts.memory_cost,
        secret: &[],
        thread_mode: ThreadMode::Sequential,
        time_cost: opts.time_cost,
        variant,
        version: Version::Version13,
    };

    let mut salt = [0u8; ARGON2_SALT_LEN];
    getrandom::fill(&mut salt).map_err(|e| format!("Failed to generate salt: {}", e))?;

    argon2::hash_encoded(password.as_bytes(), &salt, &config)
        .map_err(|e| format!("argon2 hash failed: {}", e))
}

fn compute_bcrypt_hash(
    password: &str,
    opts: &HashOptions,
) -> ::std::result::Result<String, String> {
    bcrypt::hash(password, opts.cost).map_err(|e| format!("bcrypt hash failed: {}", e))
}

/// Verify a password against a hash. Returns true on match.
fn compute_verify(
    password: &str,
    hash: &str,
    algorithm: &str,
) -> ::std::result::Result<bool, String> {
    match algorithm {
        ALGO_ARGON2ID | ALGO_ARGON2I | ALGO_ARGON2D => compute_argon2_verify(password, hash),
        ALGO_BCRYPT => compute_bcrypt_verify(password, hash),
        other => Err(format!("Unknown algorithm: {}", other)),
    }
}

fn compute_argon2_verify(
    password: &str,
    encoded_hash: &str,
) -> ::std::result::Result<bool, String> {
    if !encoded_hash.is_ascii() {
        return Err("InvalidEncoding: hash must be ASCII".to_string());
    }

    // Normalise version: if no v= segment, splice in v=19$ so rust-argon2 uses
    // Version13 (matching Zig/Bun semantics). Also validate v=19 only.
    let normalised: Cow<'_, str> = 'norm: {
        let Some(after_dollar) = encoded_hash.strip_prefix('$') else {
            break 'norm Cow::Borrowed(encoded_hash);
        };
        let Some(sep) = after_dollar.find('$') else {
            break 'norm Cow::Borrowed(encoded_hash);
        };
        let alg_end = 1 + sep;
        let rest = &encoded_hash[alg_end + 1..];
        if let Some(v) = rest.strip_prefix("v=") {
            let end = v.find('$').unwrap_or(v.len());
            if &v[..end] != "19" {
                return Err("InvalidEncoding: only argon2 v=19 (0x13) is supported".to_string());
            }
            Cow::Borrowed(encoded_hash)
        } else {
            let mut s = String::with_capacity(encoded_hash.len() + 5);
            s.push_str(&encoded_hash[..=alg_end]);
            s.push_str("v=19$");
            s.push_str(rest);
            Cow::Owned(s)
        }
    };

    // Validate parameter safety limits before delegating to avoid DoS.
    if let Some(after_dollar) = normalised.strip_prefix('$') {
        if let Some(sep) = after_dollar.find('$') {
            let mut rest = &after_dollar[sep + 1..];
            if let Some(after_version) = rest.strip_prefix("v=") {
                rest = match after_version.find('$') {
                    Some(end) => &after_version[end + 1..],
                    None => "",
                };
            }
            let params = &rest[..rest.find('$').unwrap_or(rest.len())];
            for pair in params.split(',') {
                let Some((key, value)) = pair.split_once('=') else {
                    continue;
                };
                let Ok(value) = value.parse::<u32>() else {
                    continue;
                };
                let limit = match key {
                    "m" => MAX_VERIFY_MEMORY_COST,
                    "t" => MAX_VERIFY_TIME_COST,
                    "p" => MAX_VERIFY_PARALLELISM,
                    _ => continue,
                };
                if value > limit {
                    return Err(format!(
                        "WeakParameters: {}={} exceeds limit {}",
                        key, value, limit
                    ));
                }
            }
        }
    }

    match argon2::verify_encoded(&normalised, password.as_bytes()) {
        Ok(matched) => Ok(matched),
        Err(e) => Err(format!("argon2 verify failed: {}", e)),
    }
}

fn compute_bcrypt_verify(password: &str, hash: &str) -> ::std::result::Result<bool, String> {
    if !hash.is_ascii() {
        return Err("InvalidEncoding: hash must be ASCII".to_string());
    }
    match bcrypt::verify(password, hash) {
        Ok(matched) => Ok(matched),
        Err(e) => Err(format!("bcrypt verify failed: {}", e)),
    }
}

// ── Promise helpers ────────────────────────────────────────────────────
// Pattern follows node_fs.rs resolve_undefined / reject_with_error.

/// Create a new Promise, resolve it with a string value, and return it.
unsafe fn resolved_promise_string(cx: *mut JSContext, value: &str) -> *mut JSObject {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;

    rooted!(&in(cx_ref) let promise = mozjs_sys::jsapi::JS::NewPromiseObject(cx, HandleObject::null()));
    if promise.get().is_null() {
        return ptr::null_mut();
    }

    let c_val = ZBox::from_bytes(value.as_bytes());
    let js_str = JS_NewStringCopyZ(cx, c_val.as_ptr());
    if js_str.is_null() {
        return promise.get();
    }
    rooted!(&in(cx_ref) let val = StringValue(&*js_str));
    mozjs_sys::jsapi::JS::ResolvePromise(cx, promise.handle().into(), val.handle().into());

    promise.get()
}

/// Create a new Promise, resolve it with a boolean value, and return it.
unsafe fn resolved_promise_bool(cx: *mut JSContext, value: bool) -> *mut JSObject {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;

    rooted!(&in(cx_ref) let promise = mozjs_sys::jsapi::JS::NewPromiseObject(cx, HandleObject::null()));
    if promise.get().is_null() {
        return ptr::null_mut();
    }

    rooted!(&in(cx_ref) let val = BooleanValue(value));
    mozjs_sys::jsapi::JS::ResolvePromise(cx, promise.handle().into(), val.handle().into());

    promise.get()
}

/// Create a new Promise, reject it with an Error object, and return it.
unsafe fn rejected_promise(cx: *mut JSContext, msg: &str) -> *mut JSObject {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;

    rooted!(&in(cx_ref) let promise = mozjs_sys::jsapi::JS::NewPromiseObject(cx, HandleObject::null()));
    if promise.get().is_null() {
        return ptr::null_mut();
    }

    rooted!(&in(cx_ref) let err_obj = JS_NewPlainObject(cx_ref));
    if !err_obj.get().is_null() {
        let c_msg = ZBox::from_bytes(msg.as_bytes());
        let js_str = JS_NewStringCopyZ(cx, c_msg.as_ptr());
        if !js_str.is_null() {
            rooted!(&in(cx_ref) let msg_val = StringValue(&*js_str));
            JS_DefineProperty(
                cx,
                err_obj.handle().into(),
                c"message".as_ptr(),
                msg_val.handle().into(),
                JSPROP_ENUMERATE as u32,
            );
        }
    }
    rooted!(&in(cx_ref) let err_val = ObjectValue(err_obj.get()));
    mozjs_sys::jsapi::JS::RejectPromise(cx, promise.handle().into(), err_val.handle().into());

    promise.get()
}

// ── Host functions ─────────────────────────────────────────────────────

/// Bun.password.hash(password, options?) → Promise<string>
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn pwd_hash(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);

    let password = match get_string_arg(cx, &args, 0, argc) {
        Some(p) => p,
        None => {
            throw_error(cx, "Bun.password.hash requires a password string");
            return false;
        }
    };

    let opts = parse_hash_options(cx, &args, argc);

    match compute_hash(&password, &opts) {
        ::std::result::Result::Ok(hash) => {
            let promise = resolved_promise_string(cx, &hash);
            if promise.is_null() {
                args.rval().set(UndefinedValue());
            } else {
                args.rval().set(ObjectValue(promise));
            }
        }
        ::std::result::Result::Err(e) => {
            let promise = rejected_promise(cx, &e);
            if promise.is_null() {
                args.rval().set(UndefinedValue());
            } else {
                args.rval().set(ObjectValue(promise));
            }
        }
    }
    true
}

/// Bun.password.hashSync(password, options?) → string
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn pwd_hash_sync(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);

    let password = match get_string_arg(cx, &args, 0, argc) {
        Some(p) => p,
        None => {
            throw_error(cx, "Bun.password.hashSync requires a password string");
            return false;
        }
    };

    let opts = parse_hash_options(cx, &args, argc);

    match compute_hash(&password, &opts) {
        ::std::result::Result::Ok(hash) => {
            let c_hash = ZBox::from_bytes(hash.as_bytes());
            let js_str = JS_NewStringCopyZ(cx, c_hash.as_ptr());
            args.rval().set(if js_str.is_null() {
                UndefinedValue()
            } else {
                StringValue(&*js_str)
            });
        }
        ::std::result::Result::Err(e) => {
            throw_error(cx, &e);
            return false;
        }
    }
    true
}

/// Bun.password.verify(password, hash, options?) → Promise<bool>
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn pwd_verify(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);

    let password = match get_string_arg(cx, &args, 0, argc) {
        Some(p) => p,
        None => {
            throw_error(cx, "Bun.password.verify requires a password string");
            return false;
        }
    };

    let hash = match get_string_arg(cx, &args, 1, argc) {
        Some(h) => h,
        None => {
            throw_error(cx, "Bun.password.verify requires a hash string");
            return false;
        }
    };

    let algorithm = parse_verify_options(cx, &args, argc)
        .unwrap_or_else(|| detect_algorithm(&hash).to_string());

    match compute_verify(&password, &hash, &algorithm) {
        ::std::result::Result::Ok(matched) => {
            let promise = resolved_promise_bool(cx, matched);
            if promise.is_null() {
                args.rval().set(UndefinedValue());
            } else {
                args.rval().set(ObjectValue(promise));
            }
        }
        ::std::result::Result::Err(e) => {
            let promise = rejected_promise(cx, &e);
            if promise.is_null() {
                args.rval().set(UndefinedValue());
            } else {
                args.rval().set(ObjectValue(promise));
            }
        }
    }
    true
}

/// Bun.password.verifySync(password, hash, options?) → bool
#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn pwd_verify_sync(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);

    let password = match get_string_arg(cx, &args, 0, argc) {
        Some(p) => p,
        None => {
            throw_error(cx, "Bun.password.verifySync requires a password string");
            return false;
        }
    };

    let hash = match get_string_arg(cx, &args, 1, argc) {
        Some(h) => h,
        None => {
            throw_error(cx, "Bun.password.verifySync requires a hash string");
            return false;
        }
    };

    let algorithm = parse_verify_options(cx, &args, argc)
        .unwrap_or_else(|| detect_algorithm(&hash).to_string());

    match compute_verify(&password, &hash, &algorithm) {
        ::std::result::Result::Ok(matched) => {
            args.rval().set(BooleanValue(matched));
        }
        ::std::result::Result::Err(e) => {
            throw_error(cx, &e);
            return false;
        }
    }
    true
}