wasmrun 0.19.0

A WebAssembly Runtime
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
use crate::commands::{verify_wasm, VerificationResult};
use crate::error::{Result, WasmrunError};
use crate::utils::{CommandExecutor, PathResolver};
use std::fs;
use std::path::Path;

/// Comprehensive WASM file analysis for CLI display
#[derive(Debug)]
pub struct WasmAnalysis {
    pub path: String,
    pub filename: String,
    pub file_size: String,
    #[allow(dead_code)]
    pub file_size_bytes: u64,
    #[allow(dead_code)]
    pub verification: Option<VerificationResult>,
    pub is_valid: bool,
    pub entry_points: Vec<String>,
    #[allow(dead_code)]
    pub is_wasm_bindgen: bool,
    #[allow(dead_code)]
    pub is_wasi: bool,
    pub module_type: ModuleType,
    #[allow(dead_code)]
    pub imports_count: usize,
    pub exports_count: usize,
    pub functions_count: usize,
}

#[derive(Debug, Clone)]
pub enum ModuleType {
    StandardWasm,
    WasmBindgen,
    WasiModule,
    #[allow(dead_code)]
    WebApplication,
    Unknown,
}

impl std::fmt::Display for ModuleType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ModuleType::StandardWasm => write!(f, "Standard WebAssembly"),
            ModuleType::WasmBindgen => write!(f, "WASM-Bindgen Module"),
            ModuleType::WasiModule => write!(f, "WASI Module"),
            ModuleType::WebApplication => write!(f, "Web Application"),
            ModuleType::Unknown => write!(f, "Unknown"),
        }
    }
}

impl WasmAnalysis {
    pub fn analyze(path: &str) -> Result<Self> {
        let path_obj = Path::new(path);

        // Validate file exists and has correct extension
        PathResolver::validate_wasm_file(path)?;

        let filename = PathResolver::get_filename(path)?;
        let file_size_bytes = fs::metadata(path)
            .map_err(|e| WasmrunError::add_context(format!("Getting file size for {path}"), e))?
            .len();

        let file_size = CommandExecutor::format_file_size(file_size_bytes);

        // Verify wasm
        let verification = verify_wasm(path).ok();

        let is_valid = verification.as_ref().is_some_and(|v| v.valid_magic);

        // Analyze entry points
        let entry_points = if let Some(ref verify_result) = verification {
            extract_entry_points(verify_result)
        } else {
            Vec::new()
        };

        // Determine module characteristics
        let is_wasm_bindgen = detect_wasm_bindgen(path_obj);
        let is_wasi = verification.as_ref().is_some_and(|v| {
            v.has_export_section && v.export_names.iter().any(|name| name == "_start")
        });

        // Determine module type
        let module_type = determine_module_type(&verification, is_wasm_bindgen, is_wasi);

        let (imports_count, exports_count, functions_count) =
            if let Some(ref verify_result) = verification {
                (
                    0,
                    verify_result.export_names.len(),
                    verify_result.function_count,
                )
            } else {
                (0, 0, 0)
            };

        Ok(WasmAnalysis {
            path: path.to_string(),
            filename,
            file_size,
            file_size_bytes,
            verification,
            is_valid,
            entry_points,
            is_wasm_bindgen,
            is_wasi,
            module_type,
            imports_count,
            exports_count,
            functions_count,
        })
    }

