tishlang_compile 1.7.0

Tish native compiler backend
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
//! Module resolver: resolves relative imports, builds dependency graph, detects cycles.
//! Supports native imports: `tish:…`, `cargo:…`, `@scope/pkg` (via package.json).

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tishlang_ast::{ExportDeclaration, Expr, ImportSpecifier, Program, Statement};

/// Resolved native module: crate path and init expression.
#[derive(Debug, Clone)]
pub struct ResolvedNativeModule {
    pub spec: String,
    /// Cargo package name (e.g. tish-egui) for [dependencies]
    pub package_name: String,
    /// Rust crate name with underscores (e.g. tish_egui) for use in generated code
    pub crate_name: String,
    pub crate_path: PathBuf,
    pub export_fn: String,
    /// When false, omit `path = …` in the generated Cargo.toml (crate comes from `tish.rustDependencies` only).
    pub use_path_dependency: bool,
}

/// How codegen links a native import to Rust (`generateNativeWrapper` for `tish:*`; `cargo:*` always generated).
#[derive(Debug, Clone)]
pub enum NativeModuleInit {
    /// Call `external_crate::export_fn()` and read named exports from the returned object.
    Legacy {
        crate_name: String,
        export_fn: String,
    },
    /// Call `crate::generated_native::export_fn()` — object built from per-export fns on `shim_crate`.
    Generated {
        shim_crate: String,
        export_fn: String,
    },
}

/// Extra native build inputs produced alongside Rust source (Cargo merge + optional wrapper).
#[derive(Debug, Clone)]
pub struct NativeBuildArtifacts {
    /// Extra `[dependencies]` lines from `tish.rustDependencies`.
    pub rust_dependencies_toml: String,
    /// Generated `generated_native.rs` when using [`NativeModuleInit::Generated`].
    pub generated_native_rs: Option<String>,
    pub native_init: std::collections::HashMap<String, NativeModuleInit>,
}

/// Node-compatible aliases for built-in modules (fs -> tish:fs, etc.).
const BUILTIN_ALIASES: &[(&str, &str)] = &[
    ("fs", "tish:fs"),
    ("http", "tish:http"),
    ("process", "tish:process"),
    ("ws", "tish:ws"),
];

/// Normalize built-in spec to canonical form. E.g. "fs" -> "tish:fs".
pub fn normalize_builtin_spec(spec: &str) -> Option<String> {
    if spec.starts_with("tish:") {
        return Some(spec.to_string());
    }
    BUILTIN_ALIASES
        .iter()
        .find(|(alias, _)| *alias == spec)
        .map(|(_, canonical)| (*canonical).to_string())
}

/// Built-in modules that come from tishlang_runtime, not from package.json.
pub fn is_builtin_native_spec(spec: &str) -> bool {
    matches!(spec, "tish:fs" | "tish:http" | "tish:process" | "tish:ws")
        || matches!(spec, "fs" | "http" | "process" | "ws")
}

/// Resolve all native imports in a merged program via package.json lookup.
/// Built-in modules (tish:fs, tish:http, tish:process) are skipped - they use tishlang_runtime directly.
/// Handles both lowered `NativeModuleLoad` (merged modules) and raw `import { … } from 'tish:…'`.
pub fn resolve_native_modules(
    program: &Program,
    project_root: &Path,
) -> Result<Vec<ResolvedNativeModule>, String> {
    let root_canon = project_root
        .canonicalize()
        .map_err(|e| format!("Cannot canonicalize project root: {}", e))?;
    let mut seen = HashSet::new();
    let mut modules = Vec::new();
    for stmt in &program.statements {
        let specs: Vec<String> = match stmt {
            Statement::VarDecl {
                init: Some(Expr::NativeModuleLoad { spec, .. }),
                ..
            } => vec![spec.as_ref().to_string()],
            Statement::Import { from, .. } if is_native_import(from.as_ref()) => {
                vec![normalize_builtin_spec(from.as_ref()).unwrap_or_else(|| from.to_string())]
            }
            _ => continue,
        };
        for s in specs {
            if is_builtin_native_spec(&s) {
                continue;
            }
            if !seen.insert(s.clone()) {
                continue;
            }
            let m = if s.starts_with("cargo:") {
                resolve_cargo_native_module(&s, &root_canon)?
            } else {
                resolve_native_module(&s, &root_canon)?
            };
            modules.push(m);
        }
    }
    Ok(modules)
}

/// True for `cargo:…` specs (Cargo-backed imports; Rust native backend only).
pub fn is_cargo_native_spec(spec: &str) -> bool {
    spec.starts_with("cargo:")
}

