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