weaveffi-core 0.9.0

Generator trait, orchestrator, validation, and shared utilities for WeaveFFI
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
//! The **binding model**: a normalized, fully-lowered view of an [`Api`] that
//! every language backend consumes.
//!
//! Before this module existed, each of the eleven generators re-walked the IR,
//! re-derived the C ABI calling convention, and re-invented every emitted C
//! symbol name. They drifted: iterators were lowered as lists in some targets,
//! listeners were emitted only by the C header, and a custom `c_prefix` reached
//! only the C and C++ outputs while the other nine hard-coded `weaveffi_`.
//!
//! [`BindingModel::build`] walks the IR exactly once and produces a flat list
//! of [`ModuleBinding`]s in which:
//!
//! * every emitted **C symbol name** is precomputed once (so all backends agree
//!   by construction, and a non-default prefix is honored everywhere); and
//! * every function/struct/callback is paired with its lowered [`AbiFn`]
//!   signature (built from [`crate::abi`]), so no backend re-derives parameter
//!   arity, ordering, or `out_*`/`out_err` placement.
//!
//! A backend reads the *idiomatic* shape from the retained [`TypeRef`]s
//! (`param.ty`, `field.ty`, …) and the *native* shape from the [`AbiFn`]s, then
//! writes only the marshalling that bridges the two in its own idioms. The hard,
//! drift-prone facts live here; only language syntax lives in the backends.

use heck::ToUpperCamelCase;
use weaveffi_ir::ir::{
    Api, CallbackDef, EnumDef, Function, ListenerDef, Module, StructDef, TypeRef,
};

use crate::abi::{
    self, async_callback_params, async_input_params, context_param, error_out_param, lower_param,
    lower_return, sync_signature, AbiParam, CType,
};

/// A single lowered C symbol: its name, ordered ABI parameter slots, and C
/// return type. This is what a backend declares to its FFI layer and calls.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbiFn {
    /// The fully-qualified, prefixed C symbol (e.g. `weaveffi_math_add`).
    pub symbol: String,
    /// Ordered parameter slots, including any trailing `out_*` and `out_err`.
    pub params: Vec<AbiParam>,
    /// The C return type.
    pub ret: CType,
}

/// How a function crosses the boundary. Exactly one shape applies to any given
/// function: synchronous, asynchronous (callback-completed), or iterator-returning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallShape {
    /// A plain blocking call: [`AbiFn`] is the symbol to invoke.
    Sync(AbiFn),
    /// An async launcher plus its completion-callback typedef.
    Async(AsyncBinding),
    /// An iterator-returning function: an opaque handle plus `next`/`destroy`.
    Iterator(IteratorBinding),
}

/// The lowered surface of an `async` function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AsyncBinding {
    /// The launcher: input slots, optional `cancel_token`, then `callback` and
    /// `context`. Returns `void`.
    pub launch: AbiFn,
    /// The completion-callback function-pointer typedef name
    /// (`{symbol}_callback`).
    pub callback_type: String,
    /// The callback's parameter slots: `(void* context, {prefix}_error* err,
    /// <result fields>)`.
    pub callback_params: Vec<AbiParam>,
}

/// The lowered surface of an `iter<T>`-returning function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IteratorBinding {
    /// The element type `T` of `iter<T>`.
    pub elem: TypeRef,
    /// The opaque iterator tag (`{prefix}_{path}_{Pascal}Iterator`).
    pub iter_tag: String,
    /// The launcher returning `{iter_tag}*`.
    pub launch: AbiFn,
    /// `int32_t {iter_tag}_next({iter_tag}* iter, T* out_item, …, error* out_err)`.
    pub next: AbiFn,
    /// `void {iter_tag}_destroy({iter_tag}* iter)`.
    pub destroy_symbol: String,
}

/// One IR parameter, retained with its lowered ABI slots.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParamBinding {
    pub name: String,
    pub ty: TypeRef,
    pub mutable: bool,
    pub doc: Option<String>,
    /// The ordered C ABI slots this single parameter expands into.
    pub abi: Vec<AbiParam>,
}

