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
// public transpile function which can also be used by gcs to transpile clientside ts

use crate::jsutils::JsError;
use crate::jsutils::Script;
use crate::quickjs_utils::modules::detect_module;
use crate::quickjsruntimeadapter::QuickJsRuntimeAdapter;
use std::cell::RefCell;
use std::collections::HashMap;
use std::io;
use std::str::FromStr;
use std::sync::Arc;
use swc::Compiler;
use swc_common::errors::{ColorConfig, Handler};
use swc_common::{FileName, SourceMap};

pub enum TargetVersion {
    Es3,
    Es5,
    Es2016,
    Es2020,
    Es2021,
    Es2022,
}

impl TargetVersion {
    fn as_str(&self) -> &str {
        match self {
            TargetVersion::Es3 => "es3",
            TargetVersion::Es5 => "es5",
            TargetVersion::Es2016 => "es2016",
            TargetVersion::Es2020 => "es2020",
            TargetVersion::Es2021 => "es2021",
            TargetVersion::Es2022 => "es2022",
        }
    }
}

pub struct TypeScriptTranspiler {
    minify: bool,
    mangle: bool,
    external_helpers: bool,
    target: TargetVersion,
    compiler: Compiler,
    source_map: Arc<SourceMap>,
}

impl TypeScriptTranspiler {
    pub fn new(target: TargetVersion, minify: bool, external_helpers: bool, mangle: bool) -> Self {
        let source_map = Arc::<SourceMap>::default();
        let compiler = swc::Compiler::new(source_map.clone());

        Self {
            minify,
            mangle,
            external_helpers,
            target,
            source_map,
            compiler,
        }
    }
    // todo custom target
    pub fn transpile(
        &self,
        code: &str,
        file_name: &str,
        is_module: bool,
    ) -> Result<(String, Option<String>), JsError> {
        let globals = swc_common::Globals::new();
        swc_common::GLOBALS.set(&globals, || {
            let handler = Handler::with_tty_emitter(
                ColorConfig::Auto,
                true,
                false,
                Some(self.source_map.clone()),
            );

            let fm = self
                .source_map
                .new_source_file(FileName::Custom(file_name.into()), code.into());

            let mangle_config = if self.mangle {
                r#"
                    {
                        "topLevel": false,
                        "keepClassNames": true
                    }
                "#
            } else {
                "false"
            };

            let minify_options = if self.minify {
                format!(
                    r#"
                "minify": {{
                  "compress": {{
                    "unused": true
                  }},
                  "format": {{
                    "comments": false
                  }},
                  "mangle": {mangle_config}
                }},
            "#
                )
            } else {
                r#"
                "minify": {
                  "format": {
                    "comments": false
                  }
                },
                "#
                .to_string()
            };

            let module = if is_module {
                r#"
                "module": {
                    "type": "es6",
                    "strict": true,
                    "strictMode": true,
                    "lazy": false,
                    "noInterop": false,
                    "ignoreDynamic": true
                },
                "#
            } else {
                ""
            };

            let cfg_json = format!(
                r#"

            {{
              "minify": {},
              "sourceMaps": true,
              {}
              "jsc": {{
                {}
                "externalHelpers": {},
                "parser": {{
                  "syntax": "typescript",
                  "jsx": true,
                  "tsx": true,
                  "decorators": true,
                  "decoratorsBeforeExport": true,
                  "dynamicImport": true,
                  "preserveAllComments": false
                }},
                "transform": {{
                  "legacyDecorator": true,
                  "decoratorMetadata": true,
                  "react": {{
                      "runtime": "classic",
                      "useBuiltins": true,
                      "refresh": true
                  }}
                }},
                "target": "{}",
                "keepClassNames": true
              }}
            }}

        "#,
                self.minify,
                module,
                minify_options,
                self.external_helpers,
                self.target.as_str()
            );

            log::trace!("using config {}", cfg_json);

            let cfg = serde_json::from_str(cfg_json.as_str())
                .map_err(|e| JsError::new_string(format!("{e}")))?;

            let ops = swc::config::Options {
                config: cfg,
                ..Default::default()
            };

            // todo see https://github.com/swc-project/swc/discussions/4126
            // for better example

            let res = self.compiler.process_js_file(fm, &handler, &ops);

            match res {
                Ok(to) => Ok((to.code, to.map)),
                Err(e) => Err(JsError::new_string(format!("transpile failed: {e}"))),
            }
        })
    }

