[jsbuilt_ins] Fixing circular imports
This commit is contained in:
parent
0136be4a19
commit
53f8eff485
@ -26,13 +26,14 @@ from . import (
|
||||
try_statement,
|
||||
with_statement,
|
||||
debug,
|
||||
unshift
|
||||
unshift,
|
||||
stringprototype
|
||||
)
|
||||
|
||||
|
||||
modules = [basic, calc, empty_return, morespace, strange_chars, operators, unary, array_access, parens, assignments,
|
||||
comments, precedence, call, getfield, branch, switch, for_loop, for_empty, for_in, do_loop, while_loop,
|
||||
label, func_expr, object_literal, try_statement, with_statement, debug, unshift]
|
||||
label, func_expr, object_literal, try_statement, with_statement, debug, unshift, stringprototype]
|
||||
|
||||
|
||||
def gettestcases():
|
||||
|
12
test/jstests/stringprototype.py
Normal file
12
test/jstests/stringprototype.py
Normal file
@ -0,0 +1,12 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
skip = {'parse': 'Ast not yet implemented'}
|
||||
|
||||
tests = [
|
||||
{
|
||||
'code': '"hello".split("");',
|
||||
'globals': {},
|
||||
'asserts': [{'value': ['h', 'e', 'l', 'l', 'o']}],
|
||||
'ast': []
|
||||
}
|
||||
]
|
@ -8,10 +8,8 @@ from . import jsboolean
|
||||
from . import jsstring
|
||||
from . import jsnumber
|
||||
|
||||
undefined = base.JSBase('undefined')
|
||||
null = base.JSBase('null')
|
||||
true = jsboolean.JSBooleanPrototype(True)
|
||||
false = jsboolean.JSBooleanPrototype(False)
|
||||
from .base import null, undefined
|
||||
from .jsboolean import false, true
|
||||
|
||||
|
||||
def _eval(code):
|
||||
|
@ -3,13 +3,6 @@ from __future__ import unicode_literals
|
||||
from types import FunctionType
|
||||
|
||||
from ...compat import compat_str
|
||||
from . import undefined
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsfunction import JSFunctionPrototype
|
||||
from .jsarray import JSArrayPrototype
|
||||
from .jsboolean import JSBooleanPrototype
|
||||
from .jsstring import JSStringPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
|
||||
|
||||
class JSBase(object):
|
||||
@ -21,6 +14,10 @@ class JSBase(object):
|
||||
own = {}
|
||||
|
||||
|
||||
undefined = JSBase('undefined')
|
||||
null = JSBase('null')
|
||||
|
||||
|
||||
class JSProtoBase(JSBase):
|
||||
|
||||
def __init__(self):
|
||||
@ -60,39 +57,6 @@ class JSProtoBase(JSBase):
|
||||
jsclass = ''
|
||||
|
||||
|
||||
def _get_formal_args(func):
|
||||
return func.__code__.co_varnames[func.__code__.co_argcount - len((func.__defaults__))]
|
||||
|
||||
|
||||
def to_js(o, name=None):
|
||||
if isinstance(o, JSProtoBase):
|
||||
return o
|
||||
elif o is None:
|
||||
return undefined
|
||||
elif isinstance(o, native_bool):
|
||||
return JSBooleanPrototype(o)
|
||||
elif isinstance(o, native_string):
|
||||
return JSStringPrototype(o)
|
||||
elif isinstance(o, native_number):
|
||||
return JSNumberPrototype(o)
|
||||
elif isinstance(o, native_object):
|
||||
return JSObjectPrototype(o)
|
||||
elif isinstance(o, native_function):
|
||||
return JSFunctionPrototype(name, o, _get_formal_args(o))
|
||||
elif isinstance(o, JSBase) and hasattr(o, 'call'):
|
||||
return JSFunctionPrototype(o.name, o, _get_formal_args(o.call))
|
||||
elif isinstance(o, native_array):
|
||||
return JSArrayPrototype(o)
|
||||
else:
|
||||
raise Exception('Not allowed conversion %s to js' % type(o))
|
||||
|
||||
|
||||
def js(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
return to_js(*func(*args, **kwargs))
|
||||
return wrapper
|
||||
|
||||
|
||||
native_bool = bool
|
||||
native_string = compat_str
|
||||
native_number = (int, float)
|
||||
|
@ -4,12 +4,8 @@ import re
|
||||
from math import isnan, isinf, log10
|
||||
from sys import float_info
|
||||
|
||||
from . import undefined, null, true, false
|
||||
from .base import to_js, native_bool, native_string, native_number, native_object
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsboolean import JSBooleanPrototype
|
||||
from .jsstring import JSStringPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
from .base import native_bool, native_string, native_number, native_object
|
||||
from .utils import to_js
|
||||
from ..jsgrammar import __HEXADECIMAL_RE
|
||||
|
||||
undefined_type = object()
|
||||
@ -21,6 +17,9 @@ object_type = object()
|
||||
|
||||
|
||||
def jstype(o):
|
||||
from .base import null, undefined
|
||||
from .jsboolean import true, false
|
||||
|
||||
if o is undefined:
|
||||
return undefined_type
|
||||
elif o is None or o is null:
|
||||
@ -42,6 +41,12 @@ def to_primitive(o, hint=None):
|
||||
|
||||
|
||||
def to_boolean(o):
|
||||
from .base import undefined, null
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsboolean import JSBooleanPrototype, false, true
|
||||
from .jsstring import JSStringPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
|
||||
if o is undefined or o is null:
|
||||
return false
|
||||
elif isinstance(o, JSBooleanPrototype):
|
||||
@ -57,6 +62,11 @@ def to_boolean(o):
|
||||
|
||||
|
||||
def to_number(o):
|
||||
from .base import null, undefined
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsboolean import JSBooleanPrototype, false, true
|
||||
from .jsstring import JSStringPrototype
|
||||
|
||||
if o is undefined:
|
||||
return float('nan')
|
||||
elif o is null or isinstance(o, JSBooleanPrototype) and o.value is false:
|
||||
@ -129,6 +139,11 @@ def to_uint16(o):
|
||||
|
||||
|
||||
def to_string(o):
|
||||
from .base import null, undefined
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsboolean import JSBooleanPrototype, false, true
|
||||
from .jsnumber import JSNumberPrototype
|
||||
|
||||
if o is undefined:
|
||||
return 'undefined'
|
||||
elif o is null:
|
||||
@ -184,6 +199,12 @@ def to_string(o):
|
||||
|
||||
|
||||
def to_object(o):
|
||||
from .base import null, undefined
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsboolean import JSBooleanPrototype
|
||||
from .jsstring import JSStringPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
|
||||
if o is undefined or o is null:
|
||||
raise Exception('TypeError: Cannot convert undefined or null to object')
|
||||
elif isinstance(o, JSBooleanPrototype):
|
||||
|
@ -1,7 +1,6 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from . import undefined
|
||||
from .base import native_number
|
||||
from .base import native_number, undefined
|
||||
from .jsobject import JSObject, JSObjectPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from . import true
|
||||
from .internals import jstype, boolean_type, object_type, to_boolean
|
||||
from .base import to_js
|
||||
from .utils import to_js
|
||||
from .jsobject import JSObject, JSObjectPrototype
|
||||
|
||||
|
||||
@ -42,6 +41,10 @@ class JSBooleanPrototype(JSObjectPrototype):
|
||||
}
|
||||
|
||||
|
||||
true = JSBooleanPrototype(True)
|
||||
false = JSBooleanPrototype(False)
|
||||
|
||||
|
||||
class JSBoolean(JSObject):
|
||||
|
||||
@staticmethod
|
||||
|
@ -1,8 +1,9 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from . import undefined, null
|
||||
from .base import undefined, null
|
||||
from .internals import to_string, throw_type_error
|
||||
from .base import to_js, native_function, JSBase
|
||||
from .base import native_function, JSBase
|
||||
from .utils import to_js
|
||||
from .jsobject import JSObject, JSObjectPrototype
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .internals import jstype, number_type, to_number
|
||||
from .base import to_js
|
||||
from .utils import to_js
|
||||
from .jsobject import JSObject, JSObjectPrototype
|
||||
|
||||
|
||||
|
@ -1,11 +1,8 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from . import null, undefined
|
||||
from .base import JSProtoBase, to_js, js, JSBase
|
||||
from .base import JSProtoBase, JSBase, null, undefined
|
||||
from .utils import to_js, js
|
||||
from .internals import to_object
|
||||
from .jsboolean import JSBooleanPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
from .jsstring import JSStringPrototype
|
||||
|
||||
|
||||
class JSObjectPrototype(JSProtoBase):
|
||||
@ -63,6 +60,10 @@ class JSObject(JSBase):
|
||||
|
||||
@staticmethod
|
||||
def construct(value=None):
|
||||
from .jsboolean import JSBooleanPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
from .jsstring import JSStringPrototype
|
||||
|
||||
value = to_js(value)
|
||||
# TODO set [[Prototype]], [[Class]], [[Extensible]], internal methods
|
||||
if value is undefined or value is null:
|
||||
|
45
youtube_dl/jsinterp/jsbuilt_ins/utils.py
Normal file
45
youtube_dl/jsinterp/jsbuilt_ins/utils.py
Normal file
@ -0,0 +1,45 @@
|
||||
from .base import (
|
||||
JSProtoBase, native_bool, native_string, native_number, native_object, native_function, JSBase, native_array
|
||||
)
|
||||
|
||||
|
||||
def _get_formal_args(func):
|
||||
return func.__code__.co_varnames[func.__code__.co_argcount - len((func.__defaults__))]
|
||||
|
||||
|
||||
def to_js(o, name=None):
|
||||
from .base import undefined
|
||||
|
||||
from .jsarray import JSArrayPrototype
|
||||
from .jsboolean import JSBooleanPrototype
|
||||
from .jsfunction import JSFunctionPrototype
|
||||
from .jsnumber import JSNumberPrototype
|
||||
from .jsobject import JSObjectPrototype
|
||||
from .jsstring import JSStringPrototype
|
||||
|
||||
if isinstance(o, JSProtoBase):
|
||||
return o
|
||||
elif o is None:
|
||||
return undefined
|
||||
elif isinstance(o, native_bool):
|
||||
return JSBooleanPrototype(o)
|
||||
elif isinstance(o, native_string):
|
||||
return JSStringPrototype(o)
|
||||
elif isinstance(o, native_number):
|
||||
return JSNumberPrototype(o)
|
||||
elif isinstance(o, native_object):
|
||||
return JSObjectPrototype(o)
|
||||
elif isinstance(o, native_function):
|
||||
return JSFunctionPrototype(name, o, _get_formal_args(o))
|
||||
elif isinstance(o, JSBase) and hasattr(o, 'call'):
|
||||
return JSFunctionPrototype(o.name, o, _get_formal_args(o.call))
|
||||
elif isinstance(o, native_array):
|
||||
return JSArrayPrototype(o)
|
||||
else:
|
||||
raise Exception('Not allowed conversion %s to js' % type(o))
|
||||
|
||||
|
||||
def js(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
return to_js(*func(*args, **kwargs))
|
||||
return wrapper
|
Loading…
x
Reference in New Issue
Block a user