redjs 0.3.0

Full-control Rust embedding layer over Bun's JavaScriptCore prebuilt. C ABI designed for zero-alloc C# (NativeAOT) codegen bindings.
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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
//! Native `URL` + `URLSearchParams` backed by the `url` crate (rust-url is
//! the reference WHATWG URL implementation โ€” servo/deno use it), so parsing,
//! serialization and component setters follow the standard for free.
//!
//! URL instances are host objects carrying `Box<::url::Url>`; URLSearchParams
//! carry their pair list plus an optional protected link to the parent URL
//! (spec: the two stay in sync).
//!
//! `URL.createObjectURL` / `revokeObjectURL` keep a real blob URL store per
//! context. Everything in redjs that takes a URL resolves `blob:` through it:
//! `fetch`, an `Image`'s `src` and a `createImageBitmap` string (image.rs).
//! A host resolves them with [`resolve_blob_url`] (skathi: `<img>`,
//! `<video>`, `<lottie>`). The store is per context: once Workers exist, a
//! URL made in a worker will not resolve in the window. A browser has one
//! store per user agent, where it does โ€” a note for the Workers design.

use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};

use super::util::*;
use crate::glue;

pub struct UrlData {
    pub url: ::url::Url,
    /// Cached searchParams instance (protected), 0 until first access.
    pub usp: u64,
}

pub struct UspData {
    pub list: Vec<(String, String)>,
    /// Parent URL host object (protected) โ€” mutations serialize back into it.
    pub parent: u64,
}

unsafe fn this_url<'a>(g: *mut c_void, cf: *mut c_void) -> Option<&'a mut UrlData> {
    host_data::<UrlData>(g, state(g).url_class, cf_this(cf))
}

unsafe fn this_usp<'a>(g: *mut c_void, cf: *mut c_void) -> Option<&'a mut UspData> {
    host_data::<UspData>(g, state(g).usp_class, cf_this(cf))
}

// ---------------------------------------------------------------------------
// URL
// ---------------------------------------------------------------------------

#[crate::host_fn]
unsafe fn url_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    let st = state(g);
    let input = read_string(g, cf_arg(cf, 0)).unwrap_or_default();
    let base_v = cf_arg(cf, 1);

    let parsed = if base_v != UNDEF && base_v != NULL {
        let base_s = read_string(g, base_v).unwrap_or_default();
        match ::url::Url::parse(&base_s) {
            Ok(b) => b.join(&input),
            Err(e) => {
                return throw_type_error(g, &format!("URL constructor: invalid base URL \"{base_s}\": {e}"));
            }
        }
    } else {
        ::url::Url::parse(&input)
    };

    match parsed {
        Ok(u) => {
            let nt = cf_new_target(cf);
            let mut proto = if is_cell(nt) { get(g, nt, "prototype") } else { UNDEF };
            if !is_cell(proto) {
                proto = st.url.proto;
            }
            host_create(g, st.url_class, proto, UrlData { url: u, usp: 0 })
        }
        Err(e) => throw_type_error(g, &format!("URL constructor: \"{input}\" is not a valid URL: {e}")),
    }
}

macro_rules! url_getter {
    ($name:ident, |$u:ident| $body:expr) => {
        #[crate::host_fn]
        unsafe fn $name(g: *mut c_void, cf: *mut c_void) -> u64 {
            match this_url(g, cf) {
                Some(d) => {
                    let $u = &d.url;
                    let s: String = $body;
                    js_string(g, &s)
                }
                None => throw_type_error(g, "URL getter called on a non-URL"),
            }
        }
    };
}

url_getter!(get_href, |u| u.as_str().to_string());
url_getter!(get_protocol, |u| format!("{}:", u.scheme()));
url_getter!(get_hostname, |u| u.host_str().unwrap_or("").to_string());
url_getter!(get_port, |u| u.port().map(|p| p.to_string()).unwrap_or_default());
url_getter!(get_pathname, |u| u.path().to_string());
url_getter!(get_username, |u| u.username().to_string());
url_getter!(get_password, |u| u.password().unwrap_or("").to_string());
url_getter!(get_search, |u| match u.query() {
    Some(q) if !q.is_empty() => format!("?{q}"),
    _ => String::new(),
});
url_getter!(get_hash, |u| match u.fragment() {
    Some(f) if !f.is_empty() => format!("#{f}"),
    _ => String::new(),
});
url_getter!(get_host, |u| {
    let host = u.host_str().unwrap_or("");
    match u.port() {
        Some(p) => format!("{host}:{p}"),
        None => host.to_string(),
    }
});
url_getter!(get_origin, |u| u.origin().ascii_serialization());

unsafe fn arg_str(g: *mut c_void, cf: *mut c_void) -> String {
    read_string(g, cf_arg(cf, 0)).unwrap_or_default()
}

/// Keeps the cached searchParams list in sync after URL-side mutations (spec).
unsafe fn resync_usp(g: *mut c_void, d: &mut UrlData) {
    if d.usp != 0 {
        if let Some(usp) = host_data::<UspData>(g, state(g).usp_class, d.usp) {
            usp.list = d.url.query_pairs().map(|(k, v)| (k.into_owned(), v.into_owned())).collect();
        }
    }
}

#[crate::host_fn]
unsafe fn set_href(g: *mut c_void, cf: *mut c_void) -> u64 {
    let s = arg_str(g, cf);
    let Some(d) = this_url(g, cf) else {
        return throw_type_error(g, "URL setter called on a non-URL");
    };
    match ::url::Url::parse(&s) {
        Ok(u) => {
            d.url = u;
            resync_usp(g, d);
            UNDEF
        }
        Err(e) => throw_type_error(g, &format!("URL.href: invalid URL: {e}")),
    }
}

