js2zig-core 0.18.0

Core JS-to-Zig transpiler library
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
// zigir/passes/validate.rs
// ValidatePass — structural validation of an IrModule.
//
// Checks:
//   1. Type consistency: expressions have plausible types
//   2. Name uniqueness: no duplicate top-level identifiers
//   3. Closure integrity: captured lists match actual references
//   4. C ABI compatibility: exported function signatures use C-safe types

use crate::types::ZigType;
use crate::zigir::passes::{IrPass, PassResult};
use crate::zigir::source_span::{DiagnosticLevel, IrDiagnostic};
use crate::zigir::types::{IrAssignTarget, IrBlock, IrDecl, IrExpr, IrFnDecl, IrModule, IrStmt};

use std::cell::RefCell;

use super::{collect_idents, walk};

/// Validation pass: checks structural integrity of the IR.
///
/// Produces warnings for suspicious patterns and errors for violations
/// that would cause incorrect Zig output. Does NOT modify the IR.
pub struct ValidatePass {
    /// Collected diagnostics for the current run.
    diagnostics: Vec<IrDiagnostic>,
}

impl ValidatePass {
    pub fn new() -> Self {
        Self {
            diagnostics: Vec::new(),
        }
    }

    fn error(&mut self, msg: String) {
        self.diagnostics.push(IrDiagnostic {
            level: DiagnosticLevel::Error,
            span: None,
            message: msg,
        });
    }

    fn warn(&mut self, msg: String) {
        self.diagnostics.push(IrDiagnostic {
            level: DiagnosticLevel::Warning,
            span: None,
            message: msg,
        });
    }

    // ── Top-level name uniqueness ────────────────────

    fn check_name_uniqueness(&mut self, module: &IrModule) {
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();

        // Check typedefs
        for td in &module.typedefs {
            if !seen.insert(td.name.clone()) {
                self.error(format!("duplicate top-level name: '{}'", td.name));
            }
        }

        // Check closure structs
        for cs in &module.closure_structs {
            let zig_name = cs.name.zig_name.clone();
            if !seen.insert(zig_name.clone()) {
                self.error(format!("duplicate top-level name: '{}'", zig_name));
            }
        }

        // Check declarations
        for decl in &module.declarations {
            let name = match decl {
                IrDecl::Var(v) => v.name.zig_name.clone(),
                IrDecl::Fn(f) => f.name.zig_name.clone(),
                IrDecl::Class(c) => c.name.zig_name.clone(),
                IrDecl::CompileError { .. } => continue,
            };
            if !seen.insert(name.clone()) {
                self.error(format!("duplicate top-level name: '{}'", name));
            }
        }
    }

    // ── C ABI compatibility ──────────────────────────

    fn check_cabi_compatibility(&mut self, module: &IrModule) {
        for decl in &module.declarations {
            if let IrDecl::Fn(f) = decl
                && f.is_cabi
            {
                self.check_cabi_fn(f);
            }
        }
        for export in &module.cabi_exports {
            // Skip exports with Anytype/AsyncIo params/return — they'll be const-aliased
            // or have AsyncIo filtered from C ABI wrappers, so C-safety validation doesn't apply.
            let skip_type = matches!(export.return_type, ZigType::Anytype | ZigType::AsyncIo)
                || export
                    .params
                    .iter()
                    .any(|p| matches!(p.zig_type, ZigType::Anytype | ZigType::AsyncIo));
            if skip_type {
                continue;
            }
            // Async exports with NamedStruct returns use out-pointer C ABI wrappers
            let ret_ok = is_c_safe_type(&export.return_type)
                || (export.is_async && matches!(export.return_type, ZigType::NamedStruct(_)));
            if !ret_ok {
                self.error(format!(
                    "C ABI export '{}' has non-C-safe return type: {:?}",
                    export.name, export.return_type
                ));
            }
            for param in &export.params {
                if !is_c_safe_type(&param.zig_type) {
                    self.error(format!(
                        "C ABI export '{}' has non-C-safe parameter '{}': {:?}",
                        export.name, param.name.zig_name, param.zig_type
                    ));
                }
            }
        }
    }

    fn check_cabi_fn(&mut self, f: &IrFnDecl) {
        // The 'io: AsyncIo' parameter is injected by the lowerer for async functions
        // and replaced by js_runtime.getIo() in C ABI wrappers — skip it.
        // NamedStruct/Str returns for async functions use out-pointer/String C ABI wrappers.
        let async_ret_exempt =
            f.is_async && matches!(f.return_type, ZigType::NamedStruct(_) | ZigType::Str);
        if !is_c_safe_type(&f.return_type) && !async_ret_exempt {
            self.error(format!(
                "C ABI function '{}' has non-C-safe return type: {:?}",
                f.name.zig_name, f.return_type
            ));
        }
        for param in &f.params {
            if matches!(param.zig_type, ZigType::AsyncIo) {
                continue;
            }
            if !is_c_safe_type(&param.zig_type) {
                self.error(format!(
                    "C ABI function '{}' has non-C-safe parameter '{}': {:?}",
                    f.name.zig_name, param.name.zig_name, param.zig_type
                ));
            }
        }
    }

