js2rust-bridge-macro 0.18.0

Proc-macro for generating Rust FFI bindings from js2rust C ABI export metadata
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
//! Proc-macro for generating Rust FFI bindings from js2rust C ABI export metadata.
//!
//! ## Usage
//!
//! ```rust,ignore
//! js2rust_bridge!();
//! ```
//!
//! The macro reads `js2rust.toml` from the crate root, transpiles JS to Zig,
//! writes output to `.js2zig-cache/{project_name}/`, and generates Rust FFI bindings.
//! The project name is derived from the file name (sanitized for Zig identifiers).
//! A minimal `build.rs` only needs `js2rust_bridge::build()`.

use indexmap::IndexMap;
use js2zig_core::toml_config::HostFnToml;
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use serde::Deserialize;

mod host_fn;

/// `#[host_fn]` attribute macro — eliminates unsafe C ABI plumbing from host functions.
///
/// Write a normal Rust function using SDK types (`HostStr`, `JsStr`, `JsStrField`).
/// The macro generates the `unsafe extern "C"` wrapper with correct C ABI signature.
///
/// ## Example
///
/// ```rust,ignore
/// use js2rust_bridge::sdk::{HostStr, JsStr};
///
/// #[host_fn]
/// fn host_concat(s1: HostStr, s2: HostStr) -> JsStr {
///     JsStr::new(&format!("{}{}", &s1, &s2))
/// }
/// ```
#[proc_macro_attribute]
pub fn host_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
    host_fn::host_fn_impl(attr.into(), item.into()).into()
}

// ── C ABI export metadata (mirrors the JSON schema) ───────────────

#[derive(Debug, Deserialize)]
struct CabiExport {
    name: String,
    params: Vec<CabiParam>,
    ret_type: String,
    #[serde(default)]
    can_throw: bool,
    /// Struct return: name of the returned struct (e.g. "FetchUserResult")
    #[serde(default)]
    ret_struct_name: Option<String>,
    /// Struct return: fields of the returned struct (for generating #[repr(C)] struct)
    #[serde(default)]
    ret_struct_fields: Option<Vec<CabiStructField>>,
}

#[derive(Debug, Deserialize)]
struct CabiStructField {
    name: String,
    /// C ABI type string (for FFI)
    cabi_type: String,
}

#[derive(Debug, Deserialize)]
struct CabiParam {
    name: String,
    zig_type: String,
}

// ── Main proc-macro entry point ───────────────────────────────────

/// Function-like proc-macro: `js2rust_bridge!()`.
///
/// Reads `js2rust.toml` from the crate root, transpiles JS to Zig,
/// and generates Rust FFI bindings.  Zero-argument — all configuration
/// lives in the TOML file.
#[proc_macro]
pub fn js2rust_bridge(input: TokenStream) -> TokenStream {
    // Accept empty input only
    let _input: proc_macro2::TokenStream = input.into();

    generate().unwrap_or_else(|e| e.into())
}

// ── Transpile + generate FFI ──────────────────────────────────────