/// A function, fully lowered.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnBinding {
    pub name: String,
    pub doc: Option<String>,
    pub deprecated: Option<String>,
    pub since: Option<String>,
    pub cancellable: bool,
    pub is_async: bool,
    /// IR input parameters with their lowered slots.
    pub params: Vec<ParamBinding>,
    /// The IR return type (`None` = void). For an iterator function this is the
    /// `iter<T>` type itself; the element `T` also lives in [`IteratorBinding`].
    pub ret: Option<TypeRef>,
    /// Base C symbol (`{prefix}_{module_path}_{name}`) before any
    /// `_async`/iterator suffixing.
    pub c_base: String,
    /// The call shape (sync / async / iterator).
    pub shape: CallShape,
}

/// A struct field, retained with its getter symbol and lowered return.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldBinding {
    pub name: String,
    pub doc: Option<String>,
    pub ty: TypeRef,
    /// `{c_tag}_get_{field}`. Receiver is an implicit `const {c_tag}* ptr`; any
    /// `out_*` slots are in [`getter_out_params`](Self::getter_out_params).
    pub getter_symbol: String,
    /// The C return type of the getter.
    pub getter_ret: CType,
    /// Trailing `out_*` slots of the getter (e.g. `size_t* out_len` for bytes).
    pub getter_out_params: Vec<AbiParam>,
    /// The ABI slots this field expands into when passed *in* (struct create,
    /// builder setter).
    pub value_params: Vec<AbiParam>,
}

/// The fluent builder lowered for a struct that opted in with `builder: true`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BuilderBinding {
    /// `{c_tag}Builder`.
    pub builder_tag: String,
    /// `{c_tag}_Builder_new`.
    pub new_symbol: String,
    /// `{c_tag}_Builder_build` (carries a trailing `out_err`).
    pub build_symbol: String,
    /// `{c_tag}_Builder_destroy`.
    pub destroy_symbol: String,
    /// One `(field_name, setter_symbol)` per field; the value slots are the
    /// field's [`FieldBinding::value_params`].
    pub setters: Vec<(String, String)>,
}

/// A struct, fully lowered.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructBinding {
    pub name: String,
    pub doc: Option<String>,
    /// `{prefix}_{module_path}_{name}` — the opaque tag.
    pub c_tag: String,
    pub fields: Vec<FieldBinding>,
    /// `{c_tag}_create(<field slots>, error* out_err) -> {c_tag}*`.
    pub create: AbiFn,
    /// `{c_tag}_destroy`.
    pub destroy_symbol: String,
    /// Present when `builder: true`.
    pub builder: Option<BuilderBinding>,
}

/// An enum, fully lowered. WeaveFFI enums are C-style integer discriminants.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumBinding {
    pub name: String,
    pub doc: Option<String>,
    /// `{prefix}_{module_path}_{name}`.
    pub c_tag: String,
    pub variants: Vec<EnumVariantBinding>,
}

/// A single enum variant with its precomputed C constant name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumVariantBinding {
    pub name: String,
    pub value: i32,
    pub doc: Option<String>,
    /// `{enum_c_tag}_{variant}`.
    pub c_const: String,
}

/// A callback function-pointer typedef declared at module scope.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallbackBinding {
    pub name: String,
    pub doc: Option<String>,
    /// `{prefix}_{module_path}_{name}_fn`.
    pub c_fn_type: String,
    /// IR parameters of the callback (without the trailing context).
    pub params: Vec<ParamBinding>,
    /// The full ABI slot list, including the trailing `void* context`.
    pub abi_params: Vec<AbiParam>,
}

/// A listener: a register/unregister pair bound to a callback.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListenerBinding {
    pub name: String,
    pub doc: Option<String>,
    /// The callback this listener fires (name within the same module).
    pub event_callback: String,
    /// The referenced callback's `_fn` typedef name.
    pub callback_c_fn_type: String,
    /// `uint64_t {prefix}_{path}_register_{name}({cb}_fn callback, void* context)`.
    pub register_symbol: String,
    /// `void {prefix}_{path}_unregister_{name}(uint64_t id)`.
    pub unregister_symbol: String,
}