/// Stable Rust symbol for the generated namespace function, e.g. `cargo:my-crate` → `cargo_native_my_crate_object`.
pub fn cargo_export_fn_name(spec: &str) -> String {
    let tail = spec.strip_prefix("cargo:").unwrap_or(spec);
    let mut out = String::from("cargo_native_");
    for c in tail.chars() {
        if c.is_ascii_alphanumeric() {
            out.push(c);
        } else {
            out.push('_');
        }
    }
    if out == "cargo_native_" {
        out.push_str("unnamed");
    }
    out.push_str("_object");
    out
}

fn resolve_cargo_native_module(
    spec: &str,
    project_root: &Path,
) -> Result<ResolvedNativeModule, String> {
    let tail = spec
        .strip_prefix("cargo:")
        .ok_or_else(|| format!("Invalid cargo native spec: {}", spec))?;
    if tail.is_empty() {
        return Err(
            "cargo: import needs a dependency name, e.g. import { x } from 'cargo:my_crate'".into(),
        );
    }
    let dep_key = tail.to_string();
    let tish = read_project_tish_config(project_root);
    let rust_deps = tish.get("rustDependencies").and_then(|v| v.as_object()).ok_or_else(|| {
        format!(
            "cargo:{} requires package.json \"tish\": {{ \"rustDependencies\": {{ \"{}\": \"\" }} }}",
            tail, dep_key
        )
    })?;
    if !rust_deps.contains_key(&dep_key) {
        return Err(format!(
            "cargo:{}: add \"{}\" to tish.rustDependencies in package.json (version string or inline table)",
            tail, dep_key
        ));
    }
    let crate_name = dep_key.replace('-', "_");
    let export_fn = cargo_export_fn_name(spec);
    let crate_path = project_root
        .canonicalize()
        .unwrap_or_else(|_| project_root.to_path_buf());
    Ok(ResolvedNativeModule {
        spec: spec.to_string(),
        package_name: dep_key.clone(),
        crate_name,
        crate_path,
        export_fn,
        use_path_dependency: false,
    })
}

fn resolve_native_module(spec: &str, project_root: &Path) -> Result<ResolvedNativeModule, String> {
    let package_name = if spec.starts_with("tish:") {
        format!("tish-{}", spec.strip_prefix("tish:").unwrap_or(spec))
    } else if spec.starts_with('@') {
        spec.to_string()
    } else {
        return Err(format!("Unsupported native import spec: {}", spec));
    };
    let pkg_dir = find_package_dir(&package_name, project_root)?;
    let pkg_json = pkg_dir.join("package.json");
    let content = std::fs::read_to_string(&pkg_json)
        .map_err(|e| format!("Cannot read {}: {}", pkg_json.display(), e))?;
    let json: serde_json::Value = serde_json::from_str(&content)
        .map_err(|e| format!("Invalid JSON in {}: {}", pkg_json.display(), e))?;
    let tish = json
        .get("tish")
        .and_then(|v| v.as_object())
        .ok_or_else(|| {
            format!(
                "Package {} has no \"tish\" config in package.json",
                package_name
            )
        })?;
    if !tish
        .get("module")
        .and_then(|v| v.as_bool())
        .unwrap_or(false)
    {
        return Err(format!(
            "Package {} is not a Tish native module (tish.module must be true)",
            package_name
        ));
    }
    let raw_crate = tish
        .get("crate")
        .and_then(|v| v.as_str())
        .unwrap_or(&package_name)
        .to_string();
    let module_part = spec.strip_prefix("tish:").unwrap_or(spec);
    let export_fn = tish
        .get("export")
        .and_then(|v| v.as_str())
        .map(String::from)
        .unwrap_or_else(|| format!("{}_object", str::replace(module_part, "-", "_")));
    let crate_path = pkg_dir.canonicalize().unwrap_or(pkg_dir);
    Ok(ResolvedNativeModule {
        spec: spec.to_string(),
        package_name: raw_crate.clone(),
        crate_name: raw_crate.replace('-', "_"),
        crate_path,
        export_fn,
        use_path_dependency: true,
    })
}

/// Read the `tish` object from the project root `package.json` (empty JSON object if missing).
pub fn read_project_tish_config(project_root: &Path) -> serde_json::Value {
    let path = project_root.join("package.json");
    let Ok(content) = std::fs::read_to_string(&path) else {
        return serde_json::json!({});
    };
    let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) else {
        return serde_json::json!({});
    };
    json.get("tish")
        .cloned()
        .unwrap_or_else(|| serde_json::json!({}))
}

fn resolve_cargo_path_for_toml(project_root: &Path, raw: &str) -> String {
    let p = Path::new(raw);
    let resolved = if p.is_absolute() {
        p.to_path_buf()
    } else {
        project_root.join(p)
    };
    let resolved = resolved.canonicalize().unwrap_or(resolved);
    resolved.display().to_string().replace('\\', "/")
}

