[jsinterp] Handle regexp literals and throw/catch execution (#31182)

* based on f6ca640b12, thanks pukkandan
* adds parse support for regexp flags
This commit is contained in:
dirkf
2022-08-19 11:45:04 +01:00
committed by GitHub
parent b0a60ce203
commit 538ec65ba7
3 changed files with 139 additions and 22 deletions

View File

@ -9,6 +9,7 @@ import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import math
import re
from youtube_dl.jsinterp import JSInterpreter
undefined = JSInterpreter.undefined
@ -316,19 +317,39 @@ class TestJSInterpreter(unittest.TestCase):
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.assertIs(jsi.call_function('x'), undefined)
jsi = JSInterpreter('''
function x() { let a = {m1: 42, m2: 0 }; return a?.qq; }
''')
self.assertIs(jsi.call_function('x'), undefined)
def test_regex(self):
jsi = JSInterpreter('''
function x() { let a=/,,[/,913,/](,)}/; }
''')
self.assertIs(jsi.call_function('x'), None)
jsi = JSInterpreter('''
function x() { let a=/,,[/,913,/](,)}/; return a; }
''')
# Pythons disagree on the type of a pattern
self.assertTrue(isinstance(jsi.call_function('x'), type(re.compile(''))))
jsi = JSInterpreter('''
function x() { let a=/,,[/,913,/](,)}/i; return a; }
''')
self.assertEqual(jsi.call_function('x').flags & re.I, re.I)
if __name__ == '__main__':
unittest.main()