    // ── Closure integrity ────────────────────────────

    fn check_closure_integrity(&mut self, module: &IrModule) {
        for cs in &module.closure_structs {
            // Check that each captured variable is actually referenced in the body
            let referenced = {
                let mut names = std::collections::HashSet::new();
                collect_idents::collect_block_idents(&cs.body, &mut names);
                names
            };
            for capture in &cs.captured {
                if !referenced.contains(&capture.name.zig_name) {
                    self.warn(format!(
                        "closure struct '{}' captures '{}' but it is not referenced in the body",
                        cs.name.zig_name, capture.name.zig_name
                    ));
                }
            }
        }

        // Check IrClosure expressions in declarations
        for decl in &module.declarations {
            self.check_closure_refs_in_decl(decl);
        }
    }

    fn check_closure_refs_in_decl(&mut self, decl: &IrDecl) {
        let this = RefCell::new(&mut *self);
        walk::for_each_decl_child(
            decl,
            &mut |block| {
                this.borrow_mut().check_closure_refs_in_block(block);
            },
            &mut |expr| {
                this.borrow_mut().check_closure_refs_in_expr(expr);
            },
        );
    }

    fn check_closure_refs_in_block(&mut self, block: &IrBlock) {
        for stmt in &block.stmts {
            self.check_closure_refs_in_stmt(stmt);
        }
    }

    fn check_closure_refs_in_stmt(&mut self, stmt: &IrStmt) {
        // Special case: NestedFnDecl — check struct body + capture names (not closure body)
        if let IrStmt::NestedFnDecl {
            struct_def,
            instance,
        } = stmt
        {
            self.check_closure_refs_in_block(&struct_def.body);
            if let Some(closure) = instance {
                for cap in &closure.captured {
                    self.check_closure_refs_in_expr(&IrExpr::Ident(cap.name.clone()));
                }
            }
            return;
        }

        // Standard walk; on_target is no-op because validate at stmt level
        // does not visit Assign.target (that's done at expr level)
        let this = RefCell::new(&mut *self);
        walk::for_each_stmt_child(
            stmt,
            &mut |block| {
                this.borrow_mut().check_closure_refs_in_block(block);
            },
            &mut |s| {
                this.borrow_mut().check_closure_refs_in_stmt(s);
            },
            &mut |expr| {
                this.borrow_mut().check_closure_refs_in_expr(expr);
            },
            &mut |_| {},
        );
    }

    fn check_closure_refs_in_expr(&mut self, expr: &IrExpr) {
        // Special case: Closure — check capture references (don't recurse into body)
        if let IrExpr::Closure(closure) = expr {
            let referenced = {
                let mut names = std::collections::HashSet::new();
                collect_idents::collect_block_idents(&closure.body, &mut names);
                names
            };
            for capture in &closure.captured {
                if !referenced.contains(&capture.name.zig_name) {
                    self.warn(format!(
                        "closure '{}' captures '{}' but it is not referenced in the body",
                        closure.struct_name.zig_name, capture.name.zig_name
                    ));
                }
            }
            return;
        }

        let this = RefCell::new(&mut *self);
        walk::for_each_expr_child(
            expr,
            &mut |block| {
                this.borrow_mut().check_closure_refs_in_block(block);
            },
            &mut |s| {
                this.borrow_mut().check_closure_refs_in_stmt(s);
            },
            &mut |expr| {
                this.borrow_mut().check_closure_refs_in_expr(expr);
            },
            &mut |target| {
                this.borrow_mut()
                    .check_closure_refs_in_assign_target(target);
            },
        );
    }

    fn check_closure_refs_in_assign_target(&mut self, target: &IrAssignTarget) {
        let this = RefCell::new(&mut *self);
        walk::for_each_target_child(target, &mut |expr| {
            this.borrow_mut().check_closure_refs_in_expr(expr);
        });
    }
}

