tsrun 0.1.23

A TypeScript interpreter designed for embedding in applications
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
//! Tests for the global eval() function
//!
//! eval() executes JavaScript/TypeScript code in a string and returns the result.
//! Unlike the Function constructor, eval() has access to the current scope.

use super::{eval, throws_error};
use tsrun::JsValue;

// ============================================================================
// Basic eval() behavior
// ============================================================================

#[test]
fn test_eval_basic_expression() {
    assert_eq!(eval("eval('1 + 2')"), JsValue::Number(3.0));
}

#[test]
fn test_eval_string_literal() {
    assert_eq!(eval("eval('\"hello\"')"), JsValue::from("hello"));
}

#[test]
fn test_eval_number_literal() {
    assert_eq!(eval("eval('42')"), JsValue::Number(42.0));
}

#[test]
fn test_eval_boolean_literal() {
    assert_eq!(eval("eval('true')"), JsValue::Boolean(true));
    assert_eq!(eval("eval('false')"), JsValue::Boolean(false));
}

#[test]
fn test_eval_null() {
    assert_eq!(eval("eval('null')"), JsValue::Null);
}

#[test]
fn test_eval_undefined() {
    assert_eq!(eval("eval('undefined')"), JsValue::Undefined);
}

#[test]
fn test_eval_empty_string() {
    // eval('') returns undefined
    assert_eq!(eval("eval('')"), JsValue::Undefined);
}

// ============================================================================
// eval() with statements
// ============================================================================

#[test]
fn test_eval_variable_declaration() {
    // In strict mode, var declarations in eval are scoped to eval itself
    // So x is not visible after eval returns
    assert_eq!(
        eval("eval('var x = 42'); typeof x"),
        JsValue::from("undefined")
    );
}

#[test]
fn test_eval_variable_inside_eval() {
    // But var is visible inside eval
    assert_eq!(eval("eval('var x = 42; x')"), JsValue::Number(42.0));
}

#[test]
fn test_eval_let_declaration() {
    // let in eval is scoped to eval in strict mode
    assert_eq!(
        eval("eval('let y = 100'); typeof y"),
        JsValue::from("undefined")
    );
}

#[test]
fn test_eval_multiple_statements() {
    // eval returns the value of the last statement/expression
    assert_eq!(eval("eval('1; 2; 3')"), JsValue::Number(3.0));
}

#[test]
fn test_eval_function_declaration() {
    // In strict mode, functions declared in eval are scoped to eval itself
    assert_eq!(
        eval("eval('function add(a, b) { return a + b; }'); typeof add"),
        JsValue::from("undefined")
    );
}

#[test]
fn test_eval_function_inside_eval() {
    // But the function is usable inside eval
    assert_eq!(
        eval("eval('function add(a, b) { return a + b; } add(2, 3)')"),
        JsValue::Number(5.0)
    );
}

// ============================================================================
// eval() scope access
// ============================================================================

#[test]
fn test_eval_access_outer_variable() {
    // eval can read variables from enclosing scope
    assert_eq!(eval("let x = 10; eval('x')"), JsValue::Number(10.0));
}

#[test]
fn test_eval_modify_outer_variable() {
    // eval can modify variables from enclosing scope
    assert_eq!(eval("let x = 10; eval('x = 20'); x"), JsValue::Number(20.0));
}

#[test]
fn test_eval_access_function_scope() {
    // eval inside a function can access function scope
    assert_eq!(
        eval(
            r#"
            function test() {
                let y = 42;
                return eval('y');
            }
            test()
        "#
        ),
        JsValue::Number(42.0)
    );
}

#[test]
fn test_eval_access_global() {
    // eval can access global variables
    assert_eq!(
        eval("var globalVar = 100; eval('globalVar')"),
        JsValue::Number(100.0)
    );
}

// ============================================================================
// eval() properties
// ============================================================================

#[test]
fn test_eval_length() {
    // eval.length === 1
    assert_eq!(eval("eval.length"), JsValue::Number(1.0));
}

