0
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-30 03:33:02 +00:00

[jsinterp] Add support for quoted names and indexers (closes #13123, closes #13130)

This commit is contained in:
Sergey M․ 2017-05-18 22:18:33 +07:00
parent 650bd94716
commit 7769f83701
No known key found for this signature in database
GPG key ID: 2C393E0F18A9236D

View file

@ -6,6 +6,7 @@
from .utils import ( from .utils import (
ExtractorError, ExtractorError,
remove_quotes,
) )
_OPERATORS = [ _OPERATORS = [
@ -57,7 +58,6 @@ def interpret_statement(self, stmt, local_vars, allow_recursion=100):
def interpret_expression(self, expr, local_vars, allow_recursion): def interpret_expression(self, expr, local_vars, allow_recursion):
expr = expr.strip() expr = expr.strip()
if expr == '': # Empty expression if expr == '': # Empty expression
return None return None
@ -121,11 +121,19 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
pass pass
m = re.match( m = re.match(
r'(?P<var>%s)\.(?P<member>[^(]+)(?:\(+(?P<args>[^()]*)\))?$' % _NAME_RE, r'(?P<in>%s)\[(?P<idx>.+)\]$' % _NAME_RE, expr)
if m:
val = local_vars[m.group('in')]
idx = self.interpret_expression(
m.group('idx'), local_vars, allow_recursion - 1)
return val[idx]
m = re.match(
r'(?P<var>%s)(?:\.(?P<member>[^(]+)|\[(?P<member2>[^]]+)\])\s*(?:\(+(?P<args>[^()]*)\))?$' % _NAME_RE,
expr) expr)
if m: if m:
variable = m.group('var') variable = m.group('var')
member = m.group('member') member = remove_quotes(m.group('member') or m.group('member2'))
arg_str = m.group('args') arg_str = m.group('args')
if variable in local_vars: if variable in local_vars:
@ -173,14 +181,6 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
return obj[member](argvals) return obj[member](argvals)
m = re.match(
r'(?P<in>%s)\[(?P<idx>.+)\]$' % _NAME_RE, expr)
if m:
val = local_vars[m.group('in')]
idx = self.interpret_expression(
m.group('idx'), local_vars, allow_recursion - 1)
return val[idx]
for op, opfunc in _OPERATORS: for op, opfunc in _OPERATORS:
m = re.match(r'(?P<x>.+?)%s(?P<y>.+)' % re.escape(op), expr) m = re.match(r'(?P<x>.+?)%s(?P<y>.+)' % re.escape(op), expr)
if not m: if not m:
@ -211,21 +211,22 @@ def interpret_expression(self, expr, local_vars, allow_recursion):
raise ExtractorError('Unsupported JS expression %r' % expr) raise ExtractorError('Unsupported JS expression %r' % expr)
def extract_object(self, objname): def extract_object(self, objname):
_FUNC_NAME_RE = r'''(?:[a-zA-Z$0-9]+|"[a-zA-Z$0-9]+"|'[a-zA-Z$0-9]+')'''
obj = {} obj = {}
obj_m = re.search( obj_m = re.search(
(r'(?<!this\.)%s\s*=\s*\{' % re.escape(objname)) + (r'(?<!this\.)%s\s*=\s*\{' % re.escape(objname)) +
r'\s*(?P<fields>([a-zA-Z$0-9]+\s*:\s*function\(.*?\)\s*\{.*?\}(?:,\s*)?)*)' + r'\s*(?P<fields>(%s\s*:\s*function\(.*?\)\s*\{.*?\}(?:,\s*)?)*)' +
r'\}\s*;', r'\}\s*;' % _FUNC_NAME_RE,
self.code) self.code)
fields = obj_m.group('fields') fields = obj_m.group('fields')
# Currently, it only supports function definitions # Currently, it only supports function definitions
fields_m = re.finditer( fields_m = re.finditer(
r'(?P<key>[a-zA-Z$0-9]+)\s*:\s*function' r'(?P<key>%s)\s*:\s*function'
r'\((?P<args>[a-z,]+)\){(?P<code>[^}]+)}', r'\((?P<args>[a-z,]+)\){(?P<code>[^}]+)}' % _FUNC_NAME_RE,
fields) fields)
for f in fields_m: for f in fields_m:
argnames = f.group('args').split(',') argnames = f.group('args').split(',')
obj[f.group('key')] = self.build_function(argnames, f.group('code')) obj[remove_quotes(f.group('key'))] = self.build_function(argnames, f.group('code'))
return obj return obj