impl IrPass for ValidatePass {
    fn name(&self) -> &'static str {
        "validate"
    }

    fn description(&self) -> &'static str {
        "Validates structural integrity of the IR (name uniqueness, C ABI compatibility, closure integrity)"
    }

    fn run(&mut self, module: &mut IrModule) -> PassResult {
        self.diagnostics.clear();

        self.check_name_uniqueness(module);
        self.check_cabi_compatibility(module);
        self.check_closure_integrity(module);

        let diagnostics = std::mem::take(&mut self.diagnostics);
        PassResult::with_diagnostics(diagnostics)
    }
}

impl Default for ValidatePass {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════
//  Helpers
// ═══════════════════════════════════════════════════════

/// Check if a ZigType is safe for C ABI boundaries.
///
/// C-safe types: i64, f64, Bool, Void, Str (as pointer).
/// NOT safe: JsAny, ArrayList, HashMap, NamedStruct, Anytype, AnytypeReturn, etc.
fn is_c_safe_type(ty: &ZigType) -> bool {
    matches!(
        ty,
        ZigType::I64 | ZigType::F64 | ZigType::Bool | ZigType::Void | ZigType::Str
    )
}

// ═══════════════════════════════════════════════════════
//  Tests
// ═══════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use crate::zigir::ident::IrIdent;
    use crate::zigir::types::{IrBlock, IrCapture, IrDecl, IrFnDecl, IrParam, IrStmt, IrVarDecl};

    #[test]
    fn test_validate_clean_module() {
        let mut module = super::super::make_clean_add_module();

        let mut pass = ValidatePass::new();
        let result = pass.run(&mut module);
        assert!(
            result.diagnostics.is_empty(),
            "clean module should have no diagnostics: {:?}",
            result.diagnostics
        );
    }

    #[test]
    fn test_validate_duplicate_names() {
        let mut module = IrModule::new("test".to_string());
        // Add two variables with the same name
        module.declarations.push(IrDecl::Var(IrVarDecl::new_const(
            "x",
            Some(ZigType::I64),
            Some(IrExpr::IntLiteral(1)),
        )));
        module.declarations.push(IrDecl::Var(IrVarDecl::new_const(
            "x",
            Some(ZigType::I64),
            Some(IrExpr::IntLiteral(2)),
        )));

        let mut pass = ValidatePass::new();
        let result = pass.run(&mut module);
        assert_eq!(result.diagnostics.len(), 1);
        assert!(matches!(
            result.diagnostics[0].level,
            DiagnosticLevel::Error
        ));
        assert!(result.diagnostics[0].message.contains("duplicate"));
    }

    #[test]
    fn test_validate_cabi_unsafe_type() {
        let mut module = IrModule::new("test".to_string());
        module.declarations.push(IrDecl::Fn(IrFnDecl {
            name: IrIdent::new("export_fn"),
            params: vec![IrParam {
                name: IrIdent::new("data"),
                zig_type: ZigType::JsAny, // NOT C-safe
                is_unused: false,
                is_rest: false,
            }],
            return_type: ZigType::JsAny, // NOT C-safe
            body: IrBlock::new(vec![]),
            is_export: false,
            is_async: false,
            can_throw: false,
            is_cabi: true,
            typeof_return_body: None,
        }));

        let mut pass = ValidatePass::new();
        let result = pass.run(&mut module);
        let errors: Vec<_> = result
            .diagnostics
            .iter()
            .filter(|d| matches!(d.level, DiagnosticLevel::Error))
            .collect();
        assert_eq!(
            errors.len(),
            2,
            "should have 2 C ABI errors (param + return), got: {:?}",
            result.diagnostics
        );
    }

    #[test]
    fn test_validate_closure_capture_not_referenced() {
        let mut module = IrModule::new("test".to_string());
        // Add a closure struct with a captured var not referenced in body
        module
            .closure_structs
            .push(crate::zigir::types::IrClosureStruct {
                name: IrIdent::new("_closure_0"),
                captured: vec![IrCapture {
                    name: IrIdent::new("unused_var"),
                    zig_type: ZigType::I64,
                    is_mut: false,
                    init_expr: None,
                }],
                fn_params: vec![IrParam {
                    name: IrIdent::new("x"),
                    zig_type: ZigType::I64,
                    is_unused: false,
                    is_rest: false,
                }],
                return_type: ZigType::I64,
                typeof_return_body: None,
                body: IrBlock::new(vec![IrStmt::Return {
                    value: Some(IrExpr::Ident(IrIdent::new("x"))),
                }]),
            });

        let mut pass = ValidatePass::new();
        let result = pass.run(&mut module);
        let warnings: Vec<_> = result
            .diagnostics
            .iter()
            .filter(|d| matches!(d.level, DiagnosticLevel::Warning))
            .collect();
        assert_eq!(warnings.len(), 1, "should warn about unreferenced capture");
        assert!(warnings[0].message.contains("unused_var"));
    }
}