#[test]
fn test_eval_name() {
    // eval.name === "eval"
    assert_eq!(eval("eval.name"), JsValue::from("eval"));
}

#[test]
fn test_eval_typeof() {
    // typeof eval === "function"
    assert_eq!(eval("typeof eval"), JsValue::from("function"));
}

#[test]
fn test_eval_is_callable() {
    // eval can be called
    assert_eq!(eval("typeof eval('1 + 1')"), JsValue::from("number"));
}

// ============================================================================
// eval() with non-string arguments
// ============================================================================

#[test]
fn test_eval_non_string_number() {
    // If argument is not a string, return it directly
    assert_eq!(eval("eval(42)"), JsValue::Number(42.0));
}

#[test]
fn test_eval_non_string_boolean() {
    assert_eq!(eval("eval(true)"), JsValue::Boolean(true));
}

#[test]
fn test_eval_non_string_null() {
    assert_eq!(eval("eval(null)"), JsValue::Null);
}

#[test]
fn test_eval_non_string_undefined() {
    assert_eq!(eval("eval(undefined)"), JsValue::Undefined);
}

#[test]
fn test_eval_non_string_object() {
    // Objects passed directly are returned as-is
    assert_eq!(eval("let obj = {a: 1}; eval(obj).a"), JsValue::Number(1.0));
}

#[test]
fn test_eval_non_string_array() {
    assert_eq!(eval("eval([1, 2, 3])[1]"), JsValue::Number(2.0));
}

// ============================================================================
// eval() error handling
// ============================================================================

#[test]
fn test_eval_syntax_error() {
    // Invalid syntax should throw SyntaxError
    assert!(throws_error("eval('{')", "SyntaxError"));
}

#[test]
fn test_eval_reference_error() {
    // Reference to undefined variable should throw ReferenceError
    assert!(throws_error("eval('undefinedVariable')", "ReferenceError"));
}

#[test]
fn test_eval_type_error() {
    // Type errors should propagate
    assert!(throws_error("eval('null.foo')", "TypeError"));
}

// ============================================================================
// eval() is not a constructor
// ============================================================================

#[test]
fn test_eval_not_constructor() {
    // new eval() should throw TypeError
    assert!(throws_error("new eval()", "TypeError"));
}

#[test]
fn test_eval_not_constructor_with_arg() {
    // new eval('code') should throw TypeError
    assert!(throws_error("new eval('1 + 1')", "TypeError"));
}

// ============================================================================
// eval() with complex expressions
// ============================================================================

#[test]
fn test_eval_object_literal() {
    // eval can parse and return objects
    assert_eq!(eval("eval('({a: 1, b: 2})').a"), JsValue::Number(1.0));
}

#[test]
fn test_eval_array_literal() {
    assert_eq!(eval("eval('[1, 2, 3]').length"), JsValue::Number(3.0));
}

#[test]
fn test_eval_arrow_function() {
    assert_eq!(eval("eval('((x) => x * 2)(5)')"), JsValue::Number(10.0));
}

#[test]
fn test_eval_template_literal() {
    assert_eq!(
        eval("let x = 'world'; eval('`hello ${x}`')"),
        JsValue::from("hello world")
    );
}

#[test]
fn test_eval_regex() {
    assert_eq!(eval("eval('/abc/g').source"), JsValue::from("abc"));
}

// ============================================================================
// eval() and strict mode
// ============================================================================

#[test]
fn test_eval_strict_mode_this() {
    // In strict mode, `this` inside eval should be undefined in functions
    assert_eq!(
        eval(
            r#"
            function test() {
                return eval('this');
            }
            test()
        "#
        ),
        JsValue::Undefined
    );
}

#[test]
fn test_eval_strict_assignment_to_undeclared() {
    // Assigning to undeclared variable should throw ReferenceError in strict mode
    assert!(throws_error("eval('undeclaredVar = 1')", "ReferenceError"));
}