macro_rules! url_setter {
    ($name:ident, |$u:ident, $s:ident| $body:expr) => {
        #[crate::host_fn]
        unsafe fn $name(g: *mut c_void, cf: *mut c_void) -> u64 {
            let $s = arg_str(g, cf);
            let Some(d) = this_url(g, cf) else {
                return throw_type_error(g, "URL setter called on a non-URL");
            };
            {
                let $u = &mut d.url;
                let _ = $body; // spec: component setters ignore failures
            }
            resync_usp(g, d);
            UNDEF
        }
    };
}

url_setter!(set_protocol, |u, s| u.set_scheme(s.trim_end_matches(':')));
url_setter!(set_hostname, |u, s| u.set_host(if s.is_empty() { None } else { Some(&s) }));
url_setter!(set_port, |u, s| u.set_port(if s.is_empty() { None } else { s.parse().ok() }));
url_setter!(set_pathname, |u, s| {
    u.set_path(&s);
    Ok::<(), ()>(())
});
url_setter!(set_username, |u, s| u.set_username(&s));
url_setter!(set_password, |u, s| u.set_password(if s.is_empty() { None } else { Some(&s) }));
url_setter!(set_search, |u, s| {
    let q = s.strip_prefix('?').unwrap_or(&s);
    u.set_query(if q.is_empty() { None } else { Some(q) });
    Ok::<(), ()>(())
});
url_setter!(set_hash, |u, s| {
    let f = s.strip_prefix('#').unwrap_or(&s);
    u.set_fragment(if f.is_empty() { None } else { Some(f) });
    Ok::<(), ()>(())
});
url_setter!(set_host, |u, s| {
    // host = hostname[:port]
    if let Some((h, p)) = s.rsplit_once(':') {
        if let Ok(port) = p.parse::<u16>() {
            let _ = u.set_host(Some(h));
            let _ = u.set_port(Some(port));
            Ok(())
        } else {
            u.set_host(if s.is_empty() { None } else { Some(&s) })
        }
    } else {
        u.set_host(if s.is_empty() { None } else { Some(&s) })
    }
});

#[crate::host_fn]
unsafe fn url_to_string(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_url(g, cf) {
        Some(d) => {
            let s = d.url.as_str().to_string();
            js_string(g, &s)
        }
        None => throw_type_error(g, "URL.prototype.toString called on a non-URL"),
    }
}

#[crate::host_fn]
unsafe fn get_search_params(g: *mut c_void, cf: *mut c_void) -> u64 {
    let st = state(g);
    let Some(d) = this_url(g, cf) else {
        return throw_type_error(g, "URL.searchParams called on a non-URL");
    };
    if d.usp == 0 {
        let list = d.url.query_pairs().map(|(k, v)| (k.into_owned(), v.into_owned())).collect();
        let parent = cf_this(cf);
        let usp = host_create(g, st.usp_class, st.usp.proto, UspData { list, parent });
        crate::ffi::redjs_protect(g, usp);
        crate::ffi::redjs_protect(g, parent); // keep the pair alive together
        d.usp = usp;
    }
    d.usp
}

#[crate::host_fn]
unsafe fn can_parse(g: *mut c_void, cf: *mut c_void) -> u64 {
    let input = read_string(g, cf_arg(cf, 0)).unwrap_or_default();
    let base_v = cf_arg(cf, 1);
    let ok = if base_v != UNDEF && base_v != NULL {
        let base_s = read_string(g, base_v).unwrap_or_default();
        ::url::Url::parse(&base_s).map(|b| b.join(&input).is_ok()).unwrap_or(false)
    } else {
        ::url::Url::parse(&input).is_ok()
    };
    enc_bool(ok)
}

// ---------------------------------------------------------------------------
// Blob URLs (File API ยง8): the per-context store
// ---------------------------------------------------------------------------
//
// An entry holds the Blob's SOURCE โ€” the `Arc` of its bytes or its path, and
// its window โ€” never the Blob object: no JS value is protected, so the store
// cannot keep a heap alive, and a host consumer on another thread (sk-tree's
// `<img>`) can read it. The bytes stay alive until `revokeObjectURL`, or until
// the context goes, as in a browser.

/// What a blob URL resolves to.
#[derive(Clone, Debug)]
pub(crate) struct BlobEntry {
    pub range: super::blob::Range,
    pub mime: String,
}

#[derive(Default)]
struct BlobUrls {
    /// The origin part of this context's blob URLs, made on first use.
    origin: Option<String>,
    urls: HashMap<String, BlobEntry>,
}

fn blob_urls() -> &'static Mutex<HashMap<usize, BlobUrls>> {
    static S: OnceLock<Mutex<HashMap<usize, BlobUrls>>> = OnceLock::new();
    S.get_or_init(|| Mutex::new(HashMap::new()))
}

/// The store belonged to a context that is gone; its bytes go with it.
pub(crate) fn forget(gone: crate::context::Gone) {
    blob_urls().lock().unwrap().remove(&gone.key);
}

