prebindgen-jni 0.5.0

JNI / Kotlin binding generator for prebindgen
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
use prebindgen_registry::Conversions;

use super::*;

fn callback_snapshot_pipeline() -> (String, std::collections::BTreeMap<String, String>) {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let items: Vec<(syn::Item, SourceLocation)> = vec![
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_thing_name(this_: &ZThing) -> String {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_thing_sub(
                    cb: impl Fn(ZThing) + Send + Sync + 'static,
                    on_close: impl Fn() + Send + Sync + 'static,
                ) {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_other_sub(cb: impl Fn(ZOther) + Send + Sync + 'static) {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
    ];
    let registry =
        crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");

    let jni = JniGenBuilder::new()
        .set_package_prefix("io.test.jni")
        .package(
            crate::package!("thing")
                .class(
                    crate::ptr_class!(ZThing)
                        .method(prebindgen_registry::fun!(z_thing_name).name("name")),
                )
                // ZOther: plain ptr_class, no canonical output ⇒ whole-handle fallback.
                .class(crate::ptr_class!(ZOther))
                .fun(prebindgen_registry::fun!(z_thing_sub))
                .fun(prebindgen_registry::fun!(z_other_sub)),
        )
        // Canonical output: handle (identity) + its string form — a callback
        // arg of ZThing decomposes into these 2 leaves.
        .expand(
            prebindgen_registry::expand_return!(ZThing)
                .field_self()
                .field(prebindgen_registry::fun!(z_thing_name)),
        );

    let dir = unique_test_dir("jnigen_cb_snap");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let gen = jni.build_with(registry).expect("resolve");
    let rust_path = gen.write_rust(dir.join("gen.rs")).expect("write_rust");
    let rust = std::fs::read_to_string(&rust_path).unwrap();

    let kdir = dir.join("kotlin");
    let paths = gen.write_kotlin(&kdir).expect("write_kotlin");
    let mut kotlin = std::collections::BTreeMap::new();
    for p in &paths {
        let name = p.file_name().unwrap().to_string_lossy().to_string();
        kotlin.insert(name, std::fs::read_to_string(p).unwrap());
    }
    (rust, kotlin)
}

#[test]
fn callback_snapshot_rust_side() {
    let (rust, _) = callback_snapshot_pipeline();
    let rc: String = rust.split_whitespace().collect();
    // The trampoline invokes the typed callback interface's `run` — never the
    // erased `FunctionN.invoke`.
    assert!(rc.contains(r#""run""#), "{rust}");
    assert!(!rc.contains(r#""invoke""#), "{rust}");
    // Decomposed ZThing arg: 2 typed leaves (raw jlong handle + String),
    // void return; on_close ⇒ zero-arg `()V`.
    assert!(rc.contains(r#""(JLjava/lang/String;)V""#), "{rust}");
    assert!(rc.contains(r#""()V""#), "{rust}");
    // Plan-less ZOther arg (Phase 3): crosses as a raw `jlong` (`(J)V`), NOT a
    // boxed handle object — so the Rust trampoline neither `new_object`s the
    // typed class nor `close()`s it (the Kotlin `asRaw` proxy wraps + closes).
    assert!(rc.contains(r#""(J)V""#), "{rust}");
    assert!(!rc.contains(r#""close""#), "{rust}");
    assert!(!rc.contains("io/test/jni/thing/ZOther"), "{rust}");
    // Daemon-thread attachment + local-frame bracketing kept from the old
    // trampoline.
    assert!(rc.contains("attach_current_thread_as_daemon"), "{rust}");
    assert!(rc.contains("push_local_frame"), "{rust}");
    assert!(rc.contains("pop_local_frame"), "{rust}");
    // Identity leaf of the decomposed arg: moved into a fresh box and crosses
    // as a RAW jlong jvalue — no native `new_object` of the typed class.
    assert!(rc.contains("jni::sys::jvalue{j:"), "{rust}");
    assert!(!rc.contains("io/test/jni/thing/ZThing"), "{rust}");
    // The decomposed leaf encode calls the accessor off the owned root.
    assert!(rc.contains("myflat::z_thing_name"), "{rust}");
}

#[test]
fn callback_snapshot_kotlin_side() {
    let (_, kotlin) = callback_snapshot_pipeline();
    let names: Vec<&String> = kotlin.keys().collect();

    // Extern tier: callbacks erased to `Any`, like the errorSink.
    let native: String = kotlin
        .values()
        .find(|v| v.contains("object JNINative"))
        .map(|v| v.split_whitespace().collect())
        .unwrap_or_else(|| {
            panic!("no generated file contains `object JNINative`; files: {names:?}")
        });
    assert!(native.contains("cb:Any"), "{native}");
    assert!(native.contains("onClose:Any"), "{native}");

    // Typed callback `fun interface`s with NAMED parameters — decomposed
    // ZThing's identity leaf is `handle`, its accessor leaf carries the literal
    // author-supplied name (`z_thing_name` declared as `"name"`); `Fn()` ⇒
    // the shared zero-arg `VoidCallback` (root package); the plan-less
    // fallback arg is the decapped type short (`zOther`).
    let all: String = kotlin
        .values()
        .cloned()
        .collect::<Vec<_>>()
        .join("\n")
        .split_whitespace()
        .collect();
    assert!(
        all.contains("funinterfaceZThingCallback{publicfunrun(handle:ZThing,name:String)"),
        "{all}"
    );
    assert!(
        all.contains("funinterfaceZOtherCallback{publicfunrun(zOther:ZOther)"),
        "{all}"
    );
    assert!(
        all.contains("funinterfaceVoidCallback{publicfunrun()"),
        "{all}"
    );
    // Raw twin + proxy adapter for the decomposed-arg callback (raw jlong
    // handle at the wire); the all-passthrough interfaces get no twin.
    assert!(
        all.contains("funinterfaceZThingCallbackRaw{publicfunrun(handle:Long,name:String)"),
        "{all}"
    );
    assert!(
        all.contains("funZThingCallback.asRaw():ZThingCallbackRaw=ZThingCallbackRaw{handle,name->run(ZThing(handle),name)}"),
        "{all}"
    );
    // Plan-less ZOther arg (Phase 3): a raw twin `run(zOther: Long)` + an `asRaw`
    // proxy that wraps the pointer into the handle class AND `close()`s it in a
    // `finally` (close-unless-taken) — the Rust side delivers only the raw jlong.
    assert!(
        all.contains("funinterfaceZOtherCallbackRaw{publicfunrun(zOther:Long)"),
        "{all}"
    );
    assert!(all.contains("val__own0=ZOther(zOther)"), "{all}");
    assert!(all.contains("finally{__own0.close()}"), "{all}");
    assert!(!all.contains("VoidCallbackRaw"), "{all}");

    // Wrapper tier: the params are the typed interfaces, forwarded bare.
    let pkg = kotlin
        .values()
        .find(|v| v.contains("public fun zThingSub"))
        .cloned()
        .unwrap_or_default();
    let pc: String = pkg.split_whitespace().collect();
    assert!(pc.contains("cb:ZThingCallback"), "{pkg}");
    assert!(pc.contains("cb.asRaw()"), "{pkg}");
    assert!(pc.contains("onClose:VoidCallback"), "{pkg}");
    assert!(pc.contains("cb:ZOtherCallback"), "{pkg}");
}

/// Regression: a callback-delivered type that has BOTH a nested handle identity
/// (a child `ptr_class` reached by an accessor) AND its own root identity
/// (`expand_return!` `.field_self()`) must emit the root MOVE after every borrow of
/// the owned value — otherwise the nested child clone (which borrows the root)
/// follows `Box::into_raw(Box::new(value))` and fails to compile with "use of
/// moved value". Declaring `.field_self()` LAST guarantees the
/// correct order (the emitter emits identity leaves in declaration order, after
/// all non-identity leaves). This mirrors the zenoh-flat `ZQuery` queryable
/// callback (handle + decomposed fields, nested `ZKeyExpr` identity).
#[test]
fn callback_root_identity_moved_after_nested_borrow() {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let items: Vec<(syn::Item, SourceLocation)> = vec![
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_parent_child(this_: &ZParent) -> &ZChild {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_child_name(this_: &ZChild) -> String {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_parent_sub(
                    cb: impl Fn(ZParent) + Send + Sync + 'static,
                    on_close: impl Fn() + Send + Sync + 'static,
                ) {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
    ];
    let registry =
        crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");

    let jni = JniGenBuilder::new()
        .set_package_prefix("io.test.jni")
        .package(
            crate::package!("thing")
                .class(
                    crate::ptr_class!(ZChild)
                        .method(prebindgen_registry::fun!(z_child_name).name("name")),
                )
                .class(
                    crate::ptr_class!(ZParent)
                        .method(prebindgen_registry::fun!(z_parent_child).name("child")),
                )
                .fun(prebindgen_registry::fun!(z_parent_sub)),
        )
        // Child handle: canonical output = identity (clone) + its name string.
        .expand(
            prebindgen_registry::expand_return!(ZChild)
                .field_self()
                .field(prebindgen_registry::fun!(z_child_name)),
        )
        // Parent: a nested child-handle record, then its OWN root identity LAST.
        .expand(
            prebindgen_registry::expand_return!(ZParent)
                .field(prebindgen_registry::fun!(z_parent_child))
                .field_self(),
        );

    let dir = unique_test_dir("jnigen_root_id_order");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let gen = jni.build_with(registry).expect("resolve");
    let rust_path = gen.write_rust(dir.join("gen.rs")).expect("write_rust");
    let rust = std::fs::read_to_string(&rust_path).unwrap();
    let rc: String = rust.split_whitespace().collect();

    // The root `ZParent` identity is a move (`Box::new(__cb_arg0)`); the nested
    // `ZChild` identity (and its `z_child_name` leaf) borrow the same owned arg
    // via `z_parent_child(&__cb_arg0)`. Every borrow must precede the move.
    let move_pos = rc
        .find("Box::new(__cb_arg0")
        .unwrap_or_else(|| panic!("root identity move not found in:\n{rust}"));
    let last_borrow = rc
        .rfind("z_parent_child(&__cb_arg0")
        .unwrap_or_else(|| panic!("nested child borrow not found in:\n{rust}"));
    assert!(
        last_borrow < move_pos,
        "root identity move must follow every borrow of the owned arg\n{rust}"
    );
}

/// ZReply-shaped product decomposition: the callback arg's plan contains leaf
/// paths with MULTIPLE `Option`-returning nesting steps (`z_reply_sample` →
/// `z_sample_timestamp`), a nested handle identity reached *through* an
/// `Option` step (`z_reply_sample` → `z_sample_key_expr`), and an Acc leaf
/// whose own return keeps its full `Option<…>` as the converter input
/// (`z_reply_zid -> Option<ZId>`, a data class with no canonical child).
/// Every `Option` nesting step must become its own `match` (`None` ⇒ null
/// leaf) — never a blind accessor compose through an `Option`.
#[test]
fn callback_double_option_unwrap_pipeline() {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let fns: &[&str] = &[
        "pub fn z_reply_zid(r: &ZReply) -> Option<ZId> { unimplemented!() }",
        "pub fn z_reply_is_ok(r: &ZReply) -> bool { unimplemented!() }",
        "pub fn z_reply_sample(r: &ZReply) -> Option<&ZSample> { unimplemented!() }",
        "pub fn z_reply_err(r: &ZReply) -> Option<&ZErr> { unimplemented!() }",
        "pub fn z_sample_key_expr(s: &ZSample) -> &ZKeyExpr { unimplemented!() }",
        "pub fn z_sample_timestamp(s: &ZSample) -> Option<&ZTs> { unimplemented!() }",
        "pub fn z_ts_ntp64(t: &ZTs) -> i64 { unimplemented!() }",
        "pub fn z_keyexpr_as_str(ke: &ZKeyExpr) -> &str { unimplemented!() }",
        "pub fn z_err_payload(e: &ZErr) -> Vec<u8> { unimplemented!() }",
    ];
    let mut items: Vec<(syn::Item, SourceLocation)> = fns
        .iter()
        .map(|src| {
            let f: syn::ItemFn = syn::parse_str(src).expect("parse fn");
            (syn::Item::Fn(f), loc.clone())
        })
        .collect();
    // `ZId` is the value leaf of the outer `Option`; it needs a real struct so
    // it can be a `data_class!`.
    items.push((
        syn::Item::Struct(syn::parse_quote!(
            pub struct ZId {
                pub hi: i64,
                pub lo: i64,
            }
        )),
        loc.clone(),
    ));
    items.push((
        syn::Item::Fn(syn::parse_quote!(
            pub fn z_get(cb: impl Fn(ZReply) + Send + Sync + 'static) {
                unimplemented!()
            }
        )),
        loc.clone(),
    ));
    let registry =
        crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");

    let jni = JniGenBuilder::new()
        .set_package_prefix("io.test.jni")
        .package(
            crate::package!("query")
                .class(crate::data_class!(ZId))
                .class(
                    crate::ptr_class!(ZKeyExpr)
                        .method(prebindgen_registry::fun!(z_keyexpr_as_str).name("asStr")),
                )
                .class(
                    crate::ptr_class!(ZTs)
                        .method(prebindgen_registry::fun!(z_ts_ntp64).name("ntp64")),
                )
                .class(
                    crate::ptr_class!(ZSample)
                        .method(prebindgen_registry::fun!(z_sample_key_expr).name("keyExpr"))
                        .method(prebindgen_registry::fun!(z_sample_timestamp).name("timestamp")),
                )
                .class(
                    crate::ptr_class!(ZErr)
                        .method(prebindgen_registry::fun!(z_err_payload).name("payload")),
                )
                .class(
                    crate::ptr_class!(ZReply)
                        .method(prebindgen_registry::fun!(z_reply_zid).name("zid"))
                        .method(prebindgen_registry::fun!(z_reply_is_ok).name("isOk"))
                        .method(prebindgen_registry::fun!(z_reply_sample).name("sample"))
                        .method(prebindgen_registry::fun!(z_reply_err).name("err")),
                )
                .fun(prebindgen_registry::fun!(z_get)),
        )
        .expand(
            prebindgen_registry::expand_return!(ZKeyExpr)
                .field_self()
                .field(prebindgen_registry::fun!(z_keyexpr_as_str)),
        )
        .expand(
            prebindgen_registry::expand_return!(ZTs).field(prebindgen_registry::fun!(z_ts_ntp64)),
        )
        .expand(
            prebindgen_registry::expand_return!(ZSample)
                .field(prebindgen_registry::fun!(z_sample_key_expr))
                .field(prebindgen_registry::fun!(z_sample_timestamp)),
        )
        .expand(
            prebindgen_registry::expand_return!(ZErr)
                .field(prebindgen_registry::fun!(z_err_payload)),
        )
        .expand(
            prebindgen_registry::expand_return!(ZReply)
                .field(prebindgen_registry::fun!(z_reply_zid))
                .field(prebindgen_registry::fun!(z_reply_is_ok))
                .field(prebindgen_registry::fun!(z_reply_sample))
                .field(prebindgen_registry::fun!(z_reply_err)),
        );

    let dir = unique_test_dir("jnigen_double_opt");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let gen = jni.build_with(registry).expect("resolve");
    let rust_path = gen.write_rust(dir.join("gen.rs")).expect("write_rust");
    let rust = std::fs::read_to_string(&rust_path).unwrap();
    let rc: String = rust.split_whitespace().collect();

    // Both Option nesting steps of the timestamp leaf get their own match;
    // the innermost accessor composes off the second unwrap binding.
    assert!(rc.contains("matchmyflat::z_reply_sample("), "{rust}");
    assert!(rc.contains("matchmyflat::z_sample_timestamp("), "{rust}");
    assert!(rc.contains("myflat::z_ts_ntp64(__n1)"), "{rust}");
    // Never a blind compose through an `Option`-returning accessor.
    assert!(
        !rc.contains("myflat::z_ts_ntp64(myflat::z_sample_timestamp("),
        "{rust}"
    );
    assert!(
        !rc.contains("myflat::z_sample_key_expr(myflat::z_reply_sample("),
        "{rust}"
    );
    // The nested keyexpr identity is reached through the `Option` unwrap and
    // has a null arm.
    assert!(rc.contains("myflat::z_sample_key_expr(__n0)"), "{rust}");
    assert!(rc.contains("jni::objects::JObject::null()"), "{rust}");
    // The `Option<ZId>` Acc leaf composes its full return directly into the
    // converter — no unwrap of the leaf's own `Option`.
    assert!(rc.contains("myflat::z_reply_zid(&__cb_arg0)"), "{rust}");
    assert!(!rc.contains("matchmyflat::z_reply_zid("), "{rust}");
    // 6 leaves ⇒ typed `run` descriptor: nullable `ZId` data class, raw `Z`
    // for the non-null bool discriminator, typed handle class (full FQN),
    // nullable String, BOXED Long for the nullable timestamp, nullable `[B`.
    assert!(
        rc.contains(
            "\"(Lio/test/jni/query/ZId;ZLjava/lang/Long;Ljava/lang/String;Ljava/lang/Long;[B)V\""
        ),
        "{rust}"
    );
    // The non-null bool crosses as a raw typed jvalue — never boxed.
    assert!(rc.contains("jni::sys::jvalue{z:"), "{rust}");

    // Kotlin tier: the generated callback `fun interface` carries the typed
    // params — ok-arm and err-arm leaves nullable (the value may be absent),
    // the discriminator non-null; the nested `ZId` data class surfaces as its
    // typed (nullable) Kotlin class.
    let kdir = dir.join("kotlin");
    let paths = gen.write_kotlin(&kdir).expect("write_kotlin");
    let iface_file = paths
        .iter()
        .filter_map(|p| std::fs::read_to_string(p).ok())
        .find(|v| v.contains("fun interface ZReplyCallback"))
        .unwrap_or_default();
    // Scope to the interface block — the merged package file also holds the
    // ZId data class and other decls.
    let iface = iface_file
        .split("fun interface ZReplyCallback")
        .nth(1)
        .and_then(|s| s.split_once('}').map(|(b, _)| b.to_string()))
        .unwrap_or_default();
    let ic: String = iface.split_whitespace().collect();
    assert!(ic.contains("isOk:Boolean"), "{iface}");
    assert!(ic.contains("sample__keyExpr:ZKeyExpr?"), "{iface}");
    assert!(ic.contains(":Long?"), "{iface}");
    assert!(ic.contains(":ZId?"), "{iface}");
    // The wrapper takes the typed interface and forwards it bare.
    let pkg = paths
        .iter()
        .filter_map(|p| std::fs::read_to_string(p).ok())
        .find(|v| v.contains("public fun zGet"))
        .unwrap_or_default();
    let pc: String = pkg.split_whitespace().collect();
    assert!(pc.contains("cb:ZReplyCallback"), "{pkg}");
    // The call site forwards the generated raw-proxy adapter — the typed
    // interface is the user surface, the extern receives the raw twin.
    assert!(pc.contains("JNINative.zGet(cb.asRaw(),"), "{pkg}");
}

// ────────────────────────────────────────────────────────────────────────
// Declaration-keyed interfaces: a type may have several decompositions —
// the default (unnamed) deconstructor and per-fn inline records
// (`.expand_return`). Interface identity follows the DECLARATION, so
// differently-decomposed functions get distinct interfaces instead of
// colliding on one type-keyed name.
// ────────────────────────────────────────────────────────────────────────

// ────────────────────────────────────────────────────────────────────────
// Spec memo (issue #107): every consumer — resolve-time trampoline,
// per-function plan, declaration emitter — reads ONE derivation per
// interface identity through `JniGenBuilder::iface_spec`.
// ────────────────────────────────────────────────────────────────────────

#[test]
fn iface_spec_memo_shares_one_derivation() {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let items: Vec<(syn::Item, SourceLocation)> = vec![
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_thing_name(this_: &ZThing) -> String {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_things_all() -> Vec<ZThing> {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
        (
            syn::Item::Fn(syn::parse_quote!(
                pub fn z_thing_sub(cb: impl Fn(ZThing) + Send + Sync + 'static) {
                    unimplemented!()
                }
            )),
            loc.clone(),
        ),
    ];
    let registry =
        crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");
    let jni = JniGenBuilder::new()
        .set_package_prefix("io.test.jni")
        .package(
            crate::package!("thing")
                .class(
                    crate::ptr_class!(ZThing)
                        .method(prebindgen_registry::fun!(z_thing_name).name("name")),
                )
                .fun(prebindgen_registry::fun!(z_things_all))
                .fun(prebindgen_registry::fun!(z_thing_sub)),
        )
        .expand(
            prebindgen_registry::expand_return!(ZThing)
                .field_self()
                .field(prebindgen_registry::fun!(z_thing_name)),
        );
    let gen = jni.build_with(registry).expect("resolve");
    let (ext, registry) = (gen.declarations(), gen.registry());

    // Same key twice ⇒ the same allocation (resolve already populated the
    // memo through the trampoline — a hit also exercises the debug-build
    // re-derivation assert).
    let a = ext
        .iface_spec(registry, &SpecKey::JniErrorHandler)
        .expect("global handler spec");
    let b = ext
        .iface_spec(registry, &SpecKey::JniErrorHandler)
        .expect("global handler spec");
    assert!(Arc::ptr_eq(&a, &b), "one derivation per identity");

    // The plan-facing dispatcher and a direct key lookup share one
    // allocation — the wrapper surface and the interface declaration cannot
    // diverge from the fold upcall's descriptor.
    let plan = registry
        .unfold_plans()
        .get(&syn::parse_str::<syn::Ident>("z_things_all").unwrap())
        .expect("fold plan");
    let via_plan = folder_iface_for_plan(ext, registry, plan).expect("folder spec");
    let decon = plan.decon.clone().expect("record-built fold");
    let direct = ext
        .iface_spec(registry, &SpecKey::Folder(decon))
        .expect("folder spec");
    assert!(Arc::ptr_eq(&via_plan, &direct), "folder identity shared");

    // The impl-Fn identity: the trampoline (resolve time) and the wrapper
    // surface key on the same canonical arg types.
    let args = vec![registry
        .reading_of(&syn::parse_quote!(ZThing))
        .expect("ZThing is interned")];
    let cb1 = ext
        .iface_spec(registry, &SpecKey::callback(&args))
        .expect("callback spec");
    let cb2 = ext
        .iface_spec(registry, &SpecKey::callback(&args))
        .expect("callback spec");
    assert!(Arc::ptr_eq(&cb1, &cb2), "callback identity shared");
    assert_eq!(cb1.descr, "(JLjava/lang/String;)V");
}

// ────────────────────────────────────────────────────────────────────────
// Function-plan memo (the #90 "build once, store" capstone): validation
// and every emitter share ONE `JniFunctionPlan` per function ident.
// ────────────────────────────────────────────────────────────────────────

#[test]
fn fn_plan_memo_shares_one_derivation() {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let items: Vec<(syn::Item, SourceLocation)> = vec![(
        syn::Item::Fn(syn::parse_quote!(
            pub fn z_do_thing(x: i64) -> i64 {
                unimplemented!()
            }
        )),
        loc.clone(),
    )];
    let registry =
        crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");
    let jni = JniGenBuilder::new()
        .set_package_prefix("io.test.jni")
        .package(crate::package!("thing").fun(prebindgen_registry::fun!(z_do_thing)));
    // resolve runs validation, which builds and stores every function's plan.
    let gen = jni.build_with(registry).expect("resolve");
    let (ext, registry) = (gen.declarations(), gen.registry());
    let f = registry.flat().function("z_do_thing").expect("indexed");

    // The plan is already in the memo (populated at resolve by validation) —
    // repeated lookups return the SAME allocation, and it equals what a fresh
    // `JniFunctionPlan::build` derives.
    let a = ext.fn_plan(registry, f).expect("plan");
    let b = ext.fn_plan(registry, f).expect("plan");
    assert!(std::rc::Rc::ptr_eq(&a, &b), "one plan per function ident");
    assert_eq!(a.native_symbol, b.native_symbol);
    let fresh = JniFunctionPlan::build(ext, registry, f).expect("fresh build");
    assert_eq!(a.native_symbol, fresh.native_symbol);
    assert_eq!(a.jni_method, fresh.jni_method);
}

/// A callback identity is the same whether its args come from the **reading**
/// or from the signature's syntax.
///
/// `SpecKey::Callback` is a memo key, so it holds `TypeKey`s — a `TypeRef`
/// could not go in it (`Ord` is required, and an `Origin` carries a
/// `SourceLocation`, so two identical readings from different files would
/// compare unequal). That means the args reach `SpecKey::callback` as
/// spellings, and #275's last part changes *which* spelling: from
/// `extract_fn_trait_args(&pt.ty)` to each arg `TypeRef`'s `spell()`.
///
/// If those two disagreed, the memo would split one interface identity into
/// two — an extra generated `fun interface` and a descriptor mismatch. Nothing
/// else would fail: not a panic, not an unresolved type, just a duplicate. So
/// it is pinned here rather than trusted.
///
/// BLOCKED by the prebindgen-jni crate split: calls `Emit::for_test()`, a
/// `pub(crate)` constructor of `prebindgen_registry::Emit` — reachable when this
/// test lived inside the `prebindgen` crate, not from the separate
/// `prebindgen-jni` crate it moved to. Left in place, not deleted, pending a
/// `prebindgen`-side test-support hook (see the carve-prebindgen-jni report).
#[test]
fn a_callback_identity_is_the_same_from_the_reading_or_the_syntax() {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let items: Vec<(syn::Item, SourceLocation)> = vec![(
        syn::Item::Fn(syn::parse_quote!(
            pub fn z_sub(cb: impl Fn(ZThing) + Send + Sync + 'static) {
                unimplemented!()
            }
        )),
        loc.clone(),
    )];
    let registry =
        crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");
    let jni = JniGenBuilder::new()
        .set_package_prefix("io.test.jni")
        .package(
            crate::package!()
                .class(crate::ptr_class!(ZThing))
                .fun(prebindgen_registry::fun!(z_sub)),
        );
    let gen = jni.build_with(registry).expect("resolve");
    let registry = gen.registry();

    let f = registry.flat().function("z_sub").expect("the declared fn");
    let cb = f
        .params
        .iter()
        .find_map(|p| match p.ty.kind() {
            prebindgen_registry::flat::TypeKind::Callback { args } => Some((p, args)),
            _ => None,
        })
        .expect("z_sub takes a callback");
    let (param, arg_readings) = cb;

    // The two routes to the same identity. The syntax route builds the key's
    // own `Vec<TypeKey>` directly, because `SpecKey::callback` now takes
    // readings — which is exactly the claim being pinned: the readings and the
    // signature's own `impl Fn` bounds agree on every arg's identity.
    let from_reading = SpecKey::callback(arg_readings);
    let from_syntax = SpecKey::Callback(
        prebindgen_registry::flat::extract_fn_trait_args(
            &prebindgen_registry::Emit::for_test().spell_ty(&param.ty),
        )
        .expect("the param is an impl Fn")
        .iter()
        .map(prebindgen_registry::TypeKey::from_type)
        .collect(),
    );

    assert_eq!(
        from_reading, from_syntax,
        "a callback keyed off its readings must be the SAME memo identity as one \
         keyed off the signature's syntax — otherwise the memo silently emits two \
         interfaces for one callback"
    );
}

/// A callback argument spelled as a **wrapped** borrow — `Box<&T>` — is refused,
/// rather than generating a trampoline the consumer cannot compile.
///
/// `Box` is transparent, so `Box<&T>`'s kind is `Ref` exactly as `&T`'s is: the
/// model answers "borrow" for both, and that answer is correct. What differs is
/// the *spelling*, and the generated trampoline is written in the spelling. Neutralise
/// the guard — pass the canonical `&T` as `produced` at `selector.rs`'s borrow arm
/// instead of the crossing's own `syntax` — and this test fails on emitted code that
/// hands a `Box<&ZThing>` to a `ZThing_to_jlong(v: &myflat::ZThing)`:
///
/// ```ignore
/// Box::new(move |__cb_arg0: Box<&myflat::ZThing>| {
///     let __cb0_enc = ZThing_to_jlong_11822692(&mut env, __cb_arg0)?;
/// ```
///
/// The guard that holds is [`Declarations::output_wrapper_shape`]'s borrowed-opaque
/// arm, which matches `syn::Type::Reference` on `produced` structurally: `Box<&T>`
/// is a `Type::Path`, so it gets no whole-value output converter, and a callback
/// arg's is a required type. Same `kind`-classifies / spelling-decides split as
/// #272's `decoded_vec_satisfies` and `is_unsized_spelling`.
///
/// A local re-check inside `emit/callback.rs` was tried and dropped (#279
/// review): it changed no output, and added a spelling classification that
/// never fires. This test is the protection instead.
#[test]
fn a_wrapped_borrow_callback_arg_declines() {
    use prebindgen::SourceLocation;
    let loc = myflat_loc();
    let build = |argty: syn::Type| -> Result<String, String> {
        let items: Vec<(syn::Item, SourceLocation)> = vec![
            (
                syn::Item::Struct(syn::parse_quote!(
                    pub struct ZThing {
                        pub v: i64,
                    }
                )),
                loc.clone(),
            ),
            (
                syn::Item::Fn(syn::parse_quote!(
                    pub fn z_sub(cb: impl Fn(#argty) + Send + Sync + 'static) {
                        unimplemented!()
                    }
                )),
                loc.clone(),
            ),
        ];
        let registry =
            crate::test_util::reg_from_items(declare_referenced(items)).expect("index items");
        let jni = JniGenBuilder::new()
            .set_package_prefix("io.test.jni")
            .package(
                crate::package!()
                    .class(crate::ptr_class!(ZThing))
                    .fun(prebindgen_registry::fun!(z_sub)),
            );
        let dir = unique_test_dir("jnigen_wrapped_cb");
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        match jni.build_with(registry) {
            Ok(g) => Ok(std::fs::read_to_string(
                g.write_rust(dir.join("g.rs")).expect("write_rust"),
            )
            .expect("read rust")),
            Err(e) => Err(format!("{e}")),
        }
    };

    // The canonical borrow still resolves and still clones through the core.
    let plain = build(syn::parse_quote!(&ZThing)).expect("a plain borrow resolves");
    assert!(
        plain.contains("__cb_arg0: &myflat::ZThing"),
        "the trampoline takes the borrow as written:\n{plain}"
    );

    // Wrapped, the clone is inexpressible, so nothing claims it.
    let err = build(syn::parse_quote!(Box<&ZThing>))
        .expect_err("a wrapped borrow callback arg must not resolve");
    assert!(
        err.contains("could not be resolved"),
        "the refusal names the type: {err}"
    );
}