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