    pub fn transpile_script(&self, script: &mut Script) -> Result<(), JsError> {
        if script.get_path().ends_with(".ts") {
            let code = script.get_code();

            let is_module = detect_module(code);

            let js = self.transpile(code, script.get_path(), is_module)?;
            log::debug!("map: {:?}", js.1);
            script.set_transpiled_code(js.0, js.1);
        }
        log::debug!(
            "TypeScriptPreProcessor:process file={} result = {}",
            script.get_path(),
            script.get_runnable_code()
        );

        Ok(())
    }
}

impl Default for TypeScriptTranspiler {
    fn default() -> Self {
        Self::new(TargetVersion::Es2020, false, true, false)
    }
}

thread_local! {
    // we store this in a thread local inb the worker thread so they are dropped when the runtimefacade is dropped
    static SOURCE_MAPS: RefCell<HashMap<String, String>> = RefCell::new(HashMap::new());
    static TRANSPILER: RefCell<TypeScriptTranspiler> = RefCell::new(TypeScriptTranspiler::new(TargetVersion::Es2020, false, true, false));
}

// fix stacktrace method
pub(crate) fn transpile_serverside(
    _rt: &QuickJsRuntimeAdapter,
    script: &mut Script,
) -> Result<(), JsError> {
    // transpile and store map in qjsrt

    // transpile
    TRANSPILER.with(|rc| {
        let transpiler: &TypeScriptTranspiler = &rc.borrow();
        transpiler.transpile_script(script)
    })?;

    // register in source_maps so fix_stack can use it later
    if let Some(map_str) = script.get_map() {
        SOURCE_MAPS.with(|rc| {
            let maps = &mut *rc.borrow_mut();
            maps.insert(script.get_path().to_string(), map_str.to_string());
        })
    }
    Ok(())
}

#[derive(Debug)]
struct StackEntry {
    function_name: String,
    file_name: String,
    line_number: Option<u32>,
    column_number: Option<u32>,
}

impl FromStr for StackEntry {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // remove 'at '
        let s = &s[3..];

        let mut parts = s.splitn(2, ' ');
        let function_name = parts.next().unwrap_or("unnamed").to_string();
        let mut file_name = parts.next().unwrap_or("(unknown)").to_string();
        if file_name.starts_with('(') {
            file_name = file_name.as_str()[1..].to_string();
        }
        if file_name.ends_with(')') {
            file_name = file_name.as_str()[..file_name.len() - 1].to_string();
        }
        file_name = file_name.replace("://", "_double_point_placeholder_//");

        let parts: Vec<&str> = file_name.split(':').collect();

        let file_name = parts[0]
            .to_string()
            .replace("_double_point_placeholder_//", "://");
        let line_number = parts.get(1).and_then(|s| s.parse::<u32>().ok());
        let column_number = parts.get(2).and_then(|s| s.parse::<u32>().ok());

        Ok(StackEntry {
            function_name,
            file_name,
            column_number,
            line_number,
        })
    }
}

fn parse_stack_trace(stack_trace: &str) -> Result<Vec<StackEntry>, String> {
    let entries: Vec<StackEntry> = stack_trace
        .lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .map(|line| line.parse::<StackEntry>())
        .collect::<Result<Vec<_>, _>>()?;

    Ok(entries)
}

fn serialize_stack(entries: &[StackEntry]) -> String {
    let mut result = String::new();

    for entry in entries {
        let fname_lnum = if let Some(line_number) = entry.line_number {
            if let Some(column_number) = entry.column_number {
                format!("{}:{line_number}:{column_number}", entry.file_name)
            } else {
                format!("{}:{line_number}", entry.file_name)
            }
        } else {
            entry.file_name.clone()
        };

        result.push_str(&format!("    at {} ({fname_lnum})", entry.function_name));

        result.push('\n');
    }

    result
}

pub(crate) fn unmap_stack_trace(stack_trace: &str) -> String {
    // todo: not the fastest way to impl this.. should I keep instances of source map instead of string? what does that do to mem consumtion?
    SOURCE_MAPS.with(|rc| fix_stack_trace(stack_trace, &rc.borrow()))
}