/// One module, flattened with its underscore-joined symbol path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleBinding {
    pub name: String,
    /// Path segments from the root (e.g. `["outer", "inner"]`).
    pub segments: Vec<String>,
    /// Underscore-joined path used as the C symbol segment (e.g. `outer_inner`).
    pub path: String,
    pub doc: Option<String>,
    pub enums: Vec<EnumBinding>,
    pub structs: Vec<StructBinding>,
    pub callbacks: Vec<CallbackBinding>,
    pub listeners: Vec<ListenerBinding>,
    pub functions: Vec<FnBinding>,
}

impl ModuleBinding {
    /// Find a callback declared in this module by name.
    pub fn callback(&self, name: &str) -> Option<&CallbackBinding> {
        self.callbacks.iter().find(|c| c.name == name)
    }

    /// True when this module declares no API surface at all.
    pub fn is_empty(&self) -> bool {
        self.enums.is_empty()
            && self.structs.is_empty()
            && self.callbacks.is_empty()
            && self.listeners.is_empty()
            && self.functions.is_empty()
    }
}

/// The whole API, normalized and lowered for code generation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BindingModel {
    /// The C symbol prefix every emitted name is built from.
    pub prefix: String,
    /// The IR schema version of the source `Api`.
    pub version: String,
    /// Modules in depth-first pre-order, each carrying its joined symbol path.
    pub modules: Vec<ModuleBinding>,
}

impl BindingModel {
    /// Build the model from a validated [`Api`], using `prefix` for every C
    /// symbol name. `prefix` is the single global ABI prefix (default
    /// `"weaveffi"`); passing the same prefix to every backend is what keeps
    /// the producer header and all consumers calling identical symbols.
    pub fn build(api: &Api, prefix: &str) -> Self {
        let mut modules = Vec::new();
        for m in &api.modules {
            lower_module(m, &[], prefix, &mut modules);
        }
        Self {
            prefix: prefix.to_string(),
            version: api.version.clone(),
            modules,
        }
    }

    /// Iterate every function across all modules, paired with its module.
    pub fn functions(&self) -> impl Iterator<Item = (&ModuleBinding, &FnBinding)> {
        self.modules
            .iter()
            .flat_map(|m| m.functions.iter().map(move |f| (m, f)))
    }
}

/// Recursively lower `module` and its descendants into the flat `out` list,
/// pre-order (parent before children) so symbol declarations precede uses.
fn lower_module(module: &Module, parent: &[String], prefix: &str, out: &mut Vec<ModuleBinding>) {
    let mut segments = parent.to_vec();
    segments.push(module.name.clone());
    let path = segments.join("_");

    let enums = module
        .enums
        .iter()
        .map(|e| lower_enum(e, &path, prefix))
        .collect();
    let structs = module
        .structs
        .iter()
        .map(|s| lower_struct(s, &path, prefix))
        .collect();
    let callbacks: Vec<CallbackBinding> = module
        .callbacks
        .iter()
        .map(|c| lower_callback(c, &path, prefix))
        .collect();
    let listeners = module
        .listeners
        .iter()
        .map(|l| lower_listener(l, &path, prefix))
        .collect();
    let functions = module
        .functions
        .iter()
        .map(|f| lower_function(f, &path, prefix))
        .collect();

    // Module doc is synthesized from the first documented function, matching
    // the `EmptyModuleDoc` lint's notion of "the module is documented".
    let doc = module.functions.iter().find_map(|f| f.doc.clone());

    out.push(ModuleBinding {
        name: module.name.clone(),
        segments: segments.clone(),
        path,
        doc,
        enums,
        structs,
        callbacks,
        listeners,
        functions,
    });

    for child in &module.modules {
        lower_module(child, &segments, prefix, out);
    }
}