    /// Print comprehensive analysis to console
    pub fn print_analysis(&self) {
        println!("\n\x1b[1;34m╭─────────────────────────────────────────────────────────────────╮\x1b[0m");
        println!("\x1b[1;34m│\x1b[0m  🔍 \x1b[1;36mWASM File Analysis\x1b[0m                                     \x1b[1;34m│\x1b[0m");
        println!(
            "\x1b[1;34m├─────────────────────────────────────────────────────────────────┤\x1b[0m"
        );

        println!("\x1b[1;34m│\x1b[0m  📦 \x1b[1;34mFile:\x1b[0m \x1b[1;33m{:<51}\x1b[0m \x1b[1;34m│\x1b[0m", 
                 self.filename);
        println!("\x1b[1;34m│\x1b[0m  📂 \x1b[1;34mPath:\x1b[0m \x1b[0;37m{:<51}\x1b[0m \x1b[1;34m│\x1b[0m", 
                 self.path);
        println!("\x1b[1;34m│\x1b[0m  💾 \x1b[1;34mSize:\x1b[0m \x1b[1;33m{:<51}\x1b[0m \x1b[1;34m│\x1b[0m", 
                 self.file_size);

        if self.is_valid {
            println!("\x1b[1;34m│\x1b[0m  ✅ \x1b[1;34mStatus:\x1b[0m \x1b[1;32mValid WebAssembly{:<32}\x1b[0m \x1b[1;34m│\x1b[0m", "");
            println!("\x1b[1;34m│\x1b[0m  🏷️  \x1b[1;34mType:\x1b[0m \x1b[1;36m{:<49}\x1b[0m \x1b[1;34m│\x1b[0m", 
                     self.module_type.to_string());
            println!("\x1b[1;34m│\x1b[0m  📊 \x1b[1;34mExports:\x1b[0m \x1b[1;33m{:<47}\x1b[0m \x1b[1;34m│\x1b[0m", 
                     self.exports_count);
            println!("\x1b[1;34m│\x1b[0m  🔧 \x1b[1;34mFunctions:\x1b[0m \x1b[1;33m{:<45}\x1b[0m \x1b[1;34m│\x1b[0m", 
                     self.functions_count);
        } else {
            println!("\x1b[1;34m│\x1b[0m  ❌ \x1b[1;34mStatus:\x1b[0m \x1b[1;31mInvalid Format{:<36}\x1b[0m \x1b[1;34m│\x1b[0m", "");
        }

        println!(
            "\x1b[1;34m╰─────────────────────────────────────────────────────────────────╯\x1b[0m"
        );
    }

    /// Get summary of WASM Module
    pub fn get_summary(&self) -> String {
        if !self.is_valid {
            return format!("❌ Invalid WASM file ({})", self.file_size);
        }

        let type_indicator = match self.module_type {
            ModuleType::WasiModule => "🔧",
            ModuleType::WasmBindgen => "🌐",
            ModuleType::WebApplication => "📱",
            _ => "",
        };

        let entry_info = if !self.entry_points.is_empty() {
            format!(" • Entry: {}", self.entry_points[0])
        } else {
            String::new()
        };

        format!(
            "{} {} ({}{} exports{})",
            type_indicator, self.module_type, self.file_size, self.exports_count, entry_info
        )
    }
}

/// Comprehensive project analysis for directories
#[derive(Debug)]
pub struct ProjectAnalysis {
    pub path: String,
    pub project_name: String,
    pub language: crate::compiler::ProjectLanguage,
    // pub is_web_app: bool,
    #[allow(dead_code)]
    pub has_cargo_toml: bool,
    pub entry_files: Vec<String>,
    pub build_files: Vec<String>,
}

impl ProjectAnalysis {
    /// Analyze a project directory
    pub fn analyze(path: &str) -> Result<Self> {
        PathResolver::validate_directory_exists(path)?;

        let project_name = Path::new(path)
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();

        let language = crate::compiler::detect_project_language(path);
        // let is_web_app = language == crate::compiler::ProjectLanguage::Rust
        //     && crate::compiler::is_rust_web_application(path);

        let mut entry_files = Vec::new();
        let mut build_files = Vec::new();
        let mut has_cargo_toml = false;

        // Common files
        let important_files = [
            ("Cargo.toml", true),
            ("package.json", true),
            ("Makefile", true),
            ("go.mod", true),
            ("main.rs", false),
            ("lib.rs", false),
            ("main.go", false),
            ("main.c", false),
            ("index.ts", false),
            ("index.js", false),
        ];

        for (filename, is_build_file) in &important_files {
            let file_path = PathResolver::join_paths(path, filename);
            if Path::new(&file_path).exists() {
                if *filename == "Cargo.toml" {
                    has_cargo_toml = true;
                }

                if *is_build_file {
                    build_files.push(filename.to_string());
                } else {
                    entry_files.push(filename.to_string());
                }
            }
        }

        Ok(ProjectAnalysis {
            path: path.to_string(),
            project_name,
            language,
            // is_web_app,
            has_cargo_toml,
            entry_files,
            build_files,
        })
    }