fn generate() -> Result<TokenStream, proc_macro2::TokenStream> {
    let config = js2zig_core::toml_config::Js2rustConfig::from_manifest_dir();
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());

    // js_files must not be empty
    if config.project.js_files.is_empty() {
        return Err(syn::Error::new(
            proc_macro2::Span::call_site(),
            "js2rust_bridge: project.js_files is empty in js2rust.toml",
        )
        .to_compile_error());
    }

    // Derive project name from first js_files entry stem
    let project_name = config.project_name();

    // Resolve js_dir and js_files from TOML fields.
    // js_dir is an absolute path; js_files are plain filenames.
    let js_dir: std::path::PathBuf =
        std::path::Path::new(&manifest_dir).join(&config.project.js_dir);
    let js_files = config.project.js_files.clone();

    // Resolve cache directory
    let cache_dir = std::path::Path::new(&manifest_dir).join(".js2zig-cache");

    // Convert host function declarations — now uses js2zig_core canonical implementation
    let host_config = config
        .to_host_config()
        .map_err(|e| syn::Error::new(proc_macro2::Span::call_site(), e).to_compile_error())?;

    // Determine Zig optimization level: TOML override > CARGO_CFG_DEBUG_ASSERTIONS auto-detect.
    // In proc-macro context, `PROFILE` is not available, but `CARGO_CFG_DEBUG_ASSERTIONS`
    // is set to "true" for debug builds and unset for release builds.
    let zig_optimize = config.build.zig_optimize.clone().unwrap_or_else(|| {
        let is_debug = std::env::var("CARGO_CFG_DEBUG_ASSERTIONS")
            .map(|v| v == "true")
            .unwrap_or(true);
        if is_debug {
            "Debug".into()
        } else {
            "ReleaseSafe".into()
        }
    });

    // Build ProjectConfig
    let project_config = js2zig_core::ProjectConfig {
        js_dir,
        js_files,
        out_dir: cache_dir.clone(),
        host_config,
        build: js2zig_core::BuildConfig {
            force_rebuild: config.build.force_rebuild,
            run_zig_build: config.build.run_zig_build,
            zig_optimize: Some(zig_optimize),
            is_build_script: false, // proc-macro context — suppress all stdout/stderr
        },
    };

    // Parse transpilation results (flat ProjectResult structure)
    let project_result = js2zig_core::transpile_project(&project_config).map_err(|e| {
        syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("js2rust_bridge: transpilation failed for '{project_name}': {e}"),
        )
        .to_compile_error()
    })?;

    // Parse cabi_exports_json
    let exports: Vec<CabiExport> = serde_json::from_str(&project_result.cabi_exports_json)
        .map_err(|e| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                format!(
                    "js2rust_bridge: failed to parse cabi_exports for project '{}': {}",
                    project_result.project_name, e
                ),
            )
            .to_compile_error()
        })?;

    // Generate Rust FFI bindings
    let mut generated = generate_bindings(&exports, &project_name);

    // Generate host function stub documentation
    if !config.host_functions.is_empty() {
        if let Some(host_stubs) = generate_host_stubs(&config.host_functions, &project_name) {
            generated.push('\n');
            generated.push_str(&host_stubs);
        }
    }

    generated.parse::<TokenStream>().map_err(|e| {
        syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("js2rust_bridge: internal error generating bindings for '{project_name}': {e}"),
        )
        .to_compile_error()
    })
}

// ── FFI bindings generation ───────────────────────────────────────