fn lower_param_binding(p: &weaveffi_ir::ir::Param, module: &str) -> ParamBinding {
    ParamBinding {
        name: p.name.clone(),
        ty: p.ty.clone(),
        mutable: p.mutable,
        doc: p.doc.clone(),
        abi: lower_param(&p.name, &p.ty, module, p.mutable),
    }
}

fn lower_enum(e: &EnumDef, path: &str, prefix: &str) -> EnumBinding {
    let c_tag = format!("{prefix}_{path}_{}", e.name);
    let variants = e
        .variants
        .iter()
        .map(|v| EnumVariantBinding {
            name: v.name.clone(),
            value: v.value,
            doc: v.doc.clone(),
            c_const: format!("{c_tag}_{}", v.name),
        })
        .collect();
    EnumBinding {
        name: e.name.clone(),
        doc: e.doc.clone(),
        c_tag,
        variants,
    }
}

fn lower_struct(s: &StructDef, path: &str, prefix: &str) -> StructBinding {
    let c_tag = format!("{prefix}_{path}_{}", s.name);

    let fields: Vec<FieldBinding> = s
        .fields
        .iter()
        .map(|f| {
            let r = lower_return(&f.ty, path);
            FieldBinding {
                name: f.name.clone(),
                doc: f.doc.clone(),
                ty: f.ty.clone(),
                getter_symbol: format!("{c_tag}_get_{}", f.name),
                getter_ret: r.ret,
                getter_out_params: r.out_params,
                value_params: lower_param(&f.name, &f.ty, path, false),
            }
        })
        .collect();

    // create: each field lowered as an input parameter, then out_err.
    let mut create_params: Vec<AbiParam> = s
        .fields
        .iter()
        .flat_map(|f| lower_param(&f.name, &f.ty, path, false))
        .collect();
    create_params.push(error_out_param());
    let create = AbiFn {
        symbol: format!("{c_tag}_create"),
        params: create_params,
        ret: CType::ptr(CType::Named(format!("{path}_{}", s.name))),
    };

    let builder = s.builder.then(|| {
        let builder_tag = format!("{c_tag}Builder");
        let setters = s
            .fields
            .iter()
            .map(|f| (f.name.clone(), format!("{c_tag}_Builder_set_{}", f.name)))
            .collect();
        BuilderBinding {
            builder_tag,
            new_symbol: format!("{c_tag}_Builder_new"),
            build_symbol: format!("{c_tag}_Builder_build"),
            destroy_symbol: format!("{c_tag}_Builder_destroy"),
            setters,
        }
    });

    StructBinding {
        name: s.name.clone(),
        doc: s.doc.clone(),
        c_tag: c_tag.clone(),
        fields,
        create,
        destroy_symbol: format!("{c_tag}_destroy"),
        builder,
    }
}

fn lower_callback(c: &CallbackDef, path: &str, prefix: &str) -> CallbackBinding {
    let params: Vec<ParamBinding> = c
        .params
        .iter()
        .map(|p| lower_param_binding(p, path))
        .collect();
    let mut abi_params: Vec<AbiParam> = params.iter().flat_map(|p| p.abi.clone()).collect();
    abi_params.push(context_param());
    CallbackBinding {
        name: c.name.clone(),
        doc: c.doc.clone(),
        c_fn_type: format!("{prefix}_{path}_{}_fn", c.name),
        params,
        abi_params,
    }
}

fn lower_listener(l: &ListenerDef, path: &str, prefix: &str) -> ListenerBinding {
    ListenerBinding {
        name: l.name.clone(),
        doc: l.doc.clone(),
        event_callback: l.event_callback.clone(),
        callback_c_fn_type: format!("{prefix}_{path}_{}_fn", l.event_callback),
        register_symbol: format!("{prefix}_{path}_register_{}", l.name),
        unregister_symbol: format!("{prefix}_{path}_unregister_{}", l.name),
    }
}

