logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

test_jsinterp.py (32857B)


  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. import time
  12. from youtube_dl.compat import compat_str as str
  13. from youtube_dl.jsinterp import JS_Undefined, JSInterpreter
  14. NaN = object()
  15. class TestJSInterpreter(unittest.TestCase):
  16. def _test(self, jsi_or_code, expected, func='f', args=()):
  17. if isinstance(jsi_or_code, str):
  18. jsi_or_code = JSInterpreter(jsi_or_code)
  19. got = jsi_or_code.call_function(func, *args)
  20. if expected is NaN:
  21. self.assertTrue(math.isnan(got), '{0} is not NaN'.format(got))
  22. else:
  23. self.assertEqual(got, expected)
  24. def test_basic(self):
  25. jsi = JSInterpreter('function f(){;}')
  26. self.assertEqual(repr(jsi.extract_function('f')), 'F<f>')
  27. self._test(jsi, None)
  28. self._test('function f(){return 42;}', 42)
  29. self._test('function f(){42}', None)
  30. self._test('var f = function(){return 42;}', 42)
  31. def test_add(self):
  32. self._test('function f(){return 42 + 7;}', 49)
  33. self._test('function f(){return 42 + undefined;}', NaN)
  34. self._test('function f(){return 42 + null;}', 42)
  35. self._test('function f(){return 1 + "";}', '1')
  36. self._test('function f(){return 42 + "7";}', '427')
  37. self._test('function f(){return false + true;}', 1)
  38. self._test('function f(){return "false" + true;}', 'falsetrue')
  39. self._test('function f(){return '
  40. '1 + "2" + [3,4] + {k: 56} + null + undefined + Infinity;}',
  41. '123,4[object Object]nullundefinedInfinity')
  42. def test_sub(self):
  43. self._test('function f(){return 42 - 7;}', 35)
  44. self._test('function f(){return 42 - undefined;}', NaN)
  45. self._test('function f(){return 42 - null;}', 42)
  46. self._test('function f(){return 42 - "7";}', 35)
  47. self._test('function f(){return 42 - "spam";}', NaN)
  48. def test_mul(self):
  49. self._test('function f(){return 42 * 7;}', 294)
  50. self._test('function f(){return 42 * undefined;}', NaN)
  51. self._test('function f(){return 42 * null;}', 0)
  52. self._test('function f(){return 42 * "7";}', 294)
  53. self._test('function f(){return 42 * "eggs";}', NaN)
  54. def test_div(self):
  55. jsi = JSInterpreter('function f(a, b){return a / b;}')
  56. self._test(jsi, NaN, args=(0, 0))
  57. self._test(jsi, NaN, args=(JS_Undefined, 1))
  58. self._test(jsi, float('inf'), args=(2, 0))
  59. self._test(jsi, 0, args=(0, 3))
  60. self._test(jsi, 6, args=(42, 7))
  61. self._test(jsi, 0, args=(42, float('inf')))
  62. self._test(jsi, 6, args=("42", 7))
  63. self._test(jsi, NaN, args=("spam", 7))
  64. def test_mod(self):
  65. self._test('function f(){return 42 % 7;}', 0)
  66. self._test('function f(){return 42 % 0;}', NaN)
  67. self._test('function f(){return 42 % undefined;}', NaN)
  68. self._test('function f(){return 42 % "7";}', 0)
  69. self._test('function f(){return 42 % "beans";}', NaN)
  70. def test_exp(self):
  71. self._test('function f(){return 42 ** 2;}', 1764)
  72. self._test('function f(){return 42 ** undefined;}', NaN)
  73. self._test('function f(){return 42 ** null;}', 1)
  74. self._test('function f(){return undefined ** 0;}', 1)
  75. self._test('function f(){return undefined ** 42;}', NaN)
  76. self._test('function f(){return 42 ** "2";}', 1764)
  77. self._test('function f(){return 42 ** "spam";}', NaN)
  78. def test_calc(self):
  79. self._test('function f(a){return 2*a+1;}', 7, args=[3])
  80. def test_empty_return(self):
  81. self._test('function f(){return; y()}', None)
  82. def test_morespace(self):
  83. self._test('function f (a) { return 2 * a + 1 ; }', 7, args=[3])
  84. self._test('function f () { x = 2 ; return x; }', 2)
  85. def test_strange_chars(self):
  86. self._test('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }',
  87. 21, args=[20], func='$_xY1')
  88. def test_operators(self):
  89. self._test('function f(){return 1 << 5;}', 32)
  90. self._test('function f(){return 2 ** 5}', 32)
  91. self._test('function f(){return 19 & 21;}', 17)
  92. self._test('function f(){return 11 >> 2;}', 2)
  93. self._test('function f(){return []? 2+3: 4;}', 5)
  94. # equality
  95. self._test('function f(){return 1 == 1}', True)
  96. self._test('function f(){return 1 == 1.0}', True)
  97. self._test('function f(){return 1 == "1"}', True)
  98. self._test('function f(){return 1 == 2}', False)
  99. self._test('function f(){return 1 != "1"}', False)
  100. self._test('function f(){return 1 != 2}', True)
  101. self._test('function f(){var x = {a: 1}; var y = x; return x == y}', True)
  102. self._test('function f(){var x = {a: 1}; return x == {a: 1}}', False)
  103. self._test('function f(){return NaN == NaN}', False)
  104. self._test('function f(){return null == undefined}', True)
  105. self._test('function f(){return "spam, eggs" == "spam, eggs"}', True)
  106. # strict equality
  107. self._test('function f(){return 1 === 1}', True)
  108. self._test('function f(){return 1 === 1.0}', True)
  109. self._test('function f(){return 1 === "1"}', False)
  110. self._test('function f(){return 1 === 2}', False)
  111. self._test('function f(){var x = {a: 1}; var y = x; return x === y}', True)
  112. self._test('function f(){var x = {a: 1}; return x === {a: 1}}', False)
  113. self._test('function f(){return NaN === NaN}', False)
  114. self._test('function f(){return null === undefined}', False)
  115. self._test('function f(){return null === null}', True)
  116. self._test('function f(){return undefined === undefined}', True)
  117. self._test('function f(){return "uninterned" === "uninterned"}', True)
  118. self._test('function f(){return 1 === 1}', True)
  119. self._test('function f(){return 1 === "1"}', False)
  120. self._test('function f(){return 1 !== 1}', False)
  121. self._test('function f(){return 1 !== "1"}', True)
  122. # expressions
  123. self._test('function f(){return 0 && 1 || 2;}', 2)
  124. self._test('function f(){return 0 ?? 42;}', 0)
  125. self._test('function f(){return "life, the universe and everything" < 42;}', False)
  126. # https://github.com/ytdl-org/youtube-dl/issues/32815
  127. self._test('function f(){return 0 - 7 * - 6;}', 42)
  128. def test_bitwise_operators_typecast(self):
  129. # madness
  130. self._test('function f(){return null << 5}', 0)
  131. self._test('function f(){return undefined >> 5}', 0)
  132. self._test('function f(){return 42 << NaN}', 42)
  133. self._test('function f(){return 42 << Infinity}', 42)
  134. self._test('function f(){return 0.0 << null}', 0)
  135. self._test('function f(){return NaN << 42}', 0)
  136. self._test('function f(){return "21.9" << 1}', 42)
  137. self._test('function f(){return true << "5";}', 32)
  138. self._test('function f(){return true << true;}', 2)
  139. self._test('function f(){return "19" & "21.9";}', 17)
  140. self._test('function f(){return "19" & false;}', 0)
  141. self._test('function f(){return "11.0" >> "2.1";}', 2)
  142. self._test('function f(){return 5 ^ 9;}', 12)
  143. self._test('function f(){return 0.0 << NaN}', 0)
  144. self._test('function f(){return null << undefined}', 0)
  145. self._test('function f(){return 21 << 4294967297}', 42)
  146. def test_array_access(self):
  147. self._test('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}', [5, 2, 7])
  148. def test_parens(self):
  149. self._test('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}', 7)
  150. self._test('function f(){return (1 + 2) * 3;}', 9)
  151. def test_quotes(self):
  152. self._test(r'function f(){return "a\"\\("}', r'a"\(')
  153. def test_assignments(self):
  154. self._test('function f(){var x = 20; x = 30 + 1; return x;}', 31)
  155. self._test('function f(){var x = 20; x += 30 + 1; return x;}', 51)
  156. self._test('function f(){var x = 20; x -= 30 + 1; return x;}', -11)
  157. self._test('function f(){var x = 2; var y = ["a", "b"]; y[x%y["length"]]="z"; return y}', ['z', 'b'])
  158. def test_comments(self):
  159. self._test('''
  160. function f() {
  161. var x = /* 1 + */ 2;
  162. var y = /* 30
  163. * 40 */ 50;
  164. return x + y;
  165. }
  166. ''', 52)
  167. self._test('''
  168. function f() {
  169. var x = "/*";
  170. var y = 1 /* comment */ + 2;
  171. return y;
  172. }
  173. ''', 3)
  174. self._test('''
  175. function f() {
  176. var x = ( /* 1 + */ 2 +
  177. /* 30 * 40 */
  178. 50);
  179. return x;
  180. }
  181. ''', 52)
  182. def test_precedence(self):
  183. self._test('''
  184. function f() {
  185. var a = [10, 20, 30, 40, 50];
  186. var b = 6;
  187. a[0]=a[b%a.length];
  188. return a;
  189. }
  190. ''', [20, 20, 30, 40, 50])
  191. def test_builtins(self):
  192. self._test('function f() { return NaN }', NaN)
  193. def test_Date(self):
  194. self._test('function f() { return new Date("Wednesday 31 December 1969 18:01:26 MDT") - 0; }', 86000)
  195. jsi = JSInterpreter('function f(dt) { return new Date(dt) - 0; }')
  196. # date format m/d/y
  197. self._test(jsi, 86000, args=['12/31/1969 18:01:26 MDT'])
  198. # epoch 0
  199. self._test(jsi, 0, args=['1 January 1970 00:00:00 UTC'])
  200. # undefined
  201. self._test(jsi, NaN, args=[JS_Undefined])
  202. # y,m,d, ... - may fail with older dates lacking DST data
  203. jsi = JSInterpreter(
  204. 'function f() { return new Date(%s); }'
  205. % ('2024, 5, 29, 2, 52, 12, 42',))
  206. self._test(jsi, (
  207. 1719625932042 # UK value
  208. + (
  209. + 3600 # back to GMT
  210. + (time.altzone if time.daylight # host's DST
  211. else time.timezone)
  212. ) * 1000))
  213. # no arg
  214. self.assertAlmostEqual(JSInterpreter(
  215. 'function f() { return new Date() - 0; }').call_function('f'),
  216. time.time() * 1000, delta=100)
  217. # Date.now()
  218. self.assertAlmostEqual(JSInterpreter(
  219. 'function f() { return Date.now(); }').call_function('f'),
  220. time.time() * 1000, delta=100)
  221. # Date.parse()
  222. jsi = JSInterpreter('function f(dt) { return Date.parse(dt); }')
  223. self._test(jsi, 0, args=['1 January 1970 00:00:00 UTC'])
  224. # Date.UTC()
  225. jsi = JSInterpreter('function f() { return Date.UTC(%s); }'
  226. % ('1970, 0, 1, 0, 0, 0, 0',))
  227. self._test(jsi, 0)
  228. def test_call(self):
  229. jsi = JSInterpreter('''
  230. function x() { return 2; }
  231. function y(a) { return x() + (a?a:0); }
  232. function z() { return y(3); }
  233. ''')
  234. self._test(jsi, 5, func='z')
  235. self._test(jsi, 2, func='y')
  236. def test_if(self):
  237. self._test('''
  238. function f() {
  239. let a = 9;
  240. if (0==0) {a++}
  241. return a
  242. }
  243. ''', 10)
  244. self._test('''
  245. function f() {
  246. if (0==0) {return 10}
  247. }
  248. ''', 10)
  249. self._test('''
  250. function f() {
  251. if (0!=0) {return 1}
  252. else {return 10}
  253. }
  254. ''', 10)
  255. def test_elseif(self):
  256. self._test('''
  257. function f() {
  258. if (0!=0) {return 1}
  259. else if (1==0) {return 2}
  260. else {return 10}
  261. }
  262. ''', 10)
  263. def test_for_loop(self):
  264. self._test('function f() { a=0; for (i=0; i-10; i++) {a++} return a }', 10)
  265. def test_while_loop(self):
  266. self._test('function f() { a=0; while (a<10) {a++} return a }', 10)
  267. def test_switch(self):
  268. jsi = JSInterpreter('''
  269. function f(x) { switch(x){
  270. case 1:x+=1;
  271. case 2:x+=2;
  272. case 3:x+=3;break;
  273. case 4:x+=4;
  274. default:x=0;
  275. } return x }
  276. ''')
  277. self._test(jsi, 7, args=[1])
  278. self._test(jsi, 6, args=[3])
  279. self._test(jsi, 0, args=[5])
  280. def test_switch_default(self):
  281. jsi = JSInterpreter('''
  282. function f(x) { switch(x){
  283. case 2: x+=2;
  284. default: x-=1;
  285. case 5:
  286. case 6: x+=6;
  287. case 0: break;
  288. case 1: x+=1;
  289. } return x }
  290. ''')
  291. self._test(jsi, 2, args=[1])
  292. self._test(jsi, 11, args=[5])
  293. self._test(jsi, 14, args=[9])
  294. def test_try(self):
  295. self._test('function f() { try{return 10} catch(e){return 5} }', 10)
  296. def test_catch(self):
  297. self._test('function f() { try{throw 10} catch(e){return 5} }', 5)
  298. def test_finally(self):
  299. self._test('function f() { try{throw 10} finally {return 42} }', 42)
  300. self._test('function f() { try{throw 10} catch(e){return 5} finally {return 42} }', 42)
  301. def test_nested_try(self):
  302. self._test('''
  303. function f() {try {
  304. try{throw 10} finally {throw 42}
  305. } catch(e){return 5} }
  306. ''', 5)
  307. def test_for_loop_continue(self):
  308. self._test('function f() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }', 0)
  309. def test_for_loop_break(self):
  310. self._test('function f() { a=0; for (i=0; i-10; i++) { break; a++ } return a }', 0)
  311. def test_for_loop_try(self):
  312. self._test('''
  313. function f() {
  314. for (i=0; i-10; i++) { try { if (i == 5) throw i} catch {return 10} finally {break} };
  315. return 42 }
  316. ''', 42)
  317. def test_literal_list(self):
  318. self._test('function f() { return [1, 2, "asdf", [5, 6, 7]][3] }', [5, 6, 7])
  319. def test_comma(self):
  320. self._test('function f() { a=5; a -= 1, a+=3; return a }', 7)
  321. self._test('function f() { a=5; return (a -= 1, a+=3, a); }', 7)
  322. self._test('function f() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) }', 5)
  323. def test_not(self):
  324. self._test('function f() { return ! undefined; }', True)
  325. self._test('function f() { return !0; }', True)
  326. self._test('function f() { return !!0; }', False)
  327. self._test('function f() { return ![]; }', False)
  328. self._test('function f() { return !0 !== false; }', True)
  329. def test_void(self):
  330. self._test('function f() { return void 42; }', JS_Undefined)
  331. def test_typeof(self):
  332. self._test('function f() { return typeof undefined; }', 'undefined')
  333. self._test('function f() { return typeof NaN; }', 'number')
  334. self._test('function f() { return typeof Infinity; }', 'number')
  335. self._test('function f() { return typeof true; }', 'boolean')
  336. self._test('function f() { return typeof null; }', 'object')
  337. self._test('function f() { return typeof "a string"; }', 'string')
  338. self._test('function f() { return typeof 42; }', 'number')
  339. self._test('function f() { return typeof 42.42; }', 'number')
  340. self._test('function f() { var g = function(){}; return typeof g; }', 'function')
  341. self._test('function f() { return typeof {key: "value"}; }', 'object')
  342. # not yet implemented: Symbol, BigInt
  343. def test_return_function(self):
  344. jsi = JSInterpreter('''
  345. function x() { return [1, function(){return 1}][1] }
  346. ''')
  347. self.assertEqual(jsi.call_function('x')([]), 1)
  348. def test_null(self):
  349. self._test('function f() { return null; }', None)
  350. self._test('function f() { return [null > 0, null < 0, null == 0, null === 0]; }',
  351. [False, False, False, False])
  352. self._test('function f() { return [null >= 0, null <= 0]; }', [True, True])
  353. def test_undefined(self):
  354. self._test('function f() { return undefined === undefined; }', True)
  355. self._test('function f() { return undefined; }', JS_Undefined)
  356. self._test('function f() { return undefined ?? 42; }', 42)
  357. self._test('function f() { let v; return v; }', JS_Undefined)
  358. self._test('function f() { let v; return v**0; }', 1)
  359. self._test('function f() { let v; return [v>42, v<=42, v&&42, 42&&v]; }',
  360. [False, False, JS_Undefined, JS_Undefined])
  361. self._test('''
  362. function f() { return [
  363. undefined === undefined,
  364. undefined == undefined,
  365. undefined == null
  366. ]; }
  367. ''', [True] * 3)
  368. self._test('''
  369. function f() { return [
  370. undefined < undefined,
  371. undefined > undefined,
  372. undefined === 0,
  373. undefined == 0,
  374. undefined < 0,
  375. undefined > 0,
  376. undefined >= 0,
  377. undefined <= 0,
  378. undefined > null,
  379. undefined < null,
  380. undefined === null
  381. ]; }
  382. ''', [False] * 11)
  383. jsi = JSInterpreter('''
  384. function x() { let v; return [42+v, v+42, v**42, 42**v, 0**v]; }
  385. ''')
  386. for y in jsi.call_function('x'):
  387. self.assertTrue(math.isnan(y))
  388. def test_object(self):
  389. self._test('function f() { return {}; }', {})
  390. self._test('function f() { let a = {m1: 42, m2: 0 }; return [a["m1"], a.m2]; }', [42, 0])
  391. self._test('function f() { let a; return a?.qq; }', JS_Undefined)
  392. self._test('function f() { let a = {m1: 42, m2: 0 }; return a?.qq; }', JS_Undefined)
  393. def test_indexing(self):
  394. self._test('function f() { return [1, 2, 3, 4][3]}', 4)
  395. self._test('function f() { return [1, [2, [3, [4]]]][1][1][1][0]}', 4)
  396. self._test('function f() { var o = {1: 2, 3: 4}; return o[3]}', 4)
  397. self._test('function f() { var o = {1: 2, 3: 4}; return o["3"]}', 4)
  398. self._test('function f() { return [1, [2, {3: [4]}]][1][1]["3"][0]}', 4)
  399. self._test('function f() { return [1, 2, 3, 4].length}', 4)
  400. self._test('function f() { var o = {1: 2, 3: 4}; return o.length}', JS_Undefined)
  401. self._test('function f() { var o = {1: 2, 3: 4}; o["length"] = 42; return o.length}', 42)
  402. def test_regex(self):
  403. self._test('function f() { let a=/,,[/,913,/](,)}/; }', None)
  404. self._test('function f() { let a=/,,[/,913,/](,)}/; return a.source; }', ',,[/,913,/](,)}')
  405. jsi = JSInterpreter('''
  406. function x() { let a=/,,[/,913,/](,)}/; "".replace(a, ""); return a; }
  407. ''')
  408. attrs = set(('findall', 'finditer', 'match', 'scanner', 'search',
  409. 'split', 'sub', 'subn'))
  410. if sys.version_info >= (2, 7):
  411. # documented for 2.6 but may not be found
  412. attrs.update(('flags', 'groupindex', 'groups', 'pattern'))
  413. self.assertSetEqual(set(dir(jsi.call_function('x'))) & attrs, attrs)
  414. jsi = JSInterpreter('''
  415. function x() { let a=/,,[/,913,/](,)}/i; return a; }
  416. ''')
  417. self.assertEqual(jsi.call_function('x').flags & ~re.U, re.I)
  418. jsi = JSInterpreter(r'function f() { let a=/,][}",],()}(\[)/; return a; }')
  419. self.assertEqual(jsi.call_function('f').pattern, r',][}",],()}(\[)')
  420. jsi = JSInterpreter(r'function f() { let a=[/[)\\]/]; return a[0]; }')
  421. self.assertEqual(jsi.call_function('f').pattern, r'[)\\]')
  422. def test_replace(self):
  423. self._test('function f() { let a="data-name".replace("data-", ""); return a }',
  424. 'name')
  425. self._test('function f() { let a="data-name".replace(new RegExp("^.+-"), ""); return a; }',
  426. 'name')
  427. self._test('function f() { let a="data-name".replace(/^.+-/, ""); return a; }',
  428. 'name')
  429. self._test('function f() { let a="data-name".replace(/a/g, "o"); return a; }',
  430. 'doto-nome')
  431. self._test('function f() { let a="data-name".replaceAll("a", "o"); return a; }',
  432. 'doto-nome')
  433. def test_char_code_at(self):
  434. jsi = JSInterpreter('function f(i){return "test".charCodeAt(i)}')
  435. self._test(jsi, 116, args=[0])
  436. self._test(jsi, 101, args=[1])
  437. self._test(jsi, 115, args=[2])
  438. self._test(jsi, 116, args=[3])
  439. self._test(jsi, None, args=[4])
  440. self._test(jsi, 116, args=['not_a_number'])
  441. def test_bitwise_operators_overflow(self):
  442. self._test('function f(){return -524999584 << 5}', 379882496)
  443. self._test('function f(){return 1236566549 << 5}', 915423904)
  444. def test_negative(self):
  445. self._test('function f(){return 2 * -2.0 ;}', -4)
  446. self._test('function f(){return 2 - - -2 ;}', 0)
  447. self._test('function f(){return 2 - - - -2 ;}', 4)
  448. self._test('function f(){return 2 - + + - -2;}', 0)
  449. self._test('function f(){return 2 + - + - -2;}', 0)
  450. def test_32066(self):
  451. self._test(
  452. "function f(){return Math.pow(3, 5) + new Date('1970-01-01T08:01:42.000+08:00') / 1000 * -239 - -24205;}",
  453. 70)
  454. @unittest.skip('Not yet working')
  455. def test_packed(self):
  456. self._test(
  457. '''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}''',
  458. '''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('|'))
  459. def test_join(self):
  460. test_input = list('test')
  461. tests = [
  462. 'function f(a, b){return a.join(b)}',
  463. 'function f(a, b){return Array.prototype.join.call(a, b)}',
  464. 'function f(a, b){return Array.prototype.join.apply(a, [b])}',
  465. ]
  466. for test in tests:
  467. jsi = JSInterpreter(test)
  468. self._test(jsi, 'test', args=[test_input, ''])
  469. self._test(jsi, 't-e-s-t', args=[test_input, '-'])
  470. self._test(jsi, '', args=[[], '-'])
  471. self._test('function f(){return '
  472. '[1, 1.0, "abc", {a: 1}, null, undefined, Infinity, NaN].join()}',
  473. '1,1,abc,[object Object],,,Infinity,NaN')
  474. self._test('function f(){return '
  475. '[1, 1.0, "abc", {a: 1}, null, undefined, Infinity, NaN].join("~")}',
  476. '1~1~abc~[object Object]~~~Infinity~NaN')
  477. def test_split(self):
  478. test_result = list('test')
  479. tests = [
  480. 'function f(a, b){return a.split(b)}',
  481. 'function f(a, b){return a["split"](b)}',
  482. 'function f(a, b){let x = ["split"]; return a[x[0]](b)}',
  483. 'function f(a, b){return String.prototype.split.call(a, b)}',
  484. 'function f(a, b){return String.prototype.split.apply(a, [b])}',
  485. ]
  486. for test in tests:
  487. jsi = JSInterpreter(test)
  488. self._test(jsi, test_result, args=['test', ''])
  489. self._test(jsi, test_result, args=['t-e-s-t', '-'])
  490. self._test(jsi, [''], args=['', '-'])
  491. self._test(jsi, [], args=['', ''])
  492. # RegExp split
  493. self._test('function f(){return "test".split(/(?:)/)}',
  494. ['t', 'e', 's', 't'])
  495. self._test('function f(){return "t-e-s-t".split(/[es-]+/)}',
  496. ['t', 't'])
  497. # from MDN: surrogate pairs aren't handled: case 1 fails
  498. # self._test('function f(){return "😄😄".split(/(?:)/)}',
  499. # ['\ud83d', '\ude04', '\ud83d', '\ude04'])
  500. # case 2 beats Py3.2: it gets the case 1 result
  501. if sys.version_info >= (2, 6) and not ((3, 0) <= sys.version_info < (3, 3)):
  502. self._test('function f(){return "😄😄".split(/(?:)/u)}',
  503. ['😄', '😄'])
  504. def test_slice(self):
  505. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice()}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
  506. 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])
  507. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(5)}', [5, 6, 7, 8])
  508. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(99)}', [])
  509. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-2)}', [7, 8])
  510. 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])
  511. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 0)}', [])
  512. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, 0)}', [])
  513. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 1)}', [0])
  514. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(3, 6)}', [3, 4, 5])
  515. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, -1)}', [1, 2, 3, 4, 5, 6, 7])
  516. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-1, 1)}', [])
  517. self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-3, -1)}', [6, 7])
  518. self._test('function f(){return "012345678".slice()}', '012345678')
  519. self._test('function f(){return "012345678".slice(0)}', '012345678')
  520. self._test('function f(){return "012345678".slice(5)}', '5678')
  521. self._test('function f(){return "012345678".slice(99)}', '')
  522. self._test('function f(){return "012345678".slice(-2)}', '78')
  523. self._test('function f(){return "012345678".slice(-99)}', '012345678')
  524. self._test('function f(){return "012345678".slice(0, 0)}', '')
  525. self._test('function f(){return "012345678".slice(1, 0)}', '')
  526. self._test('function f(){return "012345678".slice(0, 1)}', '0')
  527. self._test('function f(){return "012345678".slice(3, 6)}', '345')
  528. self._test('function f(){return "012345678".slice(1, -1)}', '1234567')
  529. self._test('function f(){return "012345678".slice(-1, 1)}', '')
  530. self._test('function f(){return "012345678".slice(-3, -1)}', '67')
  531. def test_splice(self):
  532. self._test('function f(){var T = ["0", "1", "2"]; T["splice"](2, 1, "0")[0]; return T }', ['0', '1', '0'])
  533. def test_pop(self):
  534. # pop
  535. self._test('function f(){var a = [0, 1, 2, 3, 4, 5, 6, 7, 8]; return [a.pop(), a]}',
  536. [8, [0, 1, 2, 3, 4, 5, 6, 7]])
  537. self._test('function f(){return [].pop()}', JS_Undefined)
  538. # push
  539. self._test('function f(){var a = [0, 1, 2]; return [a.push(3, 4), a]}',
  540. [5, [0, 1, 2, 3, 4]])
  541. self._test('function f(){var a = [0, 1, 2]; return [a.push(), a]}',
  542. [3, [0, 1, 2]])
  543. def test_shift(self):
  544. # shift
  545. self._test('function f(){var a = [0, 1, 2, 3, 4, 5, 6, 7, 8]; return [a.shift(), a]}',
  546. [0, [1, 2, 3, 4, 5, 6, 7, 8]])
  547. self._test('function f(){return [].shift()}', JS_Undefined)
  548. # unshift
  549. self._test('function f(){var a = [0, 1, 2]; return [a.unshift(3, 4), a]}',
  550. [5, [3, 4, 0, 1, 2]])
  551. self._test('function f(){var a = [0, 1, 2]; return [a.unshift(), a]}',
  552. [3, [0, 1, 2]])
  553. def test_forEach(self):
  554. self._test('function f(){var ret = []; var l = [4, 2]; '
  555. 'var log = function(e,i,a){ret.push([e,i,a]);}; '
  556. 'l.forEach(log); '
  557. 'return [ret.length, ret[0][0], ret[1][1], ret[0][2]]}',
  558. [2, 4, 1, [4, 2]])
  559. self._test('function f(){var ret = []; var l = [4, 2]; '
  560. 'var log = function(e,i,a){this.push([e,i,a]);}; '
  561. 'l.forEach(log, ret); '
  562. 'return [ret.length, ret[0][0], ret[1][1], ret[0][2]]}',
  563. [2, 4, 1, [4, 2]])
  564. def test_extract_function(self):
  565. jsi = JSInterpreter('function a(b) { return b + 1; }')
  566. func = jsi.extract_function('a')
  567. self.assertEqual(func([2]), 3)
  568. def test_extract_function_with_global_stack(self):
  569. jsi = JSInterpreter('function c(d) { return d + e + f + g; }')
  570. func = jsi.extract_function('c', {'e': 10}, {'f': 100, 'g': 1000})
  571. self.assertEqual(func([1]), 1111)
  572. if __name__ == '__main__':
  573. unittest.main()