[jsbuilt_ins] Fixing circular imports

This commit is contained in:
sulyi 2018-06-01 01:45:35 +02:00
parent 0136be4a19
commit 53f8eff485
11 changed files with 109 additions and 64 deletions

View File

@ -26,13 +26,14 @@ from . import (
try_statement, try_statement,
with_statement, with_statement,
debug, debug,
unshift unshift,
stringprototype
) )
modules = [basic, calc, empty_return, morespace, strange_chars, operators, unary, array_access, parens, assignments, 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, 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(): def gettestcases():

View 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': []
}
]

View File

@ -8,10 +8,8 @@ from . import jsboolean
from . import jsstring from . import jsstring
from . import jsnumber from . import jsnumber
undefined = base.JSBase('undefined') from .base import null, undefined
null = base.JSBase('null') from .jsboolean import false, true
true = jsboolean.JSBooleanPrototype(True)
false = jsboolean.JSBooleanPrototype(False)
def _eval(code): def _eval(code):

View File

@ -3,13 +3,6 @@ from __future__ import unicode_literals
from types import FunctionType from types import FunctionType
from ...compat import compat_str 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): class JSBase(object):
@ -21,6 +14,10 @@ class JSBase(object):
own = {} own = {}
undefined = JSBase('undefined')
null = JSBase('null')
class JSProtoBase(JSBase): class JSProtoBase(JSBase):
def __init__(self): def __init__(self):
@ -60,39 +57,6 @@ class JSProtoBase(JSBase):
jsclass = '' 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_bool = bool
native_string = compat_str native_string = compat_str
native_number = (int, float) native_number = (int, float)

View File

@ -4,12 +4,8 @@ import re
from math import isnan, isinf, log10 from math import isnan, isinf, log10
from sys import float_info from sys import float_info
from . import undefined, null, true, false from .base import native_bool, native_string, native_number, native_object
from .base import to_js, native_bool, native_string, native_number, native_object from .utils import to_js
from .jsobject import JSObjectPrototype
from .jsboolean import JSBooleanPrototype
from .jsstring import JSStringPrototype
from .jsnumber import JSNumberPrototype
from ..jsgrammar import __HEXADECIMAL_RE from ..jsgrammar import __HEXADECIMAL_RE
undefined_type = object() undefined_type = object()
@ -21,6 +17,9 @@ object_type = object()
def jstype(o): def jstype(o):
from .base import null, undefined
from .jsboolean import true, false
if o is undefined: if o is undefined:
return undefined_type return undefined_type
elif o is None or o is null: elif o is None or o is null:
@ -42,6 +41,12 @@ def to_primitive(o, hint=None):
def to_boolean(o): 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: if o is undefined or o is null:
return false return false
elif isinstance(o, JSBooleanPrototype): elif isinstance(o, JSBooleanPrototype):
@ -57,6 +62,11 @@ def to_boolean(o):
def to_number(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: if o is undefined:
return float('nan') return float('nan')
elif o is null or isinstance(o, JSBooleanPrototype) and o.value is false: 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): 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: if o is undefined:
return 'undefined' return 'undefined'
elif o is null: elif o is null:
@ -184,6 +199,12 @@ def to_string(o):
def to_object(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: if o is undefined or o is null:
raise Exception('TypeError: Cannot convert undefined or null to object') raise Exception('TypeError: Cannot convert undefined or null to object')
elif isinstance(o, JSBooleanPrototype): elif isinstance(o, JSBooleanPrototype):

View File

@ -1,7 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from . import undefined from .base import native_number, undefined
from .base import native_number
from .jsobject import JSObject, JSObjectPrototype from .jsobject import JSObject, JSObjectPrototype
from .jsnumber import JSNumberPrototype from .jsnumber import JSNumberPrototype

View File

@ -1,8 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from . import true
from .internals import jstype, boolean_type, object_type, to_boolean 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 from .jsobject import JSObject, JSObjectPrototype
@ -42,6 +41,10 @@ class JSBooleanPrototype(JSObjectPrototype):
} }
true = JSBooleanPrototype(True)
false = JSBooleanPrototype(False)
class JSBoolean(JSObject): class JSBoolean(JSObject):
@staticmethod @staticmethod

View File

@ -1,8 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from . import undefined, null from .base import undefined, null
from .internals import to_string, throw_type_error 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 from .jsobject import JSObject, JSObjectPrototype

View File

@ -1,7 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from .internals import jstype, number_type, to_number from .internals import jstype, number_type, to_number
from .base import to_js from .utils import to_js
from .jsobject import JSObject, JSObjectPrototype from .jsobject import JSObject, JSObjectPrototype

View File

@ -1,11 +1,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from . import null, undefined from .base import JSProtoBase, JSBase, null, undefined
from .base import JSProtoBase, to_js, js, JSBase from .utils import to_js, js
from .internals import to_object from .internals import to_object
from .jsboolean import JSBooleanPrototype
from .jsnumber import JSNumberPrototype
from .jsstring import JSStringPrototype
class JSObjectPrototype(JSProtoBase): class JSObjectPrototype(JSProtoBase):
@ -63,6 +60,10 @@ class JSObject(JSBase):
@staticmethod @staticmethod
def construct(value=None): def construct(value=None):
from .jsboolean import JSBooleanPrototype
from .jsnumber import JSNumberPrototype
from .jsstring import JSStringPrototype
value = to_js(value) value = to_js(value)
# TODO set [[Prototype]], [[Class]], [[Extensible]], internal methods # TODO set [[Prototype]], [[Class]], [[Extensible]], internal methods
if value is undefined or value is null: if value is undefined or value is null:

View 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