    /// Print comprehensive project analysis
    pub fn print_analysis(&self) {
        println!("\n\x1b[1;34m╭─────────────────────────────────────────────────────────────────╮\x1b[0m");
        println!("\x1b[1;34m│\x1b[0m  📁 \x1b[1;36mProject Analysis\x1b[0m                                       \x1b[1;34m│\x1b[0m");
        println!(
            "\x1b[1;34m├─────────────────────────────────────────────────────────────────┤\x1b[0m"
        );

        println!("\x1b[1;34m│\x1b[0m  📦 \x1b[1;34mName:\x1b[0m \x1b[1;33m{:<51}\x1b[0m \x1b[1;34m│\x1b[0m", 
                 truncate_string(&self.project_name, 51));
        println!("\x1b[1;34m│\x1b[0m  📂 \x1b[1;34mPath:\x1b[0m \x1b[0;37m{:<51}\x1b[0m \x1b[1;34m│\x1b[0m", 
                 truncate_string(&self.path, 51));

        let language_icon = match self.language {
            // crate::compiler::ProjectLanguage::Rust => "🦀",
            // crate::compiler::ProjectLanguage::Go => "🐹",
            crate::compiler::ProjectLanguage::C => "🔧",
            crate::compiler::ProjectLanguage::Asc => "📜",
            crate::compiler::ProjectLanguage::Python => "🐍",
            _ => "",
        };

        println!("\x1b[1;34m│\x1b[0m  {} \x1b[1;34mLanguage:\x1b[0m \x1b[1;32m{:<49}\x1b[0m \x1b[1;34m│\x1b[0m", 
                 language_icon, format!("{:?}", self.language));

        // if self.is_web_app {
        //     println!("\x1b[1;34m│\x1b[0m  🌐 \x1b[1;32mWeb Application Detected\x1b[0m                              \x1b[1;34m│\x1b[0m");
        // }

        if !self.build_files.is_empty() {
            println!("\x1b[1;34m│\x1b[0m  🔧 \x1b[1;34mBuild Files:\x1b[0m \x1b[1;33m{:<45}\x1b[0m \x1b[1;34m│\x1b[0m", 
                     self.build_files.join(", "));
        }

        if !self.entry_files.is_empty() {
            println!("\x1b[1;34m│\x1b[0m  📄 \x1b[1;34mEntry Files:\x1b[0m \x1b[1;33m{:<45}\x1b[0m \x1b[1;34m│\x1b[0m", 
                     self.entry_files.join(", "));
        }

        println!(
            "\x1b[1;34m╰─────────────────────────────────────────────────────────────────╯\x1b[0m"
        );
    }

    /// Get a brief summary
    pub fn get_summary(&self) -> String {
        let language_icon = match self.language {
            // crate::compiler::ProjectLanguage::Rust => "🦀",
            // crate::compiler::ProjectLanguage::Go => "🐹",
            crate::compiler::ProjectLanguage::C => "🔧",
            crate::compiler::ProjectLanguage::Asc => "📜",
            crate::compiler::ProjectLanguage::Python => "🐍",
            _ => "",
        };

        // let app_type = if self.is_web_app { " (Web App)" } else { "" };

        format!("{} {:?} project{}", language_icon, self.language, "")
    }
}

// Helper functions

fn extract_entry_points(verification: &VerificationResult) -> Vec<String> {
    let mut entry_points = Vec::new();

    // Check for standard entry points
    for export_name in &verification.export_names {
        if is_entry_point(export_name) {
            entry_points.push(export_name.clone());
        }
    }

    // If we have a start section, note that
    if verification.has_start_section {
        if let Some(index) = verification.start_function_index {
            entry_points.push(format!("_start (index {index})"));
        } else {
            entry_points.push("_start".to_string());
        }
    }

    entry_points
}

fn is_entry_point(name: &str) -> bool {
    matches!(
        name,
        "main" | "_start" | "start" | "init" | "run" | "execute" | "_initialize"
    )
}