fn lower_function(f: &Function, path: &str, prefix: &str) -> FnBinding {
    let params: Vec<ParamBinding> = f
        .params
        .iter()
        .map(|p| lower_param_binding(p, path))
        .collect();
    let c_base = format!("{prefix}_{path}_{}", f.name);

    let shape = if let Some(TypeRef::Iterator(inner)) = &f.returns {
        let pascal = f.name.to_upper_camel_case();
        let iter_tag = format!("{prefix}_{path}_{pascal}Iterator");
        let iter_core = format!("{path}_{pascal}Iterator");

        // launcher: input slots + out_err, returns iter_tag*.
        let mut launch_params: Vec<AbiParam> = f
            .params
            .iter()
            .flat_map(|p| lower_param(&p.name, &p.ty, path, p.mutable))
            .collect();
        launch_params.push(error_out_param());
        let launch = AbiFn {
            symbol: c_base.clone(),
            params: launch_params,
            ret: CType::ptr(CType::Named(iter_core.clone())),
        };

        // next: (iter, out_item, <item out_params>, out_err) -> int32.
        let item = lower_return(inner, path);
        let mut next_params = vec![
            AbiParam::new("iter", CType::ptr(CType::Named(iter_core.clone()))),
            AbiParam::new("out_item", CType::ptr(item.ret)),
        ];
        next_params.extend(item.out_params);
        next_params.push(error_out_param());
        let next = AbiFn {
            symbol: format!("{iter_tag}_next"),
            params: next_params,
            ret: CType::Int32,
        };

        CallShape::Iterator(IteratorBinding {
            elem: (**inner).clone(),
            iter_tag: iter_tag.clone(),
            launch,
            next,
            destroy_symbol: format!("{iter_tag}_destroy"),
        })
    } else if f.r#async {
        let callback_type = format!("{c_base}_callback");
        let mut launch_params = async_input_params(f, path);
        launch_params.push(AbiParam::new(
            "callback",
            CType::Named(format!("{path}_{}_callback", f.name)),
        ));
        launch_params.push(context_param());
        let launch = AbiFn {
            symbol: format!("{c_base}_async"),
            params: launch_params,
            ret: CType::Void,
        };
        CallShape::Async(AsyncBinding {
            launch,
            callback_type,
            callback_params: async_callback_params(f.returns.as_ref(), path),
        })
    } else {
        let sig = sync_signature(&f.params, f.returns.as_ref(), path);
        CallShape::Sync(AbiFn {
            symbol: c_base.clone(),
            params: sig.params,
            ret: sig.ret,
        })
    };

    FnBinding {
        name: f.name.clone(),
        doc: f.doc.clone(),
        deprecated: f.deprecated.clone(),
        since: f.since.clone(),
        cancellable: f.cancellable,
        is_async: f.r#async,
        params,
        ret: f.returns.clone(),
        c_base,
        shape,
    }
}

/// The element C type of an iterator's `out_item` slot (the pointee of
/// `T* out_item`). Exposed for backends that materialize iterator results.
pub fn iterator_item_ctype(elem: &TypeRef, module: &str) -> CType {
    abi::lower_return(elem, module).ret
}

#[cfg(test)]
mod tests {
    use super::*;
    use weaveffi_ir::ir::{
        CallbackDef, EnumDef, EnumVariant, Function, ListenerDef, Module, Param, StructDef,
        StructField,
    };

    fn param(name: &str, ty: TypeRef) -> Param {
        Param {
            name: name.into(),
            ty,
            mutable: false,
            doc: None,
        }
    }

    fn func(name: &str, params: Vec<Param>, returns: Option<TypeRef>) -> Function {
        Function {
            name: name.into(),
            params,
            returns,
            doc: None,
            r#async: false,
            cancellable: false,
            deprecated: None,
            since: None,
        }
    }

    fn module(name: &str) -> Module {
        Module {
            name: name.into(),
            functions: vec![],
            structs: vec![],
            enums: vec![],
            callbacks: vec![],
            listeners: vec![],
            errors: None,
            modules: vec![],
        }
    }