pub fn fix_stack_trace(stack_trace: &str, maps: &HashMap<String, String>) -> String {
    log::trace!("fix_stack_trace:\n{stack_trace}");
    match parse_stack_trace(stack_trace) {
        Ok(mut parsed_stack) => {
            for stack_trace_entry in parsed_stack.iter_mut() {
                if let Some(map_str) = maps.get(stack_trace_entry.file_name.as_str()) {
                    log::trace!(
                        "fix_stack_trace:found map for file {}:\n{map_str}",
                        stack_trace_entry.file_name.as_str()
                    );
                    if let Some(line_number) = stack_trace_entry.line_number {
                        log::trace!("lookup line number:{line_number}");
                        match swc::sourcemap::SourceMap::from_reader(io::Cursor::new(map_str)) {
                            Ok(source_map) => {
                                if let Some(original_location) = source_map.lookup_token(
                                    line_number,
                                    stack_trace_entry.column_number.unwrap_or(1),
                                ) {
                                    let original_line = original_location.get_src_line();
                                    let original_column = original_location.get_src_col();
                                    log::trace!("lookup original_line:{original_line}");
                                    stack_trace_entry.line_number = Some(original_line);
                                    stack_trace_entry.column_number = Some(original_column);
                                }
                            }
                            Err(_) => {
                                log::trace!(
                                    "could not parse source_map for {}",
                                    stack_trace_entry.file_name.as_str()
                                );
                            }
                        }
                    }
                } else {
                    log::trace!("no map found for {}", stack_trace_entry.file_name.as_str());
                }

                // Now you have the original filename and line number
                // You can use them as needed
            }

            let ret = serialize_stack(&parsed_stack);
            log::trace!("fix_stack_trace ret:\n{ret}");
            ret
        }
        Err(_) => {
            log::error!("could not parse stack: \n{}", stack_trace);
            stack_trace.to_string()
        }
    }
}

#[cfg(test)]
pub mod tests {
    use crate::facades::tests::init_test_rt;
    use crate::jsutils::{JsValueType, Script};
    use crate::typescript::{parse_stack_trace, serialize_stack};

    #[test]
    fn test_ts() {
        let rt = init_test_rt();
        println!("testing ts");
        let script = Script::new(
            "test.ts",
            r#"
            // hi
            // ho
            function t_ts(a: string, b: num): boolean {
                return true;
            }
            t_ts("hello", 1337);
        "#,
        );
        let res = rt.eval_sync(None, script).expect("script failed");
        assert!(res.get_value_type() == JsValueType::Boolean);
    }
    #[test]
    fn test_stack_map() {
        let rt = init_test_rt();
        println!("testing ts");
        let script = Script::new(
            "test.ts",
            r#"
            
            type Nonsense = {
                hello: string
            };
            
            function t_ts(a: string, b: num): boolean {
                return a.a.a === "hi";
            }

            t_ts("hello", 1337);
"#,
        );
        let _res = rt
            .eval_sync(None, script)
            .expect_err("script passed.. which it shouldnt");
        // far from perfect test, also line numbers don't yet realy match..
        // check again after https://github.com/HiRoFa/quickjs_es_runtime/issues/77
        //assert!(res.get_stack().contains("t_ts (file:///test.ts):8"));
    }
    #[test]
    fn test_stack_parse() {
        // just to init logging;
        let _rt = init_test_rt();

        let stack = r#"
            at func (file.ts:88:12)
            at doWriteTransactioned (gcsproject:///gcs_objectstore/ObjectStore.ts:170)
        "#;
        match parse_stack_trace(stack) {
            Ok(a) => {
                assert_eq!(a[0].file_name, "file.ts");
                assert_eq!(a[0].line_number, Some(88));
                assert_eq!(a[0].column_number, Some(12));
                assert_eq!(a[0].function_name, "func");

                assert_eq!(
                    a[1].file_name,
                    "gcsproject:///gcs_objectstore/ObjectStore.ts"
                );
                assert_eq!(a[1].line_number, Some(170));
                assert_eq!(a[1].column_number, None);
                assert_eq!(a[1].function_name, "doWriteTransactioned");

                assert_eq!(
                    serialize_stack(&a).as_str(),
                    r#"    at func (file.ts:88:12)
    at doWriteTransactioned (gcsproject:///gcs_objectstore/ObjectStore.ts:170)
"#
                );
            }
            Err(e) => {
                panic!("{}", e);
            }
        }
    }
}