fn json_to_cargo_inline_value(
    v: &serde_json::Value,
    project_root: &Path,
) -> Result<String, String> {
    match v {
        serde_json::Value::String(s) => Ok(format!("{:?}", s.as_str())),
        serde_json::Value::Bool(b) => Ok(b.to_string()),
        serde_json::Value::Number(n) => Ok(n.to_string()),
        serde_json::Value::Array(arr) => {
            let mut inner = Vec::new();
            for item in arr {
                inner.push(json_to_cargo_inline_value(item, project_root)?);
            }
            Ok(format!("[{}]", inner.join(", ")))
        }
        serde_json::Value::Object(map) => {
            let mut parts = Vec::new();
            for (k, v) in map {
                let rhs = if k == "path" && v.as_str().is_some() {
                    let s = v.as_str().unwrap();
                    format!("{:?}", resolve_cargo_path_for_toml(project_root, s))
                } else {
                    json_to_cargo_inline_value(v, project_root)?
                };
                parts.push(format!("{} = {}", k, rhs));
            }
            Ok(format!("{{ {} }}", parts.join(", ")))
        }
        serde_json::Value::Null => Err("null is not valid in a Cargo dependency value".to_string()),
    }
}

/// Serialize `tish.rustDependencies` from project `package.json` into Cargo.toml `[dependencies]` lines.
/// Relative `path = "…"` entries in inline tables are resolved against `project_root` so the temp build crate can find them.
pub fn format_rust_dependencies_toml(
    tish: &serde_json::Value,
    project_root: &Path,
) -> Result<String, String> {
    let Some(obj) = tish.get("rustDependencies").and_then(|v| v.as_object()) else {
        return Ok(String::new());
    };
    let mut out = String::new();
    for (name, val) in obj {
        match val {
            serde_json::Value::String(_) | serde_json::Value::Object(_) => {
                out.push_str(&format!(
                    "{} = {}\n",
                    name,
                    json_to_cargo_inline_value(val, project_root)?
                ));
            }
            _ => {
                return Err(format!(
                    "tish.rustDependencies.{} must be a string (version) or object (inline table)",
                    name
                ));
            }
        }
    }
    Ok(out)
}

/// Map a Tish export name to a Rust identifier (e.g. `readFile` → `read_file`) for shim crate symbols.
pub fn export_name_to_rust_ident(export_name: &str) -> String {
    let mut out = String::new();
    for (i, c) in export_name.chars().enumerate() {
        if c.is_uppercase() && i > 0 {
            out.push('_');
        }
        for lower in c.to_lowercase() {
            out.push(lower);
        }
    }
    if out.is_empty() {
        "native_export".to_string()
    } else {
        out
    }
}

/// Collect `(spec, export_name)` for every non-builtin native import in the program.
pub fn infer_native_module_exports(program: &Program) -> HashMap<String, HashSet<String>> {
    let mut map: HashMap<String, HashSet<String>> = HashMap::new();
    for stmt in &program.statements {
        match stmt {
            Statement::VarDecl {
                init:
                    Some(Expr::NativeModuleLoad {
                        spec, export_name, ..
                    }),
                ..
            } => {
                let s = spec.as_ref();
                if is_builtin_native_spec(s) {
                    continue;
                }
                map.entry(s.to_string())
                    .or_default()
                    .insert(export_name.to_string());
            }
            Statement::Import {
                specifiers, from, ..
            } if is_native_import(from.as_ref()) => {
                let spec =
                    normalize_builtin_spec(from.as_ref()).unwrap_or_else(|| from.to_string());
                if is_builtin_native_spec(&spec) {
                    continue;
                }
                for sp in specifiers {
                    if let ImportSpecifier::Named { name, .. } = sp {
                        map.entry(spec.clone())
                            .or_default()
                            .insert(name.to_string());
                    }
                }
            }
            _ => {}
        }
    }
    map
}