fn detect_wasm_bindgen(path: &Path) -> bool {
    // Check if there's a corresponding JS file with wasm-bindgen patterns
    if let Some(parent) = path.parent() {
        let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");

        // Look for corresponding JS file
        let js_candidates = [
            format!("{stem}.js"),
            format!("{}_bg.js", stem.trim_end_matches("_bg")),
        ];

        for js_name in &js_candidates {
            let js_path = parent.join(js_name);
            if js_path.exists() {
                if let Ok(content) = fs::read_to_string(&js_path) {
                    if content.contains("wasm_bindgen") || content.contains("__wbindgen") {
                        return true;
                    }
                }
            }
        }
    }

    false
}

fn determine_module_type(
    verification: &Option<VerificationResult>,
    is_wasm_bindgen: bool,
    is_wasi: bool,
) -> ModuleType {
    if is_wasm_bindgen {
        ModuleType::WasmBindgen
    } else if is_wasi {
        ModuleType::WasiModule
    } else if let Some(ref verify_result) = verification {
        if verify_result.valid_magic && verify_result.has_export_section {
            ModuleType::StandardWasm
        } else {
            ModuleType::Unknown
        }
    } else {
        ModuleType::Unknown
    }
}

fn truncate_string(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::VerificationResult;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    const VALID_WASM_BYTES: [u8; 8] = [0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00];

    fn create_wasm_file_with_extension(content: &[u8]) -> tempfile::NamedTempFile {
        let mut temp_file = tempfile::Builder::new().suffix(".wasm").tempfile().unwrap();
        temp_file.write_all(content).unwrap();
        temp_file
    }

    fn create_mock_verification_result() -> VerificationResult {
        VerificationResult {
            valid_magic: true,
            file_size: 100,
            section_count: 3,
            sections: vec![],
            has_export_section: true,
            export_names: vec!["main".to_string(), "test_func".to_string()],
            has_start_section: false,
            start_function_index: None,
            has_memory_section: true,
            memory_limits: Some((1, Some(10))),
            has_table_section: false,
            function_count: 5,
        }
    }

    #[test]
    fn test_module_type_display() {
        assert_eq!(
            format!("{}", ModuleType::StandardWasm),
            "Standard WebAssembly"
        );
        assert_eq!(
            format!("{}", ModuleType::WasmBindgen),
            "WASM-Bindgen Module"
        );
        assert_eq!(format!("{}", ModuleType::WasiModule), "WASI Module");
        assert_eq!(format!("{}", ModuleType::WebApplication), "Web Application");
        assert_eq!(format!("{}", ModuleType::Unknown), "Unknown");
    }

    #[test]
    fn test_extract_entry_points() {
        let verification = VerificationResult {
            export_names: vec!["main".to_string(), "init".to_string(), "other".to_string()],
            has_start_section: true,
            start_function_index: Some(0),
            ..create_mock_verification_result()
        };

        let entry_points = extract_entry_points(&verification);
        assert!(entry_points.contains(&"main".to_string()));
        assert!(entry_points.contains(&"init".to_string()));
        assert!(!entry_points.contains(&"other".to_string()));
        assert!(entry_points.iter().any(|p| p.contains("_start")));
    }

    #[test]
    fn test_is_entry_point() {
        assert!(is_entry_point("main"));
        assert!(is_entry_point("_start"));
        assert!(is_entry_point("start"));
        assert!(is_entry_point("init"));
        assert!(is_entry_point("run"));
        assert!(is_entry_point("execute"));
        assert!(is_entry_point("_initialize"));
        assert!(!is_entry_point("other"));
        assert!(!is_entry_point(""));
    }

    #[test]
    fn test_detect_wasm_bindgen() {
        let temp_dir = tempdir().unwrap();
        let wasm_path = temp_dir.path().join("test.wasm");
        let js_path = temp_dir.path().join("test.js");

        // Create WASM file
        File::create(&wasm_path).unwrap();

        // Create JS file with wasm-bindgen content
        let mut js_file = File::create(&js_path).unwrap();
        js_file
            .write_all(b"import * as wasm_bindgen from './test_bg.wasm';")
            .unwrap();

        let result = detect_wasm_bindgen(&wasm_path);
        assert!(result);
    }

    #[test]
    fn test_detect_wasm_bindgen_no_js() {
        let temp_dir = tempdir().unwrap();
        let wasm_path = temp_dir.path().join("test.wasm");
        File::create(&wasm_path).unwrap();

        let result = detect_wasm_bindgen(&wasm_path);
        assert!(!result);
    }

    #[test]
    fn test_determine_module_type_wasm_bindgen() {
        let verification = Some(create_mock_verification_result());
        let module_type = determine_module_type(&verification, true, false);
        assert!(matches!(module_type, ModuleType::WasmBindgen));
    }

    #[test]
    fn test_determine_module_type_wasi() {
        let verification = Some(create_mock_verification_result());
        let module_type = determine_module_type(&verification, false, true);
        assert!(matches!(module_type, ModuleType::WasiModule));
    }

    #[test]
    fn test_determine_module_type_standard() {
        let verification = Some(create_mock_verification_result());
        let module_type = determine_module_type(&verification, false, false);
        assert!(matches!(module_type, ModuleType::StandardWasm));
    }

    #[test]
    fn test_determine_module_type_unknown() {
        let module_type = determine_module_type(&None, false, false);
        assert!(matches!(module_type, ModuleType::Unknown));
    }

    #[test]
    fn test_truncate_string_short() {
        let result = truncate_string("short", 10);
        assert_eq!(result, "short");
    }

    #[test]
    fn test_truncate_string_long() {
        let result = truncate_string("this is a very long string", 10);
        assert_eq!(result, "this is...");
    }

    #[test]
    fn test_truncate_string_exact() {
        let result = truncate_string("exactly10!", 10);
        assert_eq!(result, "exactly10!");
    }

    #[test]
    fn test_wasm_analysis_invalid_file() {
        let temp_file = create_wasm_file_with_extension(&[0x00, 0x00, 0x00, 0x00]);
        let result = WasmAnalysis::analyze(temp_file.path().to_str().unwrap());

        // Should still succeed but with invalid WASM
        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(!analysis.is_valid);
    }

    #[test]
    fn test_wasm_analysis_get_summary_invalid() {
        let temp_file = create_wasm_file_with_extension(&[0x00, 0x00, 0x00, 0x00]);
        let analysis = WasmAnalysis::analyze(temp_file.path().to_str().unwrap()).unwrap();

        let summary = analysis.get_summary();
        assert!(summary.contains(""));
        assert!(summary.contains("Invalid"));
    }

    #[test]
    fn test_wasm_analysis_get_summary_valid() {
        let temp_file = create_wasm_file_with_extension(&VALID_WASM_BYTES);
        let analysis = WasmAnalysis::analyze(temp_file.path().to_str().unwrap()).unwrap();

        let summary = analysis.get_summary();
        assert!(
            summary.contains("")
                || summary.contains("🔧")
                || summary.contains("🌐")
                || summary.contains("📱")
        );
    }

    #[test]
    fn test_project_analysis_with_rust_project() {
        let temp_dir = tempdir().unwrap();
        let cargo_toml = temp_dir.path().join("Cargo.toml");
        let mut file = File::create(&cargo_toml).unwrap();
        file.write_all(b"[package]\nname = \"test\"").unwrap();

        let result = ProjectAnalysis::analyze(temp_dir.path().to_str().unwrap());
        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert_eq!(analysis.language, crate::compiler::ProjectLanguage::Rust);
        assert!(analysis.build_files.contains(&"Cargo.toml".to_string()));
        assert!(analysis.has_cargo_toml);
    }

    #[test]
    fn test_project_analysis_with_entry_files() {
        let temp_dir = tempdir().unwrap();

        // Create entry file
        let main_rs = temp_dir.path().join("main.rs");
        File::create(&main_rs).unwrap();

        let result = ProjectAnalysis::analyze(temp_dir.path().to_str().unwrap());
        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(analysis.entry_files.contains(&"main.rs".to_string()));
    }

    #[test]
    fn test_project_analysis_nonexistent_dir() {
        let result = ProjectAnalysis::analyze("/nonexistent/directory");
        assert!(result.is_err());
    }

    #[test]
    fn test_project_analysis_get_summary() {
        let temp_dir = tempdir().unwrap();
        let analysis = ProjectAnalysis::analyze(temp_dir.path().to_str().unwrap()).unwrap();

        let summary = analysis.get_summary();
        assert!(summary.contains("project"));
        assert!(
            summary.contains("")
                || summary.contains("🦀")
                || summary.contains("🐹")
                || summary.contains("🔧")
        );
    }
}