// ============================================================================
// Indirect eval (should use global scope)
// ============================================================================

#[test]
fn test_indirect_eval_uses_global_scope() {
    // Indirect eval (via assignment) should use global scope
    // Note: This is a subtle spec requirement - (1, eval)('code') is indirect eval
    assert_eq!(
        eval(
            r#"
            var globalX = 42;
            function test() {
                var localX = 100;
                // Indirect eval using comma operator
                return (1, eval)('globalX');
            }
            test()
        "#
        ),
        JsValue::Number(42.0)
    );
}

#[test]
fn test_indirect_eval_via_variable() {
    // Assigning eval to a variable makes it indirect
    assert_eq!(
        eval(
            r#"
            var globalY = 50;
            var indirectEval = eval;
            function test() {
                var localY = 200;
                return indirectEval('globalY');
            }
            test()
        "#
        ),
        JsValue::Number(50.0)
    );
}

// ============================================================================
// eval() with TypeScript syntax
// ============================================================================

#[test]
fn test_eval_with_type_annotation() {
    // TypeScript type annotations should be parsed and stripped
    assert_eq!(eval("eval('let x: number = 42; x')"), JsValue::Number(42.0));
}

#[test]
fn test_eval_with_type_assertion() {
    assert_eq!(
        eval("eval('let x = 42 as number; x')"),
        JsValue::Number(42.0)
    );
}

#[test]
fn test_eval_with_interface() {
    // Interface declarations are no-ops at runtime
    assert_eq!(
        eval("eval('interface Foo { x: number }; 42')"),
        JsValue::Number(42.0)
    );
}

// ============================================================================
// eval() no arguments
// ============================================================================

#[test]
fn test_eval_no_args() {
    // eval() with no arguments returns undefined
    assert_eq!(eval("eval()"), JsValue::Undefined);
}

// ============================================================================
// eval() and closures
// ============================================================================

#[test]
fn test_eval_closure() {
    // Function created via eval should capture the scope
    assert_eq!(
        eval(
            r#"
            function outer() {
                let x = 10;
                let f = eval('(function() { return x; })');
                return f();
            }
            outer()
        "#
        ),
        JsValue::Number(10.0)
    );
}

// ============================================================================
// eval() return value semantics
// ============================================================================

#[test]
fn test_eval_return_last_expression() {
    // Return value is the value of the last evaluated expression
    assert_eq!(eval("eval('1; 2; 3')"), JsValue::Number(3.0));
}

#[test]
fn test_eval_statement_returns_undefined() {
    // Statements like if/for don't produce values
    // But the block might contain expressions
    assert_eq!(eval("eval('if (true) { 42 }')"), JsValue::Number(42.0));
}

#[test]
fn test_eval_empty_block() {
    assert_eq!(eval("eval('{}')"), JsValue::Undefined);
}

// ============================================================================
// eval() edge cases
// ============================================================================

#[test]
fn test_eval_whitespace_only() {
    // Whitespace-only string should return undefined
    assert_eq!(eval("eval('   ')"), JsValue::Undefined);
}

#[test]
fn test_eval_comments_only() {
    // Comments-only string should return undefined
    assert_eq!(eval("eval('// comment')"), JsValue::Undefined);
}

#[test]
fn test_eval_multiline() {
    assert_eq!(
        eval("eval('let a = 1;\\nlet b = 2;\\na + b')"),
        JsValue::Number(3.0)
    );
}

// ============================================================================
// eval() comparison with Function constructor
// ============================================================================

#[test]
fn test_eval_vs_function_scope() {
    // eval has access to local scope, Function() doesn't
    assert_eq!(
        eval(
            r#"
            function test() {
                let localVar = 123;
                // eval can see localVar
                let evalResult = eval('localVar');
                // Function constructor cannot (uses global scope)
                let fnResult = (new Function('return typeof localVar'))();
                return evalResult + '-' + fnResult;
            }
            test()
        "#
        ),
        JsValue::from("123-undefined")
    );
}