/// A random (version 4) UUID, lowercase โ€” the shape every browser uses.
fn uuid_v4() -> String {
    let mut b = [0u8; 16];
    if getrandom::fill(&mut b).is_err() {
        // Never a constant: two URLs must not collide. The clock and a counter
        // are unique per call, if not unpredictable.
        static N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        b[..8].copy_from_slice(&crate::web::now_ms().to_bits().to_le_bytes());
        b[8..].copy_from_slice(&N.fetch_add(1, std::sync::atomic::Ordering::Relaxed).to_le_bytes());
    }
    b[6] = (b[6] & 0x0f) | 0x40;
    b[8] = (b[8] & 0x3f) | 0x80;
    let h: String = b.iter().map(|x| format!("{x:02x}")).collect();
    format!("{}-{}-{}-{}-{}", &h[0..8], &h[8..12], &h[12..16], &h[16..20], &h[20..32])
}

/// The key a lookup uses: the parsed URL without its fragment (Chromium and
/// the URL Standard's "resolve a blob URL" both ignore it). `None` = not a
/// `blob:` URL at all.
fn store_key(url: &str) -> Option<String> {
    let mut u = ::url::Url::parse(url.trim()).ok()?;
    if u.scheme() != "blob" {
        return None;
    }
    u.set_fragment(None);
    Some(u.as_str().to_string())
}

/// File API "generate a new blob URL": `blob:` + the serialization of the
/// environment's origin + `/` + a UUID. The origin is this context's
/// `location.origin`, read once; where there is none, or it is opaque
/// (`"null"`), the spec lets the value be implementation-defined, and it is
/// `sk`.
unsafe fn origin_part(g: *mut c_void) -> String {
    if let Some(o) = blob_urls().lock().unwrap().get(&(g as usize)).and_then(|s| s.origin.clone()) {
        return o;
    }
    let loc = get(g, glue::rjs_global(g), "location");
    let o = if is_cell(loc) { read_string(g, get(g, loc, "origin")).unwrap_or_default() } else { String::new() };
    let o = if o.is_empty() || o == "null" { "sk".to_string() } else { o };
    blob_urls().lock().unwrap().entry(g as usize).or_default().origin = Some(o.clone());
    o
}

#[crate::host_fn]
unsafe fn create_object_url(g: *mut c_void, cf: *mut c_void) -> u64 {
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "Failed to execute 'createObjectURL' on 'URL': 1 argument required, but only 0 present.");
    }
    let origin = origin_part(g);
    // `(Blob or MediaSource)`: there is no MediaSource here.
    let Some(b) = super::blob::data_of(g, cf_arg(cf, 0)) else {
        return throw_type_error(g, "Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.");
    };
    let entry = BlobEntry { range: b.range(), mime: b.mime.clone() };
    let url = format!("blob:{origin}/{}", uuid_v4());
    let key = store_key(&url).unwrap_or_else(|| url.clone());
    blob_urls().lock().unwrap().entry(g as usize).or_default().urls.insert(key, entry);
    js_string(g, &url)
}

/// `revokeObjectURL(url)`: anything that is not a `blob:` URL of this
/// context's store is ignored, as the spec says โ€” never an error.
#[crate::host_fn]
unsafe fn revoke_object_url(g: *mut c_void, cf: *mut c_void) -> u64 {
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "Failed to execute 'revokeObjectURL' on 'URL': 1 argument required, but only 0 present.");
    }
    let Ok(s) = to_js_string(g, cf_arg(cf, 0)) else { return UNDEF };
    let url = read_string(g, s).unwrap_or_default();
    if let Some(key) = store_key(&url) {
        if let Some(st) = blob_urls().lock().unwrap().get_mut(&(g as usize)) {
            st.urls.remove(&key);
        }
    }
    UNDEF
}

/// This context's entry for `url` (fetch's blob scheme).
pub(crate) fn lookup(g: *mut c_void, url: &str) -> Option<BlobEntry> {
    let key = store_key(url)?;
    blob_urls().lock().unwrap().get(&(g as usize))?.urls.get(&key).cloned()
}

/// A blob URL, resolved for a HOST consumer (sk-tree's `<img>` / `<video>`).
#[derive(Clone, Debug, PartialEq)]
pub enum BlobTarget {
    /// A path-backed Blob or File: load it by path, without a copy. `range`
    /// is `Some((offset, len))` for a slice and `None` for the whole file.
    /// `size` / `mtime` are the snapshot its reads are checked against; see
    /// [`BlobTarget::snapshot_holds`].
    Path { path: std::path::PathBuf, range: Option<(u64, u64)>, size: u64, mtime: Option<std::time::SystemTime>, mime: String },
    /// Bytes in memory: the shared `Arc` and the window onto it.
    Bytes { bytes: std::sync::Arc<Vec<u8>>, offset: usize, len: usize, mime: String },
}

impl BlobTarget {
    pub fn mime(&self) -> &str {
        match self {
            BlobTarget::Path { mime, .. } | BlobTarget::Bytes { mime, .. } => mime,
        }
    }

    /// Is a path-backed target's file still the one its `File` was made from
    /// (same size and mtime)? A browser fails a load whose file changed โ€”
    /// the same rule `text()` applies. Always true for bytes.
    pub fn snapshot_holds(&self) -> bool {
        match self {
            BlobTarget::Path { path, size, mtime, .. } => {
                std::fs::metadata(path).is_ok_and(|m| m.is_file() && m.len() == *size && m.modified().ok() == *mtime)
            }
            BlobTarget::Bytes { .. } => true,
        }
    }
}

