Skip to content

Commit

Permalink
Back-port JS interpreter upgrade from yt-dlp PR ytdl-org#1437
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkf committed Dec 17, 2021
1 parent 830fb46 commit d577b92
Show file tree
Hide file tree
Showing 3 changed files with 453 additions and 107 deletions.
51 changes: 51 additions & 0 deletions test/test_jsinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,57 @@ def test_call(self):
''')
self.assertEqual(jsi.call_function('z'), 5)

def test_for_loop(self):
# function x() { a=0; for (i=0; i-10; i++) {a++} a }
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i = i + 1) {a++} 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_try(self):
jsi = JSInterpreter('''
function x() { try{return 10} catch(e){return 5} }
''')
self.assertEqual(jsi.call_function('x'), 10)

def test_for_loop_continue(self):
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i++) { continue; a++ } 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++ } a }
''')
self.assertEqual(jsi.call_function('x'), 0)

def test_literal_list(self):
jsi = JSInterpreter('''
function x() { [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)


if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import sys
import xml.etree.ElementTree

try:
import collections.abc as compat_collections_abc
except ImportError:
import collections as compat_collections_abc

try:
import urllib.request as compat_urllib_request
Expand Down Expand Up @@ -3025,6 +3029,7 @@ def compat_ctypes_WINFUNCTYPE(*args, **kwargs):
'compat_b64decode',
'compat_basestring',
'compat_chr',
'compat_collections_abc',
'compat_cookiejar',
'compat_cookiejar_Cookie',
'compat_cookies',
Expand Down
Loading

0 comments on commit d577b92

Please sign in to comment.