jsinterp.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. from __future__ import unicode_literals
  2. import itertools
  3. import json
  4. import math
  5. import operator
  6. import re
  7. from .utils import (
  8. error_to_compat_str,
  9. ExtractorError,
  10. js_to_json,
  11. remove_quotes,
  12. unified_timestamp,
  13. )
  14. from .compat import (
  15. compat_basestring,
  16. compat_collections_chain_map as ChainMap,
  17. compat_itertools_zip_longest as zip_longest,
  18. compat_str,
  19. )
  20. def _js_bit_op(op):
  21. def zeroise(x):
  22. return 0 if x in (None, JS_Undefined) else x
  23. def wrapped(a, b):
  24. return op(zeroise(a), zeroise(b)) & 0xffffffff
  25. return wrapped
  26. def _js_arith_op(op):
  27. def wrapped(a, b):
  28. if JS_Undefined in (a, b):
  29. return float('nan')
  30. return op(a or 0, b or 0)
  31. return wrapped
  32. def _js_div(a, b):
  33. if JS_Undefined in (a, b) or not (a and b):
  34. return float('nan')
  35. return operator.truediv(a or 0, b) if b else float('inf')
  36. def _js_mod(a, b):
  37. if JS_Undefined in (a, b) or not b:
  38. return float('nan')
  39. return (a or 0) % b
  40. def _js_exp(a, b):
  41. if not b:
  42. return 1 # even 0 ** 0 !!
  43. elif JS_Undefined in (a, b):
  44. return float('nan')
  45. return (a or 0) ** b
  46. def _js_eq_op(op):
  47. def wrapped(a, b):
  48. if set((a, b)) <= set((None, JS_Undefined)):
  49. return op(a, a)
  50. return op(a, b)
  51. return wrapped
  52. def _js_comp_op(op):
  53. def wrapped(a, b):
  54. if JS_Undefined in (a, b):
  55. return False
  56. if isinstance(a, compat_basestring):
  57. b = compat_str(b or 0)
  58. elif isinstance(b, compat_basestring):
  59. a = compat_str(a or 0)
  60. return op(a or 0, b or 0)
  61. return wrapped
  62. def _js_ternary(cndn, if_true=True, if_false=False):
  63. """Simulate JS's ternary operator (cndn?if_true:if_false)"""
  64. if cndn in (False, None, 0, '', JS_Undefined):
  65. return if_false
  66. try:
  67. if math.isnan(cndn): # NB: NaN cannot be checked by membership
  68. return if_false
  69. except TypeError:
  70. pass
  71. return if_true
  72. # (op, definition) in order of binding priority, tightest first
  73. # avoid dict to maintain order
  74. # definition None => Defined in JSInterpreter._operator
  75. _OPERATORS = (
  76. ('>>', _js_bit_op(operator.rshift)),
  77. ('<<', _js_bit_op(operator.lshift)),
  78. ('+', _js_arith_op(operator.add)),
  79. ('-', _js_arith_op(operator.sub)),
  80. ('*', _js_arith_op(operator.mul)),
  81. ('%', _js_mod),
  82. ('/', _js_div),
  83. ('**', _js_exp),
  84. )
  85. _COMP_OPERATORS = (
  86. ('===', operator.is_),
  87. ('!==', operator.is_not),
  88. ('==', _js_eq_op(operator.eq)),
  89. ('!=', _js_eq_op(operator.ne)),
  90. ('<=', _js_comp_op(operator.le)),
  91. ('>=', _js_comp_op(operator.ge)),
  92. ('<', _js_comp_op(operator.lt)),
  93. ('>', _js_comp_op(operator.gt)),
  94. )
  95. _LOG_OPERATORS = (
  96. ('|', _js_bit_op(operator.or_)),
  97. ('^', _js_bit_op(operator.xor)),
  98. ('&', _js_bit_op(operator.and_)),
  99. )
  100. _SC_OPERATORS = (
  101. ('?', None),
  102. ('??', None),
  103. ('||', None),
  104. ('&&', None),
  105. )
  106. _OPERATOR_RE = '|'.join(map(lambda x: re.escape(x[0]), _OPERATORS + _LOG_OPERATORS))
  107. _NAME_RE = r'[a-zA-Z_$][\w$]*'
  108. _MATCHING_PARENS = dict(zip(*zip('()', '{}', '[]')))
  109. _QUOTES = '\'"/'
  110. class JS_Undefined(object):
  111. pass
  112. class JS_Break(ExtractorError):
  113. def __init__(self):
  114. ExtractorError.__init__(self, 'Invalid break')
  115. class JS_Continue(ExtractorError):
  116. def __init__(self):
  117. ExtractorError.__init__(self, 'Invalid continue')
  118. class JS_Throw(ExtractorError):
  119. def __init__(self, e):
  120. self.error = e
  121. ExtractorError.__init__(self, 'Uncaught exception ' + error_to_compat_str(e))
  122. class LocalNameSpace(ChainMap):
  123. def __getitem__(self, key):
  124. try:
  125. return super(LocalNameSpace, self).__getitem__(key)
  126. except KeyError:
  127. return JS_Undefined
  128. def __setitem__(self, key, value):
  129. for scope in self.maps:
  130. if key in scope:
  131. scope[key] = value
  132. return
  133. self.maps[0][key] = value
  134. def __delitem__(self, key):
  135. raise NotImplementedError('Deleting is not supported')
  136. def __repr__(self):
  137. return 'LocalNameSpace%s' % (self.maps, )
  138. class JSInterpreter(object):
  139. __named_object_counter = 0
  140. _RE_FLAGS = {
  141. # special knowledge: Python's re flags are bitmask values, current max 128
  142. # invent new bitmask values well above that for literal parsing
  143. # TODO: new pattern class to execute matches with these flags
  144. 'd': 1024, # Generate indices for substring matches
  145. 'g': 2048, # Global search
  146. 'i': re.I, # Case-insensitive search
  147. 'm': re.M, # Multi-line search
  148. 's': re.S, # Allows . to match newline characters
  149. 'u': re.U, # Treat a pattern as a sequence of unicode code points
  150. 'y': 4096, # Perform a "sticky" search that matches starting at the current position in the target string
  151. }
  152. _OBJ_NAME = '__youtube_dl_jsinterp_obj'
  153. OP_CHARS = None
  154. def __init__(self, code, objects=None):
  155. self.code, self._functions = code, {}
  156. self._objects = {} if objects is None else objects
  157. if type(self).OP_CHARS is None:
  158. type(self).OP_CHARS = self.OP_CHARS = self.__op_chars()
  159. class Exception(ExtractorError):
  160. def __init__(self, msg, *args, **kwargs):
  161. expr = kwargs.pop('expr', None)
  162. if expr is not None:
  163. msg = '{0} in: {1!r:.100}'.format(msg.rstrip(), expr)
  164. super(JSInterpreter.Exception, self).__init__(msg, *args, **kwargs)
  165. @classmethod
  166. def __op_chars(cls):
  167. op_chars = set(';,')
  168. for op in cls._all_operators():
  169. for c in op[0]:
  170. op_chars.add(c)
  171. return op_chars
  172. def _named_object(self, namespace, obj):
  173. self.__named_object_counter += 1
  174. name = '%s%d' % (self._OBJ_NAME, self.__named_object_counter)
  175. namespace[name] = obj
  176. return name
  177. @classmethod
  178. def _regex_flags(cls, expr):
  179. flags = 0
  180. if not expr:
  181. return flags, expr
  182. for idx, ch in enumerate(expr):
  183. if ch not in cls._RE_FLAGS:
  184. break
  185. flags |= cls._RE_FLAGS[ch]
  186. return flags, expr[idx + 1:]
  187. @classmethod
  188. def _separate(cls, expr, delim=',', max_split=None, skip_delims=None):
  189. if not expr:
  190. return
  191. # collections.Counter() is ~10% slower in both 2.7 and 3.9
  192. counters = {k: 0 for k in _MATCHING_PARENS.values()}
  193. start, splits, pos, delim_len = 0, 0, 0, len(delim) - 1
  194. in_quote, escaping, skipping = None, False, 0
  195. after_op, in_regex_char_group, skip_re = True, False, 0
  196. for idx, char in enumerate(expr):
  197. if skip_re > 0:
  198. skip_re -= 1
  199. continue
  200. if not in_quote:
  201. if char in _MATCHING_PARENS:
  202. counters[_MATCHING_PARENS[char]] += 1
  203. elif char in counters:
  204. counters[char] -= 1
  205. if not escaping:
  206. if char in _QUOTES and in_quote in (char, None):
  207. if in_quote or after_op or char != '/':
  208. in_quote = None if in_quote and not in_regex_char_group else char
  209. elif in_quote == '/' and char in '[]':
  210. in_regex_char_group = char == '['
  211. escaping = not escaping and in_quote and char == '\\'
  212. after_op = not in_quote and (char in cls.OP_CHARS or char == '[' or (char.isspace() and after_op))
  213. if char != delim[pos] or any(counters.values()) or in_quote:
  214. pos = skipping = 0
  215. continue
  216. elif skipping > 0:
  217. skipping -= 1
  218. continue
  219. elif pos == 0 and skip_delims:
  220. here = expr[idx:]
  221. for s in skip_delims if isinstance(skip_delims, (list, tuple)) else [skip_delims]:
  222. if here.startswith(s) and s:
  223. skipping = len(s) - 1
  224. break
  225. if skipping > 0:
  226. continue
  227. if pos < delim_len:
  228. pos += 1
  229. continue
  230. yield expr[start: idx - delim_len]
  231. start, pos = idx + 1, 0
  232. splits += 1
  233. if max_split and splits >= max_split:
  234. break
  235. yield expr[start:]
  236. @classmethod
  237. def _separate_at_paren(cls, expr, delim=None):
  238. if delim is None:
  239. delim = expr and _MATCHING_PARENS[expr[0]]
  240. separated = list(cls._separate(expr, delim, 1))
  241. if len(separated) < 2:
  242. raise cls.Exception('No terminating paren {delim} in {expr:.100}'.format(**locals()))
  243. return separated[0][1:].strip(), separated[1].strip()
  244. @staticmethod
  245. def _all_operators():
  246. return itertools.chain(
  247. # Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
  248. _SC_OPERATORS, _LOG_OPERATORS, _COMP_OPERATORS, _OPERATORS)
  249. def _operator(self, op, left_val, right_expr, expr, local_vars, allow_recursion):
  250. if op in ('||', '&&'):
  251. if (op == '&&') ^ _js_ternary(left_val):
  252. return left_val # short circuiting
  253. elif op == '??':
  254. if left_val not in (None, JS_Undefined):
  255. return left_val
  256. elif op == '?':
  257. right_expr = _js_ternary(left_val, *self._separate(right_expr, ':', 1))
  258. right_val = self.interpret_expression(right_expr, local_vars, allow_recursion)
  259. opfunc = op and next((v for k, v in self._all_operators() if k == op), None)
  260. if not opfunc:
  261. return right_val
  262. try:
  263. return opfunc(left_val, right_val)
  264. except Exception as e:
  265. raise self.Exception('Failed to evaluate {left_val!r} {op} {right_val!r}'.format(**locals()), expr, cause=e)
  266. def _index(self, obj, idx, allow_undefined=False):
  267. if idx == 'length':
  268. return len(obj)
  269. try:
  270. return obj[int(idx)] if isinstance(obj, list) else obj[idx]
  271. except Exception as e:
  272. if allow_undefined:
  273. return JS_Undefined
  274. raise self.Exception('Cannot get index {idx}'.format(**locals()), expr=repr(obj), cause=e)
  275. def _dump(self, obj, namespace):
  276. try:
  277. return json.dumps(obj)
  278. except TypeError:
  279. return self._named_object(namespace, obj)
  280. def interpret_statement(self, stmt, local_vars, allow_recursion=100):
  281. if allow_recursion < 0:
  282. raise self.Exception('Recursion limit reached')
  283. allow_recursion -= 1
  284. should_return = False
  285. sub_statements = list(self._separate(stmt, ';')) or ['']
  286. expr = stmt = sub_statements.pop().strip()
  287. for sub_stmt in sub_statements:
  288. ret, should_return = self.interpret_statement(sub_stmt, local_vars, allow_recursion)
  289. if should_return:
  290. return ret, should_return
  291. m = re.match(r'(?P<var>(?:var|const|let)\s)|return(?:\s+|(?=["\'])|$)|(?P<throw>throw\s+)', stmt)
  292. if m:
  293. expr = stmt[len(m.group(0)):].strip()
  294. if m.group('throw'):
  295. raise JS_Throw(self.interpret_expression(expr, local_vars, allow_recursion))
  296. should_return = not m.group('var')
  297. if not expr:
  298. return None, should_return
  299. if expr[0] in _QUOTES:
  300. inner, outer = self._separate(expr, expr[0], 1)
  301. if expr[0] == '/':
  302. flags, outer = self._regex_flags(outer)
  303. inner = re.compile(inner[1:], flags=flags) # , strict=True))
  304. else:
  305. inner = json.loads(js_to_json(inner + expr[0])) # , strict=True))
  306. if not outer:
  307. return inner, should_return
  308. expr = self._named_object(local_vars, inner) + outer
  309. if expr.startswith('new '):
  310. obj = expr[4:]
  311. if obj.startswith('Date('):
  312. left, right = self._separate_at_paren(obj[4:])
  313. expr = unified_timestamp(
  314. self.interpret_expression(left, local_vars, allow_recursion), False)
  315. if not expr:
  316. raise self.Exception('Failed to parse date {left!r}'.format(**locals()), expr=expr)
  317. expr = self._dump(int(expr * 1000), local_vars) + right
  318. else:
  319. raise self.Exception('Unsupported object {obj}'.format(**locals()), expr=expr)
  320. if expr.startswith('void '):
  321. left = self.interpret_expression(expr[5:], local_vars, allow_recursion)
  322. return None, should_return
  323. if expr.startswith('{'):
  324. inner, outer = self._separate_at_paren(expr)
  325. # try for object expression (Map)
  326. sub_expressions = [list(self._separate(sub_expr.strip(), ':', 1)) for sub_expr in self._separate(inner)]
  327. if all(len(sub_expr) == 2 for sub_expr in sub_expressions):
  328. return dict(
  329. (key_expr if re.match(_NAME_RE, key_expr) else key_expr,
  330. self.interpret_expression(val_expr, local_vars, allow_recursion))
  331. for key_expr, val_expr in sub_expressions), should_return
  332. # or statement list
  333. inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
  334. if not outer or should_abort:
  335. return inner, should_abort or should_return
  336. else:
  337. expr = self._dump(inner, local_vars) + outer
  338. if expr.startswith('('):
  339. inner, outer = self._separate_at_paren(expr)
  340. inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
  341. if not outer or should_abort:
  342. return inner, should_abort or should_return
  343. else:
  344. expr = self._dump(inner, local_vars) + outer
  345. if expr.startswith('['):
  346. inner, outer = self._separate_at_paren(expr)
  347. name = self._named_object(local_vars, [
  348. self.interpret_expression(item, local_vars, allow_recursion)
  349. for item in self._separate(inner)])
  350. expr = name + outer
  351. m = re.match(r'''(?x)
  352. (?P<try>try)\s*\{|
  353. (?P<switch>switch)\s*\(|
  354. (?P<for>for)\s*\(
  355. ''', expr)
  356. md = m.groupdict() if m else {}
  357. if md.get('try'):
  358. try_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
  359. err = None
  360. try:
  361. ret, should_abort = self.interpret_statement(try_expr, local_vars, allow_recursion)
  362. if should_abort:
  363. return ret, True
  364. except Exception as e:
  365. # XXX: This works for now, but makes debugging future issues very hard
  366. err = e
  367. pending = (None, False)
  368. m = re.match(r'catch\s*(?P<err>\(\s*{_NAME_RE}\s*\))?\{{'.format(**globals()), expr)
  369. if m:
  370. sub_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
  371. if err:
  372. catch_vars = {}
  373. if m.group('err'):
  374. catch_vars[m.group('err')] = err.error if isinstance(err, JS_Throw) else err
  375. catch_vars = local_vars.new_child(m=catch_vars)
  376. err = None
  377. pending = self.interpret_statement(sub_expr, catch_vars, allow_recursion)
  378. m = re.match(r'finally\s*\{', expr)
  379. if m:
  380. sub_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
  381. ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion)
  382. if should_abort:
  383. return ret, True
  384. ret, should_abort = pending
  385. if should_abort:
  386. return ret, True
  387. if err:
  388. raise err
  389. elif md.get('for'):
  390. constructor, remaining = self._separate_at_paren(expr[m.end() - 1:])
  391. if remaining.startswith('{'):
  392. body, expr = self._separate_at_paren(remaining)
  393. else:
  394. switch_m = re.match(r'switch\s*\(', remaining) # FIXME
  395. if switch_m:
  396. switch_val, remaining = self._separate_at_paren(remaining[switch_m.end() - 1:])
  397. body, expr = self._separate_at_paren(remaining, '}')
  398. body = 'switch(%s){%s}' % (switch_val, body)
  399. else:
  400. body, expr = remaining, ''
  401. start, cndn, increment = self._separate(constructor, ';')
  402. self.interpret_expression(start, local_vars, allow_recursion)
  403. while True:
  404. if not _js_ternary(self.interpret_expression(cndn, local_vars, allow_recursion)):
  405. break
  406. try:
  407. ret, should_abort = self.interpret_statement(body, local_vars, allow_recursion)
  408. if should_abort:
  409. return ret, True
  410. except JS_Break:
  411. break
  412. except JS_Continue:
  413. pass
  414. self.interpret_expression(increment, local_vars, allow_recursion)
  415. elif md.get('switch'):
  416. switch_val, remaining = self._separate_at_paren(expr[m.end() - 1:])
  417. switch_val = self.interpret_expression(switch_val, local_vars, allow_recursion)
  418. body, expr = self._separate_at_paren(remaining, '}')
  419. items = body.replace('default:', 'case default:').split('case ')[1:]
  420. for default in (False, True):
  421. matched = False
  422. for item in items:
  423. case, stmt = (i.strip() for i in self._separate(item, ':', 1))
  424. if default:
  425. matched = matched or case == 'default'
  426. elif not matched:
  427. matched = (case != 'default'
  428. and switch_val == self.interpret_expression(case, local_vars, allow_recursion))
  429. if not matched:
  430. continue
  431. try:
  432. ret, should_abort = self.interpret_statement(stmt, local_vars, allow_recursion)
  433. if should_abort:
  434. return ret
  435. except JS_Break:
  436. break
  437. if matched:
  438. break
  439. if md:
  440. ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
  441. return ret, should_abort or should_return
  442. # Comma separated statements
  443. sub_expressions = list(self._separate(expr))
  444. if len(sub_expressions) > 1:
  445. for sub_expr in sub_expressions:
  446. ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion)
  447. if should_abort:
  448. return ret, True
  449. return ret, False
  450. for m in re.finditer(r'''(?x)
  451. (?P<pre_sign>\+\+|--)(?P<var1>{_NAME_RE})|
  452. (?P<var2>{_NAME_RE})(?P<post_sign>\+\+|--)'''.format(**globals()), expr):
  453. var = m.group('var1') or m.group('var2')
  454. start, end = m.span()
  455. sign = m.group('pre_sign') or m.group('post_sign')
  456. ret = local_vars[var]
  457. local_vars[var] += 1 if sign[0] == '+' else -1
  458. if m.group('pre_sign'):
  459. ret = local_vars[var]
  460. expr = expr[:start] + self._dump(ret, local_vars) + expr[end:]
  461. if not expr:
  462. return None, should_return
  463. m = re.match(r'''(?x)
  464. (?P<assign>
  465. (?P<out>{_NAME_RE})(?:\[(?P<index>[^\]]+?)\])?\s*
  466. (?P<op>{_OPERATOR_RE})?
  467. =(?!=)(?P<expr>.*)$
  468. )|(?P<return>
  469. (?!if|return|true|false|null|undefined)(?P<name>{_NAME_RE})$
  470. )|(?P<indexing>
  471. (?P<in>{_NAME_RE})\[(?P<idx>.+)\]$
  472. )|(?P<attribute>
  473. (?P<var>{_NAME_RE})(?:(?P<nullish>\?)?\.(?P<member>[^(]+)|\[(?P<member2>[^\]]+)\])\s*
  474. )|(?P<function>
  475. (?P<fname>{_NAME_RE})\((?P<args>.*)\)$
  476. )'''.format(**globals()), expr)
  477. md = m.groupdict() if m else {}
  478. if md.get('assign'):
  479. left_val = local_vars.get(m.group('out'))
  480. if not m.group('index'):
  481. local_vars[m.group('out')] = self._operator(
  482. m.group('op'), left_val, m.group('expr'), expr, local_vars, allow_recursion)
  483. return local_vars[m.group('out')], should_return
  484. elif left_val in (None, JS_Undefined):
  485. raise self.Exception('Cannot index undefined variable ' + m.group('out'), expr=expr)
  486. idx = self.interpret_expression(m.group('index'), local_vars, allow_recursion)
  487. if not isinstance(idx, (int, float)):
  488. raise self.Exception('List index %s must be integer' % (idx, ), expr=expr)
  489. idx = int(idx)
  490. left_val[idx] = self._operator(
  491. m.group('op'), self._index(left_val, idx), m.group('expr'), expr, local_vars, allow_recursion)
  492. return left_val[idx], should_return
  493. elif expr.isdigit():
  494. return int(expr), should_return
  495. elif expr == 'break':
  496. raise JS_Break()
  497. elif expr == 'continue':
  498. raise JS_Continue()
  499. elif expr == 'undefined':
  500. return JS_Undefined, should_return
  501. elif expr == 'NaN':
  502. return float('NaN'), should_return
  503. elif md.get('return'):
  504. return local_vars[m.group('name')], should_return
  505. try:
  506. ret = json.loads(js_to_json(expr)) # strict=True)
  507. if not md.get('attribute'):
  508. return ret, should_return
  509. except ValueError:
  510. pass
  511. if md.get('indexing'):
  512. val = local_vars[m.group('in')]
  513. idx = self.interpret_expression(m.group('idx'), local_vars, allow_recursion)
  514. return self._index(val, idx), should_return
  515. for op, _ in self._all_operators():
  516. # hackety: </> have higher priority than <</>>, but don't confuse them
  517. skip_delim = (op + op) if op in '<>*?' else None
  518. if op == '?':
  519. skip_delim = (skip_delim, '?.')
  520. separated = list(self._separate(expr, op, skip_delims=skip_delim))
  521. if len(separated) < 2:
  522. continue
  523. right_expr = separated.pop()
  524. while op == '-' and len(separated) > 1 and not separated[-1].strip():
  525. right_expr = '-' + right_expr
  526. separated.pop()
  527. left_val = self.interpret_expression(op.join(separated), local_vars, allow_recursion)
  528. return self._operator(op, left_val, right_expr, expr, local_vars, allow_recursion), should_return
  529. if md.get('attribute'):
  530. variable, member, nullish = m.group('var', 'member', 'nullish')
  531. if not member:
  532. member = self.interpret_expression(m.group('member2'), local_vars, allow_recursion)
  533. arg_str = expr[m.end():]
  534. if arg_str.startswith('('):
  535. arg_str, remaining = self._separate_at_paren(arg_str)
  536. else:
  537. arg_str, remaining = None, arg_str
  538. def assertion(cndn, msg):
  539. """ assert, but without risk of getting optimized out """
  540. if not cndn:
  541. memb = member
  542. raise self.Exception('{member} {msg}'.format(**locals()), expr=expr)
  543. def eval_method():
  544. if (variable, member) == ('console', 'debug'):
  545. return
  546. types = {
  547. 'String': compat_str,
  548. 'Math': float,
  549. }
  550. obj = local_vars.get(variable)
  551. if obj in (JS_Undefined, None):
  552. obj = types.get(variable, JS_Undefined)
  553. if obj is JS_Undefined:
  554. try:
  555. if variable not in self._objects:
  556. self._objects[variable] = self.extract_object(variable)
  557. obj = self._objects[variable]
  558. except self.Exception:
  559. if not nullish:
  560. raise
  561. if nullish and obj is JS_Undefined:
  562. return JS_Undefined
  563. # Member access
  564. if arg_str is None:
  565. return self._index(obj, member, nullish)
  566. # Function call
  567. argvals = [
  568. self.interpret_expression(v, local_vars, allow_recursion)
  569. for v in self._separate(arg_str)]
  570. if obj == compat_str:
  571. if member == 'fromCharCode':
  572. assertion(argvals, 'takes one or more arguments')
  573. return ''.join(map(chr, argvals))
  574. raise self.Exception('Unsupported string method ' + member, expr=expr)
  575. elif obj == float:
  576. if member == 'pow':
  577. assertion(len(argvals) == 2, 'takes two arguments')
  578. return argvals[0] ** argvals[1]
  579. raise self.Exception('Unsupported Math method ' + member, expr=expr)
  580. if member == 'split':
  581. assertion(argvals, 'takes one or more arguments')
  582. assertion(len(argvals) == 1, 'with limit argument is not implemented')
  583. return obj.split(argvals[0]) if argvals[0] else list(obj)
  584. elif member == 'join':
  585. assertion(isinstance(obj, list), 'must be applied on a list')
  586. assertion(len(argvals) == 1, 'takes exactly one argument')
  587. return argvals[0].join(obj)
  588. elif member == 'reverse':
  589. assertion(not argvals, 'does not take any arguments')
  590. obj.reverse()
  591. return obj
  592. elif member == 'slice':
  593. assertion(isinstance(obj, list), 'must be applied on a list')
  594. assertion(len(argvals) == 1, 'takes exactly one argument')
  595. return obj[argvals[0]:]
  596. elif member == 'splice':
  597. assertion(isinstance(obj, list), 'must be applied on a list')
  598. assertion(argvals, 'takes one or more arguments')
  599. index, howMany = map(int, (argvals + [len(obj)])[:2])
  600. if index < 0:
  601. index += len(obj)
  602. add_items = argvals[2:]
  603. res = []
  604. for i in range(index, min(index + howMany, len(obj))):
  605. res.append(obj.pop(index))
  606. for i, item in enumerate(add_items):
  607. obj.insert(index + i, item)
  608. return res
  609. elif member == 'unshift':
  610. assertion(isinstance(obj, list), 'must be applied on a list')
  611. assertion(argvals, 'takes one or more arguments')
  612. for item in reversed(argvals):
  613. obj.insert(0, item)
  614. return obj
  615. elif member == 'pop':
  616. assertion(isinstance(obj, list), 'must be applied on a list')
  617. assertion(not argvals, 'does not take any arguments')
  618. if not obj:
  619. return
  620. return obj.pop()
  621. elif member == 'push':
  622. assertion(argvals, 'takes one or more arguments')
  623. obj.extend(argvals)
  624. return obj
  625. elif member == 'forEach':
  626. assertion(argvals, 'takes one or more arguments')
  627. assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
  628. f, this = (argvals + [''])[:2]
  629. return [f((item, idx, obj), {'this': this}, allow_recursion) for idx, item in enumerate(obj)]
  630. elif member == 'indexOf':
  631. assertion(argvals, 'takes one or more arguments')
  632. assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
  633. idx, start = (argvals + [0])[:2]
  634. try:
  635. return obj.index(idx, start)
  636. except ValueError:
  637. return -1
  638. elif member == 'charCodeAt':
  639. assertion(isinstance(obj, compat_str), 'must be applied on a string')
  640. # assertion(len(argvals) == 1, 'takes exactly one argument') # but not enforced
  641. idx = argvals[0] if isinstance(argvals[0], int) else 0
  642. if idx >= len(obj):
  643. return None
  644. return ord(obj[idx])
  645. idx = int(member) if isinstance(obj, list) else member
  646. return obj[idx](argvals, allow_recursion=allow_recursion)
  647. if remaining:
  648. ret, should_abort = self.interpret_statement(
  649. self._named_object(local_vars, eval_method()) + remaining,
  650. local_vars, allow_recursion)
  651. return ret, should_return or should_abort
  652. else:
  653. return eval_method(), should_return
  654. elif md.get('function'):
  655. fname = m.group('fname')
  656. argvals = [self.interpret_expression(v, local_vars, allow_recursion)
  657. for v in self._separate(m.group('args'))]
  658. if fname in local_vars:
  659. return local_vars[fname](argvals, allow_recursion=allow_recursion), should_return
  660. elif fname not in self._functions:
  661. self._functions[fname] = self.extract_function(fname)
  662. return self._functions[fname](argvals, allow_recursion=allow_recursion), should_return
  663. raise self.Exception(
  664. 'Unsupported JS expression ' + (expr[:40] if expr != stmt else ''), expr=stmt)
  665. def interpret_expression(self, expr, local_vars, allow_recursion):
  666. ret, should_return = self.interpret_statement(expr, local_vars, allow_recursion)
  667. if should_return:
  668. raise self.Exception('Cannot return from an expression', expr)
  669. return ret
  670. def extract_object(self, objname):
  671. _FUNC_NAME_RE = r'''(?:[a-zA-Z$0-9]+|"[a-zA-Z$0-9]+"|'[a-zA-Z$0-9]+')'''
  672. obj = {}
  673. obj_m = re.search(
  674. r'''(?x)
  675. (?<!this\.)%s\s*=\s*{\s*
  676. (?P<fields>(%s\s*:\s*function\s*\(.*?\)\s*{.*?}(?:,\s*)?)*)
  677. }\s*;
  678. ''' % (re.escape(objname), _FUNC_NAME_RE),
  679. self.code)
  680. if not obj_m:
  681. raise self.Exception('Could not find object ' + objname)
  682. fields = obj_m.group('fields')
  683. # Currently, it only supports function definitions
  684. fields_m = re.finditer(
  685. r'''(?x)
  686. (?P<key>%s)\s*:\s*function\s*\((?P<args>(?:%s|,)*)\){(?P<code>[^}]+)}
  687. ''' % (_FUNC_NAME_RE, _NAME_RE),
  688. fields)
  689. for f in fields_m:
  690. argnames = self.build_arglist(f.group('args'))
  691. obj[remove_quotes(f.group('key'))] = self.build_function(argnames, f.group('code'))
  692. return obj
  693. def extract_function_code(self, funcname):
  694. """ @returns argnames, code """
  695. func_m = re.search(
  696. r'''(?xs)
  697. (?:
  698. function\s+%(name)s|
  699. [{;,]\s*%(name)s\s*=\s*function|
  700. (?:var|const|let)\s+%(name)s\s*=\s*function
  701. )\s*
  702. \((?P<args>[^)]*)\)\s*
  703. (?P<code>{.+})''' % {'name': re.escape(funcname)},
  704. self.code)
  705. code, _ = self._separate_at_paren(func_m.group('code')) # refine the match
  706. if func_m is None:
  707. raise self.Exception('Could not find JS function "{funcname}"'.format(**locals()))
  708. return self.build_arglist(func_m.group('args')), code
  709. def extract_function(self, funcname):
  710. return self.extract_function_from_code(*self.extract_function_code(funcname))
  711. def extract_function_from_code(self, argnames, code, *global_stack):
  712. local_vars = {}
  713. while True:
  714. mobj = re.search(r'function\((?P<args>[^)]*)\)\s*{', code)
  715. if mobj is None:
  716. break
  717. start, body_start = mobj.span()
  718. body, remaining = self._separate_at_paren(code[body_start - 1:], '}')
  719. name = self._named_object(local_vars, self.extract_function_from_code(
  720. [x.strip() for x in mobj.group('args').split(',')],
  721. body, local_vars, *global_stack))
  722. code = code[:start] + name + remaining
  723. return self.build_function(argnames, code, local_vars, *global_stack)
  724. def call_function(self, funcname, *args):
  725. return self.extract_function(funcname)(args)
  726. @classmethod
  727. def build_arglist(cls, arg_text):
  728. if not arg_text:
  729. return []
  730. def valid_arg(y):
  731. y = y.strip()
  732. if not y:
  733. raise cls.Exception('Missing arg in "%s"' % (arg_text, ))
  734. return y
  735. return [valid_arg(x) for x in cls._separate(arg_text)]
  736. def build_function(self, argnames, code, *global_stack):
  737. global_stack = list(global_stack) or [{}]
  738. argnames = tuple(argnames)
  739. def resf(args, kwargs={}, allow_recursion=100):
  740. global_stack[0].update(
  741. zip_longest(argnames, args, fillvalue=None))
  742. global_stack[0].update(kwargs)
  743. var_stack = LocalNameSpace(*global_stack)
  744. ret, should_abort = self.interpret_statement(code.replace('\n', ' '), var_stack, allow_recursion - 1)
  745. if should_abort:
  746. return ret
  747. return resf