js2rust-bridge-macro 0.2.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
//! Proc-macro for generating Rust FFI bindings from js2rust C ABI export metadata.
//!
//! ## Usage
//!
//! ```rust,ignore
//! js2rust_bridge! {
//!     "js_src/main.js",    // core JS file path (relative to CARGO_MANIFEST_DIR)
//!     // Sync host functions (optional, comma-separated):
//!     host_add(i64, i64) -> i64,
//!     host_concat(str, str) -> str,
//!     // Async host functions (called with `await` from JS):
//!     async fetch_user(str) -> { id: i64, name: str },
//! }
//! ```
//!
//! The macro transpiles JS to Zig inline, writes output to
//! `.js2zig-cache/{group}/`, and generates Rust FFI bindings.
//! The group name is derived from the file name (sanitized for Zig identifiers).
//! A minimal `build.rs` is only needed to link the compiled static library.

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use serde::Deserialize;
use syn::{
    braced, parenthesized, parse::{Parse, ParseStream}, Ident, LitStr, Token
};

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

#[derive(Debug, Deserialize)]
struct CabiExport {
    name: String,
    params: Vec<CabiParam>,
    ret_type: String,
    has_free_func: bool,
}

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

// ── Macro input parsing ───────────────────────────────────────────

/// Parsed host function declaration: `[async] name(type1, type2) -> ret_type`
struct HostFnDecl {
    name: String,
    params: Vec<String>,
    return_type: String,
    is_async: bool,
    /// For async functions with struct return: Vec<(field_name, field_type)>
    async_return_fields: Vec<(String, String)>,
}

/// Full macro input.
struct MacroInput {
    /// Core JS file path (e.g. "js_src/main.js").
    js_file: String,
    /// Group name derived from the file stem (sanitized).
    group: String,
    host_fns: Vec<HostFnDecl>,
}

impl Parse for MacroInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        // Core JS file path (string literal)
        let js_file_lit: LitStr = input.parse()?;
        let js_file = js_file_lit.value();

        // Derive group name from file stem, sanitized for Zig identifiers.
        let stem = std::path::Path::new(&js_file)
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("main");
        let group = js2zig_core::analyzer::sanitize_module_name(stem);

        // Optional host function declarations
        let mut host_fns = Vec::new();
        while input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
            if input.is_empty() {
                break;
            }

            // Parse first — could be `async` keyword or function name
            let is_async = input.peek(Token![async]);
            if is_async {
                input.parse::<Token![async]>()?;
            }
            let name: Ident = input.parse()?;
            let name_str = name.to_string();

            // parameter types in parentheses
            let paren_content;
            parenthesized!(paren_content in input);
            let mut params = Vec::new();
            while !paren_content.is_empty() {
                let ty: Ident = paren_content.parse()?;
                params.push(ty.to_string());
                if paren_content.peek(Token![,]) {
                    paren_content.parse::<Token![,]>()?;
                }
            }

            // return type after `->`
            input.parse::<Token![->]>()?;

            // Check for struct return type `{ field: type, ... }` or simple Ident
            let (return_type, async_fields) = if input.peek(syn::token::Brace) {
                let struct_content;
                braced!(struct_content in input);
                let mut fields = Vec::new();
                while !struct_content.is_empty() {
                    let field_name: Ident = struct_content.parse()?;
                    struct_content.parse::<Token![:]>()?;
                    let field_type: Ident = struct_content.parse()?;
                    fields.push((field_name.to_string(), field_type.to_string()));
                    if struct_content.peek(Token![,]) {
                        struct_content.parse::<Token![,]>()?;
                    }
                }
                // For struct returns, return_type is "void" (actual type is in async_fields)
                ("void".to_string(), fields)
            } else {
                let ret: Ident = input.parse()?;
                (ret.to_string(), Vec::new())
            };

            host_fns.push(HostFnDecl {
                name: name_str,
                params,
                return_type,
                is_async,
                async_return_fields: async_fields,
            });
        }

        Ok(MacroInput {
            js_file,
            group,
            host_fns,
        })
    }
}

