+ def test_builtins(self):
+ jsi = JSInterpreter('''
+ function x() { return NaN }
+ ''')
+ self.assertTrue(math.isnan(jsi.call_function('x')))
+
+ jsi = JSInterpreter('''
+ function x() { return new Date('Wednesday 31 December 1969 18:01:26 MDT') - 0; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 86000)
+ jsi = JSInterpreter('''
+ function x(dt) { return new Date(dt) - 0; }
+ ''')
+ self.assertEqual(jsi.call_function('x', 'Wednesday 31 December 1969 18:01:26 MDT'), 86000)
+
+ def test_call(self):
+ jsi = JSInterpreter('''
+ function x() { return 2; }
+ function y(a) { return x() + (a?a:0); }
+ function z() { return y(3); }
+ ''')
+ self.assertEqual(jsi.call_function('z'), 5)
+ self.assertEqual(jsi.call_function('y'), 2)
+
+ def test_if(self):
+ jsi = JSInterpreter('''
+ function x() {
+ let a = 9;
+ if (0==0) {a++}
+ return a
+ }''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ jsi = JSInterpreter('''
+ function x() {
+ if (0==0) {return 10}
+ }''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ jsi = JSInterpreter('''
+ function x() {
+ if (0!=0) {return 1}
+ else {return 10}
+ }''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ """ # Unsupported
+ jsi = JSInterpreter('''
+ function x() {
+ if (0!=0) {return 1}
+ else if (1==0) {return 2}
+ else {return 10}
+ }''')
+ self.assertEqual(jsi.call_function('x'), 10)
+ """
+
+ def test_for_loop(self):
+ jsi = JSInterpreter('''
+ function x() { a=0; for (i=0; i-10; i++) {a++} return a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ def test_switch(self):
+ jsi = JSInterpreter('''
+ function x(f) { switch(f){
+ case 1:f+=1;
+ case 2:f+=2;
+ case 3:f+=3;break;
+ case 4:f+=4;
+ default:f=0;
+ } return f }
+ ''')
+ self.assertEqual(jsi.call_function('x', 1), 7)
+ self.assertEqual(jsi.call_function('x', 3), 6)
+ self.assertEqual(jsi.call_function('x', 5), 0)
+
+ def test_switch_default(self):
+ jsi = JSInterpreter('''
+ function x(f) { switch(f){
+ case 2: f+=2;
+ default: f-=1;
+ case 5:
+ case 6: f+=6;
+ case 0: break;
+ case 1: f+=1;
+ } return f }
+ ''')
+ self.assertEqual(jsi.call_function('x', 1), 2)
+ self.assertEqual(jsi.call_function('x', 5), 11)
+ self.assertEqual(jsi.call_function('x', 9), 14)
+
+ def test_try(self):
+ jsi = JSInterpreter('''
+ function x() { try{return 10} catch(e){return 5} }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
+ def test_catch(self):
+ jsi = JSInterpreter('''
+ function x() { try{throw 10} catch(e){return 5} }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 5)
+
+ def test_finally(self):
+ jsi = JSInterpreter('''
+ function x() { try{throw 10} finally {return 42} }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 42)
+ jsi = JSInterpreter('''
+ function x() { try{throw 10} catch(e){return 5} finally {return 42} }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 42)
+
+ def test_nested_try(self):
+ jsi = JSInterpreter('''
+ function x() {try {
+ try{throw 10} finally {throw 42}
+ } catch(e){return 5} }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 5)
+
+ def test_for_loop_continue(self):
+ jsi = JSInterpreter('''
+ function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 0)
+
+ def test_for_loop_break(self):
+ jsi = JSInterpreter('''
+ function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 0)
+
+ def test_for_loop_try(self):
+ jsi = JSInterpreter('''
+ function x() {
+ for (i=0; i-10; i++) { try { if (i == 5) throw i} catch {return 10} finally {break} };
+ return 42 }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 42)
+
+ def test_literal_list(self):
+ jsi = JSInterpreter('''
+ function x() { return [1, 2, "asdf", [5, 6, 7]][3] }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [5, 6, 7])
+
+ def test_comma(self):
+ jsi = JSInterpreter('''
+ function x() { a=5; a -= 1, a+=3; return a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 7)
+
+ jsi = JSInterpreter('''
+ function x() { a=5; return (a -= 1, a+=3, a); }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 7)
+
+ jsi = JSInterpreter('''
+ function x() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 5)
+
+ def test_void(self):
+ jsi = JSInterpreter('''
+ function x() { return void 42; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), None)
+
+ def test_return_function(self):
+ jsi = JSInterpreter('''
+ function x() { return [1, function(){return 1}][1] }
+ ''')
+ self.assertEqual(jsi.call_function('x')([]), 1)
+
+ def test_null(self):
+ jsi = JSInterpreter('''
+ function x() { return null; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), None)
+
+ jsi = JSInterpreter('''
+ function x() { return [null > 0, null < 0, null == 0, null === 0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [False, False, False, False])
+
+ jsi = JSInterpreter('''
+ function x() { return [null >= 0, null <= 0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [True, True])
+
+ def test_undefined(self):
+ jsi = JSInterpreter('''
+ function x() { return undefined === undefined; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), True)
+
+ jsi = JSInterpreter('''
+ function x() { return undefined; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), JS_Undefined)
+
+ jsi = JSInterpreter('''
+ function x() { let v; return v; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), JS_Undefined)
+
+ jsi = JSInterpreter('''
+ function x() { return [undefined === undefined, undefined == undefined, undefined < undefined, undefined > undefined]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [True, True, False, False])
+
+ jsi = JSInterpreter('''
+ function x() { return [undefined === 0, undefined == 0, undefined < 0, undefined > 0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [False, False, False, False])
+
+ jsi = JSInterpreter('''
+ function x() { return [undefined >= 0, undefined <= 0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [False, False])
+
+ jsi = JSInterpreter('''
+ function x() { return [undefined > null, undefined < null, undefined == null, undefined === null]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [False, False, True, False])
+
+ jsi = JSInterpreter('''
+ function x() { return [undefined === null, undefined == null, undefined < null, undefined > null]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [False, True, False, False])
+
+ jsi = JSInterpreter('''
+ function x() { let v; return [42+v, v+42, v**42, 42**v, 0**v]; }
+ ''')
+ for y in jsi.call_function('x'):
+ self.assertTrue(math.isnan(y))
+
+ jsi = JSInterpreter('''
+ function x() { let v; return v**0; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 1)
+
+ jsi = JSInterpreter('''
+ function x() { let v; return [v>42, v<=42, v&&42, 42&&v]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [False, False, JS_Undefined, JS_Undefined])
+
+ jsi = JSInterpreter('function x(){return undefined ?? 42; }')
+ self.assertEqual(jsi.call_function('x'), 42)
+
+ def test_object(self):
+ jsi = JSInterpreter('''
+ function x() { return {}; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), {})
+
+ jsi = JSInterpreter('''
+ function x() { let a = {m1: 42, m2: 0 }; return [a["m1"], a.m2]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), [42, 0])
+
+ jsi = JSInterpreter('''
+ function x() { let a; return a?.qq; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), JS_Undefined)
+
+ jsi = JSInterpreter('''
+ function x() { let a = {m1: 42, m2: 0 }; return a?.qq; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), JS_Undefined)
+
+ def test_regex(self):
+ jsi = JSInterpreter('''
+ function x() { let a=/,,[/,913,/](,)}/; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), None)
+
+ jsi = JSInterpreter('''
+ function x() { let a=/,,[/,913,/](,)}/; return a; }
+ ''')
+ self.assertIsInstance(jsi.call_function('x'), re.Pattern)
+
+ jsi = JSInterpreter('''
+ function x() { let a=/,,[/,913,/](,)}/i; return a; }
+ ''')
+ self.assertEqual(jsi.call_function('x').flags & re.I, re.I)
+
+ jsi = JSInterpreter(R'''
+ function x() { let a=/,][}",],()}(\[)/; return a; }
+ ''')
+ self.assertEqual(jsi.call_function('x').pattern, r',][}",],()}(\[)')
+
+ jsi = JSInterpreter(R'''
+ function x() { let a=[/[)\\]/]; return a[0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x').pattern, r'[)\\]')
+
+ def test_char_code_at(self):
+ jsi = JSInterpreter('function x(i){return "test".charCodeAt(i)}')
+ self.assertEqual(jsi.call_function('x', 0), 116)
+ self.assertEqual(jsi.call_function('x', 1), 101)
+ self.assertEqual(jsi.call_function('x', 2), 115)
+ self.assertEqual(jsi.call_function('x', 3), 116)
+ self.assertEqual(jsi.call_function('x', 4), None)
+ self.assertEqual(jsi.call_function('x', 'not_a_number'), 116)
+
+ def test_bitwise_operators_overflow(self):
+ jsi = JSInterpreter('function x(){return -524999584 << 5}')
+ self.assertEqual(jsi.call_function('x'), 379882496)
+
+ jsi = JSInterpreter('function x(){return 1236566549 << 5}')
+ self.assertEqual(jsi.call_function('x'), 915423904)
+