/// Resolves a `blob:` URL against context `g`'s store. `None` for a URL
/// that was never created here, was revoked, or names a Blob that could no
/// longer be read when it was made. Takes no JS lock and runs no JS: the
/// store is plain Rust data.
pub fn resolve_blob_url(g: *mut c_void, url: &str) -> Option<BlobTarget> {
    use super::blob::Source;
    let e = lookup(g, url)?;
    match e.range.source {
        Source::Disk(f) => {
            let whole = e.range.offset == 0 && e.range.len == f.size;
            Some(BlobTarget::Path {
                path: f.path.clone(),
                range: (!whole).then_some((e.range.offset, e.range.len)),
                size: f.size,
                mtime: f.mtime,
                mime: e.mime,
            })
        }
        Source::Memory(bytes) => {
            Some(BlobTarget::Bytes { bytes, offset: e.range.offset as usize, len: e.range.len as usize, mime: e.mime })
        }
        Source::Unreadable(_) => None,
    }
}

// ---------------------------------------------------------------------------
// URLSearchParams
// ---------------------------------------------------------------------------

fn parse_query(q: &str) -> Vec<(String, String)> {
    ::url::form_urlencoded::parse(q.as_bytes()).map(|(k, v)| (k.into_owned(), v.into_owned())).collect()
}

fn serialize_query(list: &[(String, String)]) -> String {
    let mut s = ::url::form_urlencoded::Serializer::new(String::new());
    for (k, v) in list {
        s.append_pair(k, v);
    }
    s.finish()
}

/// `GetMethod(v, @@iterator)`: `Ok(None)` for undefined / null, a TypeError
/// for anything else that cannot be called. An exception is left pending.
unsafe fn iterator_method(g: *mut c_void, v: u64) -> Result<Option<u64>, ()> {
    let a = glue::rjs_atom_iterator(g);
    let mut method = UNDEF;
    let rc = glue::rjs_get(g, v, a, &mut method);
    glue::rjs_atom_free(g, a);
    if rc != 0 {
        glue::rjs_throw(g, method);
        return Err(());
    }
    if method == UNDEF || method == NULL {
        return Ok(None);
    }
    if glue::rjs_kind(g, method) != 9 {
        throw_type_error(g, "Failed to construct 'URLSearchParams': @@iterator is not a function.");
        return Err(());
    }
    Ok(Some(method))
}

/// WebIDL `sequence<T>` through the iteration protocol, each element handed
/// to `each` as it comes (so nothing is held where the collector cannot see).
unsafe fn for_each_of(g: *mut c_void, v: u64, method: u64, mut each: impl FnMut(u64) -> Result<(), ()>) -> Result<(), ()> {
    let iter = call(g, method, v, &[]).map_err(|e| {
        glue::rjs_throw(g, e);
    })?;
    if !super::blob::is_object(g, iter) {
        throw_type_error(g, "Result of the Symbol.iterator method is not an object");
        return Err(());
    }
    let next = try_get(g, iter, "next")?;
    loop {
        let r = call(g, next, iter, &[]).map_err(|e| {
            glue::rjs_throw(g, e);
        })?;
        if !super::blob::is_object(g, r) {
            throw_type_error(g, "Iterator result is not an object");
            return Err(());
        }
        if glue::rjs_to_boolean(g, try_get(g, r, "done")?) != 0 {
            return Ok(());
        }
        each(try_get(g, r, "value")?)?;
    }
}

/// ToUSVString, rethrowing (a Symbol, a throwing `toString`).
unsafe fn usv(g: *mut c_void, v: u64) -> Result<String, ()> {
    let s = to_js_string(g, v)?;
    Ok(read_string(g, s).unwrap_or_default())
}

/// URL ยง6.2 "URLSearchParams(init)": `init` is a
/// `(sequence<sequence<USVString>> or record<USVString, USVString> or
/// USVString)`. An object with `@@iterator` (an array of pairs, a Map, another
/// URLSearchParams) is the sequence, any other object the record, anything
/// else a query string whose leading `?` is dropped.
unsafe fn usp_init(g: *mut c_void, init: u64) -> Result<Vec<(String, String)>, ()> {
    if init == UNDEF {
        return Ok(Vec::new());
    }
    if !super::blob::is_object(g, init) {
        let s = usv(g, init)?;
        return Ok(parse_query(s.strip_prefix('?').unwrap_or(&s)));
    }
    let mut list = Vec::new();
    if let Some(method) = iterator_method(g, init)? {
        for_each_of(g, init, method, |pair| {
            let not_a_sequence = || {
                throw_type_error(
                    g,
                    "Failed to construct 'URLSearchParams': The provided value cannot be converted to a sequence.",
                );
            };
            if !super::blob::is_object(g, pair) {
                not_a_sequence();
                return Err(());
            }
            let Some(inner) = iterator_method(g, pair)? else {
                not_a_sequence();
                return Err(());
            };
            let mut items: Vec<String> = Vec::new();
            for_each_of(g, pair, inner, |item| {
                items.push(usv(g, item)?);
                Ok(())
            })?;
            if items.len() != 2 {
                throw_type_error(
                    g,
                    "Failed to construct 'URLSearchParams': Sequence initializer must only contain pair elements",
                );
                return Err(());
            }
            let value = items.pop().unwrap_or_default();
            let name = items.pop().unwrap_or_default();
            list.push((name, value));
            Ok(())
        })?;
        return Ok(list);
    }
    // The record: own enumerable string keys, in [[OwnPropertyKeys]] order.
    let mut keys = UNDEF;
    if glue::rjs_own_keys(g, init, &mut keys) != 0 || !is_cell(keys) {
        // A Proxy's `ownKeys` trap threw: that exception is the answer.
        if keys != UNDEF {
            glue::rjs_throw(g, keys);
        } else {
            throw_type_error(g, "Failed to construct 'URLSearchParams': the record could not be enumerated.");
        }
        return Err(());
    }
    for i in 0..array_len(g, keys) {
        let k = get_i(g, keys, i);
        let name = usv(g, k)?;
        let value = usv(g, try_get(g, init, &name)?)?;
        list.push((name, value));
    }
    Ok(list)
}

