[jsinterp] Fixing compatibility

- compat_str
- unittest2
This commit is contained in:
sulyi 2016-12-10 00:52:04 +01:00
parent c5c1273ba5
commit 6fa4eb6208
3 changed files with 16 additions and 3 deletions

View File

@ -5,7 +5,10 @@ from __future__ import unicode_literals
# Allow direct execution
import os
import sys
import unittest
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from youtube_dl.jsinterp import JSInterpreter
@ -127,6 +130,7 @@ class TestJSInterpreter(unittest.TestCase):
''')
self.assertEqual(jsi.call_function('c'), 0)
@unittest.skip('Context creation not yet implemented')
def test_getfield(self):
jsi = JSInterpreter('function c() { return a.var; }', objects={'a': {'var': 3}})
self.assertEqual(jsi.call_function('c'), 3)

View File

@ -5,7 +5,10 @@ from __future__ import unicode_literals
# Allow direct execution
import os
import sys
import unittest
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from youtube_dl.jsinterp import JSInterpreter

View File

@ -1,7 +1,9 @@
from __future__ import unicode_literals
from ..compat import compat_str
import re
from ..utils import ExtractorError
from .tstream import TokenStream
from .jsgrammar import Token
@ -13,6 +15,7 @@ class Context(object):
def __init__(self, ended=False, vaiables=None, objects=None, functions=None):
self.ended = ended
self.local_vars = {} if vaiables is None else vaiables
# XXX There's probably no need for these
self.objects = {} if objects is None else objects
self.functions = {} if functions is None else functions
@ -29,6 +32,7 @@ class JSInterpreter(object):
def __init__(self, code, objects=None):
self.code = code
self.global_vars = {}
self.context = Context(objects=objects)
self._context_stack = []
@ -459,7 +463,8 @@ class JSInterpreter(object):
# TODO use context instead local_vars in argument
def getvalue(self, ref, local_vars):
if ref.value is None or ref.value is self.undefined or isinstance(ref.value, (int, float, str, list)):
if (ref.value is None or ref.value is self.undefined or
isinstance(ref.value, (int, float, str, compat_str, list))):
return ref.value
ref_id, ref_value = ref.value
if ref_id is Token.ID:
@ -643,6 +648,7 @@ class JSInterpreter(object):
def build_function(self, argnames, code):
def resf(args):
# TODO Create context
local_vars = dict(zip(argnames, args))
for stmt in self.statements(code):
res, abort = self.interpret_statement(stmt, local_vars)