    fn api(modules: Vec<Module>) -> Api {
        Api {
            version: "0.3.0".into(),
            modules,
            generators: None,
            package: None,
        }
    }

    #[test]
    fn sync_function_symbol_and_sig() {
        let m = Module {
            functions: vec![func(
                "add",
                vec![param("a", TypeRef::I32), param("b", TypeRef::I32)],
                Some(TypeRef::I32),
            )],
            ..module("math")
        };
        let model = BindingModel::build(&api(vec![m]), "weaveffi");
        let f = &model.modules[0].functions[0];
        assert_eq!(f.c_base, "weaveffi_math_add");
        match &f.shape {
            CallShape::Sync(abi) => {
                assert_eq!(abi.symbol, "weaveffi_math_add");
                assert_eq!(abi.ret, CType::Int32);
                let rendered: Vec<String> = abi
                    .params
                    .iter()
                    .map(|p| format!("{} {}", p.ty.render_c("weaveffi"), p.name))
                    .collect();
                assert_eq!(
                    rendered,
                    ["int32_t a", "int32_t b", "weaveffi_error* out_err"]
                );
            }
            _ => panic!("expected sync"),
        }
    }

    #[test]
    fn prefix_is_honored_everywhere() {
        let m = Module {
            functions: vec![func("ping", vec![], None)],
            ..module("net")
        };
        let model = BindingModel::build(&api(vec![m]), "acme");
        let f = &model.modules[0].functions[0];
        assert_eq!(f.c_base, "acme_net_ping");
    }

    #[test]
    fn async_function_has_launch_and_callback() {
        let m = Module {
            functions: vec![Function {
                cancellable: true,
                r#async: true,
                ..func(
                    "fetch",
                    vec![param("id", TypeRef::I64)],
                    Some(TypeRef::StringUtf8),
                )
            }],
            ..module("net")
        };
        let model = BindingModel::build(&api(vec![m]), "weaveffi");
        match &model.modules[0].functions[0].shape {
            CallShape::Async(a) => {
                assert_eq!(a.launch.symbol, "weaveffi_net_fetch_async");
                assert_eq!(a.callback_type, "weaveffi_net_fetch_callback");
                let last_two: Vec<&str> = a
                    .launch
                    .params
                    .iter()
                    .rev()
                    .take(2)
                    .map(|p| p.name.as_str())
                    .collect();
                assert_eq!(last_two, ["context", "callback"]);
                // cancel_token slot is present before callback/context.
                assert!(a.launch.params.iter().any(|p| p.name == "cancel_token"));
                // callback prefix is (context, err, result).
                assert_eq!(a.callback_params[0].name, "context");
                assert_eq!(a.callback_params[1].name, "err");
            }
            _ => panic!("expected async"),
        }
    }

    #[test]
    fn iterator_function_has_next_and_destroy() {
        let m = Module {
            functions: vec![func(
                "get_messages",
                vec![],
                Some(TypeRef::Iterator(Box::new(TypeRef::StringUtf8))),
            )],
            ..module("events")
        };
        let model = BindingModel::build(&api(vec![m]), "weaveffi");
        match &model.modules[0].functions[0].shape {
            CallShape::Iterator(it) => {
                assert_eq!(it.iter_tag, "weaveffi_events_GetMessagesIterator");
                assert_eq!(it.launch.symbol, "weaveffi_events_get_messages");
                assert_eq!(it.next.symbol, "weaveffi_events_GetMessagesIterator_next");
                assert_eq!(
                    it.destroy_symbol,
                    "weaveffi_events_GetMessagesIterator_destroy"
                );
                assert_eq!(it.next.ret, CType::Int32);
                // out_item is `const char** out_item` for a string element.
                let out_item = &it.next.params[1];
                assert_eq!(out_item.name, "out_item");
                assert_eq!(out_item.ty.render_c("weaveffi"), "const char**");
            }
            _ => panic!("expected iterator"),
        }
    }