#[crate::host_fn]
unsafe fn usp_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    let st = state(g);
    let Ok(list) = usp_init(g, cf_arg(cf, 0)) else { return UNDEF };
    let nt = cf_new_target(cf);
    let mut proto = if is_cell(nt) { get(g, nt, "prototype") } else { UNDEF };
    if !is_cell(proto) {
        proto = st.usp.proto;
    }
    host_create(g, st.usp_class, proto, UspData { list, parent: 0 })
}

/// Spec: USP mutations write back into the linked URL's query.
unsafe fn sync_parent(g: *mut c_void, d: &UspData) {
    if d.parent == 0 {
        return;
    }
    if let Some(pu) = host_data::<UrlData>(g, state(g).url_class, d.parent) {
        let q = serialize_query(&d.list);
        pu.url.set_query(if q.is_empty() { None } else { Some(&q) });
    }
}

unsafe fn usp_arg(g: *mut c_void, cf: *mut c_void, i: u32) -> String {
    read_string(g, cf_arg(cf, i)).unwrap_or_default()
}

#[crate::host_fn]
unsafe fn usp_get(g: *mut c_void, cf: *mut c_void) -> u64 {
    let k = usp_arg(g, cf, 0);
    match this_usp(g, cf) {
        Some(d) => match d.list.iter().find(|(key, _)| *key == k) {
            Some((_, v)) => {
                let v = v.clone();
                js_string(g, &v)
            }
            None => NULL,
        },
        None => throw_type_error(g, "URLSearchParams method called on wrong this"),
    }
}

#[crate::host_fn]
unsafe fn usp_get_all(g: *mut c_void, cf: *mut c_void) -> u64 {
    let k = usp_arg(g, cf, 0);
    let arr = new_array(g);
    if let Some(d) = this_usp(g, cf) {
        let values: Vec<String> = d.list.iter().filter(|(key, _)| *key == k).map(|(_, v)| v.clone()).collect();
        for (i, v) in values.iter().enumerate() {
            let sv = js_string(g, v);
            set_i(g, arr, i as u32, sv);
        }
    }
    arr
}

#[crate::host_fn]
unsafe fn usp_has(g: *mut c_void, cf: *mut c_void) -> u64 {
    let k = usp_arg(g, cf, 0);
    match this_usp(g, cf) {
        Some(d) => enc_bool(d.list.iter().any(|(key, _)| *key == k)),
        None => throw_type_error(g, "URLSearchParams method called on wrong this"),
    }
}

