Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def boolean_constructor(value):
temp = PyJsObject(prototype=BooleanPrototype)
temp.Class = 'Boolean'
#temp.TYPE = 'Boolean'
temp.value = value.to_boolean().value
return temp
# mod = {}
# for name in dir(val):
# value = getattr(val, name)
# if isinstance(value, ModuleType):
# continue # prevent recursive module conversion
# try:
# jsval = HJs(value)
# except RuntimeError:
# print 'Could not convert %s to PyJs object!' % name
# continue
# mod[name] = jsval
# return Js(mod)
#elif isintance(val, ClassType):
elif isinstance(val, dict): # convert to object
temp = PyJsObject({}, ObjectPrototype)
for k, v in six.iteritems(val):
temp.put(Js(k), Js(v))
return temp
elif isinstance(val, (list, tuple)): #Convert to array
return PyJsArray(val, ArrayPrototype)
# convert to typedarray
elif isinstance(val, JsObjectWrapper):
return val.__dict__['_obj']
elif NUMPY_AVAILABLE and isinstance(val, numpy.ndarray):
if val.dtype == numpy.int8:
return PyJsInt8Array(val, Int8ArrayPrototype)
elif val.dtype == numpy.uint8 and not Clamped:
return PyJsUint8Array(val, Uint8ArrayPrototype)
elif val.dtype == numpy.uint8 and Clamped:
return PyJsUint8ClampedArray(val, Uint8ClampedArrayPrototype)
elif val.dtype == numpy.int16:
if char not in CHAR_BANK:
Js(char) # this will add char to CHAR BANK
return CHAR_BANK[char]
except Exception:
pass
return PyJs.get(self, prop)
def can_put(self, prop):
return False
def __iter__(self):
for i in xrange(len(self.value)):
yield Js(i) # maybe create an int bank?
StringPrototype = PyJsObject({}, ObjectPrototype)
StringPrototype.Class = 'String'
StringPrototype.value = ''
CHAR_BANK[''] = Js('')
#Boolean
class PyJsBoolean(PyJs):
TYPE = 'Boolean'
Class = 'Boolean'
BooleanPrototype = PyJsObject({}, ObjectPrototype)
BooleanPrototype.Class = 'Boolean'
BooleanPrototype.value = False
StringPrototype = PyJsObject({}, ObjectPrototype)
StringPrototype.Class = 'String'
StringPrototype.value = ''
CHAR_BANK[''] = Js('')
#Boolean
class PyJsBoolean(PyJs):
TYPE = 'Boolean'
Class = 'Boolean'
BooleanPrototype = PyJsObject({}, ObjectPrototype)
BooleanPrototype.Class = 'Boolean'
BooleanPrototype.value = False
true = PyJsBoolean(True, BooleanPrototype)
false = PyJsBoolean(False, BooleanPrototype)
#Undefined
class PyJsUndefined(PyJs):
TYPE = 'Undefined'
Class = 'Undefined'
def __init__(self):
pass
def construct(self, *args):
proto = self.get('prototype')
if not proto.is_object(): # set to standard prototype
proto = ObjectPrototype
obj = PyJsObject(prototype=proto)
cand = self.call(obj, *args)
return cand if cand.is_object() else obj
#Object
class PyJsObject(PyJs):
Class = 'Object'
def __init__(self, prop_descs={}, prototype=None, extensible=True):
self.prototype = prototype
self.extensible = extensible
self.own = {}
for prop, desc in six.iteritems(prop_descs):
self.define_own_property(prop, desc)
def __repr__(self):
return repr(self.to_python().to_dict())
ObjectPrototype = PyJsObject()
#Function
class PyJsFunction(PyJs):
Class = 'Function'
def __init__(self, func, prototype=None, extensible=True, source=None):
cand = fix_js_args(func)
has_scope = cand is func
func = cand
self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
self.code = func
self.source = source if source else '{ [python code] }'
self.func_name = func.__name__ if not func.__name__.startswith(
'PyJs_anonymous') else ''
self.extensible = extensible
def number_constructor():
temp = PyJsObject(prototype=NumberPrototype)
temp.Class = 'Number'
#temp.TYPE = 'Number'
if len(arguments):
temp.value = arguments[0].to_number().value
else:
temp.value = 0
return temp
def create(self, *args):
proto = self.get('prototype')
if not proto.is_object():
proto = ObjectPrototype
new = PyJsObject(prototype=proto)
res = self.call(new, args)
if res.is_object():
return res
return new
def string_constructor():
temp = PyJsObject(prototype=StringPrototype)
temp.Class = 'String'
#temp.TYPE = 'String'
if not len(arguments):
temp.value = ''
else:
temp.value = arguments[0].to_string().value
for i, ch in enumerate(temp.value): # this will make things long...
temp.own[str(i)] = {
'value': Js(ch),
'writable': False,
'enumerable': True,
'configurable': True
}
temp.own['length'] = {
'value': Js(len(temp.value)),
'writable': False,
return None
elif isinstance(val, PyJsNumber):
# this can be either float or long/int better to assume its int/long when a whole number...
v = val.value
try:
i = int(v) if v == v else v # nan...
return v if i != v else i
except:
return v
elif isinstance(val, (PyJsString, PyJsBoolean)):
return val.value
elif isinstance(val, PyObjectWrapper):
return val.__dict__['obj']
elif isinstance(val, PyJsArray) and val.CONVERT_TO_PY_PRIMITIVES:
return to_list(val)
elif isinstance(val, PyJsObject) and val.CONVERT_TO_PY_PRIMITIVES:
return to_dict(val)
else:
return JsObjectWrapper(val)