From 848aa79a02212b76fe28234f34a2afddba87e173 Mon Sep 17 00:00:00 2001 From: sulyi Date: Sun, 10 Jun 2018 04:23:52 +0200 Subject: [PATCH] [jsinterp] Fixing incomplete refactor --- test/test_jstest_jsinterp2.py | 8 ++++---- test/test_jstest_jsinterp2_parse.py | 8 ++++---- test/test_jstest_legacy.py | 8 ++++---- youtube_dl/extractor/youtube.py | 4 ++-- youtube_dl/jsinterp2/jsbuilt_ins/internals.py | 3 +++ youtube_dl/jsinterp2/jsbuilt_ins/jsarray.py | 12 ++++++++++-- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/test/test_jstest_jsinterp2.py b/test/test_jstest_jsinterp2.py index 308710759..f75f3b632 100644 --- a/test/test_jstest_jsinterp2.py +++ b/test/test_jstest_jsinterp2.py @@ -16,7 +16,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from youtube_dl.jsinterp2 import JSInterpreter from .jstests import gettestcases -__doc__ = """see: `js2tests`""" +__doc__ = """see: `jstests`""" defs = gettestcases() @@ -71,12 +71,12 @@ def generator(test_case, my_log): return test_template -# And add them to TestJSInterpreter2 +# And add them to TestJSTestsJSInterpreter2 for testcase in defs: reason = testcase['skip'].get('interpret', False) tname = 'test_' + str(testcase['name']) i = 1 - while hasattr(TestJSInterpreter2, tname): + while hasattr(TestJSTestsJSInterpreter2, tname): tname = 'test_%s_%d' % (testcase['name'], i) i += 1 @@ -93,7 +93,7 @@ for testcase in defs: if reason is not False: test_method.__unittest_skip__ = True test_method.__unittest_skip_why__ = reason - setattr(TestJSInterpreter2, test_method.__name__, test_method) + setattr(TestJSTestsJSInterpreter2, test_method.__name__, test_method) del test_method else: log.info('Skipping %s:%s' % (tname, log_reason)) diff --git a/test/test_jstest_jsinterp2_parse.py b/test/test_jstest_jsinterp2_parse.py index da4731f98..6b82a2f7c 100644 --- a/test/test_jstest_jsinterp2_parse.py +++ b/test/test_jstest_jsinterp2_parse.py @@ -17,7 +17,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from youtube_dl.jsinterp2.jsparser import Parser from .jstests import gettestcases -__doc__ = """see: `js2tests`""" +__doc__ = """see: `jstests`""" def traverse(node, tree_types=(list, tuple)): @@ -64,12 +64,12 @@ def generator(test_case, my_log): return test_template -# And add them to TestJSInterpreter2Parse +# And add them to TestJSTestsJSInterpreter2Parse for testcase in defs: reason = testcase['skip'].get('parse', False) tname = 'test_' + str(testcase['name']) i = 1 - while hasattr(TestJSInterpreter2Parse, tname): + while hasattr(TestJSTestsJSInterpreter2Parse, tname): tname = 'test_%s_%d' % (testcase['name'], i) i += 1 @@ -86,7 +86,7 @@ for testcase in defs: if reason is not False: test_method.__unittest_skip__ = True test_method.__unittest_skip_why__ = reason - setattr(TestJSInterpreter2Parse, test_method.__name__, test_method) + setattr(TestJSTestsJSInterpreter2Parse, test_method.__name__, test_method) del test_method else: log.info('Skipping %s:%s' % (tname, log_reason)) diff --git a/test/test_jstest_legacy.py b/test/test_jstest_legacy.py index 8596b1282..b1bfba429 100644 --- a/test/test_jstest_legacy.py +++ b/test/test_jstest_legacy.py @@ -16,7 +16,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from youtube_dl.jsinterp import JSInterpreter from .jstests import gettestcases -__doc__ = """see: `js2tests`""" +__doc__ = """see: `jstests`""" defs = gettestcases() @@ -74,12 +74,12 @@ def generator(test_case, my_log): return test_template -# And add them to TestJSInterpreter +# And add them to TestJSTestsJSInterpreter for testcase in defs: reason = testcase['skip'].get('jsinterp', False) tname = 'test_' + str(testcase['name']) i = 1 - while hasattr(TestJSInterpreter, tname): + while hasattr(TestJSTestsJSInterpreter, tname): tname = 'test_%s_%d' % (testcase['name'], i) i += 1 @@ -96,7 +96,7 @@ for testcase in defs: if reason is not False: test_method.__unittest_skip__ = True test_method.__unittest_skip_why__ = reason - setattr(TestJSInterpreter, test_method.__name__, test_method) + setattr(TestJSTestsJSInterpreter, test_method.__name__, test_method) del test_method else: log.info('Skipping %s:%s' % (tname, log_reason)) diff --git a/youtube_dl/extractor/youtube.py b/youtube_dl/extractor/youtube.py index 379559825..e35e94ac6 100644 --- a/youtube_dl/extractor/youtube.py +++ b/youtube_dl/extractor/youtube.py @@ -12,7 +12,7 @@ import time import traceback from .common import InfoExtractor, SearchInfoExtractor -from ..jsinterp import JSInterpreter +from ..jsinterp2 import JSInterpreter from ..swfinterp import SWFInterpreter from ..compat import ( compat_chr, @@ -1165,7 +1165,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor): jsi = JSInterpreter(jscode) initial_function = jsi.extract_function(funcname) - return lambda s: initial_function([s]) + return lambda s: initial_function(*s) def _parse_sig_swf(self, file_contents): swfi = SWFInterpreter(file_contents) diff --git a/youtube_dl/jsinterp2/jsbuilt_ins/internals.py b/youtube_dl/jsinterp2/jsbuilt_ins/internals.py index 3e888b4ab..9f438b043 100644 --- a/youtube_dl/jsinterp2/jsbuilt_ins/internals.py +++ b/youtube_dl/jsinterp2/jsbuilt_ins/internals.py @@ -66,6 +66,7 @@ def to_number(o): from .jsobject import JSObjectPrototype from .jsboolean import JSBooleanPrototype, false, true from .jsstring import JSStringPrototype + from .jsnumber import JSNumberPrototype if o is undefined: return float('nan') @@ -73,6 +74,8 @@ def to_number(o): return 0 elif isinstance(o, JSBooleanPrototype) and o.value is true: return 1 + elif isinstance(o, JSNumberPrototype): + return o.value elif isinstance(o, JSStringPrototype): _STR_FLOAT_RE = r'(?:(?:[0-9]+(?:\.[0-9]*)?)|(?:\.[0-9]+))(?:[eE][+-]?[0-9]+)?' m = re.match(r'^[\s\n]*(?P(?:[+-]*(?:Infinity|%(float)s))|%(hex)s)?[\s\n]*$' % {'float': _STR_FLOAT_RE, diff --git a/youtube_dl/jsinterp2/jsbuilt_ins/jsarray.py b/youtube_dl/jsinterp2/jsbuilt_ins/jsarray.py index f6c329f3f..1fb0c2e37 100644 --- a/youtube_dl/jsinterp2/jsbuilt_ins/jsarray.py +++ b/youtube_dl/jsinterp2/jsbuilt_ins/jsarray.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +from youtube_dl.jsinterp2.jsbuilt_ins.internals import to_uint32, to_integer from .base import native_number, undefined from .jsobject import JSObject, JSObjectPrototype from .jsnumber import JSNumberPrototype @@ -55,8 +56,15 @@ class JSArrayPrototype(JSObjectPrototype): def _shift(self): return 'array shift' - def _slice(self, start, end): - return 'array slice' + def _slice(self, start, end=None): + from .utils import to_js + + length = to_uint32(to_js(len(self.value))) + start = to_integer(to_js(start)) + end = length if end is undefined else to_integer(to_js(end)) + start = min(start, length) if start > 0 else max(length + start, 0) + + return self.value[start:end] def _sort(self, cmp): return 'array sort'