/// Emit `generated_native.rs` for [`NativeModuleInit::Generated`] modules.
pub fn generate_native_wrapper_rs(
    modules: &[ResolvedNativeModule],
    inferred: &HashMap<String, HashSet<String>>,
    init_by_spec: &HashMap<String, NativeModuleInit>,
) -> String {
    let mut file = String::from(
        "//! Generated by `tish build` — do not edit.\n\
         use std::cell::RefCell;\n\
         use std::rc::Rc;\n\
         use std::sync::Arc;\n\
         use tishlang_runtime::{ObjectMap, Value};\n\n",
    );
    let mut any = false;
    for m in modules {
        let Some(NativeModuleInit::Generated {
            shim_crate,
            export_fn,
        }) = init_by_spec.get(&m.spec)
        else {
            continue;
        };
        let Some(names) = inferred.get(&m.spec) else {
            continue;
        };
        if names.is_empty() {
            continue;
        }
        any = true;
        let mut keys: Vec<_> = names.iter().cloned().collect();
        keys.sort();
        file.push_str(&format!("pub fn {}() -> Value {{\n", export_fn));
        file.push_str("    let mut m = ObjectMap::default();\n");
        for export_name in keys {
            let rust_fn = export_name_to_rust_ident(&export_name);
            let key_lit = format!("{:?}", export_name);
            file.push_str(&format!(
                "    m.insert(Arc::from({}), Value::Function(Rc::new(|args: &[Value]| {{\n        {}::{}(args)\n    }})));\n",
                key_lit, shim_crate, rust_fn
            ));
        }
        file.push_str("    Value::Object(Rc::new(RefCell::new(m)))\n}\n\n");
    }
    if !any {
        return String::new();
    }
    file
}

/// Combine project `package.json`, inferred exports, and resolved native modules into build artifacts.
pub fn compute_native_build_artifacts(
    program: &Program,
    project_root: &Path,
    native_modules: &[ResolvedNativeModule],
) -> Result<NativeBuildArtifacts, String> {
    let tish = read_project_tish_config(project_root);
    let rust_dependencies_toml = format_rust_dependencies_toml(&tish, project_root)?;
    let inferred = infer_native_module_exports(program);
    let gen_tish = tish
        .get("generateNativeWrapper")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let mut native_init: HashMap<String, NativeModuleInit> = HashMap::new();
    for m in native_modules {
        let use_gen = if is_cargo_native_spec(&m.spec) {
            inferred
                .get(&m.spec)
                .map(|s| !s.is_empty())
                .unwrap_or(false)
        } else {
            gen_tish
                && inferred
                    .get(&m.spec)
                    .map(|s| !s.is_empty())
                    .unwrap_or(false)
        };
        let init = if use_gen {
            NativeModuleInit::Generated {
                shim_crate: m.crate_name.clone(),
                export_fn: m.export_fn.clone(),
            }
        } else {
            NativeModuleInit::Legacy {
                crate_name: m.crate_name.clone(),
                export_fn: m.export_fn.clone(),
            }
        };
        native_init.insert(m.spec.clone(), init);
    }

    let generated_native_rs = {
        let s = generate_native_wrapper_rs(native_modules, &inferred, &native_init);
        if s.trim().is_empty() {
            None
        } else {
            Some(s)
        }
    };

    Ok(NativeBuildArtifacts {
        rust_dependencies_toml,
        generated_native_rs,
        native_init,
    })
}

fn find_package_dir(package_name: &str, project_root: &Path) -> Result<PathBuf, String> {
    let mut search = project_root.to_path_buf();
    loop {
        let node_mod = search.join("node_modules").join(package_name);
        if node_mod.join("package.json").exists()
            && read_package_name(&node_mod.join("package.json")) == Some(package_name.to_string())
        {
            return Ok(node_mod);
        }
        let sibling = search.join(package_name);
        if sibling.join("package.json").exists()
            && read_package_name(&sibling.join("package.json")) == Some(package_name.to_string())
        {
            return Ok(sibling);
        }
        if search.join("package.json").exists()
            && read_package_name(&search.join("package.json")) == Some(package_name.to_string())
        {
            return Ok(search);
        }
        if let Some(parent) = search.parent() {
            search = parent.to_path_buf();
        } else {
            break;
        }
    }
    Err(format!(
        "Native module {} not found. Add it as a dependency or place it in node_modules/ or as a sibling directory.",
        package_name
    ))
}

fn read_package_name(pkg_path: &Path) -> Option<String> {
    let content = std::fs::read_to_string(pkg_path).ok()?;
    let json: serde_json::Value = serde_json::from_str(&content).ok()?;
    json.get("name").and_then(|v| v.as_str()).map(String::from)
}

fn stmt_native_specs(stmt: &Statement) -> Vec<String> {
    match stmt {
        Statement::VarDecl {
            init: Some(Expr::NativeModuleLoad { spec, .. }),
            ..
        } => vec![spec.to_string()],
        Statement::Import { from, .. } if is_native_import(from.as_ref()) => {
            vec![normalize_builtin_spec(from.as_ref()).unwrap_or_else(|| from.to_string())]
        }
        _ => vec![],
    }
}

