test_jsinterp.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. import math
  10. import re
  11. from youtube_dl.compat import compat_str as str
  12. from youtube_dl.jsinterp import JS_Undefined, JSInterpreter
  13. NaN = object()
  14. class TestJSInterpreter(unittest.TestCase):
  15. def _test(self, jsi_or_code, expected, func='f', args=()):
  16. if isinstance(jsi_or_code, str):
  17. jsi_or_code = JSInterpreter(jsi_or_code)
  18. got = jsi_or_code.call_function(func, *args)
  19. if expected is NaN:
  20. self.assertTrue(math.isnan(got), '{0} is not NaN'.format(got))
  21. else:
  22. self.assertEqual(got, expected)
  23. def test_basic(self):
  24. jsi = JSInterpreter('function f(){;}')
  25. self.assertEqual(repr(jsi.extract_function('f')), 'F<f>')
  26. self._test(jsi, None)
  27. self._test('function f(){return 42;}', 42)
  28. self._test('function f(){42}', None)
  29. self._test('var f = function(){return 42;}', 42)
  30. def test_add(self):
  31. self._test('function f(){return 42 + 7;}', 49)
  32. self._test('function f(){return 42 + undefined;}', NaN)
  33. self._test('function f(){return 42 + null;}', 42)
  34. self._test('function f(){return 1 + "";}', '1')
  35. self._test('function f(){return 42 + "7";}', '427')
  36. self._test('function f(){return false + true;}', 1)
  37. self._test('function f(){return "false" + true;}', 'falsetrue')
  38. self._test('function f(){return '
  39. '1 + "2" + [3,4] + {k: 56} + null + undefined + Infinity;}',
  40. '123,4[object Object]nullundefinedInfinity')
  41. def test_sub(self):
  42. self._test('function f(){return 42 - 7;}', 35)
  43. self._test('function f(){return 42 - undefined;}', NaN)
  44. self._test('function f(){return 42 - null;}', 42)
  45. self._test('function f(){return 42 - "7";}', 35)
  46. self._test('function f(){return 42 - "spam";}', NaN)
  47. def test_mul(self):
  48. self._test('function f(){return 42 * 7;}', 294)
  49. self._test('function f(){return 42 * undefined;}', NaN)
  50. self._test('function f(){return 42 * null;}', 0)
  51. self._test('function f(){return 42 * "7";}', 294)
  52. self._test('function f(){return 42 * "eggs";}', NaN)
  53. def test_div(self):
  54. jsi = JSInterpreter('function f(a, b){return a / b;}')
  55. self._test(jsi, NaN, args=(0, 0))
  56. self._test(jsi, NaN, args=(JS_Undefined, 1))
  57. self._test(jsi, float('inf'), args=(2, 0))
  58. self._test(jsi, 0, args=(0, 3))
  59. self._test(jsi, 6, args=(42, 7))
  60. self._test(jsi, 0, args=(42, float('inf')))
  61. self._test(jsi, 6, args=("42", 7))
  62. self._test(jsi, NaN, args=("spam", 7))
  63. def test_mod(self):
  64. self._test('function f(){return 42 % 7;}', 0)
  65. self._test('function f(){return 42 % 0;}', NaN)
  66. self._test('function f(){return 42 % undefined;}', NaN)
  67. self._test('function f(){return 42 % "7";}', 0)
  68. self._test('function f(){return 42 % "beans";}', NaN)
  69. def test_exp(self):
  70. self._test('function f(){return 42 ** 2;}', 1764)
  71. self._test('function f(){return 42 ** undefined;}', NaN)
  72. self._test('function f(){return 42 ** null;}', 1)
  73. self._test('function f(){return undefined ** 0;}', 1)
  74. self._test('function f(){return undefined ** 42;}', NaN)
  75. self._test('function f(){return 42 ** "2";}', 1764)
  76. self._test('function f(){return 42 ** "spam";}', NaN)
  77. def test_calc(self):
  78. self._test('function f(a){return 2*a+1;}', 7, args=[3])
  79. def test_empty_return(self):
  80. self._test('function f(){return; y()}', None)
  81. def test_morespace(self):
  82. self._test('function f (a) { return 2 * a + 1 ; }', 7, args=[3])
  83. self._test('function f () { x = 2 ; return x; }', 2)
  84. def test_strange_chars(self):
  85. self._test('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }',
  86. 21, args=[20], func='$_xY1')
  87. def test_operators(self):
  88. self._test('function f(){return 1 << 5;}', 32)
  89. self._test('function f(){return 2 ** 5}', 32)
  90. self._test('function f(){return 19 & 21;}', 17)
  91. self._test('function f(){return 11 >> 2;}', 2)
  92. self._test('function f(){return []? 2+3: 4;}', 5)
  93. # equality
  94. self._test('function f(){return 1 == 1}', True)
  95. self._test('function f(){return 1 == 1.0}', True)
  96. self._test('function f(){return 1 == "1"}', True)
  97. self._test('function f(){return 1 == 2}', False)
  98. self._test('function f(){return 1 != "1"}', False)
  99. self._test('function f(){return 1 != 2}', True)
  100. self._test('function f(){var x = {a: 1}; var y = x; return x == y}', True)
  101. self._test('function f(){var x = {a: 1}; return x == {a: 1}}', False)
  102. self._test('function f(){return NaN == NaN}', False)
  103. self._test('function f(){return null == undefined}', True)
  104. self._test('function f(){return "spam, eggs" == "spam, eggs"}', True)
  105. # strict equality
  106. self._test('function f(){return 1 === 1}', True)
  107. self._test('function f(){return 1 === 1.0}', True)
  108. self._test('function f(){return 1 === "1"}', False)
  109. self._test('function f(){return 1 === 2}', False)
  110. self._test('function f(){var x = {a: 1}; var y = x; return x === y}', True)
  111. self._test('function f(){var x = {a: 1}; return x === {a: 1}}', False)
  112. self._test('function f(){return NaN === NaN}', False)
  113. self._test('function f(){return null === undefined}', False)
  114. self._test('function f(){return null === null}', True)
  115. self._test('function f(){return undefined === undefined}', True)
  116. self._test('function f(){return "uninterned" === "uninterned"}', True)
  117. self._test('function f(){return 1 === 1}', True)
  118. self._test('function f(){return 1 === "1"}', False)
  119. self._test('function f(){return 1 !== 1}', False)
  120. self._test('function f(){return 1 !== "1"}', True)
  121. # expressions
  122. self._test('function f(){return 0 && 1 || 2;}', 2)
  123. self._test('function f(){return 0 ?? 42;}', 0)
  124. self._test('function f(){return "life, the universe and everything" < 42;}', False)
  125. # https://github.com/ytdl-org/youtube-dl/issues/32815
  126. self._test('function f(){return 0 - 7 * - 6;}', 42)
  127. def test_array_access(self):
  128. self._test('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}', [5, 2, 7])
  129. def test_parens(self):
  130. self._test('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}', 7)
  131. self._test('function f(){return (1 + 2) * 3;}', 9)
  132. def test_quotes(self):
  133. self._test(r'function f(){return "a\"\\("}', r'a"\(')
  134. def test_assignments(self):
  135. self._test('function f(){var x = 20; x = 30 + 1; return x;}', 31)
  136. self._test('function f(){var x = 20; x += 30 + 1; return x;}', 51)
  137. self._test('function f(){var x = 20; x -= 30 + 1; return x;}', -11)
  138. @unittest.skip('Not yet fully implemented')
  139. def test_comments(self):
  140. self._test('''
  141. function f() {
  142. var x = /* 1 + */ 2;
  143. var y = /* 30
  144. * 40 */ 50;
  145. return x + y;
  146. }
  147. ''', 52)
  148. self._test('''
  149. function f() {
  150. var x = "/*";
  151. var y = 1 /* comment */ + 2;
  152. return y;
  153. }
  154. ''', 3)
  155. def test_precedence(self):
  156. self._test('''
  157. function f() {
  158. var a = [10, 20, 30, 40, 50];
  159. var b = 6;
  160. a[0]=a[b%a.length];
  161. return a;
  162. }
  163. ''', [20, 20, 30, 40, 50])
  164. def test_builtins(self):
  165. self._test('function f() { return NaN }', NaN)
  166. def test_Date(self):
  167. self._test('function f() { return new Date("Wednesday 31 December 1969 18:01:26 MDT") - 0; }', 86000)
  168. jsi = JSInterpreter('function f(dt) { return new Date(dt) - 0; }')
  169. # date format m/d/y
  170. self._test(jsi, 86000, args=['12/31/1969 18:01:26 MDT'])
  171. # epoch 0
  172. self._test(jsi, 0, args=['1 January 1970 00:00:00 UTC'])
  173. def test_call(self):
  174. jsi = JSInterpreter('''
  175. function x() { return 2; }
  176. function y(a) { return x() + (a?a:0); }
  177. function z() { return y(3); }
  178. ''')
  179. self._test(jsi, 5, func='z')
  180. self._test(jsi, 2, func='y')
  181. def test_if(self):
  182. self._test('''
  183. function f() {
  184. let a = 9;
  185. if (0==0) {a++}
  186. return a
  187. }
  188. ''', 10)
  189. self._test('''
  190. function f() {
  191. if (0==0) {return 10}
  192. }
  193. ''', 10)
  194. self._test('''
  195. function f() {
  196. if (0!=0) {return 1}
  197. else {return 10}
  198. }
  199. ''', 10)
  200. def test_elseif(self):
  201. self._test('''
  202. function f() {
  203. if (0!=0) {return 1}
  204. else if (1==0) {return 2}
  205. else {return 10}
  206. }
  207. ''', 10)
  208. def test_for_loop(self):
  209. self._test('function f() { a=0; for (i=0; i-10; i++) {a++} return a }', 10)
  210. def test_while_loop(self):
  211. self._test('function f() { a=0; while (a<10) {a++} return a }', 10)
  212. def test_switch(self):
  213. jsi = JSInterpreter('''
  214. function f(x) { switch(x){
  215. case 1:x+=1;
  216. case 2:x+=2;
  217. case 3:x+=3;break;
  218. case 4:x+=4;
  219. default:x=0;
  220. } return x }
  221. ''')
  222. self._test(jsi, 7, args=[1])
  223. self._test(jsi, 6, args=[3])
  224. self._test(jsi, 0, args=[5])
  225. def test_switch_default(self):
  226. jsi = JSInterpreter('''
  227. function f(x) { switch(x){
  228. case 2: x+=2;
  229. default: x-=1;
  230. case 5:
  231. case 6: x+=6;
  232. case 0: break;
  233. case 1: x+=1;
  234. } return x }
  235. ''')
  236. self._test(jsi, 2, args=[1])
  237. self._test(jsi, 11, args=[5])
  238. self._test(jsi, 14, args=[9])
  239. def test_try(self):
  240. self._test('function f() { try{return 10} catch(e){return 5} }', 10)
  241. def test_catch(self):
  242. self._test('function f() { try{throw 10} catch(e){return 5} }', 5)
  243. def test_finally(self):
  244. self._test('function f() { try{throw 10} finally {return 42} }', 42)
  245. self._test('function f() { try{throw 10} catch(e){return 5} finally {return 42} }', 42)
  246. def test_nested_try(self):
  247. self._test('''
  248. function f() {try {
  249. try{throw 10} finally {throw 42}
  250. } catch(e){return 5} }
  251. ''', 5)
  252. def test_for_loop_continue(self):
  253. self._test('function f() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }', 0)
  254. def test_for_loop_break(self):
  255. self._test('function f() { a=0; for (i=0; i-10; i++) { break; a++ } return a }', 0)
  256. def test_for_loop_try(self):
  257. self._test('''
  258. function f() {
  259. for (i=0; i-10; i++) { try { if (i == 5) throw i} catch {return 10} finally {break} };
  260. return 42 }
  261. ''', 42)
  262. def test_literal_list(self):
  263. self._test('function f() { return [1, 2, "asdf", [5, 6, 7]][3] }', [5, 6, 7])
  264. def test_comma(self):
  265. self._test('function f() { a=5; a -= 1, a+=3; return a }', 7)
  266. self._test('function f() { a=5; return (a -= 1, a+=3, a); }', 7)
  267. self._test('function f() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) }', 5)
  268. def test_void(self):
  269. self._test('function f() { return void 42; }', JS_Undefined)
  270. def test_typeof(self):
  271. self._test('function f() { return typeof undefined; }', 'undefined')
  272. self._test('function f() { return typeof NaN; }', 'number')
  273. self._test('function f() { return typeof Infinity; }', 'number')
  274. self._test('function f() { return typeof true; }', 'boolean')
  275. self._test('function f() { return typeof null; }', 'object')
  276. self._test('function f() { return typeof "a string"; }', 'string')
  277. self._test('function f() { return typeof 42; }', 'number')
  278. self._test('function f() { return typeof 42.42; }', 'number')
  279. self._test('function f() { var g = function(){}; return typeof g; }', 'function')
  280. self._test('function f() { return typeof {key: "value"}; }', 'object')
  281. # not yet implemented: Symbol, BigInt
  282. def test_return_function(self):
  283. jsi = JSInterpreter('''
  284. function x() { return [1, function(){return 1}][1] }
  285. ''')
  286. self.assertEqual(jsi.call_function('x')([]), 1)
  287. def test_null(self):
  288. self._test('function f() { return null; }', None)
  289. self._test('function f() { return [null > 0, null < 0, null == 0, null === 0]; }',
  290. [False, False, False, False])
  291. self._test('function f() { return [null >= 0, null <= 0]; }', [True, True])
  292. def test_undefined(self):
  293. self._test('function f() { return undefined === undefined; }', True)
  294. self._test('function f() { return undefined; }', JS_Undefined)
  295. self._test('function f() { return undefined ?? 42; }', 42)
  296. self._test('function f() { let v; return v; }', JS_Undefined)
  297. self._test('function f() { let v; return v**0; }', 1)
  298. self._test('function f() { let v; return [v>42, v<=42, v&&42, 42&&v]; }',
  299. [False, False, JS_Undefined, JS_Undefined])
  300. self._test('''
  301. function f() { return [
  302. undefined === undefined,
  303. undefined == undefined,
  304. undefined == null
  305. ]; }
  306. ''', [True] * 3)
  307. self._test('''
  308. function f() { return [
  309. undefined < undefined,
  310. undefined > undefined,
  311. undefined === 0,
  312. undefined == 0,
  313. undefined < 0,
  314. undefined > 0,
  315. undefined >= 0,
  316. undefined <= 0,
  317. undefined > null,
  318. undefined < null,
  319. undefined === null
  320. ]; }
  321. ''', [False] * 11)
  322. jsi = JSInterpreter('''
  323. function x() { let v; return [42+v, v+42, v**42, 42**v, 0**v]; }
  324. ''')
  325. for y in jsi.call_function('x'):
  326. self.assertTrue(math.isnan(y))
  327. def test_object(self):
  328. self._test('function f() { return {}; }', {})
  329. self._test('function f() { let a = {m1: 42, m2: 0 }; return [a["m1"], a.m2]; }', [42, 0])
  330. self._test('function f() { let a; return a?.qq; }', JS_Undefined)
  331. self._test('function f() { let a = {m1: 42, m2: 0 }; return a?.qq; }', JS_Undefined)
  332. def test_indexing(self):
  333. self._test('function f() { return [1, 2, 3, 4][3]}', 4)
  334. self._test('function f() { return [1, [2, [3, [4]]]][1][1][1][0]}', 4)
  335. self._test('function f() { var o = {1: 2, 3: 4}; return o[3]}', 4)
  336. self._test('function f() { var o = {1: 2, 3: 4}; return o["3"]}', 4)
  337. self._test('function f() { return [1, [2, {3: [4]}]][1][1]["3"][0]}', 4)
  338. self._test('function f() { return [1, 2, 3, 4].length}', 4)
  339. self._test('function f() { var o = {1: 2, 3: 4}; return o.length}', JS_Undefined)
  340. self._test('function f() { var o = {1: 2, 3: 4}; o["length"] = 42; return o.length}', 42)
  341. def test_regex(self):
  342. self._test('function f() { let a=/,,[/,913,/](,)}/; }', None)
  343. jsi = JSInterpreter('''
  344. function x() { let a=/,,[/,913,/](,)}/; "".replace(a, ""); return a; }
  345. ''')
  346. attrs = set(('findall', 'finditer', 'match', 'scanner', 'search',
  347. 'split', 'sub', 'subn'))
  348. if sys.version_info >= (2, 7):
  349. # documented for 2.6 but may not be found
  350. attrs.update(('flags', 'groupindex', 'groups', 'pattern'))
  351. self.assertSetEqual(set(dir(jsi.call_function('x'))) & attrs, attrs)
  352. jsi = JSInterpreter('''
  353. function x() { let a=/,,[/,913,/](,)}/i; return a; }
  354. ''')
  355. self.assertEqual(jsi.call_function('x').flags & ~re.U, re.I)
  356. jsi = JSInterpreter(r'function f() { let a=/,][}",],()}(\[)/; return a; }')
  357. self.assertEqual(jsi.call_function('f').pattern, r',][}",],()}(\[)')
  358. jsi = JSInterpreter(r'function f() { let a=[/[)\\]/]; return a[0]; }')
  359. self.assertEqual(jsi.call_function('f').pattern, r'[)\\]')
  360. def test_replace(self):
  361. self._test('function f() { let a="data-name".replace("data-", ""); return a }',
  362. 'name')
  363. self._test('function f() { let a="data-name".replace(new RegExp("^.+-"), ""); return a; }',
  364. 'name')
  365. self._test('function f() { let a="data-name".replace(/^.+-/, ""); return a; }',
  366. 'name')
  367. self._test('function f() { let a="data-name".replace(/a/g, "o"); return a; }',
  368. 'doto-nome')
  369. self._test('function f() { let a="data-name".replaceAll("a", "o"); return a; }',
  370. 'doto-nome')
  371. def test_char_code_at(self):
  372. jsi = JSInterpreter('function f(i){return "test".charCodeAt(i)}')
  373. self._test(jsi, 116, args=[0])
  374. self._test(jsi, 101, args=[1])
  375. self._test(jsi, 115, args=[2])
  376. self._test(jsi, 116, args=[3])
  377. self._test(jsi, None, args=[4])
  378. self._test(jsi, 116, args=['not_a_number'])
  379. def test_bitwise_operators_overflow(self):
  380. self._test('function f(){return -524999584 << 5}', 379882496)
  381. self._test('function f(){return 1236566549 << 5}', 915423904)
  382. def test_bitwise_operators_typecast(self):
  383. # madness
  384. self._test('function f(){return null << 5}', 0)
  385. self._test('function f(){return undefined >> 5}', 0)
  386. self._test('function f(){return 42 << NaN}', 42)
  387. self._test('function f(){return 42 << Infinity}', 42)
  388. def test_negative(self):
  389. self._test('function f(){return 2 * -2.0 ;}', -4)
  390. self._test('function f(){return 2 - - -2 ;}', 0)
  391. self._test('function f(){return 2 - - - -2 ;}', 4)
  392. self._test('function f(){return 2 - + + - -2;}', 0)
  393. self._test('function f(){return 2 + - + - -2;}', 0)
  394. def test_32066(self):
  395. self._test(
  396. "function f(){return Math.pow(3, 5) + new Date('1970-01-01T08:01:42.000+08:00') / 1000 * -239 - -24205;}",
  397. 70)
  398. @unittest.skip('Not yet working')
  399. def test_packed(self):
  400. self._test(
  401. '''function f(p,a,c,k,e,d){while(c--)if(k[c])p=p.replace(new RegExp('\\b'+c.toString(a)+'\\b','g'),k[c]);return p}''',
  402. '''h 7=g("1j");7.7h({7g:[{33:"w://7f-7e-7d-7c.v.7b/7a/79/78/77/76.74?t=73&s=2s&e=72&f=2t&71=70.0.0.1&6z=6y&6x=6w"}],6v:"w://32.v.u/6u.31",16:"r%",15:"r%",6t:"6s",6r:"",6q:"l",6p:"l",6o:"6n",6m:\'6l\',6k:"6j",9:[{33:"/2u?b=6i&n=50&6h=w://32.v.u/6g.31",6f:"6e"}],1y:{6d:1,6c:\'#6b\',6a:\'#69\',68:"67",66:30,65:r,},"64":{63:"%62 2m%m%61%5z%5y%5x.u%5w%5v%5u.2y%22 2k%m%1o%22 5t%m%1o%22 5s%m%1o%22 2j%m%5r%22 16%m%5q%22 15%m%5p%22 5o%2z%5n%5m%2z",5l:"w://v.u/d/1k/5k.2y",5j:[]},\'5i\':{"5h":"5g"},5f:"5e",5d:"w://v.u",5c:{},5b:l,1x:[0.25,0.50,0.75,1,1.25,1.5,2]});h 1m,1n,5a;h 59=0,58=0;h 7=g("1j");h 2x=0,57=0,56=0;$.55({54:{\'53-52\':\'2i-51\'}});7.j(\'4z\',6(x){c(5>0&&x.1l>=5&&1n!=1){1n=1;$(\'q.4y\').4x(\'4w\')}});7.j(\'13\',6(x){2x=x.1l});7.j(\'2g\',6(x){2w(x)});7.j(\'4v\',6(){$(\'q.2v\').4u()});6 2w(x){$(\'q.2v\').4t();c(1m)19;1m=1;17=0;c(4s.4r===l){17=1}$.4q(\'/2u?b=4p&2l=1k&4o=2t-4n-4m-2s-4l&4k=&4j=&4i=&17=\'+17,6(2r){$(\'#4h\').4g(2r)});$(\'.3-8-4f-4e:4d("4c")\').2h(6(e){2q();g().4b(0);g().4a(l)});6 2q(){h $14=$("<q />").2p({1l:"49",16:"r%",15:"r%",48:0,2n:0,2o:47,46:"45(10%, 10%, 10%, 0.4)","44-43":"42"});$("<41 />").2p({16:"60%",15:"60%",2o:40,"3z-2n":"3y"}).3x({\'2m\':\'/?b=3w&2l=1k\',\'2k\':\'0\',\'2j\':\'2i\'}).2f($14);$14.2h(6(){$(3v).3u();g().2g()});$14.2f($(\'#1j\'))}g().13(0);}6 3t(){h 9=7.1b(2e);2d.2c(9);c(9.n>1){1r(i=0;i<9.n;i++){c(9[i].1a==2e){2d.2c(\'!!=\'+i);7.1p(i)}}}}7.j(\'3s\',6(){g().1h("/2a/3r.29","3q 10 28",6(){g().13(g().27()+10)},"2b");$("q[26=2b]").23().21(\'.3-20-1z\');g().1h("/2a/3p.29","3o 10 28",6(){h 12=g().27()-10;c(12<0)12=0;g().13(12)},"24");$("q[26=24]").23().21(\'.3-20-1z\');});6 1i(){}7.j(\'3n\',6(){1i()});7.j(\'3m\',6(){1i()});7.j("k",6(y){h 9=7.1b();c(9.n<2)19;$(\'.3-8-3l-3k\').3j(6(){$(\'#3-8-a-k\').1e(\'3-8-a-z\');$(\'.3-a-k\').p(\'o-1f\',\'11\')});7.1h("/3i/3h.3g","3f 3e",6(){$(\'.3-1w\').3d(\'3-8-1v\');$(\'.3-8-1y, .3-8-1x\').p(\'o-1g\',\'11\');c($(\'.3-1w\').3c(\'3-8-1v\')){$(\'.3-a-k\').p(\'o-1g\',\'l\');$(\'.3-a-k\').p(\'o-1f\',\'l\');$(\'.3-8-a\').1e(\'3-8-a-z\');$(\'.3-8-a:1u\').3b(\'3-8-a-z\')}3a{$(\'.3-a-k\').p(\'o-1g\',\'11\');$(\'.3-a-k\').p(\'o-1f\',\'11\');$(\'.3-8-a:1u\').1e(\'3-8-a-z\')}},"39");7.j("38",6(y){1d.37(\'1c\',y.9[y.36].1a)});c(1d.1t(\'1c\')){35("1s(1d.1t(\'1c\'));",34)}});h 18;6 1s(1q){h 9=7.1b();c(9.n>1){1r(i=0;i<9.n;i++){c(9[i].1a==1q){c(i==18){19}18=i;7.1p(i)}}}}',36,270,'|||jw|||function|player|settings|tracks|submenu||if||||jwplayer|var||on|audioTracks|true|3D|length|aria|attr|div|100|||sx|filemoon|https||event|active||false|tt|seek|dd|height|width|adb|current_audio|return|name|getAudioTracks|default_audio|localStorage|removeClass|expanded|checked|addButton|callMeMaybe|vplayer|0fxcyc2ajhp1|position|vvplay|vvad|220|setCurrentAudioTrack|audio_name|for|audio_set|getItem|last|open|controls|playbackRates|captions|rewind|icon|insertAfter||detach|ff00||button|getPosition|sec|png|player8|ff11|log|console|track_name|appendTo|play|click|no|scrolling|frameborder|file_code|src|top|zIndex|css|showCCform|data|1662367683|383371|dl|video_ad|doPlay|prevt|mp4|3E||jpg|thumbs|file|300|setTimeout|currentTrack|setItem|audioTrackChanged|dualSound|else|addClass|hasClass|toggleClass|Track|Audio|svg|dualy|images|mousedown|buttons|topbar|playAttemptFailed|beforePlay|Rewind|fr|Forward|ff|ready|set_audio_track|remove|this|upload_srt|prop|50px|margin|1000001|iframe|center|align|text|rgba|background|1000000|left|absolute|pause|setCurrentCaptions|Upload|contains|item|content|html|fviews|referer|prem|embed|3e57249ef633e0d03bf76ceb8d8a4b65|216|83|hash|view|get|TokenZir|window|hide|show|complete|slow|fadeIn|video_ad_fadein|time||cache|Cache|Content|headers|ajaxSetup|v2done|tott|vastdone2|vastdone1|vvbefore|playbackRateControls|cast|aboutlink|FileMoon|abouttext|UHD|1870|qualityLabels|sites|GNOME_POWER|link|2Fiframe|3C|allowfullscreen|22360|22640|22no|marginheight|marginwidth|2FGNOME_POWER|2F0fxcyc2ajhp1|2Fe|2Ffilemoon|2F|3A||22https|3Ciframe|code|sharing|fontOpacity|backgroundOpacity|Tahoma|fontFamily|303030|backgroundColor|FFFFFF|color|userFontScale|thumbnails|kind|0fxcyc2ajhp10000|url|get_slides|start|startparam|none|preload|html5|primary|hlshtml|androidhls|duration|uniform|stretching|0fxcyc2ajhp1_xt|image|2048|sp|6871|asn|127|srv|43200|_g3XlBcu2lmD9oDexD2NLWSmah2Nu3XcDrl93m9PwXY|m3u8||master|0fxcyc2ajhp1_x|00076|01|hls2|to|s01|delivery|storage|moon|sources|setup'''.split('|'))
  403. def test_join(self):
  404. test_input = list('test')
  405. tests = [
  406. 'function f(a, b){return a.join(b)}',
  407. 'function f(a, b){return Array.prototype.join.call(a, b)}',
  408. 'function f(a, b){return Array.prototype.join.apply(a, [b])}',
  409. ]
  410. for test in tests:
  411. jsi = JSInterpreter(test)
  412. self._test(jsi, 'test', args=[test_input, ''])
  413. self._test(jsi, 't-e-s-t', args=[test_input, '-'])
  414. self._test(jsi, '', args=[[], '-'])
  415. def test_split(self):
  416. test_result = list('test')
  417. tests = [
  418. 'function f(a, b){return a.split(b)}',
  419. 'function f(a, b){return String.prototype.split.call(a, b)}',
  420. 'function f(a, b){return String.prototype.split.apply(a, [b])}',
  421. ]
  422. for test in tests:
  423. jsi = JSInterpreter(test)
  424. self._test(jsi, test_result, args=['test', ''])
  425. self._test(jsi, test_result, args=['t-e-s-t', '-'])
  426. self._test(jsi, [''], args=['', '-'])
  427. self._test(jsi, [], args=['', ''])
  428. def test_slice(self):
  429. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice()}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
  430. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0)}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
  431. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(5)}', [5, 6, 7, 8])
  432. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(99)}', [])
  433. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-2)}', [7, 8])
  434. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-99)}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
  435. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 0)}', [])
  436. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, 0)}', [])
  437. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 1)}', [0])
  438. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(3, 6)}', [3, 4, 5])
  439. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, -1)}', [1, 2, 3, 4, 5, 6, 7])
  440. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-1, 1)}', [])
  441. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-3, -1)}', [6, 7])
  442. self._test('function f(){return "012345678".slice()}', '012345678')
  443. self._test('function f(){return "012345678".slice(0)}', '012345678')
  444. self._test('function f(){return "012345678".slice(5)}', '5678')
  445. self._test('function f(){return "012345678".slice(99)}', '')
  446. self._test('function f(){return "012345678".slice(-2)}', '78')
  447. self._test('function f(){return "012345678".slice(-99)}', '012345678')
  448. self._test('function f(){return "012345678".slice(0, 0)}', '')
  449. self._test('function f(){return "012345678".slice(1, 0)}', '')
  450. self._test('function f(){return "012345678".slice(0, 1)}', '0')
  451. self._test('function f(){return "012345678".slice(3, 6)}', '345')
  452. self._test('function f(){return "012345678".slice(1, -1)}', '1234567')
  453. self._test('function f(){return "012345678".slice(-1, 1)}', '')
  454. self._test('function f(){return "012345678".slice(-3, -1)}', '67')
  455. if __name__ == '__main__':
  456. unittest.main()