2014-03-30 07:02:58 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-07-24 10:41:12 +02:00
|
|
|
import json
|
2015-02-01 22:38:26 +01:00
|
|
|
import operator
|
2014-03-30 07:02:58 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
from .utils import (
|
|
|
|
ExtractorError,
|
|
|
|
)
|
|
|
|
|
2016-11-24 21:48:11 +01:00
|
|
|
__DECIMAL_RE = r'(?:[1-9][0-9]*)|0'
|
2016-11-30 07:49:47 +01:00
|
|
|
__OCTAL_RE = r'0[0-7]+'
|
2016-11-26 04:45:55 +01:00
|
|
|
__HEXADECIMAL_RE = r'0[xX][0-9a-fA-F]+'
|
2016-11-28 06:53:28 +01:00
|
|
|
__ESC_UNICODE_RE = r'u[0-9a-fA-F]{4}'
|
|
|
|
__ESC_HEX_RE = r'x[0-9a-fA-F]{2}'
|
2016-11-23 02:34:20 +01:00
|
|
|
|
2016-11-30 07:37:47 +01:00
|
|
|
_OPERATORS = [
|
2015-02-01 22:38:26 +01:00
|
|
|
('|', operator.or_),
|
|
|
|
('^', operator.xor),
|
|
|
|
('&', operator.and_),
|
|
|
|
('>>', operator.rshift),
|
|
|
|
('<<', operator.lshift),
|
2016-11-30 07:49:47 +01:00
|
|
|
('>>>', lambda cur, right: cur >> right if cur >= 0 else (cur + 0x100000000) >> right),
|
2015-02-01 22:38:26 +01:00
|
|
|
('-', operator.sub),
|
|
|
|
('+', operator.add),
|
|
|
|
('%', operator.mod),
|
2015-02-02 01:49:32 +01:00
|
|
|
('/', operator.truediv),
|
2016-11-23 02:34:20 +01:00
|
|
|
('*', operator.mul)
|
2016-11-30 07:37:47 +01:00
|
|
|
]
|
|
|
|
_ASSIGN_OPERATORS = [(op + '=', opfunc) for op, opfunc in _OPERATORS]
|
|
|
|
_ASSIGN_OPERATORS.append(('=', lambda cur, right: right))
|
2015-02-01 22:38:26 +01:00
|
|
|
|
2016-11-26 04:45:55 +01:00
|
|
|
# TODO flow control and others probably
|
2016-11-30 07:37:47 +01:00
|
|
|
_RESERVED_WORDS = ['function', 'var', 'const', 'return']
|
2016-11-23 02:34:20 +01:00
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
_NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*'
|
|
|
|
|
2016-11-28 06:53:28 +01:00
|
|
|
# non-escape char also can be escaped, but line continuation and quotes has to be
|
|
|
|
# XXX unicode and hexadecimal escape sequences should be validated
|
|
|
|
_SINGLE_QUOTED_RE = r"""'(?:(?:\\'|\n)|[^'\n])*'"""
|
|
|
|
_DOUBLE_QUOTED_RE = r'''"(?:(?:\\"|\n)|[^"\n])*"'''
|
|
|
|
_STRING_RE = r'(?:%s)|(?:%s)' % (_SINGLE_QUOTED_RE, _DOUBLE_QUOTED_RE)
|
2016-11-23 02:34:20 +01:00
|
|
|
|
2016-11-28 06:53:28 +01:00
|
|
|
_INTEGER_RE = r'(?:%(hex)s)|(?:%(dec)s)|(?:%(oct)s)' % {'hex': __HEXADECIMAL_RE, 'dec': __DECIMAL_RE, 'oct': __OCTAL_RE}
|
|
|
|
_FLOAT_RE = r'(?:(?:%(dec)s\.[0-9]*)|(?:\.[0-9]+))(?:[eE][+-]?[0-9]+)?' % {'dec': __DECIMAL_RE}
|
2016-11-23 02:34:20 +01:00
|
|
|
|
|
|
|
_BOOL_RE = r'true|false'
|
2016-11-28 06:53:28 +01:00
|
|
|
_NULL_RE = r'null'
|
|
|
|
|
|
|
|
# XXX early validation might needed
|
|
|
|
# r'''/(?!\*)
|
|
|
|
# (?:(?:\\(?:[tnvfr0.\\+*?^$\[\]{}()|/]|[0-7]{3}|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Z]|))|[^/\n])*
|
|
|
|
# /(?:(?![gimy]*(?P<flag>[gimy])[gimy]*(?P=flag))[gimy]{0,4}\b|\s|$)'''
|
2016-11-28 13:14:37 +01:00
|
|
|
_REGEX_FLAGS_RE = r'(?![gimy]*(?P<reflag>[gimy])[gimy]*(?P=reflag))(?P<reflags>[gimy]{0,4}\b)'
|
|
|
|
_REGEX_RE = r'/(?!\*)(?P<rebody>(?:[^/\n]|(?:\\/))*)/(?:(?:%s)|(?:\s|$))' % _REGEX_FLAGS_RE
|
|
|
|
|
|
|
|
re.compile(_REGEX_RE)
|
2016-11-28 06:53:28 +01:00
|
|
|
|
2016-11-30 07:49:47 +01:00
|
|
|
_TOKENS = [
|
2016-11-28 06:53:28 +01:00
|
|
|
('id', _NAME_RE),
|
|
|
|
('null', _NULL_RE),
|
|
|
|
('bool', _BOOL_RE),
|
|
|
|
('str', _STRING_RE),
|
|
|
|
('int', _INTEGER_RE),
|
|
|
|
('float', _FLOAT_RE),
|
|
|
|
('regex', _REGEX_RE)
|
2016-11-30 07:49:47 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
_RELATIONS = {
|
|
|
|
'lt': '<',
|
|
|
|
'gt': '>',
|
|
|
|
'le': '<=',
|
|
|
|
'ge': '>=',
|
|
|
|
'eq': '==',
|
|
|
|
'ne': '!=',
|
|
|
|
'seq': '===',
|
|
|
|
'sne': '!=='
|
|
|
|
}
|
2016-11-28 06:53:28 +01:00
|
|
|
|
2016-11-28 13:14:37 +01:00
|
|
|
_PUNCTUATIONS = {
|
|
|
|
'copen': '{',
|
|
|
|
'cclose': '}',
|
|
|
|
'popen': '(',
|
|
|
|
'pclose': ')',
|
|
|
|
'sopen': '[',
|
|
|
|
'sclose': ']',
|
|
|
|
'dot': '.',
|
|
|
|
'end': ';',
|
2016-11-30 07:49:47 +01:00
|
|
|
'comma': ',',
|
|
|
|
'inc': '++',
|
|
|
|
'dec': '--',
|
|
|
|
'not': '!',
|
|
|
|
'bnot': '~',
|
|
|
|
'and': '&&',
|
|
|
|
'or': '||',
|
|
|
|
'hook': '?',
|
|
|
|
'colon': ':'
|
2016-11-28 13:14:37 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 07:37:47 +01:00
|
|
|
token_ids = dict((token[0], i) for i, token in enumerate(_TOKENS))
|
2016-11-30 08:04:08 +01:00
|
|
|
op_ids = dict((op[0], i) for i, op in enumerate(_OPERATORS))
|
|
|
|
aop_ids = dict((aop[0], i)for i, aop in enumerate(_ASSIGN_OPERATORS))
|
2016-11-30 07:37:47 +01:00
|
|
|
|
2016-11-28 06:53:28 +01:00
|
|
|
_COMMENT_RE = r'(?P<comment>/\*(?:(?!\*/)(?:\n|.))*\*/)'
|
|
|
|
_TOKENS_RE = r'|'.join('(?P<%(id)s>%(value)s)' % {'id': name, 'value': value}
|
2016-11-30 07:37:47 +01:00
|
|
|
for name, value in _TOKENS)
|
|
|
|
_RESERVED_WORDS_RE = r'(?:(?P<rsv>%s)\b)' % r'|'.join(_RESERVED_WORDS)
|
2016-11-28 06:53:28 +01:00
|
|
|
_PUNCTUATIONS_RE = r'|'.join(r'(?P<%(id)s>%(value)s)' % {'id': name, 'value': re.escape(value)}
|
|
|
|
for name, value in _PUNCTUATIONS.items())
|
2016-11-30 07:49:47 +01:00
|
|
|
_RELATIONS_RE = r'|'.join(r'(?P<%(id)s>%(value)s)' % {'id': name, 'value': re.escape(value)}
|
|
|
|
for name, value in _RELATIONS.items())
|
2016-11-30 07:37:47 +01:00
|
|
|
_OPERATORS_RE = r'(?P<op>%s)' % r'|'.join(re.escape(op) for op, opfunc in _OPERATORS)
|
|
|
|
_ASSIGN_OPERATORS_RE = r'(?P<assign>%s)' % r'|'.join(re.escape(op) for op, opfunc in _ASSIGN_OPERATORS)
|
2016-11-28 06:53:28 +01:00
|
|
|
|
2016-11-30 08:04:08 +01:00
|
|
|
input_element = re.compile(r'''\s*(?:%(comment)s|%(rsv)s|%(token)s|%(punct)s|%(assign)s|%(op)s|%(rel)s)\s*''' % {
|
2016-11-24 21:48:11 +01:00
|
|
|
'comment': _COMMENT_RE,
|
2016-11-28 13:14:37 +01:00
|
|
|
'rsv': _RESERVED_WORDS_RE,
|
2016-11-28 06:53:28 +01:00
|
|
|
'token': _TOKENS_RE,
|
|
|
|
'punct': _PUNCTUATIONS_RE,
|
|
|
|
'assign': _ASSIGN_OPERATORS_RE,
|
2016-11-30 08:04:08 +01:00
|
|
|
'op': _OPERATORS_RE,
|
|
|
|
'rel': _RELATIONS_RE
|
2016-11-23 02:34:20 +01:00
|
|
|
})
|
|
|
|
|
2016-11-30 07:49:47 +01:00
|
|
|
undefined = object()
|
|
|
|
|
2014-03-30 07:02:58 +02:00
|
|
|
|
|
|
|
class JSInterpreter(object):
|
2015-02-01 22:38:26 +01:00
|
|
|
def __init__(self, code, objects=None):
|
|
|
|
if objects is None:
|
|
|
|
objects = {}
|
2015-02-18 10:47:40 +01:00
|
|
|
self.code = code
|
2014-03-30 07:02:58 +02:00
|
|
|
self._functions = {}
|
2015-02-01 22:38:26 +01:00
|
|
|
self._objects = objects
|
|
|
|
|
2016-11-23 02:34:20 +01:00
|
|
|
@staticmethod
|
2016-11-30 07:37:47 +01:00
|
|
|
def _next_statement(code, pos=0, stack_size=100):
|
|
|
|
def next_statement(lookahead, stack_top=100):
|
2016-11-26 01:47:39 +01:00
|
|
|
# TODO migrate interpretation
|
2016-11-28 13:14:37 +01:00
|
|
|
statement = []
|
2016-11-28 06:53:28 +01:00
|
|
|
feed_m = None
|
2016-11-28 13:14:37 +01:00
|
|
|
while lookahead < len(code):
|
|
|
|
feed_m = input_element.match(code, lookahead)
|
2016-11-30 07:37:47 +01:00
|
|
|
if feed_m is not None:
|
2016-11-25 21:54:25 +01:00
|
|
|
token_id = feed_m.lastgroup
|
2016-11-30 07:37:47 +01:00
|
|
|
if token_id in ('pclose', 'sclose', 'cclose', 'comma', 'end'):
|
2016-11-28 13:14:37 +01:00
|
|
|
return statement, lookahead, feed_m.end()
|
2016-11-28 06:53:28 +01:00
|
|
|
token_value = feed_m.group(token_id)
|
2016-11-28 13:14:37 +01:00
|
|
|
lookahead = feed_m.end()
|
2016-11-25 21:54:25 +01:00
|
|
|
if token_id == 'comment':
|
|
|
|
pass
|
2016-11-30 07:37:47 +01:00
|
|
|
elif token_id == 'rsv':
|
2016-11-30 07:49:47 +01:00
|
|
|
# XXX backward compatibility till parser migration
|
|
|
|
statement.append((token_id, token_value + ' '))
|
2016-11-30 07:37:47 +01:00
|
|
|
if token_value == 'return':
|
|
|
|
expressions, lookahead, _ = next_statement(lookahead, stack_top - 1)
|
2016-11-28 13:14:37 +01:00
|
|
|
statement.extend(expressions)
|
2016-11-30 07:37:47 +01:00
|
|
|
elif token_id in ('id', 'op', 'dot'):
|
|
|
|
if token_id == 'id':
|
|
|
|
# TODO handle label
|
|
|
|
pass
|
2016-11-28 13:14:37 +01:00
|
|
|
statement.append((token_id, token_value))
|
2016-11-30 07:37:47 +01:00
|
|
|
elif token_id in token_ids:
|
2016-11-28 13:14:37 +01:00
|
|
|
# TODO date
|
|
|
|
# TODO error handling
|
|
|
|
if token_id == 'null':
|
|
|
|
statement.append((token_id, None))
|
|
|
|
elif token_id == 'bool':
|
|
|
|
statement.append((token_id, {'true': True, 'false': False}[token_value]))
|
|
|
|
elif token_id == 'str':
|
|
|
|
statement.append((token_id, token_value))
|
|
|
|
elif token_id == 'int':
|
|
|
|
statement.append((token_id, int(token_value)))
|
|
|
|
elif token_id == 'float':
|
|
|
|
statement.append((token_id, float(token_value)))
|
|
|
|
elif token_id == 'regex':
|
|
|
|
regex = re.compile(feed_m.group('rebody'))
|
|
|
|
statement.append((token_id, {'re': regex, 'flags': feed_m.group('reflags')}))
|
2016-11-28 06:53:28 +01:00
|
|
|
elif token_id in ('assign', 'popen', 'sopen'):
|
2016-11-28 13:14:37 +01:00
|
|
|
statement.append((token_id, token_value))
|
|
|
|
while lookahead < len(code):
|
2016-11-30 07:37:47 +01:00
|
|
|
expressions, lookahead, _ = next_statement(lookahead, stack_top - 1)
|
2016-11-28 13:14:37 +01:00
|
|
|
statement.extend(expressions)
|
|
|
|
peek = input_element.match(code, lookahead)
|
2016-11-30 07:37:47 +01:00
|
|
|
if peek is not None:
|
2016-11-25 21:54:25 +01:00
|
|
|
peek_id = peek.lastgroup
|
2016-11-28 06:53:28 +01:00
|
|
|
peek_value = peek.group(peek_id)
|
2016-11-28 13:14:37 +01:00
|
|
|
if ((token_id == 'popen' and peek_id == 'pclose') or
|
|
|
|
(token_id == 'sopen' and peek_id == 'sclose')):
|
|
|
|
statement.append((peek_id, peek_value))
|
|
|
|
lookahead = peek.end()
|
2016-11-25 21:54:25 +01:00
|
|
|
break
|
2016-11-28 13:14:37 +01:00
|
|
|
elif peek_id == 'comma':
|
|
|
|
statement.append((peek_id, peek_value))
|
|
|
|
lookahead = peek.end()
|
2016-11-25 21:54:25 +01:00
|
|
|
elif peek_id == 'end':
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ExtractorError('Unexpected character %s at %d' % (
|
2016-11-28 06:53:28 +01:00
|
|
|
peek_value, peek.start(peek_id)))
|
2016-11-24 21:48:11 +01:00
|
|
|
else:
|
2016-11-25 21:54:25 +01:00
|
|
|
raise ExtractorError("Not yet implemented")
|
2016-11-24 21:48:11 +01:00
|
|
|
else:
|
2016-11-25 21:54:25 +01:00
|
|
|
raise ExtractorError("Not yet implemented")
|
2016-11-30 07:37:47 +01:00
|
|
|
return statement, lookahead, lookahead if feed_m is None else feed_m.end()
|
2016-11-24 21:48:11 +01:00
|
|
|
|
2016-11-23 02:34:20 +01:00
|
|
|
while pos < len(code):
|
2016-11-30 07:37:47 +01:00
|
|
|
stmt, _, pos = next_statement(pos, stack_size)
|
2016-11-28 13:14:37 +01:00
|
|
|
# XXX backward compatibility till parser migration
|
2016-11-30 07:37:47 +01:00
|
|
|
yield ''.join(str(value) for _, value in stmt)
|
2016-11-25 21:54:25 +01:00
|
|
|
raise StopIteration
|
2016-11-23 02:34:20 +01:00
|
|
|
|
2016-11-30 07:49:47 +01:00
|
|
|
@staticmethod
|
|
|
|
def _interpret_statement(stmt, local_vars, stack_size=100):
|
|
|
|
while stmt:
|
|
|
|
token_id, token_value = stmt.pop(0)
|
|
|
|
if token_id == 'copen':
|
|
|
|
# TODO block
|
|
|
|
pass
|
|
|
|
elif token_id == 'rsv':
|
|
|
|
if token_value == 'var':
|
|
|
|
has_another = True
|
|
|
|
while has_another:
|
|
|
|
next_token_id, next_token_value = stmt.pop(0)
|
|
|
|
if next_token_id in ('sopen', 'copen'):
|
|
|
|
pass
|
|
|
|
elif next_token_id != 'id':
|
|
|
|
raise ExtractorError('Missing variable name')
|
|
|
|
local_vars[token_value] = undefined
|
|
|
|
|
|
|
|
if stmt[0][0] == 'assign':
|
|
|
|
pass
|
|
|
|
|
|
|
|
if stmt[0][0] != 'comma':
|
|
|
|
break
|
|
|
|
elif token_value == 'function':
|
|
|
|
pass
|
|
|
|
elif token_value == 'if':
|
|
|
|
pass
|
|
|
|
elif token_value in ('break', 'continue'):
|
|
|
|
pass
|
|
|
|
elif token_value == 'return':
|
|
|
|
pass
|
|
|
|
elif token_value == 'with':
|
|
|
|
pass
|
|
|
|
elif token_value == 'switch':
|
|
|
|
pass
|
|
|
|
elif token_value == 'throw':
|
|
|
|
pass
|
|
|
|
elif token_value == 'try':
|
|
|
|
pass
|
|
|
|
elif token_value == 'debugger':
|
|
|
|
pass
|
|
|
|
elif token_id == 'label':
|
|
|
|
pass
|
|
|
|
elif token_id == 'id':
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# lefthand-side_expr -> new_expr | call_expr
|
|
|
|
# call_expr -> member_expr args | call_expr args | call_expr [ expr ] | call_expr . id_name
|
|
|
|
# new_expr -> member_expr | new member_expr
|
|
|
|
# member_expr -> prime_expr | func_expr |
|
|
|
|
# member_expr [ expr ] | member_expr . id_name | new member_expr args
|
|
|
|
pass
|
|
|
|
|
|
|
|
# empty statement goes straight here
|
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
def interpret_statement(self, stmt, local_vars, allow_recursion=100):
|
2014-03-30 07:02:58 +02:00
|
|
|
if allow_recursion < 0:
|
|
|
|
raise ExtractorError('Recursion limit reached')
|
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
should_abort = False
|
|
|
|
stmt = stmt.lstrip()
|
|
|
|
stmt_m = re.match(r'var\s', stmt)
|
|
|
|
if stmt_m:
|
|
|
|
expr = stmt[len(stmt_m.group(0)):]
|
2014-03-30 07:02:58 +02:00
|
|
|
else:
|
2015-02-01 22:38:26 +01:00
|
|
|
return_m = re.match(r'return(?:\s+|$)', stmt)
|
|
|
|
if return_m:
|
|
|
|
expr = stmt[len(return_m.group(0)):]
|
|
|
|
should_abort = True
|
|
|
|
else:
|
|
|
|
# Try interpreting it as an expression
|
|
|
|
expr = stmt
|
2014-03-30 07:02:58 +02:00
|
|
|
|
|
|
|
v = self.interpret_expression(expr, local_vars, allow_recursion)
|
2015-02-01 22:38:26 +01:00
|
|
|
return v, should_abort
|
2014-03-30 07:02:58 +02:00
|
|
|
|
|
|
|
def interpret_expression(self, expr, local_vars, allow_recursion):
|
2015-02-01 22:38:26 +01:00
|
|
|
expr = expr.strip()
|
|
|
|
|
|
|
|
if expr == '': # Empty expression
|
|
|
|
return None
|
|
|
|
|
|
|
|
if expr.startswith('('):
|
|
|
|
parens_count = 0
|
|
|
|
for m in re.finditer(r'[()]', expr):
|
|
|
|
if m.group(0) == '(':
|
|
|
|
parens_count += 1
|
|
|
|
else:
|
|
|
|
parens_count -= 1
|
|
|
|
if parens_count == 0:
|
|
|
|
sub_expr = expr[1:m.start()]
|
|
|
|
sub_result = self.interpret_expression(
|
|
|
|
sub_expr, local_vars, allow_recursion)
|
|
|
|
remaining_expr = expr[m.end():].strip()
|
|
|
|
if not remaining_expr:
|
|
|
|
return sub_result
|
|
|
|
else:
|
|
|
|
expr = json.dumps(sub_result) + remaining_expr
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ExtractorError('Premature end of parens in %r' % expr)
|
|
|
|
|
2016-11-30 07:37:47 +01:00
|
|
|
for op, opfunc in _ASSIGN_OPERATORS:
|
2015-02-01 22:38:26 +01:00
|
|
|
m = re.match(r'''(?x)
|
|
|
|
(?P<out>%s)(?:\[(?P<index>[^\]]+?)\])?
|
|
|
|
\s*%s
|
|
|
|
(?P<expr>.*)$''' % (_NAME_RE, re.escape(op)), expr)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
right_val = self.interpret_expression(
|
|
|
|
m.group('expr'), local_vars, allow_recursion - 1)
|
|
|
|
|
|
|
|
if m.groupdict().get('index'):
|
|
|
|
lvar = local_vars[m.group('out')]
|
|
|
|
idx = self.interpret_expression(
|
|
|
|
m.group('index'), local_vars, allow_recursion)
|
|
|
|
assert isinstance(idx, int)
|
|
|
|
cur = lvar[idx]
|
|
|
|
val = opfunc(cur, right_val)
|
|
|
|
lvar[idx] = val
|
|
|
|
return val
|
|
|
|
else:
|
|
|
|
cur = local_vars.get(m.group('out'))
|
|
|
|
val = opfunc(cur, right_val)
|
|
|
|
local_vars[m.group('out')] = val
|
|
|
|
return val
|
|
|
|
|
2014-03-30 07:02:58 +02:00
|
|
|
if expr.isdigit():
|
|
|
|
return int(expr)
|
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
var_m = re.match(
|
|
|
|
r'(?!if|return|true|false)(?P<name>%s)$' % _NAME_RE,
|
|
|
|
expr)
|
|
|
|
if var_m:
|
|
|
|
return local_vars[var_m.group('name')]
|
2014-03-30 07:02:58 +02:00
|
|
|
|
2014-07-24 10:41:12 +02:00
|
|
|
try:
|
|
|
|
return json.loads(expr)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
m = re.match(
|
2015-02-01 22:38:26 +01:00
|
|
|
r'(?P<var>%s)\.(?P<member>[^(]+)(?:\(+(?P<args>[^()]*)\))?$' % _NAME_RE,
|
2014-07-24 10:41:12 +02:00
|
|
|
expr)
|
2014-03-30 07:02:58 +02:00
|
|
|
if m:
|
2014-07-24 10:41:12 +02:00
|
|
|
variable = m.group('var')
|
2014-03-30 07:02:58 +02:00
|
|
|
member = m.group('member')
|
2014-07-24 10:41:12 +02:00
|
|
|
arg_str = m.group('args')
|
2014-07-15 22:46:39 +02:00
|
|
|
|
2014-07-24 10:41:12 +02:00
|
|
|
if variable in local_vars:
|
|
|
|
obj = local_vars[variable]
|
|
|
|
else:
|
2016-06-20 13:29:13 +02:00
|
|
|
if variable not in self._objects:
|
|
|
|
self._objects[variable] = self.extract_object(variable)
|
|
|
|
obj = self._objects[variable]
|
2014-07-24 10:41:12 +02:00
|
|
|
|
|
|
|
if arg_str is None:
|
|
|
|
# Member access
|
|
|
|
if member == 'length':
|
|
|
|
return len(obj)
|
|
|
|
return obj[member]
|
|
|
|
|
|
|
|
assert expr.endswith(')')
|
|
|
|
# Function call
|
|
|
|
if arg_str == '':
|
|
|
|
argvals = tuple()
|
|
|
|
else:
|
|
|
|
argvals = tuple([
|
|
|
|
self.interpret_expression(v, local_vars, allow_recursion)
|
|
|
|
for v in arg_str.split(',')])
|
|
|
|
|
|
|
|
if member == 'split':
|
|
|
|
assert argvals == ('',)
|
|
|
|
return list(obj)
|
|
|
|
if member == 'join':
|
|
|
|
assert len(argvals) == 1
|
|
|
|
return argvals[0].join(obj)
|
|
|
|
if member == 'reverse':
|
|
|
|
assert len(argvals) == 0
|
2014-07-24 11:08:31 +02:00
|
|
|
obj.reverse()
|
|
|
|
return obj
|
2014-07-24 10:41:12 +02:00
|
|
|
if member == 'slice':
|
|
|
|
assert len(argvals) == 1
|
|
|
|
return obj[argvals[0]:]
|
|
|
|
if member == 'splice':
|
|
|
|
assert isinstance(obj, list)
|
|
|
|
index, howMany = argvals
|
|
|
|
res = []
|
|
|
|
for i in range(index, min(index + howMany, len(obj))):
|
2014-07-25 07:04:39 +02:00
|
|
|
res.append(obj.pop(index))
|
2014-07-24 10:41:12 +02:00
|
|
|
return res
|
|
|
|
|
|
|
|
return obj[member](argvals)
|
2014-03-30 07:02:58 +02:00
|
|
|
|
|
|
|
m = re.match(
|
2015-02-01 22:38:26 +01:00
|
|
|
r'(?P<in>%s)\[(?P<idx>.+)\]$' % _NAME_RE, expr)
|
2014-03-30 07:02:58 +02:00
|
|
|
if m:
|
|
|
|
val = local_vars[m.group('in')]
|
|
|
|
idx = self.interpret_expression(
|
|
|
|
m.group('idx'), local_vars, allow_recursion - 1)
|
|
|
|
return val[idx]
|
|
|
|
|
2016-11-30 07:37:47 +01:00
|
|
|
for op, opfunc in _OPERATORS:
|
2015-02-01 22:38:26 +01:00
|
|
|
m = re.match(r'(?P<x>.+?)%s(?P<y>.+)' % re.escape(op), expr)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
x, abort = self.interpret_statement(
|
|
|
|
m.group('x'), local_vars, allow_recursion - 1)
|
|
|
|
if abort:
|
|
|
|
raise ExtractorError(
|
|
|
|
'Premature left-side return of %s in %r' % (op, expr))
|
|
|
|
y, abort = self.interpret_statement(
|
|
|
|
m.group('y'), local_vars, allow_recursion - 1)
|
|
|
|
if abort:
|
|
|
|
raise ExtractorError(
|
|
|
|
'Premature right-side return of %s in %r' % (op, expr))
|
|
|
|
return opfunc(x, y)
|
2014-03-30 07:02:58 +02:00
|
|
|
|
|
|
|
m = re.match(
|
2016-11-05 06:11:51 +01:00
|
|
|
r'^(?P<func>%s)\((?P<args>[a-zA-Z0-9_$,]*)\)$' % _NAME_RE, expr)
|
2014-03-30 07:02:58 +02:00
|
|
|
if m:
|
|
|
|
fname = m.group('func')
|
2014-07-24 10:41:12 +02:00
|
|
|
argvals = tuple([
|
|
|
|
int(v) if v.isdigit() else local_vars[v]
|
2016-11-05 06:11:51 +01:00
|
|
|
for v in m.group('args').split(',')]) if len(m.group('args')) > 0 else tuple()
|
2016-06-20 13:29:13 +02:00
|
|
|
if fname not in self._functions:
|
|
|
|
self._functions[fname] = self.extract_function(fname)
|
2014-03-30 07:02:58 +02:00
|
|
|
return self._functions[fname](argvals)
|
2015-02-01 22:38:26 +01:00
|
|
|
|
2014-03-30 07:02:58 +02:00
|
|
|
raise ExtractorError('Unsupported JS expression %r' % expr)
|
|
|
|
|
2014-07-15 22:46:39 +02:00
|
|
|
def extract_object(self, objname):
|
|
|
|
obj = {}
|
|
|
|
obj_m = re.search(
|
|
|
|
(r'(?:var\s+)?%s\s*=\s*\{' % re.escape(objname)) +
|
2015-11-24 07:45:02 +01:00
|
|
|
r'\s*(?P<fields>([a-zA-Z$0-9]+\s*:\s*function\(.*?\)\s*\{.*?\}(?:,\s*)?)*)' +
|
2014-07-15 22:46:39 +02:00
|
|
|
r'\}\s*;',
|
|
|
|
self.code)
|
|
|
|
fields = obj_m.group('fields')
|
|
|
|
# Currently, it only supports function definitions
|
|
|
|
fields_m = re.finditer(
|
2014-07-23 02:13:48 +02:00
|
|
|
r'(?P<key>[a-zA-Z$0-9]+)\s*:\s*function'
|
2014-07-15 22:46:39 +02:00
|
|
|
r'\((?P<args>[a-z,]+)\){(?P<code>[^}]+)}',
|
|
|
|
fields)
|
|
|
|
for f in fields_m:
|
|
|
|
argnames = f.group('args').split(',')
|
|
|
|
obj[f.group('key')] = self.build_function(argnames, f.group('code'))
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2014-03-30 07:02:58 +02:00
|
|
|
def extract_function(self, funcname):
|
|
|
|
func_m = re.search(
|
2015-02-01 22:38:26 +01:00
|
|
|
r'''(?x)
|
2016-06-23 09:41:34 +07:00
|
|
|
(?:function\s+%s|[{;,]\s*%s\s*=\s*function|var\s+%s\s*=\s*function)\s*
|
2015-02-01 22:38:26 +01:00
|
|
|
\((?P<args>[^)]*)\)\s*
|
|
|
|
\{(?P<code>[^}]+)\}''' % (
|
2015-11-10 12:54:02 +08:00
|
|
|
re.escape(funcname), re.escape(funcname), re.escape(funcname)),
|
2014-03-30 07:02:58 +02:00
|
|
|
self.code)
|
2014-03-30 07:15:14 +02:00
|
|
|
if func_m is None:
|
|
|
|
raise ExtractorError('Could not find JS function %r' % funcname)
|
2014-03-30 07:02:58 +02:00
|
|
|
argnames = func_m.group('args').split(',')
|
|
|
|
|
2014-07-15 22:46:39 +02:00
|
|
|
return self.build_function(argnames, func_m.group('code'))
|
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
def call_function(self, funcname, *args):
|
|
|
|
f = self.extract_function(funcname)
|
|
|
|
return f(args)
|
|
|
|
|
2014-07-15 22:46:39 +02:00
|
|
|
def build_function(self, argnames, code):
|
2014-03-30 07:02:58 +02:00
|
|
|
def resf(args):
|
|
|
|
local_vars = dict(zip(argnames, args))
|
2016-11-23 02:34:20 +01:00
|
|
|
for stmt in self._next_statement(code):
|
2015-02-01 22:38:26 +01:00
|
|
|
res, abort = self.interpret_statement(stmt, local_vars)
|
|
|
|
if abort:
|
|
|
|
break
|
2014-03-30 07:02:58 +02:00
|
|
|
return res
|
|
|
|
return resf
|