/// Extract Cargo feature names from native imports in a merged program.
/// Used to enable tishlang_runtime features based on `import { x } from 'tish:egui'` etc.
pub fn extract_native_import_features(program: &Program) -> Vec<String> {
    let mut features = std::collections::HashSet::new();
    for stmt in &program.statements {
        for spec in stmt_native_specs(stmt) {
            if let Some(f) = native_spec_to_feature(spec.as_ref()) {
                features.insert(f);
            }
        }
    }
    features.into_iter().collect()
}

/// Returns true if the merged program contains native imports (tish:*, @scope/pkg).
pub fn has_native_imports(program: &Program) -> bool {
    program
        .statements
        .iter()
        .any(|stmt| !stmt_native_specs(stmt).is_empty())
}

/// Returns true if the merged program contains external native imports (not built-in tish:fs/http/process).
/// Cranelift/LLVM reject these; bytecode VM supports built-ins only.
pub fn has_external_native_imports(program: &Program) -> bool {
    for stmt in &program.statements {
        for spec in stmt_native_specs(stmt) {
            if !is_builtin_native_spec(spec.as_ref()) {
                return true;
            }
        }
    }
    false
}

/// A resolved module: path and its parsed program.
#[derive(Debug, Clone)]
pub struct ResolvedModule {
    pub path: PathBuf,
    pub program: Program,
}

/// Resolve all modules starting from the entry file. Returns modules in dependency order
/// (dependencies first, then dependents). Entry module is last.
pub fn resolve_project(
    entry_path: &Path,
    project_root: Option<&Path>,
) -> Result<Vec<ResolvedModule>, String> {
    let project_root =
        project_root.unwrap_or_else(|| entry_path.parent().unwrap_or(Path::new(".")));
    let entry_canon = entry_path
        .canonicalize()
        .map_err(|e| format!("Cannot canonicalize entry {}: {}", entry_path.display(), e))?;
    let root_canon = project_root.canonicalize().map_err(|e| {
        format!(
            "Cannot canonicalize project root {}: {}",
            project_root.display(),
            e
        )
    })?;

    let mut visited = HashSet::new();
    let mut path_to_module: HashMap<PathBuf, Program> = HashMap::new();
    let mut load_order: Vec<PathBuf> = Vec::new();

    load_module_recursive(
        &entry_canon,
        &root_canon,
        &mut visited,
        &mut path_to_module,
        &mut load_order,
    )?;

    Ok(load_order
        .into_iter()
        .map(|p| {
            let program = path_to_module.remove(&p).unwrap();
            ResolvedModule { path: p, program }
        })
        .collect())
}

/// Resolve modules when the entry program is read from stdin (`tish run -`).
/// Relative file imports resolve from `project_root` (typically [`std::env::current_dir()`]).
/// The synthetic entry path `<stdin>` is not a real file; dependencies load from disk as usual.
pub fn resolve_project_from_stdin(
    source: &str,
    project_root: &Path,
) -> Result<Vec<ResolvedModule>, String> {
    let root_canon = project_root.canonicalize().map_err(|e| {
        format!(
            "Cannot canonicalize project root {}: {}",
            project_root.display(),
            e
        )
    })?;

    let stdin_path = root_canon.join("<stdin>");
    let program =
        tishlang_parser::parse(source).map_err(|e| format!("Parse error (stdin): {}", e))?;

    let mut visited = HashSet::new();
    let mut path_to_module: HashMap<PathBuf, Program> = HashMap::new();
    let mut load_order: Vec<PathBuf> = Vec::new();

    let from_dir = stdin_path.parent().unwrap_or_else(|| Path::new("."));

    for stmt in &program.statements {
        if let Statement::Import { from, .. } = stmt {
            if is_native_import(from.as_ref()) {
                continue;
            }
            let dep_path = resolve_import_path(from.as_ref(), from_dir, &root_canon)?;
            if !path_to_module.contains_key(&dep_path) {
                load_module_recursive(
                    &dep_path,
                    &root_canon,
                    &mut visited,
                    &mut path_to_module,
                    &mut load_order,
                )?;
            }
        }
    }

    path_to_module.insert(stdin_path.clone(), program);
    load_order.push(stdin_path);

    Ok(load_order
        .into_iter()
        .map(|p| {
            let program = path_to_module.remove(&p).unwrap();
            ResolvedModule { path: p, program }
        })
        .collect())
}