fn generate_bindings(exports: &[CabiExport], project_name: &str) -> String {
    let mut extern_fns = Vec::new();
    let mut safe_wrappers = Vec::new();
    let mut struct_defs = Vec::new();
    let mut needs_jsstr = false;

    let raw_mod = format_ident!("__js2rust_ffi_raw_{project_name}");
    let safe_mod = format_ident!("__js2rust_ffi_safe_{project_name}");

    // Collect struct definitions from ret_struct_name/ret_struct_fields
    for exp in exports {
        if let (Some(struct_name), Some(fields)) = (&exp.ret_struct_name, &exp.ret_struct_fields) {
            if fields.is_empty() {
                continue;
            }
            let struct_ident = format_ident!("{}", struct_name);
            let mut field_defs = Vec::new();
            for f in fields {
                let field_name = format_ident!("{}", f.name);
                let field_ty = cabi_type_to_rust_ffi(&f.cabi_type);
                field_defs.push(quote! { pub #field_name: #field_ty });
            }
            struct_defs.push(quote! {
                #[repr(C)]
                #[derive(Debug, Copy, Clone)]
                pub struct #struct_ident {
                    #(#field_defs),*
                }
            });
        }
    }

    for exp in exports {
        // Skip runtime init/deinit — they are provided by runtime_init block below.
        if exp.name == "js2rust_init" || exp.name == "js2rust_deinit" {
            // Still generate the extern FFI binding so the raw_mod can link them.
            let fn_name = format_ident!("{}", exp.name);
            let ret_ty = zig_ret_type_to_rust_ffi(&exp.ret_type);
            extern_fns.push(quote! {
                pub fn #fn_name() -> #ret_ty;
            });
            continue;
        }

        let fn_name = format_ident!("{}", exp.name);

        let mut extern_params = Vec::new();
        for param in &exp.params {
            let param_ident = format_ident!("{}", param.name);
            let param_ty = zig_type_to_rust_ffi_type(&param.zig_type);
            extern_params.push(quote! { #param_ident: #param_ty });
        }

        // Struct return: use out-pointer parameter
        if let Some(ret_struct_name) = &exp.ret_struct_name {
            let struct_name = format_ident!("{}", ret_struct_name);
            extern_params.push(quote! { out: *mut #struct_name });
            extern_fns.push(quote! {
                pub fn #fn_name( #(#extern_params),* );
            });
        } else {
            // Non-struct return: normal C ABI
            let has_err_out = exp.can_throw && exp.ret_type != "StrRet";
            if has_err_out {
                extern_params.push(quote! { err_out: *mut *const std::ffi::c_char });
            }
            let ret_ty = zig_ret_type_to_rust_ffi(&exp.ret_type);
            extern_fns.push(quote! {
                pub fn #fn_name( #(#extern_params),* ) -> #ret_ty;
            });
            if exp.ret_type == "StrRet" {
                needs_jsstr = true;
            }
        }

        let safe_wrapper = generate_safe_wrapper(exp, &fn_name, &raw_mod);
        safe_wrappers.push(safe_wrapper);
    }

    // Always provide js2rust_init/deinit safe wrappers
    let runtime_init = quote! {
        use std::sync::Once;

        static INIT: Once = Once::new();

        /// Initialize the Zig runtime (allocator + Io for async functions).
        /// Call this before any async export function.
        /// Safe to call multiple times (uses Once internally).
        /// Panics if the Zig-side allocator initialization fails (OOM).
        pub fn js2rust_init() {
            extern "C" {
                #[link_name = "js2rust_init"]
                fn _js2rust_init();
            }
            INIT.call_once(|| {
                unsafe { _js2rust_init() };
            });
        }

        /// Ensure the Zig runtime is initialized (calls js2rust_init() exactly once).
        /// Called automatically by all generated wrapper functions.
        fn ensure_initialized() {
            js2rust_init();
        }

        /// Release Zig runtime resources.
        pub fn js2rust_deinit() {
            extern "C" {
                #[link_name = "js2rust_deinit"]
                fn _js2rust_deinit();
            }
            unsafe { _js2rust_deinit() };
        }
    };
    safe_wrappers.push(runtime_init);

    // Conditionally define __JsStr in raw_mod for StrRet-returning functions.
    let jsstr_def = if needs_jsstr {
        quote! {
            #[repr(C)]
            #[derive(Debug, Copy, Clone)]
            pub struct __JsStr { pub ptr: *const u8, pub len: isize }
        }
    } else {
        quote! {}
    };

    let output = quote! {
        #[allow(non_snake_case)]
        #[allow(dead_code)]
        #[allow(clippy::not_unsafe_ptr_arg_deref)]
        mod #raw_mod {
            #jsstr_def
            #(#struct_defs)*
            unsafe extern "C" {
                #(#extern_fns)*
            }
        }

        #[allow(non_snake_case)]
        #[allow(dead_code)]
        #[allow(clippy::not_unsafe_ptr_arg_deref)]
        mod #safe_mod {
            use super::#raw_mod;

            #(#safe_wrappers)*
        }

        pub use #safe_mod::*;
    };

    output.to_string()
}

/// Generate host function stubs using SDK types (HostStr, JsStr, JsStrField).
///
/// Generated stubs call `HostStr::from_raw(ptr, len)` at the top and
/// `JsStr::new(&result)` for string returns — no raw pointer manipulation
/// needed in the user's business logic.
///
/// Async functions that return a struct get the struct definition with
/// `JsStrField` fields, already `#[repr(C)]`-compatible.
fn generate_host_stubs(host_fns: &[HostFnToml], project_name: &str) -> Option<String> {
    if host_fns.is_empty() {
        return None;
    }

    let stub_mod = format_ident!("__js2rust_host_stubs_{project_name}");

    let mut async_struct_defs = Vec::new();
    let mut extern_fns = Vec::new();

    for hf in host_fns {
        let fn_sym = format_ident!("{}", hf.name);

        // Build C ABI param types: string → ptr+len, others → native
        let mut cabi_params = Vec::new();
        let mut param_conversions = Vec::new();

        for (i, param_ty) in hf.params.iter().enumerate() {
            if param_ty == "str" {
                let ptr_name = format_ident!("arg{}_ptr", i);
                let len_name = format_ident!("arg{}_len", i);
                let var_name = format_ident!("arg{}", i);
                cabi_params.push(quote! { #ptr_name: *const u8, #len_name: usize });
                param_conversions.push(quote! {
                    let #var_name = js2rust_bridge::sdk::HostStr::from_raw(#ptr_name, #len_name);
                });
            } else {
                let name = format_ident!("arg{}", i);
                let rust_ty = host_type_to_rust_cabi_ffi(param_ty);
                cabi_params.push(quote! { #name: #rust_ty });
                // No conversion needed for primitives
            }
        }

        // Return type
        let ret_ty = if !hf.async_returns.is_empty() {
            let struct_name = format_ident!(
                "Host{}Result",
                js2zig_core::toml_config::pascal_case(&hf.name)
            );
            async_struct_defs.push(generate_async_struct(&struct_name, &hf.async_returns));
            quote! { #struct_name }
        } else if hf.returns.as_deref() == Some("str") {
            quote! { js2rust_bridge::sdk::JsStr }
        } else if hf.returns.as_deref() == Some("void") || hf.returns.is_none() {
            quote! { () }
        } else {
            host_type_to_rust_cabi_ffi(hf.returns.as_deref().expect("checked is_none above"))
        };

        // Doc comment
        let doc = build_host_stub_doc(hf);

        // Build the body — param conversions + unimplemented placeholder
        let body = if hf.returns.as_deref() == Some("str") {
            // String return: show example of JsStr::new()
            let arg_refs: Vec<_> = (0..hf.params.len())
                .map(|i| {
                    let name = format_ident!("arg{}", i);
                    if hf.params[i] == "str" {
                        quote! { &#name }
                    } else {
                        quote! { #name }
                    }
                })
                .collect();
            let todo_msg = format!(
                "TODO: implement {} — replace with your logic, return sdk::JsStr::new(&result)",
                hf.name
            );
            quote! {
                #(#param_conversions)*
                let _ = (#(#arg_refs),*); // suppress unused variable warnings
                unimplemented!(#todo_msg);
            }
        } else if !hf.async_returns.is_empty() {
            // Async struct return: show example of struct construction with JsStrField::new()
            let todo_msg = format!(
                "TODO: implement {} — replace with your async logic, use JsStrField::new(&name)",
                hf.name
            );
            quote! {
                #(#param_conversions)*
                unimplemented!(#todo_msg);
            }
        } else {
            // Plain return (i64, f64, etc.)
            quote! {
                #(#param_conversions)*
                unimplemented!("TODO: implement {}", stringify!(#fn_sym));
            }
        };

        extern_fns.push(quote! {
            #[doc = #doc]
            #[allow(dead_code)]
            pub unsafe extern "C" fn #fn_sym( #(#cabi_params),* ) -> #ret_ty {
                #body
            }
        });
    }

    let output = quote! {
        /// Host function stubs generated by js2rust_bridge — uses `js2rust_bridge::sdk` types.
        ///
        /// Copy the function signatures below into your `host.rs` with `#[unsafe(no_mangle)]`
        /// and replace the `unimplemented!()` bodies with your actual business logic.
        ///
        /// The SDK handles all C ABI conversion automatically:
        /// - `HostStr::from_raw(ptr, len)` converts string params to `&str`
        /// - `JsStr::new(&s)` allocates return strings in Zig Arena
        /// - `JsStrField::new(&s)` for async struct string fields
        #[allow(dead_code, non_snake_case, clippy::not_unsafe_ptr_arg_deref)]
        mod #stub_mod {
            // SDK types are accessed via crate path, no import needed

            #(#async_struct_defs)*

            #(#extern_fns)*
        }
    };

    Some(output.to_string())
}

/// Generate an async return struct definition with SDK types.
fn generate_async_struct(
    struct_name: &syn::Ident,
    fields: &IndexMap<String, String>,
) -> proc_macro2::TokenStream {
    let mut struct_fields = Vec::new();
    for (name, ty) in fields {
        let field_name = format_ident!("{}", name);
        let field_type = if ty == "str" {
            quote! { js2rust_bridge::sdk::JsStrField }
        } else {
            host_type_to_rust_cabi_ffi(ty)
        };
        struct_fields.push(quote! {
            pub #field_name: #field_type,
        });
    }

    let struct_doc = format!(
        "C ABI return struct for `{}` (generated by js2rust_bridge).\n\
         Uses `JsStrField` for string fields — allocated in Zig Arena.",
        struct_name
    );

    quote! {
        #[doc = #struct_doc]
        #[repr(C)]
        pub struct #struct_name {
            #(#struct_fields)*
        }
    }
}

/// Build a doc string for the host function stub.
fn build_host_stub_doc(hf: &HostFnToml) -> String {
    let js_params = hf.params.join(", ");
    let js_ret = if !hf.async_returns.is_empty() {
        format!(
            "{{ {} }}",
            hf.async_returns
                .iter()
                .map(|(k, v)| format!("{}: {}", k, v))
                .collect::<Vec<_>>()
                .join(", ")
        )
    } else {
        hf.returns.as_deref().unwrap_or("void").to_string()
    };

    let cabi_sig = hf
        .params
        .iter()
        .enumerate()
        .map(|(i, t)| {
            if t == "str" {
                format!("arg{}_ptr: *const u8, arg{}_len: usize", i, i)
            } else {
                format!("arg{}: {}", i, t)
            }
        })
        .collect::<Vec<_>>()
        .join(", ");

    let ret_sig = if hf.returns.as_deref() == Some("str") {
        "JsStr".to_string()
    } else if !hf.async_returns.is_empty() {
        format!(
            "Host{}Result",
            js2zig_core::toml_config::pascal_case(&hf.name)
        )
    } else if hf.returns.as_deref() == Some("void") || hf.returns.is_none() {
        "void".to_string()
    } else {
        hf.returns
            .as_deref()
            .expect("checked is_none above")
            .to_string()
    };

    let sdk_note = if hf.params.contains(&"str".to_string())
        || hf.returns.as_deref() == Some("str")
        || !hf.async_returns.is_empty()
    {
        "\nSDK types used: HostStr::from_raw(ptr,len) for params, JsStr::new(&result) / JsStrField::new(&field) for returns"
    } else {
        ""
    };

    format!(
        "Host fn: {name}({js_params}) -> {js_ret}\
         \nC ABI: fn({cabi_sig}) -> {ret_sig}\
         {sdk_note}",
        name = hf.name,
        js_params = js_params,
        js_ret = js_ret,
        cabi_sig = cabi_sig,
        ret_sig = ret_sig,
        sdk_note = sdk_note,
    )
}

/// Convert type name to Rust C ABI FFI type.
fn host_type_to_rust_cabi_ffi(type_name: &str) -> proc_macro2::TokenStream {
    match type_name {
        "i64" => quote! { i64 },
        "i32" => quote! { i32 },
        "f64" => quote! { f64 },
        "bool" => quote! { bool },
        "void" => quote! { () },
        _ => quote! { *mut std::ffi::c_void },
    }
}

fn generate_safe_wrapper(
    exp: &CabiExport,
    fn_name: &syn::Ident,
    raw_mod: &syn::Ident,
) -> proc_macro2::TokenStream {
    let wrapper_name = fn_name.clone();
    let mut safe_params = Vec::new();
    let mut ffi_args = Vec::new();
    let mut cstr_bindings = Vec::new();

    for param in &exp.params {
        let param_ident = format_ident!("{}", param.name);
        let safe_ty = zig_type_to_rust_safe_type(&param.zig_type);
        safe_params.push(quote! { #param_ident: #safe_ty });
        if param.zig_type == "[]const u8" {
            // Bind CString to a local variable so it outlives the FFI call,
            // then pass its pointer. This avoids the memory leak from into_raw()
            // where ownership was transferred to the caller but never reclaimed.
            let cstr_ident = format_ident!("_cstr_{}", param.name);
            cstr_bindings.push(quote! {
                let #cstr_ident = std::ffi::CString::new(#param_ident)
                    .expect("input string contains NUL byte");
            });
            ffi_args.push(quote! { #cstr_ident.as_ptr() });
        } else {
            ffi_args.push(quote! { #param_ident });
        }
    }

    // Struct return: use out-pointer
    if let Some(ref struct_name) = exp.ret_struct_name {
        let struct_ident = format_ident!("{}", struct_name);
        let ret_ty = quote! { #raw_mod::#struct_ident };
        let call_expr = quote! {
            {
                #(#cstr_bindings)*
                let mut result: #raw_mod::#struct_ident = unsafe { std::mem::zeroed() };
                unsafe { super::#raw_mod::#fn_name(#(#ffi_args),* , &mut result) };
                result
            }
        };
        return quote! {
            #[allow(non_snake_case)]
            pub fn #wrapper_name( #(#safe_params),* ) -> #ret_ty {
                ensure_initialized();
                #call_expr
            }
        };
    }

    // Non-struct returns
    let needs_jsstr = exp.ret_type == "StrRet";
    let has_err_out = exp.can_throw && exp.ret_type != "StrRet";

    let (ret_ty, call_expr) = if needs_jsstr {
        (
            quote! { Result<String, String> },
            quote! {
                {
                    #(#cstr_bindings)*
                    let ret: #raw_mod::__JsStr = unsafe { super::#raw_mod::#fn_name(#(#ffi_args),*) };
                    if ret.len < 0 {
                        let err_len = (-ret.len) as usize;
                        let err_msg = if err_len > 0 && !ret.ptr.is_null() {
                            let slice = unsafe { std::slice::from_raw_parts(ret.ptr, err_len) };
                            String::from_utf8_lossy(slice).into_owned()
                        } else {
                            "unknown async error".to_string()
                        };
                        return Err(err_msg);
                    }
                    if ret.ptr.is_null() {
                        Ok(String::new())
                    } else {
                        let len = ret.len as usize;
                        let slice = unsafe { std::slice::from_raw_parts(ret.ptr, len) };
                        Ok(String::from_utf8_lossy(slice).into_owned())
                    }
                }
            },
        )
    } else if has_err_out {
        let rust_ret = zig_ret_type_to_rust_safe(&exp.ret_type);
        let rust_ret_wrapped = match exp.ret_type.as_str() {
            "void" => quote! { Result<(), String> },
            _ => quote! { Result<#rust_ret, String> },
        };
        let extract_result = match exp.ret_type.as_str() {
            "void" => quote! { Ok(()) },
            _ => quote! { Ok(result) },
        };
        let mut all_ffi_args: Vec<proc_macro2::TokenStream> = ffi_args.clone();
        all_ffi_args.push(quote! { &mut err_ptr });
        (
            rust_ret_wrapped,
            quote! {
                {
                    #(#cstr_bindings)*
                    let mut err_ptr: *const std::ffi::c_char = std::ptr::null();
                    let result = unsafe { super::#raw_mod::#fn_name(#(#all_ffi_args),*) };
                    if !err_ptr.is_null() {
                        let err_msg = unsafe { std::ffi::CStr::from_ptr(err_ptr).to_string_lossy().into_owned() };
                        return Err(err_msg);
                    }
                    #extract_result
                }
            },
        )
    } else {
        let rust_ret = zig_ret_type_to_rust_safe(&exp.ret_type);
        (
            rust_ret,
            quote! {
                { #(#cstr_bindings)* unsafe { super::#raw_mod::#fn_name(#(#ffi_args),*) } }
            },
        )
    };

    quote! {
        #[allow(non_snake_case)]
        pub fn #wrapper_name( #(#safe_params),* ) -> #ret_ty {
            ensure_initialized();
            #call_expr
        }
    }
}

// ── Type conversion helpers ───────────────────────────────────────

fn zig_type_to_rust_ffi_type(zig_type: &str) -> proc_macro2::TokenStream {
    match zig_type {
        "[]const u8" => quote! { *const std::ffi::c_char },
        "i32" => quote! { i32 },
        "i64" => quote! { i64 },
        "f64" => quote! { f64 },
        "bool" => quote! { bool },
        "void" => quote! { () },
        "*usize" => quote! { *mut usize },
        _ => quote! { *mut std::ffi::c_void },
    }
}

fn zig_ret_type_to_rust_ffi(ret_type: &str) -> proc_macro2::TokenStream {
    match ret_type {
        "[]const u8" => quote! { *const std::ffi::c_char },
        "StrRet" => quote! { __JsStr },
        "i32" => quote! { i32 },
        "i64" => quote! { i64 },
        "f64" => quote! { f64 },
        "bool" => quote! { bool },
        "void" => quote! { () },
        _ => quote! { *mut std::ffi::c_void },
    }
}

fn zig_type_to_rust_safe_type(zig_type: &str) -> proc_macro2::TokenStream {
    match zig_type {
        "[]const u8" => quote! { &str },
        "i32" => quote! { i32 },
        "i64" => quote! { i64 },
        "f64" => quote! { f64 },
        "bool" => quote! { bool },
        _ => quote! { *mut std::ffi::c_void },
    }
}

fn zig_ret_type_to_rust_safe(ret_type: &str) -> proc_macro2::TokenStream {
    match ret_type {
        "[]const u8" => quote! { String },
        "i32" => quote! { i32 },
        "i64" => quote! { i64 },
        "f64" => quote! { f64 },
        "bool" => quote! { bool },
        "void" => quote! { () },
        _ => quote! { *mut std::ffi::c_void },
    }
}

/// Convert C ABI type string to Rust FFI type (for struct fields).
fn cabi_type_to_rust_ffi(cabi_type: &str) -> proc_macro2::TokenStream {
    match cabi_type {
        "i64" => quote! { i64 },
        "f64" => quote! { f64 },
        "bool" => quote! { bool },
        "str" => quote! { js2rust_bridge::sdk::JsStrField },
        "StrRet" => quote! { __JsStr },
        "struct" => quote! { *mut std::ffi::c_void }, // Should not happen for struct fields
        // Handle [N]u8 as string type (JsStrField)
        other if other.starts_with('[') && other.ends_with("]u8") => {
            quote! { js2rust_bridge::sdk::JsStrField }
        }
        _ => {
            // Unknown cabi_type — fallback to opaque pointer
            quote! { *mut std::ffi::c_void }
        }
    }
}