depyler-wasm 2.2.0

WebAssembly bindings for Depyler Python-to-Rust transpiler
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
mod utils;

#[cfg(test)]
mod tests;

use depyler_core::DepylerPipeline;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
pub fn set_panic_hook() {
    #[cfg(feature = "console_error_panic_hook")]
    console_error_panic_hook::set_once();
}

#[wasm_bindgen]
extern "C" {
    fn alert(s: &str);

    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);

    #[wasm_bindgen(js_namespace = console, js_name = log)]
    fn log_u32(a: u32);

    #[wasm_bindgen(js_namespace = console, js_name = log)]
    fn log_many(a: &str, b: &str);
}

macro_rules! console_log {
    ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}

#[wasm_bindgen]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmTranspileOptions {
    #[wasm_bindgen(skip)]
    pub verify: bool,
    #[wasm_bindgen(skip)]
    pub optimize: bool,
    #[wasm_bindgen(skip)]
    pub emit_docs: bool,
    #[wasm_bindgen(skip)]
    pub target_version: String,
}

impl Default for WasmTranspileOptions {
    fn default() -> Self {
        Self {
            verify: true,
            optimize: true,
            emit_docs: false,
            target_version: "1.83".to_string(),
        }
    }
}

#[wasm_bindgen]
impl WasmTranspileOptions {
    #[wasm_bindgen(constructor)]
    pub fn new() -> WasmTranspileOptions {
        WasmTranspileOptions::default()
    }

    #[wasm_bindgen(getter)]
    pub fn verify(&self) -> bool {
        self.verify
    }

    #[wasm_bindgen(setter)]
    pub fn set_verify(&mut self, verify: bool) {
        self.verify = verify;
    }

    #[wasm_bindgen(getter)]
    pub fn optimize(&self) -> bool {
        self.optimize
    }

    #[wasm_bindgen(setter)]
    pub fn set_optimize(&mut self, optimize: bool) {
        self.optimize = optimize;
    }

    #[wasm_bindgen(getter)]
    pub fn emit_docs(&self) -> bool {
        self.emit_docs
    }

    #[wasm_bindgen(setter)]
    pub fn set_emit_docs(&mut self, emit_docs: bool) {
        self.emit_docs = emit_docs;
    }

    #[wasm_bindgen(getter)]
    pub fn target_version(&self) -> String {
        self.target_version.clone()
    }

    #[wasm_bindgen(setter)]
    pub fn set_target_version(&mut self, target_version: String) {
        self.target_version = target_version;
    }
}

#[wasm_bindgen]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmTranspileResult {
    #[wasm_bindgen(skip)]
    pub success: bool,
    #[wasm_bindgen(skip)]
    pub rust_code: String,
    #[wasm_bindgen(skip)]
    pub errors: Vec<String>,
    #[wasm_bindgen(skip)]
    pub warnings: Vec<String>,
    #[wasm_bindgen(skip)]
    pub transpile_time_ms: f64,
    #[wasm_bindgen(skip)]
    pub memory_usage_mb: f64,
    #[wasm_bindgen(skip)]
    pub energy_estimate: WasmEnergyEstimate,
    #[wasm_bindgen(skip)]
    pub quality_metrics: WasmQualityMetrics,
}

#[wasm_bindgen]
impl WasmTranspileResult {
    #[wasm_bindgen(getter)]
    pub fn success(&self) -> bool {
        self.success
    }