fn load_module_recursive(
    module_path: &Path,
    project_root: &Path,
    visited: &mut HashSet<PathBuf>,
    path_to_module: &mut HashMap<PathBuf, Program>,
    load_order: &mut Vec<PathBuf>,
) -> Result<(), String> {
    let canonical = module_path
        .canonicalize()
        .map_err(|e| format!("Cannot read {}: {}", module_path.display(), e))?;

    if visited.contains(&canonical) {
        return Ok(());
    }
    visited.insert(canonical.clone());

    let source = std::fs::read_to_string(&canonical)
        .map_err(|e| format!("Cannot read {}: {}", canonical.display(), e))?;
    let program = tishlang_parser::parse(&source)
        .map_err(|e| format!("Parse error in {}: {}", canonical.display(), e))?;

    // Collect imports and load dependencies first (skip native imports)
    let dir = canonical.parent().unwrap_or(Path::new("."));
    for stmt in &program.statements {
        if let Statement::Import { from, .. } = stmt {
            if is_native_import(from.as_ref()) {
                continue; // Native imports don't load files
            }
            let dep_path = resolve_import_path(from.as_ref(), dir, project_root)?;
            if !path_to_module.contains_key(&dep_path) {
                load_module_recursive(
                    &dep_path,
                    project_root,
                    visited,
                    path_to_module,
                    load_order,
                )?;
            }
        }
    }

    path_to_module.insert(canonical.clone(), program);
    load_order.push(canonical);
    Ok(())
}

/// Returns true for native module imports that don't resolve to files.
/// - fs, http, process, ws (Node-compatible aliases for tish:fs, tish:http, tish:process, tish:ws)
/// - tish:egui, tish:polars, etc.
/// - cargo:… (Cargo `rustDependencies` + generated wrapper; Rust native backend)
/// - @scope/package (npm-style)
pub fn is_native_import(spec: &str) -> bool {
    spec.starts_with("tish:")
        || spec.starts_with("cargo:")
        || spec.starts_with('@')
        || matches!(spec, "fs" | "http" | "process" | "ws")
}

/// Map native spec to Cargo feature name for built-in tish:* modules.
pub fn native_spec_to_feature(spec: &str) -> Option<String> {
    let canonical = normalize_builtin_spec(spec)?;
    canonical.strip_prefix("tish:").map(|s| s.to_string())
}

/// Resolve a bare specifier (e.g. "lattish") to a path via node_modules.
fn resolve_bare_spec(spec: &str, from_dir: &Path, _project_root: &Path) -> Option<PathBuf> {
    let mut search = from_dir.to_path_buf();
    loop {
        let node_mod = search.join("node_modules").join(spec);
        let pkg_json = node_mod.join("package.json");
        if pkg_json.exists() {
            if let Some(name) = read_package_name(&pkg_json) {
                if name == spec {
                    let content = std::fs::read_to_string(&pkg_json).ok()?;
                    let json: serde_json::Value = serde_json::from_str(&content).ok()?;
                    let entry = json
                        .get("tish")
                        .and_then(|t| t.get("module"))
                        .and_then(|m| m.as_str())
                        .or_else(|| json.get("main").and_then(|m| m.as_str()));
                    let entry = entry.unwrap_or("index.tish");
                    let entry_clean = entry.trim_start_matches("./");
                    let resolved = node_mod.join(entry_clean);
                    if resolved.exists() {
                        return resolved.canonicalize().ok();
                    }
                }
            }
        }
        if let Some(parent) = search.parent() {
            if parent == search {
                break;
            }
            search = parent.to_path_buf();
        } else {
            break;
        }
    }
    None
}

/// Resolve an import specifier (e.g. "./foo.tish", "../lib/utils", "lattish") to an absolute path.
fn resolve_import_path(
    spec: &str,
    from_dir: &Path,
    project_root: &Path,
) -> Result<PathBuf, String> {
    if is_native_import(spec) {
        return Err(format!(
            "resolve_import_path called for native import (use merge_modules native branch): {}",
            spec
        ));
    }
    if !spec.starts_with("./") && !spec.starts_with("../") {
        if let Some(path) = resolve_bare_spec(spec, from_dir, project_root) {
            return Ok(path);
        }
        return Err(format!(
            "Package '{}' not found in node_modules. Install it with: npm install {}",
            spec, spec
        ));
    }
    let base = from_dir.join(spec);
    // Try with .tish extension if the path has no extension
    let path = if base.extension().is_none() {
        let with_ext = base.with_extension("tish");
        if with_ext.exists() {
            with_ext
        } else {
            base
        }
    } else {
        base
    };
    path.canonicalize().map_err(|e| {
        format!(
            "Cannot resolve import '{}' from {}: {}",
            spec,
            from_dir.display(),
            e
        )
    })
}

