1 from collections
.abc
import MutableMapping
15 ('>>', operator
.rshift
),
16 ('<<', operator
.lshift
),
20 ('/', operator
.truediv
),
23 _ASSIGN_OPERATORS
= [(op
+ '=', opfunc
) for op
, opfunc
in _OPERATORS
]
24 _ASSIGN_OPERATORS
.append(('=', (lambda cur
, right
: right
)))
26 _NAME_RE
= r
'[a-zA-Z_$][a-zA-Z_$0-9]*'
29 class JS_Break(ExtractorError
):
31 ExtractorError
.__init
__(self
, 'Invalid break')
34 class JS_Continue(ExtractorError
):
36 ExtractorError
.__init
__(self
, 'Invalid continue')
39 class LocalNameSpace(MutableMapping
):
40 def __init__(self
, *stack
):
41 self
.stack
= tuple(stack
)
43 def __getitem__(self
, key
):
44 for scope
in self
.stack
:
49 def __setitem__(self
, key
, value
):
50 for scope
in self
.stack
:
55 self
.stack
[0][key
] = value
58 def __delitem__(self
, key
):
59 raise NotImplementedError('Deleting is not supported')
62 for scope
in self
.stack
:
65 def __len__(self
, key
):
66 return len(iter(self
))
69 return f
'LocalNameSpace{self.stack}'
72 class JSInterpreter(object):
73 def __init__(self
, code
, objects
=None):
78 self
._objects
= objects
79 self
.__named
_object
_counter
= 0
81 def _named_object(self
, namespace
, obj
):
82 self
.__named
_object
_counter
+= 1
83 name
= f
'__yt_dlp_jsinterp_obj{self.__named_object_counter}'
88 def _seperate(expr
, delim
=',', max_split
=None):
91 parens
= {'(': 0, '{': 0, '[': 0, ']': 0, '}
': 0, ')': 0}
92 start, splits, pos, max_pos = 0, 0, 0, len(delim) - 1
93 for idx, char in enumerate(expr):
96 is_in_parens = (parens['['] - parens[']']
97 or parens['('] - parens[')']
98 or parens['{'] - parens['}
'])
99 if char == delim[pos] and not is_in_parens:
102 yield expr[start: idx - max_pos]
105 if max_split and splits >= max_split:
114 def _seperate_at_paren(expr, delim):
115 seperated = list(JSInterpreter._seperate(expr, delim, 1))
116 if len(seperated) < 2:
117 raise ExtractorError(f'No terminating paren {delim}
in {expr}
')
118 return seperated[0][1:].strip(), seperated[1].strip()
120 def interpret_statement(self, stmt, local_vars, allow_recursion=100):
121 if allow_recursion < 0:
122 raise ExtractorError('Recursion limit reached
')
124 sub_statements = list(self._seperate(stmt, ';'))
125 stmt = (sub_statements or ['']).pop()
126 for sub_stmt in sub_statements:
127 ret, should_abort = self.interpret_statement(sub_stmt, local_vars, allow_recursion - 1)
133 stmt_m = re.match(r'var\s
', stmt)
135 expr = stmt[len(stmt_m.group(0)):]
137 return_m = re.match(r'return(?
:\s
+|$
)', stmt)
139 expr = stmt[len(return_m.group(0)):]
142 # Try interpreting it as an expression
145 v = self.interpret_expression(expr, local_vars, allow_recursion)
146 return v, should_abort
148 def interpret_expression(self, expr, local_vars, allow_recursion):
150 if expr == '': # Empty expression
153 if expr.startswith('{'):
154 inner, outer = self._seperate_at_paren(expr, '}')
155 inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion - 1)
156 if not outer or should_abort:
159 expr = json.dumps(inner) + outer
161 if expr.startswith('('):
162 inner, outer = self._seperate_at_paren(expr, ')')
163 inner = self.interpret_expression(inner, local_vars, allow_recursion)
167 expr = json.dumps(inner) + outer
169 if expr.startswith('['):
170 inner, outer = self._seperate_at_paren(expr, ']')
171 name = self._named_object(local_vars, [
172 self.interpret_expression(item, local_vars, allow_recursion)
173 for item in self._seperate(inner)])
176 m = re.match(r'try\s
*', expr)
178 if expr[m.end()] == '{':
179 try_expr, expr = self._seperate_at_paren(expr[m.end():], '}')
181 try_expr, expr = expr[m.end() - 1:], ''
182 ret, should_abort = self.interpret_statement(try_expr, local_vars, allow_recursion - 1)
185 return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
187 m = re.match(r'catch\s
*\
(', expr)
189 # We ignore the catch block
190 _, expr = self._seperate_at_paren(expr, '}')
191 return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
193 m = re.match(r'for\s
*\
(', expr)
195 constructor, remaining = self._seperate_at_paren(expr[m.end() - 1:], ')')
196 if remaining.startswith('{'):
197 body, expr = self._seperate_at_paren(remaining, '}')
199 m = re.match(r'switch\s
*\
(', remaining) # FIXME
201 switch_val, remaining = self._seperate_at_paren(remaining[m.end() - 1:], ')')
202 body, expr = self._seperate_at_paren(remaining, '}')
203 body = 'switch(%s){%s}
' % (switch_val, body)
205 body, expr = remaining, ''
206 start, cndn, increment = self._seperate(constructor, ';')
207 if self.interpret_statement(start, local_vars, allow_recursion - 1)[1]:
208 raise ExtractorError(
209 f'Premature
return in the initialization of a
for loop
in {constructor!r}
')
211 if not self.interpret_expression(cndn, local_vars, allow_recursion):
214 ret, should_abort = self.interpret_statement(body, local_vars, allow_recursion - 1)
221 if self.interpret_statement(increment, local_vars, allow_recursion - 1)[1]:
222 raise ExtractorError(
223 f'Premature
return in the initialization of a
for loop
in {constructor!r}
')
224 return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
226 m = re.match(r'switch\s
*\
(', expr)
228 switch_val, remaining = self._seperate_at_paren(expr[m.end() - 1:], ')')
229 switch_val = self.interpret_expression(switch_val, local_vars, allow_recursion)
230 body, expr = self._seperate_at_paren(remaining, '}')
231 body, default = body.split('default
:') if 'default
:' in body else (body, None)
232 items = body.split('case
')[1:]
234 items.append(f'default
:{default}
')
237 case, stmt = [i.strip() for i in self._seperate(item, ':', 1)]
238 matched = matched or case == 'default
' or switch_val == self.interpret_expression(case, local_vars, allow_recursion)
241 ret, should_abort = self.interpret_statement(stmt, local_vars, allow_recursion - 1)
246 return self.interpret_statement(expr, local_vars, allow_recursion - 1)[0]
248 # Comma seperated statements
249 sub_expressions = list(self._seperate(expr))
250 expr = sub_expressions.pop().strip() if sub_expressions else ''
251 for sub_expr in sub_expressions:
252 self.interpret_expression(sub_expr, local_vars, allow_recursion)
254 for m in re.finditer(rf'''(?x)
255 (?P<pre_sign>\+\+|--)(?P<var1>{_NAME_RE})|
256 (?P<var2>{_NAME_RE})(?P<post_sign>\+\+|--)''', expr):
257 var = m.group('var1
') or m.group('var2
')
258 start, end = m.span()
259 sign = m.group('pre_sign
') or m.group('post_sign
')
260 ret = local_vars[var]
261 local_vars[var] += 1 if sign[0] == '+' else -1
262 if m.group('pre_sign
'):
263 ret = local_vars[var]
264 expr = expr[:start] + json.dumps(ret) + expr[end:]
266 for op, opfunc in _ASSIGN_OPERATORS:
267 m = re.match(r'''(?x)
268 (?P<out>%s)(?:\[(?P<index>[^\]]+?)\])?
270 (?P<expr>.*)$''' % (_NAME_RE, re.escape(op)), expr)
273 right_val = self.interpret_expression(m.group('expr
'), local_vars, allow_recursion)
275 if m.groupdict().get('index
'):
276 lvar = local_vars[m.group('out
')]
277 idx = self.interpret_expression(m.group('index
'), local_vars, allow_recursion)
278 if not isinstance(idx, int):
279 raise ExtractorError(f'List indices must be integers
: {idx}
')
281 val = opfunc(cur, right_val)
285 cur = local_vars.get(m.group('out
'))
286 val = opfunc(cur, right_val)
287 local_vars[m.group('out
')] = val
295 elif expr == 'continue':
299 r'(?
!if|
return|true|false|null
)(?P
<name
>%s)$
' % _NAME_RE,
302 return local_vars[var_m.group('name
')]
305 return json.loads(expr)
310 r'(?P
<in>%s)\
[(?P
<idx
>.+)\
]$
' % _NAME_RE, expr)
312 val = local_vars[m.group('in')]
313 idx = self.interpret_expression(m.group('idx
'), local_vars, allow_recursion)
316 for op, opfunc in _OPERATORS:
317 seperated = list(self._seperate(expr, op))
318 if len(seperated) < 2:
320 right_val = seperated.pop()
321 left_val = op.join(seperated)
322 left_val, should_abort = self.interpret_statement(
323 left_val, local_vars, allow_recursion - 1)
325 raise ExtractorError(f'Premature left
-side
return of {op}
in {expr!r}
')
326 right_val, should_abort = self.interpret_statement(
327 right_val, local_vars, allow_recursion - 1)
329 raise ExtractorError(f'Premature right
-side
return of {op}
in {expr!r}
')
330 return opfunc(left_val or 0, right_val)
333 r'(?P
<var
>%s)(?
:\
.(?P
<member
>[^
(]+)|\
[(?P
<member2
>[^
]]+)\
])\s
*' % _NAME_RE,
336 variable = m.group('var
')
337 member = remove_quotes(m.group('member
') or m.group('member2
'))
338 arg_str = expr[m.end():]
339 if arg_str.startswith('('):
340 arg_str, remaining = self._seperate_at_paren(arg_str, ')')
342 arg_str, remaining = None, arg_str
344 def assertion(cndn, msg):
345 """ assert, but without risk of getting optimized out """
347 raise ExtractorError(f'{member} {msg}
: {expr}
')
351 if variable == 'String
':
353 elif variable in local_vars:
354 obj = local_vars[variable]
356 if variable not in self._objects:
357 self._objects[variable] = self.extract_object(variable)
358 obj = self._objects[variable]
362 if member == 'length
':
368 self.interpret_expression(v, local_vars, allow_recursion)
369 for v in self._seperate(arg_str)]
372 if member == 'fromCharCode
':
373 assertion(argvals, 'takes one
or more arguments
')
374 return ''.join(map(chr, argvals))
375 raise ExtractorError(f'Unsupported string method {member}
')
377 if member == 'split
':
378 assertion(argvals, 'takes one
or more arguments
')
379 assertion(argvals == [''], 'with arguments
is not implemented
')
381 elif member == 'join
':
382 assertion(isinstance(obj, list), 'must be applied on a
list')
383 assertion(len(argvals) == 1, 'takes exactly one argument
')
384 return argvals[0].join(obj)
385 elif member == 'reverse
':
386 assertion(not argvals, 'does
not take any arguments
')
389 elif member == 'slice':
390 assertion(isinstance(obj, list), 'must be applied on a
list')
391 assertion(len(argvals) == 1, 'takes exactly one argument
')
392 return obj[argvals[0]:]
393 elif member == 'splice
':
394 assertion(isinstance(obj, list), 'must be applied on a
list')
395 assertion(argvals, 'takes one
or more arguments
')
396 index, howMany = (argvals + [len(obj)])[:2]
399 add_items = argvals[2:]
401 for i in range(index, min(index + howMany, len(obj))):
402 res.append(obj.pop(index))
403 for i, item in enumerate(add_items):
404 obj.insert(index + i, item)
406 elif member == 'unshift
':
407 assertion(isinstance(obj, list), 'must be applied on a
list')
408 assertion(argvals, 'takes one
or more arguments
')
409 for item in reversed(argvals):
412 elif member == 'pop
':
413 assertion(isinstance(obj, list), 'must be applied on a
list')
414 assertion(not argvals, 'does
not take any arguments
')
418 elif member == 'push
':
419 assertion(argvals, 'takes one
or more arguments
')
422 elif member == 'forEach
':
423 assertion(argvals, 'takes one
or more arguments
')
424 assertion(len(argvals) <= 2, 'takes at
-most
2 arguments
')
425 f, this = (argvals + [''])[:2]
426 return [f((item, idx, obj), this=this) for idx, item in enumerate(obj)]
427 elif member == 'indexOf
':
428 assertion(argvals, 'takes one
or more arguments
')
429 assertion(len(argvals) <= 2, 'takes at
-most
2 arguments
')
430 idx, start = (argvals + [0])[:2]
432 return obj.index(idx, start)
436 if isinstance(obj, list):
438 return obj[member](argvals)
441 return self.interpret_expression(
442 self._named_object(local_vars, eval_method()) + remaining,
443 local_vars, allow_recursion)
447 m = re.match(r'^
(?P
<func
>%s)\
((?P
<args
>[a
-zA
-Z0
-9_$
,]*)\
)$
' % _NAME_RE, expr)
449 fname = m.group('func
')
451 int(v) if v.isdigit() else local_vars[v]
452 for v in self._seperate(m.group('args
'))])
453 if fname in local_vars:
454 return local_vars[fname](argvals)
455 elif fname not in self._functions:
456 self._functions[fname] = self.extract_function(fname)
457 return self._functions[fname](argvals)
460 raise ExtractorError('Unsupported JS expression
%r' % expr)
462 def extract_object(self, objname):
463 _FUNC_NAME_RE = r'''(?:[a-zA-Z$0-9]+|"[a-zA-Z$0-9]+"|'[a
-zA
-Z$
0-9]+')'''
467 (?<!this\.)%s\s*=\s*{\s*
468 (?P<fields>(%s\s*:\s*function\s*\(.*?\)\s*{.*?}(?:,\s*)?)*)
470 ''' % (re.escape(objname), _FUNC_NAME_RE),
472 fields = obj_m.group('fields
')
473 # Currently, it only supports function definitions
474 fields_m = re.finditer(
476 (?P<key>%s)\s*:\s*function\s*\((?P<args>[a-z,]+)\){(?P<code>[^}]+)}
480 argnames = f.group('args
').split(',')
481 obj[remove_quotes(f.group('key
'))] = self.build_function(argnames, f.group('code
'))
485 def extract_function_code(self, funcname):
486 """ @returns argnames, code """
489 (?:function\s+%s|[{;,]\s*%s\s*=\s*function|var\s+%s\s*=\s*function)\s*
490 \((?P<args>[^)]*)\)\s*
491 (?P<code>\{(?:(?!};)[^"]|"([^"]|\\")*")+\})''' % (
492 re.escape(funcname), re.escape(funcname), re.escape(funcname)),
494 code, _ = self._seperate_at_paren(func_m.group('code
'), '}') # refine the match
496 raise ExtractorError('Could
not find JS function
%r' % funcname)
497 return func_m.group('args
').split(','), code
499 def extract_function(self, funcname):
500 return self.extract_function_from_code(*self.extract_function_code(funcname))
502 def extract_function_from_code(self, argnames, code, *global_stack):
505 mobj = re.search(r'function\
((?P
<args
>[^
)]*)\
)\s
*{', code)
508 start, body_start = mobj.span()
509 body, remaining = self._seperate_at_paren(code[body_start - 1:], '}')
510 name = self._named_object(
512 self.extract_function_from_code(
513 [str.strip(x) for x in mobj.group('args
').split(',')],
514 body, local_vars, *global_stack))
515 code = code[:start] + name + remaining
516 return self.build_function(argnames, code, local_vars, *global_stack)
518 def call_function(self, funcname, *args):
519 return self.extract_function(funcname)(args)
521 def build_function(self, argnames, code, *global_stack):
522 global_stack = list(global_stack) or [{}]
523 local_vars = global_stack.pop(0)
525 def resf(args, **kwargs):
527 **dict(zip(argnames, args)),
530 var_stack = LocalNameSpace(local_vars, *global_stack)
531 for stmt in self._seperate(code.replace('\n', ''), ';'):
532 ret, should_abort = self.interpret_statement(stmt, var_stack)