]> jfr.im git - yt-dlp.git/blame - yt_dlp/jsinterp.py
[extractor/instagram] Fix bugs in 7d3b98be4c4567b985ba7d7b17057e930457edc9 (#4701)
[yt-dlp.git] / yt_dlp / jsinterp.py
CommitLineData
19a03940 1import collections
2import contextlib
8f53dc44 3import itertools
825abb81 4import json
8f53dc44 5import math
9e3f1991 6import operator
2b25cb5d
PH
7import re
8
8f53dc44 9from .utils import (
10 NO_DEFAULT,
11 ExtractorError,
12 js_to_json,
13 remove_quotes,
14 truncate_string,
15 unified_timestamp,
16 write_string,
17)
2b25cb5d 18
230d5c82 19_NAME_RE = r'[a-zA-Z_$][\w$]*'
49b4ceae 20
21# Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
8f53dc44 22_OPERATORS = { # None => Defined in JSInterpreter._operator
23 '?': None,
24
25 '||': None,
26 '&&': None,
f6ca640b 27 '&': lambda a, b: (a or 0) & (b or 0),
28 '|': lambda a, b: (a or 0) | (b or 0),
29 '^': lambda a, b: (a or 0) ^ (b or 0),
8f53dc44 30
49b4ceae 31 '===': operator.is_,
32 '!==': operator.is_not,
33 '==': operator.eq,
34 '!=': operator.ne,
8f53dc44 35
6d3e7424 36 '<=': lambda a, b: (a or 0) <= (b or 0),
37 '>=': lambda a, b: (a or 0) >= (b or 0),
38 '<': lambda a, b: (a or 0) < (b or 0),
39 '>': lambda a, b: (a or 0) > (b or 0),
8f53dc44 40
49b4ceae 41 '>>': operator.rshift,
42 '<<': operator.lshift,
43
6d3e7424 44 '+': lambda a, b: (a or 0) + (b or 0),
45 '-': lambda a, b: (a or 0) - (b or 0),
8f53dc44 46
6d3e7424 47 '*': lambda a, b: (a or 0) * (b or 0),
f6ca640b 48 '/': lambda a, b: (a or 0) / b if b else float('NaN'),
49 '%': lambda a, b: (a or 0) % b if b else float('NaN'),
49b4ceae 50
51 '**': operator.pow,
230d5c82 52}
9e3f1991 53
49b4ceae 54_COMP_OPERATORS = {'===', '!==', '==', '!=', '<=', '>=', '<', '>'}
55
06dfe0a0 56_MATCHING_PARENS = dict(zip('({[', ')}]'))
f6ca640b 57_QUOTES = '\'"/'
06dfe0a0 58
2b25cb5d 59
8f53dc44 60def _ternary(cndn, if_true=True, if_false=False):
61 """Simulate JS's ternary operator (cndn?if_true:if_false)"""
62 if cndn in (False, None, 0, ''):
63 return if_false
64 with contextlib.suppress(TypeError):
65 if math.isnan(cndn): # NB: NaN cannot be checked by membership
66 return if_false
67 return if_true
68
69
404f611f 70class JS_Break(ExtractorError):
71 def __init__(self):
72 ExtractorError.__init__(self, 'Invalid break')
73
74
75class JS_Continue(ExtractorError):
76 def __init__(self):
77 ExtractorError.__init__(self, 'Invalid continue')
78
79
f6ca640b 80class JS_Throw(ExtractorError):
81 def __init__(self, e):
82 self.error = e
83 ExtractorError.__init__(self, f'Uncaught exception {e}')
84
85
19a03940 86class LocalNameSpace(collections.ChainMap):
404f611f 87 def __setitem__(self, key, value):
19a03940 88 for scope in self.maps:
404f611f 89 if key in scope:
90 scope[key] = value
19a03940 91 return
92 self.maps[0][key] = value
404f611f 93
94 def __delitem__(self, key):
95 raise NotImplementedError('Deleting is not supported')
96
404f611f 97
8f53dc44 98class Debugger:
99 import sys
49b4ceae 100 ENABLED = False and 'pytest' in sys.modules
8f53dc44 101
102 @staticmethod
103 def write(*args, level=100):
104 write_string(f'[debug] JS: {" " * (100 - level)}'
105 f'{" ".join(truncate_string(str(x), 50, 50) for x in args)}\n')
106
107 @classmethod
108 def wrap_interpreter(cls, f):
109 def interpret_statement(self, stmt, local_vars, allow_recursion, *args, **kwargs):
110 if cls.ENABLED and stmt.strip():
111 cls.write(stmt, level=allow_recursion)
112 ret, should_ret = f(self, stmt, local_vars, allow_recursion, *args, **kwargs)
113 if cls.ENABLED and stmt.strip():
114 cls.write(['->', '=>'][should_ret], repr(ret), '<-|', stmt, level=allow_recursion)
115 return ret, should_ret
116 return interpret_statement
117
118
86e5f3ed 119class JSInterpreter:
230d5c82 120 __named_object_counter = 0
121
9e3f1991 122 def __init__(self, code, objects=None):
230d5c82 123 self.code, self._functions = code, {}
124 self._objects = {} if objects is None else objects
404f611f 125
a1c5bd82 126 class Exception(ExtractorError):
127 def __init__(self, msg, expr=None, *args, **kwargs):
128 if expr is not None:
8f53dc44 129 msg = f'{msg.rstrip()} in: {truncate_string(expr, 50, 50)}'
a1c5bd82 130 super().__init__(msg, *args, **kwargs)
131
404f611f 132 def _named_object(self, namespace, obj):
133 self.__named_object_counter += 1
134 name = f'__yt_dlp_jsinterp_obj{self.__named_object_counter}'
135 namespace[name] = obj
136 return name
137
138 @staticmethod
e75bb0d6 139 def _separate(expr, delim=',', max_split=None):
f6ca640b 140 OP_CHARS = '+-*/%&|^=<>!,;'
404f611f 141 if not expr:
142 return
06dfe0a0 143 counters = {k: 0 for k in _MATCHING_PARENS.values()}
144 start, splits, pos, delim_len = 0, 0, 0, len(delim) - 1
f6ca640b 145 in_quote, escaping, after_op, in_regex_char_group = None, False, True, False
404f611f 146 for idx, char in enumerate(expr):
8f53dc44 147 if not in_quote and char in _MATCHING_PARENS:
06dfe0a0 148 counters[_MATCHING_PARENS[char]] += 1
8f53dc44 149 elif not in_quote and char in counters:
06dfe0a0 150 counters[char] -= 1
64fa820c 151 elif not escaping and char in _QUOTES and in_quote in (char, None):
f6ca640b 152 if in_quote or after_op or char != '/':
153 in_quote = None if in_quote and not in_regex_char_group else char
154 elif in_quote == '/' and char in '[]':
155 in_regex_char_group = char == '['
64fa820c 156 escaping = not escaping and in_quote and char == '\\'
f6ca640b 157 after_op = not in_quote and char in OP_CHARS or (char == ' ' and after_op)
64fa820c 158
159 if char != delim[pos] or any(counters.values()) or in_quote:
404f611f 160 pos = 0
06dfe0a0 161 continue
162 elif pos != delim_len:
163 pos += 1
164 continue
165 yield expr[start: idx - delim_len]
166 start, pos = idx + 1, 0
167 splits += 1
168 if max_split and splits >= max_split:
169 break
404f611f 170 yield expr[start:]
171
230d5c82 172 @classmethod
173 def _separate_at_paren(cls, expr, delim):
174 separated = list(cls._separate(expr, delim, 1))
e75bb0d6 175 if len(separated) < 2:
a1c5bd82 176 raise cls.Exception(f'No terminating paren {delim}', expr)
e75bb0d6 177 return separated[0][1:].strip(), separated[1].strip()
9e3f1991 178
8f53dc44 179 def _operator(self, op, left_val, right_expr, expr, local_vars, allow_recursion):
180 if op in ('||', '&&'):
181 if (op == '&&') ^ _ternary(left_val):
182 return left_val # short circuiting
183 elif op == '?':
184 right_expr = _ternary(left_val, *self._separate(right_expr, ':', 1))
185
186 right_val = self.interpret_expression(right_expr, local_vars, allow_recursion)
187 if not _OPERATORS.get(op):
188 return right_val
189
190 try:
191 return _OPERATORS[op](left_val, right_val)
192 except Exception as e:
193 raise self.Exception(f'Failed to evaluate {left_val!r} {op} {right_val!r}', expr, cause=e)
194
195 def _index(self, obj, idx):
196 if idx == 'length':
197 return len(obj)
198 try:
199 return obj[int(idx)] if isinstance(obj, list) else obj[idx]
200 except Exception as e:
201 raise self.Exception(f'Cannot get index {idx}', repr(obj), cause=e)
202
203 def _dump(self, obj, namespace):
204 try:
205 return json.dumps(obj)
206 except TypeError:
207 return self._named_object(namespace, obj)
208
209 @Debugger.wrap_interpreter
9e3f1991 210 def interpret_statement(self, stmt, local_vars, allow_recursion=100):
2b25cb5d 211 if allow_recursion < 0:
a1c5bd82 212 raise self.Exception('Recursion limit reached')
8f53dc44 213 allow_recursion -= 1
2b25cb5d 214
8f53dc44 215 should_return = False
230d5c82 216 sub_statements = list(self._separate(stmt, ';')) or ['']
8f53dc44 217 expr = stmt = sub_statements.pop().strip()
230d5c82 218
404f611f 219 for sub_stmt in sub_statements:
8f53dc44 220 ret, should_return = self.interpret_statement(sub_stmt, local_vars, allow_recursion)
221 if should_return:
222 return ret, should_return
404f611f 223
f6ca640b 224 m = re.match(r'(?P<var>(?:var|const|let)\s)|return(?:\s+|(?=["\'])|$)|(?P<throw>throw\s+)', stmt)
8f53dc44 225 if m:
226 expr = stmt[len(m.group(0)):].strip()
f6ca640b 227 if m.group('throw'):
228 raise JS_Throw(self.interpret_expression(expr, local_vars, allow_recursion))
8f53dc44 229 should_return = not m.group('var')
230d5c82 230 if not expr:
8f53dc44 231 return None, should_return
232
233 if expr[0] in _QUOTES:
234 inner, outer = self._separate(expr, expr[0], 1)
f6ca640b 235 if expr[0] == '/':
236 inner = inner[1:].replace('"', R'\"')
237 inner = re.compile(json.loads(js_to_json(f'"{inner}"', strict=True)))
238 else:
239 inner = json.loads(js_to_json(f'{inner}{expr[0]}', strict=True))
8f53dc44 240 if not outer:
241 return inner, should_return
242 expr = self._named_object(local_vars, inner) + outer
243
244 if expr.startswith('new '):
245 obj = expr[4:]
246 if obj.startswith('Date('):
247 left, right = self._separate_at_paren(obj[4:], ')')
49b4ceae 248 expr = unified_timestamp(
249 self.interpret_expression(left, local_vars, allow_recursion), False)
8f53dc44 250 if not expr:
251 raise self.Exception(f'Failed to parse date {left!r}', expr)
252 expr = self._dump(int(expr * 1000), local_vars) + right
253 else:
254 raise self.Exception(f'Unsupported object {obj}', expr)
9e3f1991 255
49b4ceae 256 if expr.startswith('void '):
257 left = self.interpret_expression(expr[5:], local_vars, allow_recursion)
258 return None, should_return
259
404f611f 260 if expr.startswith('{'):
e75bb0d6 261 inner, outer = self._separate_at_paren(expr, '}')
8f53dc44 262 inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
404f611f 263 if not outer or should_abort:
8f53dc44 264 return inner, should_abort or should_return
404f611f 265 else:
8f53dc44 266 expr = self._dump(inner, local_vars) + outer
404f611f 267
9e3f1991 268 if expr.startswith('('):
e75bb0d6 269 inner, outer = self._separate_at_paren(expr, ')')
8f53dc44 270 inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
271 if not outer or should_abort:
272 return inner, should_abort or should_return
404f611f 273 else:
8f53dc44 274 expr = self._dump(inner, local_vars) + outer
404f611f 275
276 if expr.startswith('['):
e75bb0d6 277 inner, outer = self._separate_at_paren(expr, ']')
404f611f 278 name = self._named_object(local_vars, [
279 self.interpret_expression(item, local_vars, allow_recursion)
e75bb0d6 280 for item in self._separate(inner)])
404f611f 281 expr = name + outer
282
f6ca640b 283 m = re.match(rf'''(?x)
284 (?P<try>try|finally)\s*|
285 (?P<catch>catch\s*(?P<err>\(\s*{_NAME_RE}\s*\)))|
286 (?P<switch>switch)\s*\(|
287 (?P<for>for)\s*\(|''', expr)
230d5c82 288 if m and m.group('try'):
404f611f 289 if expr[m.end()] == '{':
e75bb0d6 290 try_expr, expr = self._separate_at_paren(expr[m.end():], '}')
404f611f 291 else:
292 try_expr, expr = expr[m.end() - 1:], ''
f6ca640b 293 try:
294 ret, should_abort = self.interpret_statement(try_expr, local_vars, allow_recursion)
295 if should_abort:
296 return ret, True
297 except JS_Throw as e:
298 local_vars['__ytdlp_exception__'] = e.error
299 except Exception as e:
300 # XXX: This works for now, but makes debugging future issues very hard
301 local_vars['__ytdlp_exception__'] = e
8f53dc44 302 ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
303 return ret, should_abort or should_return
404f611f 304
230d5c82 305 elif m and m.group('catch'):
f6ca640b 306 catch_expr, expr = self._separate_at_paren(expr[m.end():], '}')
307 if '__ytdlp_exception__' in local_vars:
308 catch_vars = local_vars.new_child({m.group('err'): local_vars.pop('__ytdlp_exception__')})
309 ret, should_abort = self.interpret_statement(catch_expr, catch_vars, allow_recursion)
310 if should_abort:
311 return ret, True
312
8f53dc44 313 ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
314 return ret, should_abort or should_return
404f611f 315
230d5c82 316 elif m and m.group('for'):
e75bb0d6 317 constructor, remaining = self._separate_at_paren(expr[m.end() - 1:], ')')
404f611f 318 if remaining.startswith('{'):
e75bb0d6 319 body, expr = self._separate_at_paren(remaining, '}')
404f611f 320 else:
230d5c82 321 switch_m = re.match(r'switch\s*\(', remaining) # FIXME
322 if switch_m:
323 switch_val, remaining = self._separate_at_paren(remaining[switch_m.end() - 1:], ')')
e75bb0d6 324 body, expr = self._separate_at_paren(remaining, '}')
404f611f 325 body = 'switch(%s){%s}' % (switch_val, body)
9e3f1991 326 else:
404f611f 327 body, expr = remaining, ''
e75bb0d6 328 start, cndn, increment = self._separate(constructor, ';')
8f53dc44 329 self.interpret_expression(start, local_vars, allow_recursion)
404f611f 330 while True:
8f53dc44 331 if not _ternary(self.interpret_expression(cndn, local_vars, allow_recursion)):
404f611f 332 break
333 try:
8f53dc44 334 ret, should_abort = self.interpret_statement(body, local_vars, allow_recursion)
404f611f 335 if should_abort:
8f53dc44 336 return ret, True
404f611f 337 except JS_Break:
338 break
339 except JS_Continue:
340 pass
8f53dc44 341 self.interpret_expression(increment, local_vars, allow_recursion)
342 ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
343 return ret, should_abort or should_return
404f611f 344
230d5c82 345 elif m and m.group('switch'):
e75bb0d6 346 switch_val, remaining = self._separate_at_paren(expr[m.end() - 1:], ')')
404f611f 347 switch_val = self.interpret_expression(switch_val, local_vars, allow_recursion)
e75bb0d6 348 body, expr = self._separate_at_paren(remaining, '}')
a1fc7ca0 349 items = body.replace('default:', 'case default:').split('case ')[1:]
350 for default in (False, True):
351 matched = False
352 for item in items:
86e5f3ed 353 case, stmt = (i.strip() for i in self._separate(item, ':', 1))
a1fc7ca0 354 if default:
355 matched = matched or case == 'default'
356 elif not matched:
49b4ceae 357 matched = (case != 'default'
358 and switch_val == self.interpret_expression(case, local_vars, allow_recursion))
a1fc7ca0 359 if not matched:
360 continue
404f611f 361 try:
8f53dc44 362 ret, should_abort = self.interpret_statement(stmt, local_vars, allow_recursion)
404f611f 363 if should_abort:
364 return ret
365 except JS_Break:
9e3f1991 366 break
a1fc7ca0 367 if matched:
368 break
8f53dc44 369 ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
370 return ret, should_abort or should_return
404f611f 371
e75bb0d6
U
372 # Comma separated statements
373 sub_expressions = list(self._separate(expr))
6d3e7424 374 if len(sub_expressions) > 1:
375 for sub_expr in sub_expressions:
376 ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion)
377 if should_abort:
378 return ret, True
379 return ret, False
404f611f 380
381 for m in re.finditer(rf'''(?x)
382 (?P<pre_sign>\+\+|--)(?P<var1>{_NAME_RE})|
383 (?P<var2>{_NAME_RE})(?P<post_sign>\+\+|--)''', expr):
384 var = m.group('var1') or m.group('var2')
385 start, end = m.span()
386 sign = m.group('pre_sign') or m.group('post_sign')
387 ret = local_vars[var]
388 local_vars[var] += 1 if sign[0] == '+' else -1
389 if m.group('pre_sign'):
390 ret = local_vars[var]
8f53dc44 391 expr = expr[:start] + self._dump(ret, local_vars) + expr[end:]
9e3f1991 392
230d5c82 393 if not expr:
8f53dc44 394 return None, should_return
9e3f1991 395
230d5c82 396 m = re.match(fr'''(?x)
397 (?P<assign>
398 (?P<out>{_NAME_RE})(?:\[(?P<index>[^\]]+?)\])?\s*
49b4ceae 399 (?P<op>{"|".join(map(re.escape, set(_OPERATORS) - _COMP_OPERATORS))})?
230d5c82 400 =(?P<expr>.*)$
401 )|(?P<return>
8f53dc44 402 (?!if|return|true|false|null|undefined)(?P<name>{_NAME_RE})$
230d5c82 403 )|(?P<indexing>
404 (?P<in>{_NAME_RE})\[(?P<idx>.+)\]$
405 )|(?P<attribute>
406 (?P<var>{_NAME_RE})(?:\.(?P<member>[^(]+)|\[(?P<member2>[^\]]+)\])\s*
407 )|(?P<function>
8f53dc44 408 (?P<fname>{_NAME_RE})\((?P<args>.*)\)$
230d5c82 409 )''', expr)
410 if m and m.group('assign'):
230d5c82 411 left_val = local_vars.get(m.group('out'))
412
413 if not m.group('index'):
8f53dc44 414 local_vars[m.group('out')] = self._operator(
415 m.group('op'), left_val, m.group('expr'), expr, local_vars, allow_recursion)
416 return local_vars[m.group('out')], should_return
230d5c82 417 elif left_val is None:
a1c5bd82 418 raise self.Exception(f'Cannot index undefined variable {m.group("out")}', expr)
230d5c82 419
420 idx = self.interpret_expression(m.group('index'), local_vars, allow_recursion)
8f53dc44 421 if not isinstance(idx, (int, float)):
a1c5bd82 422 raise self.Exception(f'List index {idx} must be integer', expr)
8f53dc44 423 idx = int(idx)
424 left_val[idx] = self._operator(
f6ca640b 425 m.group('op'), self._index(left_val, idx), m.group('expr'), expr, local_vars, allow_recursion)
8f53dc44 426 return left_val[idx], should_return
9e3f1991 427
230d5c82 428 elif expr.isdigit():
8f53dc44 429 return int(expr), should_return
2b25cb5d 430
230d5c82 431 elif expr == 'break':
404f611f 432 raise JS_Break()
433 elif expr == 'continue':
434 raise JS_Continue()
435
230d5c82 436 elif m and m.group('return'):
8f53dc44 437 return local_vars[m.group('name')], should_return
2b25cb5d 438
19a03940 439 with contextlib.suppress(ValueError):
8f53dc44 440 return json.loads(js_to_json(expr, strict=True)), should_return
825abb81 441
230d5c82 442 if m and m.group('indexing'):
7769f837 443 val = local_vars[m.group('in')]
404f611f 444 idx = self.interpret_expression(m.group('idx'), local_vars, allow_recursion)
8f53dc44 445 return self._index(val, idx), should_return
7769f837 446
8f53dc44 447 for op in _OPERATORS:
e75bb0d6 448 separated = list(self._separate(expr, op))
8f53dc44 449 right_expr = separated.pop()
49b4ceae 450 while op in '<>*-' and len(separated) > 1 and not separated[-1].strip():
8f53dc44 451 separated.pop()
49b4ceae 452 right_expr = f'{op}{right_expr}'
453 if op != '-':
454 right_expr = f'{separated.pop()}{op}{right_expr}'
455 if not separated:
456 continue
8f53dc44 457 left_val = self.interpret_expression(op.join(separated), local_vars, allow_recursion)
6d3e7424 458 return self._operator(op, left_val, right_expr, expr, local_vars, allow_recursion), should_return
404f611f 459
230d5c82 460 if m and m.group('attribute'):
825abb81 461 variable = m.group('var')
8f53dc44 462 member = m.group('member')
463 if not member:
464 member = self.interpret_expression(m.group('member2'), local_vars, allow_recursion)
404f611f 465 arg_str = expr[m.end():]
466 if arg_str.startswith('('):
e75bb0d6 467 arg_str, remaining = self._separate_at_paren(arg_str, ')')
825abb81 468 else:
404f611f 469 arg_str, remaining = None, arg_str
470
471 def assertion(cndn, msg):
472 """ assert, but without risk of getting optimized out """
473 if not cndn:
a1c5bd82 474 raise self.Exception(f'{member} {msg}', expr)
404f611f 475
476 def eval_method():
8f53dc44 477 if (variable, member) == ('console', 'debug'):
478 if Debugger.ENABLED:
479 Debugger.write(self.interpret_expression(f'[{arg_str}]', local_vars, allow_recursion))
480 return
481
482 types = {
483 'String': str,
484 'Math': float,
485 }
486 obj = local_vars.get(variable, types.get(variable, NO_DEFAULT))
487 if obj is NO_DEFAULT:
404f611f 488 if variable not in self._objects:
489 self._objects[variable] = self.extract_object(variable)
490 obj = self._objects[variable]
491
230d5c82 492 # Member access
404f611f 493 if arg_str is None:
8f53dc44 494 return self._index(obj, member)
404f611f 495
496 # Function call
497 argvals = [
825abb81 498 self.interpret_expression(v, local_vars, allow_recursion)
e75bb0d6 499 for v in self._separate(arg_str)]
404f611f 500
501 if obj == str:
502 if member == 'fromCharCode':
503 assertion(argvals, 'takes one or more arguments')
504 return ''.join(map(chr, argvals))
8f53dc44 505 raise self.Exception(f'Unsupported String method {member}', expr)
506 elif obj == float:
507 if member == 'pow':
508 assertion(len(argvals) == 2, 'takes two arguments')
509 return argvals[0] ** argvals[1]
510 raise self.Exception(f'Unsupported Math method {member}', expr)
404f611f 511
512 if member == 'split':
513 assertion(argvals, 'takes one or more arguments')
8f53dc44 514 assertion(len(argvals) == 1, 'with limit argument is not implemented')
515 return obj.split(argvals[0]) if argvals[0] else list(obj)
404f611f 516 elif member == 'join':
517 assertion(isinstance(obj, list), 'must be applied on a list')
518 assertion(len(argvals) == 1, 'takes exactly one argument')
519 return argvals[0].join(obj)
520 elif member == 'reverse':
521 assertion(not argvals, 'does not take any arguments')
522 obj.reverse()
523 return obj
524 elif member == 'slice':
525 assertion(isinstance(obj, list), 'must be applied on a list')
526 assertion(len(argvals) == 1, 'takes exactly one argument')
527 return obj[argvals[0]:]
528 elif member == 'splice':
529 assertion(isinstance(obj, list), 'must be applied on a list')
530 assertion(argvals, 'takes one or more arguments')
57dbe807 531 index, howMany = map(int, (argvals + [len(obj)])[:2])
404f611f 532 if index < 0:
533 index += len(obj)
534 add_items = argvals[2:]
535 res = []
536 for i in range(index, min(index + howMany, len(obj))):
537 res.append(obj.pop(index))
538 for i, item in enumerate(add_items):
539 obj.insert(index + i, item)
540 return res
541 elif member == 'unshift':
542 assertion(isinstance(obj, list), 'must be applied on a list')
543 assertion(argvals, 'takes one or more arguments')
544 for item in reversed(argvals):
545 obj.insert(0, item)
546 return obj
547 elif member == 'pop':
548 assertion(isinstance(obj, list), 'must be applied on a list')
549 assertion(not argvals, 'does not take any arguments')
550 if not obj:
551 return
552 return obj.pop()
553 elif member == 'push':
554 assertion(argvals, 'takes one or more arguments')
555 obj.extend(argvals)
556 return obj
557 elif member == 'forEach':
558 assertion(argvals, 'takes one or more arguments')
559 assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
560 f, this = (argvals + [''])[:2]
8f53dc44 561 return [f((item, idx, obj), {'this': this}, allow_recursion) for idx, item in enumerate(obj)]
404f611f 562 elif member == 'indexOf':
563 assertion(argvals, 'takes one or more arguments')
564 assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
565 idx, start = (argvals + [0])[:2]
566 try:
567 return obj.index(idx, start)
568 except ValueError:
569 return -1
570
8f53dc44 571 idx = int(member) if isinstance(obj, list) else member
572 return obj[idx](argvals, allow_recursion=allow_recursion)
404f611f 573
574 if remaining:
8f53dc44 575 ret, should_abort = self.interpret_statement(
404f611f 576 self._named_object(local_vars, eval_method()) + remaining,
577 local_vars, allow_recursion)
8f53dc44 578 return ret, should_return or should_abort
404f611f 579 else:
8f53dc44 580 return eval_method(), should_return
2b25cb5d 581
230d5c82 582 elif m and m.group('function'):
583 fname = m.group('fname')
8f53dc44 584 argvals = [self.interpret_expression(v, local_vars, allow_recursion)
585 for v in self._separate(m.group('args'))]
404f611f 586 if fname in local_vars:
8f53dc44 587 return local_vars[fname](argvals, allow_recursion=allow_recursion), should_return
404f611f 588 elif fname not in self._functions:
1f749b66 589 self._functions[fname] = self.extract_function(fname)
8f53dc44 590 return self._functions[fname](argvals, allow_recursion=allow_recursion), should_return
591
592 raise self.Exception(
593 f'Unsupported JS expression {truncate_string(expr, 20, 20) if expr != stmt else ""}', stmt)
9e3f1991 594
8f53dc44 595 def interpret_expression(self, expr, local_vars, allow_recursion):
596 ret, should_return = self.interpret_statement(expr, local_vars, allow_recursion)
597 if should_return:
598 raise self.Exception('Cannot return from an expression', expr)
599 return ret
2b25cb5d 600
ad25aee2 601 def extract_object(self, objname):
7769f837 602 _FUNC_NAME_RE = r'''(?:[a-zA-Z$0-9]+|"[a-zA-Z$0-9]+"|'[a-zA-Z$0-9]+')'''
ad25aee2
JMF
603 obj = {}
604 obj_m = re.search(
0e2d626d
S
605 r'''(?x)
606 (?<!this\.)%s\s*=\s*{\s*
607 (?P<fields>(%s\s*:\s*function\s*\(.*?\)\s*{.*?}(?:,\s*)?)*)
608 }\s*;
609 ''' % (re.escape(objname), _FUNC_NAME_RE),
ad25aee2 610 self.code)
8f53dc44 611 if not obj_m:
612 raise self.Exception(f'Could not find object {objname}')
ad25aee2
JMF
613 fields = obj_m.group('fields')
614 # Currently, it only supports function definitions
615 fields_m = re.finditer(
0e2d626d 616 r'''(?x)
49b4ceae 617 (?P<key>%s)\s*:\s*function\s*\((?P<args>(?:%s|,)*)\){(?P<code>[^}]+)}
618 ''' % (_FUNC_NAME_RE, _NAME_RE),
ad25aee2
JMF
619 fields)
620 for f in fields_m:
621 argnames = f.group('args').split(',')
7769f837 622 obj[remove_quotes(f.group('key'))] = self.build_function(argnames, f.group('code'))
ad25aee2
JMF
623
624 return obj
625
404f611f 626 def extract_function_code(self, funcname):
627 """ @returns argnames, code """
2b25cb5d 628 func_m = re.search(
8f53dc44 629 r'''(?xs)
230d5c82 630 (?:
631 function\s+%(name)s|
632 [{;,]\s*%(name)s\s*=\s*function|
49b4ceae 633 (?:var|const|let)\s+%(name)s\s*=\s*function
230d5c82 634 )\s*
9e3f1991 635 \((?P<args>[^)]*)\)\s*
8f53dc44 636 (?P<code>{.+})''' % {'name': re.escape(funcname)},
2b25cb5d 637 self.code)
8f53dc44 638 code, _ = self._separate_at_paren(func_m.group('code'), '}')
77ffa957 639 if func_m is None:
a1c5bd82 640 raise self.Exception(f'Could not find JS function "{funcname}"')
8f53dc44 641 return [x.strip() for x in func_m.group('args').split(',')], code
2b25cb5d 642
404f611f 643 def extract_function(self, funcname):
644 return self.extract_function_from_code(*self.extract_function_code(funcname))
645
646 def extract_function_from_code(self, argnames, code, *global_stack):
647 local_vars = {}
648 while True:
649 mobj = re.search(r'function\((?P<args>[^)]*)\)\s*{', code)
650 if mobj is None:
651 break
652 start, body_start = mobj.span()
e75bb0d6 653 body, remaining = self._separate_at_paren(code[body_start - 1:], '}')
230d5c82 654 name = self._named_object(local_vars, self.extract_function_from_code(
655 [x.strip() for x in mobj.group('args').split(',')],
656 body, local_vars, *global_stack))
404f611f 657 code = code[:start] + name + remaining
658 return self.build_function(argnames, code, local_vars, *global_stack)
ad25aee2 659
9e3f1991 660 def call_function(self, funcname, *args):
404f611f 661 return self.extract_function(funcname)(args)
662
663 def build_function(self, argnames, code, *global_stack):
664 global_stack = list(global_stack) or [{}]
8f53dc44 665 argnames = tuple(argnames)
404f611f 666
8f53dc44 667 def resf(args, kwargs={}, allow_recursion=100):
49b4ceae 668 global_stack[0].update(itertools.zip_longest(argnames, args, fillvalue=None))
669 global_stack[0].update(kwargs)
19a03940 670 var_stack = LocalNameSpace(*global_stack)
8f53dc44 671 ret, should_abort = self.interpret_statement(code.replace('\n', ''), var_stack, allow_recursion - 1)
672 if should_abort:
673 return ret
2b25cb5d 674 return resf