/// Check for cyclic imports. Returns Err if a cycle is detected.
pub fn detect_cycles(modules: &[ResolvedModule]) -> Result<(), String> {
    let path_to_idx: HashMap<_, _> = modules
        .iter()
        .enumerate()
        .map(|(i, m)| (m.path.clone(), i))
        .collect();

    for (idx, module) in modules.iter().enumerate() {
        let dir = module.path.parent().unwrap_or(Path::new("."));
        let mut stack = vec![idx];
        if has_cycle_from(
            dir,
            &module.program,
            &path_to_idx,
            modules,
            &mut stack,
            &mut HashSet::new(),
        )? {
            let path_names: Vec<_> = stack
                .iter()
                .map(|&i| modules[i].path.display().to_string())
                .collect();
            return Err(format!(
                "Circular import detected: {}",
                path_names.join(" -> ")
            ));
        }
    }
    Ok(())
}

fn has_cycle_from(
    from_dir: &Path,
    program: &Program,
    path_to_idx: &HashMap<PathBuf, usize>,
    modules: &[ResolvedModule],
    stack: &mut Vec<usize>,
    visiting: &mut HashSet<usize>,
) -> Result<bool, String> {
    for stmt in &program.statements {
        if let Statement::Import { from, .. } = stmt {
            if is_native_import(from.as_ref()) {
                continue;
            }
            let dep_path = resolve_import_path(from.as_ref(), from_dir, Path::new("."))?;
            if let Some(&dep_idx) = path_to_idx.get(&dep_path) {
                if stack.contains(&dep_idx) {
                    stack.push(dep_idx);
                    return Ok(true);
                }
                if !visiting.contains(&dep_idx) {
                    visiting.insert(dep_idx);
                    stack.push(dep_idx);
                    let dep = &modules[dep_idx];
                    let dep_dir = dep.path.parent().unwrap_or(Path::new("."));
                    if has_cycle_from(dep_dir, &dep.program, path_to_idx, modules, stack, visiting)?
                    {
                        return Ok(true);
                    }
                    stack.pop();
                    visiting.remove(&dep_idx);
                }
            }
        }
    }
    Ok(false)
}