// ── Type name conversion ──────────────────────────────────────────

/// Convert macro-level type name to `js2zig_core::HostType`.
fn type_name_to_host_type(name: &str) -> Result<js2zig_core::HostType, String> {
    match name {
        "i64" => Ok(js2zig_core::HostType::I64),
        "i32" => Ok(js2zig_core::HostType::I32),
        "f64" => Ok(js2zig_core::HostType::F64),
        "bool" => Ok(js2zig_core::HostType::Bool),
        "str" => Ok(js2zig_core::HostType::Str),
        "void" => Ok(js2zig_core::HostType::Void),
        other => Err(format!("js2rust_bridge: unknown host type '{}'. \
            Valid types: i64, i32, f64, bool, str, void", other)),
    }
}

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

/// Function-like proc-macro: `js2rust_bridge!("js_src/main.js", host_fns...)`.
///
/// Transpiles JS to Zig, generates Rust FFI bindings, and optionally
/// runs `zig build` to compile the static library.
#[proc_macro]
pub fn js2rust_bridge(input: TokenStream) -> TokenStream {
    let input_tokens: proc_macro2::TokenStream = input.into();

    match syn::parse2::<MacroInput>(input_tokens) {
        Ok(parsed) => generate(&parsed),
        Err(e) => e.to_compile_error().into(),
    }
}

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

fn generate(input: &MacroInput) -> TokenStream {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
        .unwrap_or_else(|_| ".".to_string());

    // Resolve core JS file path
    let js_file_path = std::path::Path::new(&manifest_dir).join(&input.js_file);

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

    // Convert host function declarations to js2zig_core::HostFunction
    let mut host_functions = Vec::new();
    for hf in &input.host_fns {
        let params: Result<Vec<_>, _> = hf.params.iter()
            .map(|t| type_name_to_host_type(t))
            .collect();
        let params = match params {
            Ok(p) => p,
            Err(e) => return syn::Error::new(proc_macro2::Span::call_site(), e)
                .to_compile_error().into(),
        };

        let return_type = match type_name_to_host_type(&hf.return_type) {
            Ok(js2zig_core::HostType::Void) => None,
            Ok(t) => Some(t),
            Err(e) => return syn::Error::new(proc_macro2::Span::call_site(), e)
                .to_compile_error().into(),
        };

        // Convert async return fields
        let async_return_fields: Result<Vec<_>, _> = hf.async_return_fields.iter()
            .map(|(name, ty)| {
                type_name_to_host_type(ty).map(|t| (name.clone(), t))
            })
            .collect();
        let async_return_fields = match async_return_fields {
            Ok(v) => v,
            Err(e) => return syn::Error::new(proc_macro2::Span::call_site(), e)
                .to_compile_error().into(),
        };

        host_functions.push(js2zig_core::HostFunction {
            name: hf.name.clone(),
            params,
            return_type,
            is_async: hf.is_async,
            async_return_fields,
        });
    }

    // Build ProjectConfig
    let config = js2zig_core::ProjectConfig {
        name: input.group.clone(),
        js_file: js_file_path.clone(),
        out_dir: cache_dir.clone(),
        host_config: if host_functions.is_empty() {
            None
        } else {
            Some(js2zig_core::HostConfig {
                functions: host_functions,
            })
        },
        force_rebuild: false,
        run_zig_build: false,
    };

    // Transpile!
    let project_result = match js2zig_core::transpile_project(&config) {
        Ok(result) => result,
        Err(e) => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("js2rust_bridge: transpilation failed: {}", e),
            )
            .to_compile_error()
            .into();
        }
    };

    // Find the group result (there is exactly one group from analyze_single_group)
    let group_result = project_result.groups.first();

    let group_result = match group_result {
        Some(g) => g,
        None => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                "js2rust_bridge: no groups found in transpilation result",
            )
            .to_compile_error()
            .into();
        }
    };

    // Parse cabi_exports_json
    let exports: Vec<CabiExport> = match serde_json::from_str(&group_result.cabi_exports_json) {
        Ok(v) => v,
        Err(e) => {
            return syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("js2rust_bridge: failed to parse cabi_exports: {}", e),
            )
            .to_compile_error()
            .into();
        }
    };

    // Optionally run zig build (side effect — generates static library for linking)
    let zig_project_dir = cache_dir.join(&input.group);
    if zig_project_dir.join("build.zig").exists() {
        let _ = std::process::Command::new("zig")
            .arg("build")
            .current_dir(&zig_project_dir)
            .status();
    }

    // Generate Rust FFI bindings from cabi exports
    let generated = generate_bindings(&exports, &input.group);

    match generated.parse::<TokenStream>() {
        Ok(ts) => ts,
        Err(e) => syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("internal error: {}", e),
        )
        .to_compile_error()
        .into(),
    }
}

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

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

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

    for exp in exports {
        let fn_name = format_ident!("{}", exp.name);
        let free_fn_name = format_ident!("free_{}", exp.name);

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

        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.has_free_func {
            extern_fns.push(quote! {
                pub fn #free_fn_name(ptr: *mut std::ffi::c_void);
            });
        }

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

    // Always provide js2rust_init/deinit safe wrappers (C ABI exports from lib.zig)
    let runtime_init = quote! {
        /// Initialize the Zig runtime (allocator + Io for async functions).
        /// Call this before any async export function.
        pub fn js2rust_init() {
            extern "C" {
                #[link_name = "js2rust_init"]
                fn _js2rust_init();
            }
            unsafe { _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);

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

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

            #(#safe_wrappers)*
        }

        pub use #safe_mod::*;
    };

    output.to_string()
}