// ============================================================================
// eval() called as method
// ============================================================================

#[test]
fn test_eval_as_method() {
    // eval can be called as a method (but `this` doesn't affect it)
    assert_eq!(
        eval(
            r#"
            let obj = { eval: eval };
            obj.eval('1 + 1')
        "#
        ),
        JsValue::Number(2.0)
    );
}

// ============================================================================
// Property descriptor tests (matching test262)
// ============================================================================

#[test]
fn test_eval_property_writable() {
    // eval should be writable on global object
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(globalThis, 'eval'); desc.writable"),
        JsValue::Boolean(true)
    );
}

#[test]
fn test_eval_property_enumerable() {
    // eval should NOT be enumerable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(globalThis, 'eval'); desc.enumerable"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_eval_property_configurable() {
    // eval should be configurable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(globalThis, 'eval'); desc.configurable"),
        JsValue::Boolean(true)
    );
}

#[test]
fn test_eval_name_property_writable() {
    // eval.name should NOT be writable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(eval, 'name'); desc.writable"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_eval_name_property_enumerable() {
    // eval.name should NOT be enumerable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(eval, 'name'); desc.enumerable"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_eval_name_property_configurable() {
    // eval.name should be configurable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(eval, 'name'); desc.configurable"),
        JsValue::Boolean(true)
    );
}

#[test]
fn test_eval_length_property_writable() {
    // eval.length should NOT be writable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(eval, 'length'); desc.writable"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_eval_length_property_enumerable() {
    // eval.length should NOT be enumerable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(eval, 'length'); desc.enumerable"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_eval_length_property_configurable() {
    // eval.length should be configurable
    assert_eq!(
        eval("let desc = Object.getOwnPropertyDescriptor(eval, 'length'); desc.configurable"),
        JsValue::Boolean(true)
    );
}

// ============================================================================
// Debug tests for eval scope investigation
// ============================================================================

#[test]
fn test_eval_scope_debug() {
    // Simplest case: eval reading a variable from enclosing function scope
    let result = eval(
        r#"
        function test() {
            let localVar = 123;
            console.log("localVar before eval:", localVar);
            let result = eval('localVar');
            console.log("eval result:", result);
            return result;
        }
        test()
    "#,
    );
    assert_eq!(result, JsValue::Number(123.0));
}

#[test]
fn test_eval_scope_with_function_constructor() {
    // Test similar to test_eval_vs_function_scope but with console.log to debug
    let result = eval(
        r#"
        function test() {
            let localVar = 123;
            console.log("Step 1: localVar =", localVar);
            // This is the line that fails in the original test
            let evalResult = eval('localVar');
            console.log("Step 2: evalResult =", evalResult);
            // Now try Function constructor
            let fnResult = (new Function('return typeof localVar'))();
            console.log("Step 3: fnResult =", fnResult);
            return evalResult + '-' + fnResult;
        }
        test()
    "#,
    );
    assert_eq!(result, JsValue::from("123-undefined"));
}

#[test]
fn test_typeof_undeclared_variable() {
    // typeof should return "undefined" for undeclared variables, not throw
    assert_eq!(
        eval("typeof nonExistentVariable"),
        JsValue::from("undefined")
    );
}

#[test]
fn test_eval_scope_minimal() {
    // Even simpler: just the function call
    assert_eq!(
        eval(
            r#"
            function f() {
                let x = 42;
                return eval('x');
            }
            f()
        "#
        ),
        JsValue::Number(42.0)
    );
}

// ============================================================================
// eval() completion value tests (test262 cptn-* tests)
// ============================================================================

#[test]
fn test_eval_if_empty_block_completion() {
    // eval('1; if (true) { }') should return undefined (from empty block)
    assert_eq!(eval("eval('1; if (true) { }')"), JsValue::Undefined);
}