/// Merge all resolved modules into a single program. Dependencies are emitted first.
/// Import statements are rewritten as bindings from already-emitted dep exports.
/// Export statements are unwrapped (the inner declaration is emitted).
pub fn merge_modules(modules: Vec<ResolvedModule>) -> Result<Program, String> {
    let path_to_idx: HashMap<PathBuf, usize> = modules
        .iter()
        .enumerate()
        .map(|(i, m)| (m.path.canonicalize().unwrap_or(m.path.clone()), i))
        .collect();

    let mut module_exports: Vec<HashMap<String, String>> = vec![HashMap::new(); modules.len()];
    for (idx, module) in modules.iter().enumerate() {
        for stmt in &module.program.statements {
            if let Statement::Export { declaration, .. } = stmt {
                match declaration.as_ref() {
                    ExportDeclaration::Named(s) => {
                        let name = match s.as_ref() {
                            Statement::VarDecl { name, .. } | Statement::FunDecl { name, .. } => {
                                name.to_string()
                            }
                            _ => continue,
                        };
                        module_exports[idx].insert(name.clone(), name);
                    }
                    ExportDeclaration::Default(_) => {
                        let default_name = format!("__default_{}", idx);
                        module_exports[idx].insert("default".to_string(), default_name);
                    }
                }
            }
        }
    }

    let mut statements = Vec::new();
    for (idx, module) in modules.iter().enumerate() {
        let dir = module.path.parent().unwrap_or(Path::new("."));
        for stmt in &module.program.statements {
            match stmt {
                Statement::Import {
                    specifiers,
                    from,
                    span,
                } => {
                    if is_native_import(from.as_ref()) {
                        // Normalize fs/http/process -> tish:fs etc. for Node compatibility
                        let canonical_spec = normalize_builtin_spec(from.as_ref())
                            .unwrap_or_else(|| from.to_string());
                        // Emit VarDecl with NativeModuleLoad for each specifier
                        for spec in specifiers {
                            match spec {
                                ImportSpecifier::Named { name, alias } => {
                                    let bind = alias.as_deref().unwrap_or(name.as_ref());
                                    let init = Expr::NativeModuleLoad {
                                        spec: Arc::from(canonical_spec.clone()),
                                        export_name: name.clone(),
                                        span: *span,
                                    };
                                    statements.push(Statement::VarDecl {
                                        name: Arc::from(bind),
                                        mutable: false,
                                        type_ann: None,
                                        init: Some(init),
                                        span: *span,
                                    });
                                }
                                ImportSpecifier::Namespace(ns) => {
                                    return Err(format!(
                                        "Namespace import (* as {}) not supported for native module '{}'",
                                        ns.as_ref(),
                                        from.as_ref()
                                    ));
                                }
                                ImportSpecifier::Default(bind) => {
                                    return Err(format!(
                                        "Default import '{}' not supported for native module '{}'. Use named import, e.g. import {{ egui }} from '{}'",
                                        bind.as_ref(),
                                        from.as_ref(),
                                        from.as_ref()
                                    ));
                                }
                            }
                        }
                        continue;
                    }
                    let dep_path = resolve_import_path(from.as_ref(), dir, Path::new("."))?;
                    let dep_path = dep_path.canonicalize().unwrap_or(dep_path);
                    let dep_idx = *path_to_idx
                        .get(&dep_path)
                        .ok_or_else(|| format!("Resolved import '{}' not in module list", from))?;
                    let dep_exports = &module_exports[dep_idx];
                    for spec in specifiers {
                        match spec {
                            ImportSpecifier::Named { name, alias } => {
                                let source = dep_exports
                                    .get(name.as_ref())
                                    .cloned()
                                    .unwrap_or_else(|| name.to_string());
                                let bind = alias.as_deref().unwrap_or(name.as_ref());
                                if bind != source {
                                    statements.push(Statement::VarDecl {
                                        name: Arc::from(bind),
                                        mutable: false,
                                        type_ann: None,
                                        init: Some(Expr::Ident {
                                            name: Arc::from(source),
                                            span: *span,
                                        }),
                                        span: *span,
                                    });
                                }
                            }
                            ImportSpecifier::Namespace(ns) => {
                                let mut props = Vec::new();
                                for (k, v) in dep_exports {
                                    props.push(tishlang_ast::ObjectProp::KeyValue(
                                        Arc::from(k.clone()),
                                        Expr::Ident {
                                            name: Arc::from(v.clone()),
                                            span: *span,
                                        },
                                    ));
                                }
                                statements.push(Statement::VarDecl {
                                    name: ns.clone(),
                                    mutable: false,
                                    type_ann: None,
                                    init: Some(Expr::Object { props, span: *span }),
                                    span: *span,
                                });
                            }
                            ImportSpecifier::Default(bind) => {
                                let source =
                                    dep_exports.get("default").cloned().ok_or_else(|| {
                                        format!("Module '{}' has no default export", from)
                                    })?;
                                statements.push(Statement::VarDecl {
                                    name: bind.clone(),
                                    mutable: false,
                                    type_ann: None,
                                    init: Some(Expr::Ident {
                                        name: Arc::from(source),
                                        span: *span,
                                    }),
                                    span: *span,
                                });
                            }
                        }
                    }
                }
                Statement::Export { declaration, .. } => match declaration.as_ref() {
                    ExportDeclaration::Named(s) => statements.push(*s.clone()),
                    ExportDeclaration::Default(e) => {
                        let default_name = format!("__default_{}", idx);
                        statements.push(Statement::VarDecl {
                            name: Arc::from(default_name),
                            mutable: false,
                            type_ann: None,
                            init: Some((*e).clone()),
                            span: e.span(),
                        });
                    }
                },
                _ => statements.push(stmt.clone()),
            }
        }
    }
    Ok(Program { statements })
}

#[cfg(test)]
mod cargo_spec_tests {
    use std::sync::Arc;

    use super::cargo_export_fn_name;
    use super::is_native_import;

    #[test]
    fn is_native_import_accepts_arc_str_ref() {
        let from: &Arc<str> = &Arc::from("cargo:demo_shim");
        assert!(is_native_import(from));
    }

    #[test]
    fn detect_cycles_skips_cargo_import() {
        use super::{detect_cycles, resolve_project};
        let dir = tempfile::tempdir().expect("tempdir");
        let p = dir.path().join("main.tish");
        let src = "import { greet } from 'cargo:demo_shim'\nconsole.log(1)\n";
        std::fs::write(&p, src).unwrap();
        let root = dir.path();
        let modules = resolve_project(&p, Some(root)).unwrap();
        detect_cycles(&modules).unwrap();
    }

    #[test]
    fn merge_modules_skips_cargo_import() {
        use super::{merge_modules, resolve_project};
        let dir = tempfile::tempdir().expect("tempdir");
        let p = dir.path().join("main.tish");
        let src = "import { greet } from 'cargo:demo_shim'\nconsole.log(1)\n";
        std::fs::write(&p, src).unwrap();
        let root = dir.path();
        let modules = resolve_project(&p, Some(root)).unwrap();
        merge_modules(modules).unwrap();
    }

    #[test]
    fn cargo_export_fn_name_sanitizes() {
        assert_eq!(
            cargo_export_fn_name("cargo:tish_serde_json"),
            "cargo_native_tish_serde_json_object"
        );
        assert_eq!(
            cargo_export_fn_name("cargo:my-crate"),
            "cargo_native_my_crate_object"
        );
    }
}