    #[wasm_bindgen(getter)]
    pub fn rust_code(&self) -> String {
        self.rust_code.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn errors(&self) -> Vec<String> {
        self.errors.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn transpile_time_ms(&self) -> f64 {
        self.transpile_time_ms
    }

    #[wasm_bindgen(getter)]
    pub fn warnings(&self) -> Vec<String> {
        self.warnings.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn memory_usage_mb(&self) -> f64 {
        self.memory_usage_mb
    }

    #[wasm_bindgen(getter)]
    pub fn energy_estimate(&self) -> WasmEnergyEstimate {
        self.energy_estimate.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn quality_metrics(&self) -> WasmQualityMetrics {
        self.quality_metrics.clone()
    }
}

#[wasm_bindgen]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmEnergyEstimate {
    #[wasm_bindgen(skip)]
    pub joules: f64,
    #[wasm_bindgen(skip)]
    pub watts_average: f64,
    #[wasm_bindgen(skip)]
    pub co2_grams: f64,
    #[wasm_bindgen(skip)]
    pub confidence: f64,
}

#[wasm_bindgen]
impl WasmEnergyEstimate {
    #[wasm_bindgen(getter)]
    pub fn joules(&self) -> f64 {
        self.joules
    }

    #[wasm_bindgen(getter)]
    pub fn watts_average(&self) -> f64 {
        self.watts_average
    }

    #[wasm_bindgen(getter)]
    pub fn co2_grams(&self) -> f64 {
        self.co2_grams
    }

    #[wasm_bindgen(getter)]
    pub fn confidence(&self) -> f64 {
        self.confidence
    }
}

#[wasm_bindgen]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmQualityMetrics {
    #[wasm_bindgen(skip)]
    pub pmat_score: f64,
    #[wasm_bindgen(skip)]
    pub productivity: f64,
    #[wasm_bindgen(skip)]
    pub maintainability: f64,
    #[wasm_bindgen(skip)]
    pub accessibility: f64,
    #[wasm_bindgen(skip)]
    pub testability: f64,
    #[wasm_bindgen(skip)]
    pub code_complexity: i32,
    #[wasm_bindgen(skip)]
    pub cyclomatic_complexity: i32,
}

#[wasm_bindgen]
impl WasmQualityMetrics {
    #[wasm_bindgen(getter)]
    pub fn pmat_score(&self) -> f64 {
        self.pmat_score
    }

    #[wasm_bindgen(getter)]
    pub fn productivity(&self) -> f64 {
        self.productivity
    }

    #[wasm_bindgen(getter)]
    pub fn maintainability(&self) -> f64 {
        self.maintainability
    }

    #[wasm_bindgen(getter)]
    pub fn accessibility(&self) -> f64 {
        self.accessibility
    }

    #[wasm_bindgen(getter)]
    pub fn testability(&self) -> f64 {
        self.testability
    }

    #[wasm_bindgen(getter)]
    pub fn code_complexity(&self) -> i32 {
        self.code_complexity
    }

    #[wasm_bindgen(getter)]
    pub fn cyclomatic_complexity(&self) -> i32 {
        self.cyclomatic_complexity
    }
}

#[wasm_bindgen]
pub struct DepylerWasm {
    initialized: bool,
}

#[wasm_bindgen]
impl DepylerWasm {
    #[wasm_bindgen(constructor)]
    pub fn new() -> DepylerWasm {
        set_panic_hook();
        console_log!("Depyler WASM initialized");

        DepylerWasm { initialized: true }
    }

    #[wasm_bindgen]
    pub fn transpile(
        &self,
        python_code: &str,
        options: &WasmTranspileOptions,
    ) -> Result<WasmTranspileResult, JsValue> {
        if !self.initialized {
            return Err(JsValue::from_str("DepylerWasm not initialized"));
        }

        let start_time = web_sys::window()
            .and_then(|w| w.performance())
            .map(|p| p.now())
            .unwrap_or(0.0);

        let mem_before = get_memory_usage();

        // Create pipeline with options
        let mut pipeline = DepylerPipeline::new();
        if options.verify {
            pipeline = pipeline.with_verification();
        }

        // Perform transpilation
        let result = match pipeline.transpile(python_code) {
            Ok(rust_code) => {
                let transpile_time = web_sys::window()
                    .and_then(|w| w.performance())
                    .map(|p| p.now() - start_time)
                    .unwrap_or(0.0);

                let mem_after = get_memory_usage();
                let memory_usage = (mem_after - mem_before).max(0.0);

                // Calculate energy estimate
                let energy_estimate = calculate_energy_estimate(transpile_time, memory_usage);

                // Calculate quality metrics
                let quality_metrics = calculate_quality_metrics(python_code, &rust_code);

                WasmTranspileResult {
                    success: true,
                    rust_code,
                    errors: vec![],
                    warnings: vec![],
                    transpile_time_ms: transpile_time,
                    memory_usage_mb: memory_usage,
                    energy_estimate,
                    quality_metrics,
                }
            }
            Err(e) => {
                let transpile_time = web_sys::window()
                    .and_then(|w| w.performance())
                    .map(|p| p.now() - start_time)
                    .unwrap_or(0.0);

                WasmTranspileResult {
                    success: false,
                    rust_code: String::new(),
                    errors: vec![e.to_string()],
                    warnings: vec![],
                    transpile_time_ms: transpile_time,
                    memory_usage_mb: 0.0,
                    energy_estimate: WasmEnergyEstimate {
                        joules: 0.0,
                        watts_average: 0.0,
                        co2_grams: 0.0,
                        confidence: 0.0,
                    },
                    quality_metrics: WasmQualityMetrics {
                        pmat_score: 0.0,
                        productivity: 0.0,
                        maintainability: 0.0,
                        accessibility: 0.0,
                        testability: 0.0,
                        code_complexity: 0,
                        cyclomatic_complexity: 0,
                    },
                }
            }
        };

        Ok(result)
    }

    #[wasm_bindgen]
    pub fn analyze_code(&self, python_code: &str) -> Result<JsValue, JsValue> {
        if !self.initialized {
            return Err(JsValue::from_str("DepylerWasm not initialized"));
        }

        let pipeline = DepylerPipeline::new();

        // Parse to HIR
        let _hir = match pipeline.parse_to_hir(python_code) {
            Ok(hir) => hir,
            Err(e) => return Err(JsValue::from_str(&format!("Parse error: {e}"))),
        };

        // Perform static analysis using simple analysis
        let analysis = perform_static_analysis(python_code);

        // Convert to JS-compatible format
        serde_wasm_bindgen::to_value(&analysis).map_err(|e| JsValue::from_str(&e.to_string()))
    }

    #[wasm_bindgen]
    pub fn get_version(&self) -> String {
        env!("CARGO_PKG_VERSION").to_string()
    }

    #[wasm_bindgen]
    pub fn benchmark(&self, python_code: &str, iterations: u32) -> Result<JsValue, JsValue> {
        if !self.initialized {
            return Err(JsValue::from_str("DepylerWasm not initialized"));
        }

        let mut results = Vec::new();
        let options = WasmTranspileOptions::default();

        for _ in 0..iterations {
            let result = self.transpile(python_code, &options)?;
            results.push(result.transpile_time_ms);
        }

        let benchmark_result = BenchmarkResult {
            iterations,
            times_ms: results.clone(),
            min_ms: results.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
            max_ms: results.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)),
            mean_ms: results.iter().sum::<f64>() / results.len() as f64,
            median_ms: {
                let mut sorted = results.clone();
                sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
                sorted[sorted.len() / 2]
            },
            std_dev_ms: {
                let mean = results.iter().sum::<f64>() / results.len() as f64;
                let variance =
                    results.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / results.len() as f64;
                variance.sqrt()
            },
        };

        serde_wasm_bindgen::to_value(&benchmark_result)
            .map_err(|e| JsValue::from_str(&e.to_string()))
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    pub iterations: u32,
    pub times_ms: Vec<f64>,
    pub min_ms: f64,
    pub max_ms: f64,
    pub mean_ms: f64,
    pub median_ms: f64,
    pub std_dev_ms: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StaticAnalysis {
    pub complexity: i32,
    pub cyclomatic_complexity: i32,
    pub functions: Vec<FunctionInfo>,
    pub imports: Vec<String>,
    pub suggestions: Vec<OptimizationSuggestion>,
    pub anti_patterns: Vec<AntiPattern>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionInfo {
    pub name: String,
    pub line_start: u32,
    pub line_end: u32,
    pub complexity: i32,
    pub parameters: Vec<String>,
    pub return_type: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSuggestion {
    pub line: u32,
    pub column: u32,
    pub message: String,
    pub suggestion_type: String,
    pub confidence: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AntiPattern {
    pub line: u32,
    pub column: u32,
    pub pattern: String,
    pub description: String,
    pub severity: String,
}

fn get_memory_usage() -> f64 {
    // Memory API is not standard, return 0 for now
    // In a real implementation, you might track WASM memory growth
    0.0
}

fn calculate_energy_estimate(execution_ms: f64, memory_mb: f64) -> WasmEnergyEstimate {
    // Energy model based on empirical data
    let cpu_joules_per_ms = 0.001_f64; // Optimized Rust
    let mem_joules_per_mb = 0.0002_f64;
    let baseline_watts = 1.0_f64;

    let cpu_energy = execution_ms * cpu_joules_per_ms;
    let mem_energy = memory_mb * mem_joules_per_mb;
    let total_joules = cpu_energy + mem_energy;

    // Calculate confidence based on execution time and memory usage
    let time_weight = (execution_ms / 100.0).min(1.0) * 0.7;
    let mem_weight = (memory_mb / 10.0).min(1.0) * 0.3;
    let confidence = time_weight + mem_weight;

    WasmEnergyEstimate {
        joules: total_joules,
        watts_average: baseline_watts,
        co2_grams: total_joules * 0.475, // US grid average
        confidence,
    }
}

fn calculate_quality_metrics(python_code: &str, rust_code: &str) -> WasmQualityMetrics {
    // Simple quality metrics calculation
    let python_lines = python_code.lines().count();
    let rust_lines = rust_code.lines().count();

    // Calculate basic complexity
    let complexity = calculate_code_complexity(python_code);
    let cyclomatic_complexity = calculate_cyclomatic_complexity(python_code);

    // PMAT scoring (simplified)
    let productivity = calculate_productivity_score(python_lines, rust_lines);
    let maintainability = calculate_maintainability_score(rust_code);
    let accessibility = 0.85; // Rust is generally accessible
    let testability = calculate_testability_score(rust_code);

    let pmat_score = (productivity + maintainability + accessibility + testability) / 4.0;

    WasmQualityMetrics {
        pmat_score,
        productivity,
        maintainability,
        accessibility,
        testability,
        code_complexity: complexity,
        cyclomatic_complexity,
    }
}

fn calculate_code_complexity(code: &str) -> i32 {
    let mut complexity = 1; // Base complexity

    for line in code.lines() {
        if line.trim_start().starts_with("if ")
            || line.trim_start().starts_with("elif ")
            || line.trim_start().starts_with("while ")
            || line.trim_start().starts_with("for ")
            || line.trim_start().starts_with("try:")
            || line.trim_start().starts_with("except ")
        {
            complexity += 1;
        }
    }

    complexity
}

fn calculate_cyclomatic_complexity(code: &str) -> i32 {
    let mut complexity = 1;

    // Count decision points
    for line in code.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("if ")
            || trimmed.starts_with("elif ")
            || trimmed.starts_with("while ")
            || trimmed.starts_with("for ")
            || trimmed.contains(" and ")
            || trimmed.contains(" or ")
        {
            complexity += 1;
        }
    }

    complexity
}

fn calculate_productivity_score(python_lines: usize, rust_lines: usize) -> f64 {
    // Productivity based on conciseness and maintainability
    if rust_lines == 0 {
        return 0.0;
    }

    let ratio = python_lines as f64 / rust_lines as f64;
    // Score higher when Rust code is reasonably verbose (better than too concise)
    if ratio > 0.5 && ratio < 2.0 {
        0.9
    } else if ratio >= 2.0 {
        0.7
    } else {
        0.5
    }
}

fn calculate_maintainability_score(rust_code: &str) -> f64 {
    let mut score = 0.8_f64; // Base score for Rust

    // Check for good practices
    if rust_code.contains("/// ") {
        score += 0.1; // Documentation
    }
    if rust_code.contains("#[test]") {
        score += 0.1; // Tests
    }
    if rust_code.contains("Result<") {
        score += 0.05; // Error handling
    }

    score.min(1.0_f64)
}

fn calculate_testability_score(rust_code: &str) -> f64 {
    let mut score = 0.7_f64; // Base score

    if rust_code.contains("pub fn ") {
        score += 0.1; // Public functions
    }
    if rust_code.contains("impl ") {
        score += 0.1; // Structured code
    }
    if rust_code.contains("#[cfg(test)]") {
        score += 0.1; // Test modules
    }

    score.min(1.0_f64)
}

fn perform_static_analysis(python_code: &str) -> StaticAnalysis {
    let lines: Vec<&str> = python_code.lines().collect();
    let mut functions = Vec::new();
    let mut suggestions = Vec::new();
    let mut anti_patterns = Vec::new();
    let mut imports = Vec::new();

    // Simple parsing for demonstration
    for (line_num, line) in lines.iter().enumerate() {
        let line_num = line_num as u32 + 1;
        let trimmed = line.trim();

        // Detect function definitions
        if trimmed.starts_with("def ") {
            if let Some(func_name) = extract_function_name(trimmed) {
                functions.push(FunctionInfo {
                    name: func_name,
                    line_start: line_num,
                    line_end: line_num, // Simplified
                    complexity: 1,
                    parameters: vec![], // Simplified
                    return_type: None,
                });
            }
        }

        // Detect imports
        if trimmed.starts_with("import ") || trimmed.starts_with("from ") {
            imports.push(trimmed.to_string());
        }

        // Detect anti-patterns
        if trimmed.contains("eval(") {
            anti_patterns.push(AntiPattern {
                line: line_num,
                column: line.find("eval(").unwrap_or(0) as u32,
                pattern: "eval()".to_string(),
                description: "Using eval() is dangerous and hard to optimize".to_string(),
                severity: "high".to_string(),
            });
        }

        // Suggest optimizations
        if trimmed.contains("range(len(") {
            suggestions.push(OptimizationSuggestion {
                line: line_num,
                column: line.find("range(len(").unwrap_or(0) as u32,
                message: "Consider using enumerate() instead of range(len())".to_string(),
                suggestion_type: "performance".to_string(),
                confidence: 0.9,
            });
        }
    }

    StaticAnalysis {
        complexity: calculate_code_complexity(python_code),
        cyclomatic_complexity: calculate_cyclomatic_complexity(python_code),
        functions,
        imports,
        suggestions,
        anti_patterns,
    }
}

fn extract_function_name(line: &str) -> Option<String> {
    // Extract function name from "def func_name(args):"
    if let Some(start) = line.find("def ") {
        let after_def = &line[start + 4..];
        if let Some(end) = after_def.find('(') {
            return Some(after_def[..end].trim().to_string());
        }
    }
    None
}

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

// Convenience alias for playground usage
pub type PlaygroundEngine = DepylerWasm;

// Called when the wasm module is instantiated
#[wasm_bindgen(start)]
pub fn main() {
    set_panic_hook();
    console_log!("Depyler WASM module loaded successfully");
}

// Helper function to calculate average complexity
#[allow(dead_code)]
fn calculate_avg_complexity(hir: &depyler_core::hir::HirModule) -> f64 {
    if hir.functions.is_empty() {
        return 0.0;
    }

    let total_complexity: u32 = hir
        .functions
        .iter()
        .map(|f| depyler_analyzer::calculate_cyclomatic(&f.body))
        .sum();

    total_complexity as f64 / hir.functions.len() as f64
}