fn generate_safe_wrapper(
    exp: &CabiExport,
    fn_name: &syn::Ident,
    free_fn_name: &syn::Ident,
    raw_mod: &syn::Ident,
    group_suffix: &str,
) -> proc_macro2::TokenStream {
    let wrapper_name = format_ident!("{}_{}", exp.name, group_suffix);
    let mut safe_params = Vec::new();
    let mut ffi_args = Vec::new();

    for (idx, param) in exp.params.iter().enumerate() {
        let param_ident = format_ident!("arg{}", idx);
        let safe_ty = zig_type_to_rust_safe_type(&param.zig_type);
        safe_params.push(quote! { #param_ident: #safe_ty });
        ffi_args.push(convert_safe_to_ffi(&param.zig_type, &param_ident));
    }

    let (ret_ty, call_expr) = if exp.ret_type == "[]const u8" {
        (
            quote! { String },
            quote! {
                {
                    let ptr = unsafe { super::#raw_mod::#fn_name(#(#ffi_args),*) };
                    if ptr.is_null() {
                        String::new()
                    } else {
                        let s = unsafe {
                            std::ffi::CStr::from_ptr(ptr)
                                .to_string_lossy()
                                .into_owned()
                        };
                        unsafe { super::#raw_mod::#free_fn_name(ptr as *mut std::ffi::c_void) };
                        s
                    }
                }
            },
        )
    } else {
        let rust_ret = zig_ret_type_to_rust_safe(&exp.ret_type);
        (
            rust_ret.clone(),
            quote! {
                unsafe { super::#raw_mod::#fn_name(#(#ffi_args),*) }
            },
        )
    };

    quote! {
        #[allow(non_snake_case)]
        pub fn #wrapper_name( #(#safe_params),* ) -> #ret_ty {
            #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! { () },
        _ => 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 },
        "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 convert_safe_to_ffi(zig_type: &str, ident: &syn::Ident) -> proc_macro2::TokenStream {
    match zig_type {
        "[]const u8" => quote! { std::ffi::CString::new(#ident).unwrap().into_raw() },
        _ => quote! { #ident },
    }
}

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 },
    }
}