From 8c87a18029523187db114a3de123f56b354c1f9f Mon Sep 17 00:00:00 2001 From: sulyi Date: Thu, 24 Nov 2016 22:33:30 +0100 Subject: [PATCH] [jsinterp] Calling field and test --- test/test_jsinterp.py | 3 ++- youtube_dl/jsinterp.py | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py index 310902e12..632105f68 100644 --- a/test/test_jsinterp.py +++ b/test/test_jsinterp.py @@ -111,7 +111,8 @@ class TestJSInterpreter(unittest.TestCase): function z() { return y(3); } ''') self.assertEqual(jsi.call_function('z'), 5) - + jsi = JSInterpreter('function w(a) { return a.split(""); }', objects={'a': 'abc'}) + self.assertEqual(jsi.call_function('w'), ["a", "b", "c"]) if __name__ == '__main__': unittest.main() diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py index ba469dcf0..524972139 100644 --- a/youtube_dl/jsinterp.py +++ b/youtube_dl/jsinterp.py @@ -34,9 +34,9 @@ _ASSIGN_OPERATORS_RE = r'|'.join(re.escape(op) for op, opfunc in _ASSIGN_OPERATO _NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*' -# can't use raw string, starts with " and end with ' -_STRING_RE = '''"(?:[^"\\\\]*(?:\\\\\\\\|\\\\[\'"nurtbfx/\\n]))*[^"\\\\]*"| - \'(?:[^\'\\\\]*(?:\\\\\\\\|\\\\[\'"nurtbfx/\\n]))*[^\'\\\\]*\'''' +_SINGLE_QUOTED = r"""'(?:[^'\\\\]*(?:\\\\\\\\|\\\\['"nurtbfx/\\n]))*[^'\\\\]*'""" +_DOUBLE_QUOTED = r'''"(?:[^"\\\\]*(?:\\\\\\\\|\\\\['"nurtbfx/\\n]))*[^"\\\\]*"''' +_STRING_RE = r'%s|%s' % (_SINGLE_QUOTED, _DOUBLE_QUOTED) _INTEGER_RE = r'%(hex)s|%(dec)s|%(oct)s' % {'hex': __HEXADECIMAL_RE, 'dec': __DECIMAL_RE, 'oct': __OCTAL_RE} _FLOAT_RE = r'%(dec)s\.%(dec)s' % {'dec': __DECIMAL_RE} @@ -46,17 +46,17 @@ _BOOL_RE = r'true|false' # r'/(?=[^*])[^/\n]*/(?![gimy]*(?P[gimy])[gimy]*\g)[gimy]{0,4}' _REGEX_RE = r'/(?=[^*])[^/\n]*/[gimy]{0,4}' -_LITERAL_RE = r'(%(int)s|%(float)s|%(str)s|%(bool)s|%(regex)s)' % { +_LITERAL_RE = r'((?P%(int)s)|(?P%(float)s)|(?P%(str)s)|(?P%(bool)s)|(?P%(regex)s))' % { 'int': _INTEGER_RE, 'float': _FLOAT_RE, 'str': _STRING_RE, 'bool': _BOOL_RE, 'regex': _REGEX_RE } -_ARRAY_RE = r'\[(%(literal)s\s*,\s*)*(%(literal)s\s*)?\]' % {'literal': _LITERAL_RE} # TODO nested array -_VALUE_RE = r'(?:%(literal)s)|(%(array)s)' % {'literal': _LITERAL_RE, 'array': _ARRAY_RE} -_CALL_RE = r'%(name)s\s*\(' % {'name': _NAME_RE} +# _ARRAY_RE = r'\[(%(literal)s\s*,\s*)*(%(literal)s\s*)?\]' % {'literal': _LITERAL_RE} +# _VALUE_RE = r'(?:%(literal)s)|(%(array)s)' % {'literal': _LITERAL_RE, 'array': _ARRAY_RE} +_CALL_RE = r'\.?%(name)s\s*\(' % {'name': _NAME_RE} # function or method! _COMMENT_RE = r'/\*(?:(?!\*/)(?:\n|.))*\*/'