#[crate::host_fn]
unsafe fn usp_set(g: *mut c_void, cf: *mut c_void) -> u64 {
    let k = usp_arg(g, cf, 0);
    let v = usp_arg(g, cf, 1);
    if let Some(d) = this_usp(g, cf) {
        let mut replaced = false;
        d.list.retain_mut(|(key, val)| {
            if *key == k {
                if replaced {
                    return false;
                }
                *val = v.clone();
                replaced = true;
            }
            true
        });
        if !replaced {
            d.list.push((k, v));
        }
        sync_parent(g, d);
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn usp_append(g: *mut c_void, cf: *mut c_void) -> u64 {
    let k = usp_arg(g, cf, 0);
    let v = usp_arg(g, cf, 1);
    if let Some(d) = this_usp(g, cf) {
        d.list.push((k, v));
        sync_parent(g, d);
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn usp_delete(g: *mut c_void, cf: *mut c_void) -> u64 {
    let k = usp_arg(g, cf, 0);
    if let Some(d) = this_usp(g, cf) {
        d.list.retain(|(key, _)| *key != k);
        sync_parent(g, d);
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn usp_sort(g: *mut c_void, cf: *mut c_void) -> u64 {
    if let Some(d) = this_usp(g, cf) {
        d.list.sort_by(|a, b| a.0.cmp(&b.0));
        sync_parent(g, d);
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn usp_for_each(g: *mut c_void, cf: *mut c_void) -> u64 {
    let cb = cf_arg(cf, 0);
    let this_arg = cf_arg(cf, 1);
    let this_v = cf_this(cf);
    let pairs: Vec<(String, String)> = match this_usp(g, cf) {
        Some(d) => d.list.clone(),
        None => return throw_type_error(g, "URLSearchParams.forEach called on wrong this"),
    };
    for (k, v) in pairs {
        let kv = js_string(g, &k);
        let vv = js_string(g, &v);
        if call(g, cb, this_arg, &[vv, kv, this_v]).is_err() {
            break;
        }
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn usp_to_string(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_usp(g, cf) {
        Some(d) => {
            let s = serialize_query(&d.list);
            js_string(g, &s)
        }
        None => throw_type_error(g, "URLSearchParams.toString called on wrong this"),
    }
}

#[crate::host_fn]
unsafe fn usp_get_size(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_usp(g, cf) {
        Some(d) => enc_i32(d.list.len() as i32),
        None => throw_type_error(g, "URLSearchParams.size called on wrong this"),
    }
}

// ---- iteration protocol (entries/keys/values/[Symbol.iterator]) -------------
//
// Spec-shaped: the iterator walks the LIVE pair list by index (mutations during
// iteration are observed, like in browsers), and keeps its URLSearchParams
// alive through a private-symbol slot โ€” GC-visible, no protect bookkeeping.

pub struct UspIterData {
    idx: usize,
    kind: u8, // 0 = entries, 1 = keys, 2 = values
}

unsafe fn make_iter(g: *mut c_void, cf: *mut c_void, kind: u8) -> u64 {
    let st = state(g);
    let this_v = cf_this(cf);
    if host_data::<UspData>(g, st.usp_class, this_v).is_none() {
        return throw_type_error(g, "URLSearchParams iterator called on wrong this");
    }
    let it = host_create(g, st.usp_iter_class, st.usp_iter_proto, UspIterData { idx: 0, kind });
    glue::rjs_define(g, it, st.s_iter_target, this_v, 0);
    it
}

#[crate::host_fn]
unsafe fn usp_entries(g: *mut c_void, cf: *mut c_void) -> u64 {
    make_iter(g, cf, 0)
}

#[crate::host_fn]
unsafe fn usp_keys(g: *mut c_void, cf: *mut c_void) -> u64 {
    make_iter(g, cf, 1)
}

#[crate::host_fn]
unsafe fn usp_values(g: *mut c_void, cf: *mut c_void) -> u64 {
    make_iter(g, cf, 2)
}

#[crate::host_fn]
unsafe fn usp_iter_next(g: *mut c_void, cf: *mut c_void) -> u64 {
    let st = state(g);
    let this_v = cf_this(cf);
    let Some(it) = host_data::<UspIterData>(g, st.usp_iter_class, this_v) else {
        return throw_type_error(g, "next called on a non-URLSearchParams-iterator");
    };
    let target = get_a(g, this_v, st.s_iter_target);
    let result = new_object(g, UNDEF);

    let entry = host_data::<UspData>(g, st.usp_class, target).and_then(|d| {
        let e = d.list.get(it.idx).cloned();
        if e.is_some() {
            it.idx += 1;
        }
        e
    });
    match entry {
        Some((k, v)) => {
            let value = match it.kind {
                1 => js_string(g, &k),
                2 => js_string(g, &v),
                _ => {
                    let pair = new_array(g);
                    set_i(g, pair, 0, js_string(g, &k));
                    set_i(g, pair, 1, js_string(g, &v));
                    pair
                }
            };
            define(g, result, "value", value, 0);
            define(g, result, "done", enc_bool(false), 0);
        }
        None => {
            define(g, result, "value", UNDEF, 0);
            define(g, result, "done", enc_bool(true), 0);
        }
    }
    result
}

#[crate::host_fn]
unsafe fn iter_self(_g: *mut c_void, cf: *mut c_void) -> u64 {
    cf_this(cf)
}

// ---------------------------------------------------------------------------

pub unsafe fn install(
    g: *mut c_void,
) -> (crate::capi::JSClassRef, Class, crate::capi::JSClassRef, Class, crate::capi::JSClassRef, u64) {
    let usp_cls = make_host_class::<UspData>("URLSearchParams");
    let usp = define_class(g, &ClassSpec {
        name: "URLSearchParams",
        arity: 0,
        construct: Some(usp_construct),
        parent_proto: 0,
        methods: &[
            ("get", 1, usp_get),
            ("getAll", 1, usp_get_all),
            ("has", 1, usp_has),
            ("set", 2, usp_set),
            ("append", 2, usp_append),
            ("delete", 1, usp_delete),
            ("sort", 0, usp_sort),
            ("forEach", 1, usp_for_each),
            ("entries", 0, usp_entries),
            ("keys", 0, usp_keys),
            ("values", 0, usp_values),
            ("toString", 0, usp_to_string),
        ],
        getters: &[("size", usp_get_size)],
        accessors: &[],
    });

    // %URLSearchParamsIteratorPrototype%: next() + self-returning
    // [Symbol.iterator]; USP's own [Symbol.iterator] is `entries` (spec).
    let usp_iter_cls = make_host_class::<UspIterData>("URLSearchParams Iterator");
    let usp_iter_proto = new_object(g, UNDEF);
    crate::ffi::redjs_protect(g, usp_iter_proto);
    define(g, usp_iter_proto, "next", make_fn(g, "next", 0, usp_iter_next), 2);
    let it_atom = glue::rjs_atom_iterator(g);
    glue::rjs_define(g, usp_iter_proto, it_atom, make_fn(g, "[Symbol.iterator]", 0, iter_self), 2);
    glue::rjs_define(g, usp.proto, it_atom, get(g, usp.proto, "entries"), 2);
    glue::rjs_atom_free(g, it_atom);

    let url_cls = make_host_class::<UrlData>("URL");
    let url = define_class(g, &ClassSpec {
        name: "URL",
        arity: 1,
        construct: Some(url_construct),
        parent_proto: 0,
        methods: &[("toString", 0, url_to_string), ("toJSON", 0, url_to_string)],
        getters: &[("origin", get_origin), ("searchParams", get_search_params)],
        accessors: &[
            ("href", get_href, set_href),
            ("protocol", get_protocol, set_protocol),
            ("host", get_host, set_host),
            ("hostname", get_hostname, set_hostname),
            ("port", get_port, set_port),
            ("pathname", get_pathname, set_pathname),
            ("search", get_search, set_search),
            ("hash", get_hash, set_hash),
            ("username", get_username, set_username),
            ("password", get_password, set_password),
        ],
    });

    // Statics on the URL constructor.
    let n16 = |s: &str| -> u64 {
        let mut f = 0u64;
        let name16: Vec<u16> = s.encode_utf16().collect();
        glue::rjs_function_create(
            g,
            name16.as_ptr(),
            name16.len(),
            1,
            match s {
                "canParse" => can_parse as *const c_void,
                "createObjectURL" => create_object_url as *const c_void,
                _ => revoke_object_url as *const c_void,
            },
            &mut f,
        );
        f
    };
    define(g, url.ctor, "canParse", n16("canParse"), 2);
    define(g, url.ctor, "createObjectURL", n16("createObjectURL"), 2);
    define(g, url.ctor, "revokeObjectURL", n16("revokeObjectURL"), 2);

    (url_cls, url, usp_cls, usp, usp_iter_cls, usp_iter_proto)
}

#[cfg(test)]
mod jsc_tests {
    use super::{resolve_blob_url, BlobTarget};
    use crate::webapi::file::testing::*;
    use crate::webapi::testing::*;

    #[test]
    fn object_urls_have_the_file_api_shape_and_revoke() {
        let c = file_ctx();
        ok(&c, "(() => { const u = URL.createObjectURL(new Blob(['x'])); \
                 return /^blob:app:\\/\\/redjs\\/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(u) \
                   && u !== URL.createObjectURL(new Blob(['x'])); })()");
        assert_eq!(throws(&c, "URL.createObjectURL({})"), "Failed to execute 'createObjectURL' on 'URL': Overload resolution failed.");
        assert_eq!(throws(&c, "URL.createObjectURL()"), "Failed to execute 'createObjectURL' on 'URL': 1 argument required, but only 0 present.");
        assert_eq!(throws(&c, "URL.revokeObjectURL()"), "Failed to execute 'revokeObjectURL' on 'URL': 1 argument required, but only 0 present.");
        ok(&c, "URL.revokeObjectURL('https://example.com/') === undefined && URL.revokeObjectURL('not a url') === undefined");
        assert_eq!(
            run(&c, "const u = URL.createObjectURL(new Blob(['hello'], { type: 'text/x' })); \
                     const r = await fetch(u + '#frag'); \
                     const t = [r.status, r.statusText, r.ok, r.type, r.url === u, r.headers.get('content-type'), r.headers.get('content-length'), await r.text()].join(); \
                     URL.revokeObjectURL(u + '#other'); \
                     try { await fetch(u); return 'fetched after revoke'; } catch (e) { return t + '|' + e.name; }"),
            "200,OK,true,basic,true,text/x,5,hello|TypeError"
        );
        // An empty type is still a header (Fetch: `Content-Type` is always set).
        assert_eq!(run(&c, "const r = await fetch(URL.createObjectURL(new Blob([]))); return JSON.stringify(r.headers.get('content-type'));"), "\"\"");
        assert!(run(&c, "await fetch(URL.createObjectURL(new Blob(['x'])), { method: 'POST', body: 'y' });").contains("only be fetched with GET"));
    }

    #[test]
    fn a_range_request_gets_206_and_bad_ranges_are_network_errors() {
        let c = file_ctx();
        s(&c, "globalThis.u = URL.createObjectURL(new Blob(['0123456789'], { type: 'a/b' })); 0");
        let r = |range: &str| {
            run(&c, &format!(
                "const r = await fetch(u, {{ headers: {{ Range: '{range}' }} }}); \
                 return [r.status, r.headers.get('content-range'), r.headers.get('content-length'), await r.text()].join();"
            ))
        };
        assert_eq!(r("bytes=2-4"), "206,bytes 2-4/10,3,234");
        assert_eq!(r("bytes=7-"), "206,bytes 7-9/10,3,789");
        assert_eq!(r("bytes=-3"), "206,bytes 7-9/10,3,789");
        assert_eq!(r("bytes=-30"), "206,bytes 0-9/10,10,0123456789");
        assert_eq!(r("bytes=8-100"), "206,bytes 8-9/10,2,89");
        // allowWhitespace: tabs and spaces around `=` and the numbers
        // (WPT fetch/range/blob.any.js "with whitespace": `bytes= 9 - 21`).
        assert_eq!(r("bytes= 1-2"), "206,bytes 1-2/10,2,12");
        assert_eq!(r("bytes \\t= 2 \\t- 4"), "206,bytes 2-4/10,3,234");
        // Trailing whitespace never reaches the parser: a header value is normalized.
        assert_eq!(r("bytes=1-2 "), "206,bytes 1-2/10,2,12");
        // An end too large for any integer is clamped like any end past the Blob;
        // a start that large starts past it.
        assert_eq!(r("bytes=3-99999999999999999999999"), "206,bytes 3-9/10,7,3456789");
        assert_eq!(r("bytes=-99999999999999999999999"), "206,bytes 0-9/10,10,0123456789");
        for bad in [
            "bytes=10-",
            "bytes=5-2",
            "bytes=1-2,4-5",
            "items=1-2",
            "bytes=-",
            "bytes=1 2-3",
            "Bytes=1-2",
            "bytes=99999999999999999999999-",
        ] {
            assert!(r(bad).starts_with("ERROR: TypeError"), "{bad}");
        }
        // Two Range headers combine into a list, which is a network error.
        assert!(run(&c, "const h = new Headers(); h.append('Range', 'bytes=0-1'); h.append('Range', 'bytes=2-3'); \
                         await fetch(u, { headers: h });")
            .starts_with("ERROR: TypeError"));
    }

    #[test]
    fn a_path_backed_blob_url_resolves_by_path_and_fetches_off_thread() {
        let c = file_ctx();
        let t = TempFile::new("pic.png", b"\x89PNG not really");
        t.expose(&c, "picked");
        let whole = s(&c, "globalThis.whole = URL.createObjectURL(picked)");
        let part = s(&c, "globalThis.part = URL.createObjectURL(picked.slice(1, 4))");
        let mem = s(&c, "URL.createObjectURL(new Blob(['mem'], { type: 'text/plain' }))");
        let g = c.raw();
        match resolve_blob_url(g, &whole) {
            Some(BlobTarget::Path { path, range: None, size: 15, mime, .. }) => {
                assert_eq!(path, t.0);
                assert_eq!(mime, "image/png");
            }
            other => panic!("{other:?}"),
        }
        assert!(matches!(resolve_blob_url(g, &part), Some(BlobTarget::Path { range: Some((1, 3)), .. })));
        match resolve_blob_url(g, &(mem.clone() + "#x")) {
            Some(BlobTarget::Bytes { bytes, offset: 0, len: 3, mime }) => {
                assert_eq!(&bytes[..], b"mem");
                assert_eq!(mime, "text/plain");
            }
            other => panic!("{other:?}"),
        }
        assert!(resolve_blob_url(g, "blob:app://redjs/00000000-0000-4000-8000-000000000000").is_none());
        assert!(resolve_blob_url(g, &whole).unwrap().snapshot_holds());
        assert_eq!(run(&c, "return await (await fetch(part)).text();"), "PNG");
        std::fs::write(&t.0, b"changed").unwrap();
        assert!(!resolve_blob_url(g, &whole).unwrap().snapshot_holds());
        assert!(run(&c, "await fetch(whole);").starts_with("ERROR: TypeError: Failed to fetch: NotReadableError"));
        // An abort that lands while the read runs still wins.
        assert_eq!(
            run(&c, "const ac = new AbortController(); const p = fetch(URL.createObjectURL(new File([picked], 'x')), { signal: ac.signal }); \
                     ac.abort(); try { await p; return 'resolved'; } catch (e) { return e.name; }"),
            "AbortError"
        );
        s(&c, "URL.revokeObjectURL(whole); 0");
        assert!(resolve_blob_url(g, &whole).is_none());
    }

    /// URL ยง6.2: `init` is a record, a sequence of pairs (an array, a Map,
    /// another URLSearchParams: anything with `@@iterator`) or a string. Until
    /// m26 every object was read as a string, so `new URLSearchParams({ a: 1 })`
    /// was `"%5Bobject+Object%5D="` and a copy of another one was empty.
    #[test]
    fn the_init_is_a_record_a_sequence_of_pairs_or_a_string() {
        let c = ctx();
        let q = |init: &str| s(&c, &format!("new URLSearchParams({init}).toString()"));
        // The record: own enumerable string keys in property order, values
        // stringified.
        assert_eq!(q("{ b: 1, a: 'x y', c: null, d: undefined }"), "b=1&a=x+y&c=null&d=undefined");
        assert_eq!(
            q("Object.defineProperty({ shown: 1 }, 'hidden', { value: 2, enumerable: false })"),
            "shown=1"
        );
        assert_eq!(q("Object.create({ inherited: 1 })"), "");
        // Sequences: an array of pairs, a Map, another URLSearchParams (a copy,
        // not a view), a generator of pairs.
        assert_eq!(q("[['a', '1'], ['a', '2'], ['b', 3]]"), "a=1&a=2&b=3");
        assert_eq!(q("new Map([['k', 'v'], ['x', 'y']])"), "k=v&x=y");
        ok(&c, "(() => { const a = new URLSearchParams('p=1&q=2'); const b = new URLSearchParams(a); \
                 b.append('r', '3'); return a.toString() === 'p=1&q=2' && b.toString() === 'p=1&q=2&r=3'; })()");
        assert_eq!(q("(function* () { yield ['g', '1']; yield new Set(['h', '2']); })()"), "g=1&h=2");
        // Strings: a leading `?` is dropped (once), and anything that is not an
        // object is stringified, `null` included (WebIDL picks USVString).
        assert_eq!(q("'?a=1&b=2'"), "a=1&b=2");
        assert_eq!(q("'??a=1'"), "%3Fa=1");
        assert_eq!(q("null"), "null=");
        assert_eq!(q("undefined"), "");
        assert_eq!(q("42"), "42=");
        // What a browser refuses, refused the same way.
        for bad in ["[['a']]", "[['a', 'b', 'c']]", "[1]", "['ab']"] {
            let m = throws(&c, &format!("new URLSearchParams({bad})"));
            assert!(m.contains("URLSearchParams"), "{bad}: {m}");
        }
        assert_eq!(
            throws(&c, "new URLSearchParams({ [Symbol.iterator]: 5 })"),
            "Failed to construct 'URLSearchParams': @@iterator is not a function."
        );
        // A throwing getter or iterator is the constructor's exception.
        assert_eq!(throws(&c, "new URLSearchParams({ get a() { throw new RangeError('getter'); } })"), "getter");
        assert_eq!(
            throws(&c, "new URLSearchParams(new Proxy({}, { ownKeys() { throw new RangeError('trap'); } }))"),
            "trap"
        );
        assert_eq!(
            throws(&c, "new URLSearchParams({ *[Symbol.iterator]() { throw new RangeError('iter'); } })"),
            "iter"
        );
        ok(&c, "new URLSearchParams({ a: 1 }) instanceof URLSearchParams");
        // The URL a URLSearchParams body makes (fetch / XHR send) is unchanged.
        ok(&c, "new URLSearchParams([['x', '1 2']]).toString() === 'x=1+2'");
    }
}