#[test]
fn test_eval_if_expression_completion() {
    // eval('2; if (true) { 3; }') should return 3 (from expression in block)
    assert_eq!(eval("eval('2; if (true) { 3; }')"), JsValue::Number(3.0));
}

#[test]
fn test_eval_switch_completion() {
    // switch completion values
    assert_eq!(
        eval("eval('1; switch (\"a\") { case \"a\": 2; }')"),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_eval_for_completion() {
    // for loop completion value is from last iteration's body
    assert_eq!(
        eval("eval('1; for (let i = 0; i < 3; i++) { i; }')"),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_eval_while_completion() {
    // while loop completion value
    assert_eq!(
        eval("eval('let i = 0; while (i < 3) { i++; }')"),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_eval_if_else_completion() {
    // if-else completion values
    assert_eq!(
        eval("eval('if (false) { 1 } else { 2 }')"),
        JsValue::Number(2.0)
    );
    assert_eq!(
        eval("eval('if (true) { 1 } else { 2 }')"),
        JsValue::Number(1.0)
    );
}

#[test]
fn test_eval_try_completion() {
    // try block completion
    assert_eq!(
        eval("eval('try { 42 } catch(e) { }')"),
        JsValue::Number(42.0)
    );
}

// ============================================================================
// try/catch/finally completion value tests (matching test262 cptn-catch.js etc)
// ============================================================================

#[test]
fn test_eval_try_catch_empty_completion() {
    // Empty catch block should have undefined completion
    // Per test262 cptn-catch.js:
    // assert.sameValue(eval('1; try { throw null; } catch (err) { }'), undefined);
    assert_eq!(
        eval("eval('1; try { throw null; } catch (err) { }')"),
        JsValue::Undefined
    );
}

#[test]
fn test_eval_try_catch_expression_completion() {
    // Catch block with expression should have that value as completion
    // Per test262 cptn-catch.js:
    // assert.sameValue(eval('2; try { throw null; } catch (err) { 3; }'), 3);
    assert_eq!(
        eval("eval('2; try { throw null; } catch (err) { 3; }')"),
        JsValue::Number(3.0)
    );
}

#[test]
fn test_eval_try_no_throw_completion() {
    // If no throw, completion is from try block
    assert_eq!(
        eval("eval('try { 5 } catch(e) { 6 }')"),
        JsValue::Number(5.0)
    );
}

#[test]
fn test_eval_try_finally_no_throw() {
    // try-finally without throw: completion from try block
    // (finally doesn't change completion unless it has abrupt completion)
    assert_eq!(eval("eval('try { 5 } finally { }')"), JsValue::Number(5.0));
}

#[test]
fn test_eval_try_catch_finally_completion() {
    // try-catch-finally: catch completion should propagate
    assert_eq!(
        eval("eval('try { throw null; } catch(e) { 7 } finally { }')"),
        JsValue::Number(7.0)
    );
}

#[test]
fn test_eval_try_catch_break_completion() {
    // Per test262 cptn-catch-empty-break.js:
    // When break happens inside catch block, completion should be undefined
    // (not the completion from previous loop iteration)
    assert_eq!(
        eval(
            r#"eval("for (var i = 0; i < 2; ++i) { if (i) { try { throw null; } catch (e) { break; } } 'bad completion'; }")"#
        ),
        JsValue::Undefined
    );
}

#[test]
fn test_eval_try_catch_continue_completion() {
    // Similar to break, continue should also reset completion to undefined
    // Per test262 cptn-catch-empty-continue.js
    assert_eq!(
        eval(
            r#"eval("var last; for (var i = 0; i < 2; ++i) { if (i) { try { throw null; } catch (e) { last = i; continue; } } 'bad completion'; }")"#
        ),
        JsValue::Undefined
    );
}

#[test]
fn test_eval_block_completion() {
    // block completion value is from last statement
    assert_eq!(eval("eval('{ 1; 2; 3; }')"), JsValue::Number(3.0));
}