    #[test]
    fn struct_create_getters_and_builder() {
        let m = Module {
            structs: vec![StructDef {
                name: "Contact".into(),
                doc: None,
                fields: vec![
                    StructField {
                        name: "name".into(),
                        ty: TypeRef::StringUtf8,
                        doc: None,
                        default: None,
                    },
                    StructField {
                        name: "age".into(),
                        ty: TypeRef::I32,
                        doc: None,
                        default: None,
                    },
                ],
                builder: true,
            }],
            ..module("contacts")
        };
        let model = BindingModel::build(&api(vec![m]), "weaveffi");
        let s = &model.modules[0].structs[0];
        assert_eq!(s.c_tag, "weaveffi_contacts_Contact");
        assert_eq!(s.create.symbol, "weaveffi_contacts_Contact_create");
        assert_eq!(s.destroy_symbol, "weaveffi_contacts_Contact_destroy");
        assert_eq!(
            s.fields[0].getter_symbol,
            "weaveffi_contacts_Contact_get_name"
        );
        let b = s.builder.as_ref().unwrap();
        assert_eq!(b.builder_tag, "weaveffi_contacts_ContactBuilder");
        assert_eq!(b.new_symbol, "weaveffi_contacts_Contact_Builder_new");
        assert_eq!(b.setters[0].1, "weaveffi_contacts_Contact_Builder_set_name");
    }

    #[test]
    fn enum_constants_are_prefixed() {
        let m = Module {
            enums: vec![EnumDef {
                name: "Color".into(),
                doc: None,
                variants: vec![
                    EnumVariant {
                        name: "Red".into(),
                        value: 0,
                        doc: None,
                    },
                    EnumVariant {
                        name: "Green".into(),
                        value: 1,
                        doc: None,
                    },
                ],
            }],
            ..module("gfx")
        };
        let model = BindingModel::build(&api(vec![m]), "weaveffi");
        let e = &model.modules[0].enums[0];
        assert_eq!(e.c_tag, "weaveffi_gfx_Color");
        assert_eq!(e.variants[0].c_const, "weaveffi_gfx_Color_Red");
        assert_eq!(e.variants[1].c_const, "weaveffi_gfx_Color_Green");
    }

    #[test]
    fn callbacks_and_listeners_are_linked() {
        let m = Module {
            callbacks: vec![CallbackDef {
                name: "on_message".into(),
                params: vec![param("text", TypeRef::StringUtf8)],
                doc: None,
            }],
            listeners: vec![ListenerDef {
                name: "messages".into(),
                event_callback: "on_message".into(),
                doc: None,
            }],
            ..module("events")
        };
        let model = BindingModel::build(&api(vec![m]), "weaveffi");
        let mb = &model.modules[0];
        let cb = &mb.callbacks[0];
        assert_eq!(cb.c_fn_type, "weaveffi_events_on_message_fn");
        // context appended last.
        assert_eq!(cb.abi_params.last().unwrap().name, "context");
        let l = &mb.listeners[0];
        assert_eq!(l.register_symbol, "weaveffi_events_register_messages");
        assert_eq!(l.unregister_symbol, "weaveffi_events_unregister_messages");
        assert_eq!(l.callback_c_fn_type, "weaveffi_events_on_message_fn");
        assert!(mb.callback("on_message").is_some());
    }

    #[test]
    fn nested_modules_flatten_pre_order_with_paths() {
        let inner = Module {
            functions: vec![func("leaf_fn", vec![], None)],
            ..module("inner")
        };
        let outer = Module {
            functions: vec![func("outer_fn", vec![], None)],
            modules: vec![inner],
            ..module("outer")
        };
        let model = BindingModel::build(&api(vec![outer]), "weaveffi");
        let paths: Vec<&str> = model.modules.iter().map(|m| m.path.as_str()).collect();
        assert_eq!(paths, ["outer", "outer_inner"]);
        assert_eq!(
            model.modules[1].functions[0].c_base,
            "weaveffi_outer_inner_leaf_fn"
        );
    }
}