63 lines
1.5 KiB
Python
Raw Normal View History

2017-02-21 22:02:56 +01:00
from __future__ import unicode_literals
from .internals import jstype, boolean_type, object_type, to_boolean
2018-06-01 01:45:35 +02:00
from .utils import to_js
2017-02-21 22:02:56 +01:00
from .jsobject import JSObject, JSObjectPrototype
class JSBooleanPrototype(JSObjectPrototype):
def __init__(self, value=None):
2017-03-02 21:22:11 +01:00
super(JSBooleanPrototype, self).__init__()
2017-02-21 22:02:56 +01:00
if value is None:
# prototype
value = False
2017-03-02 21:22:11 +01:00
else:
self.value = value
self.own = {}
2017-02-21 22:02:56 +01:00
@staticmethod
def _constructor(value=None):
return JSBoolean.construct(value)
def _to_string(self):
# TODO find way to test it in other interpreters
if jstype(self) is boolean_type:
b = self
elif jstype(self) is object_type and self.jsclass == 'Boolean':
b = self.value
else:
raise Exception('TypeError')
return 'true' if b is true else 'false'
def _value_of(self):
return 'boolean value of'
jsclass = 'Boolean'
own = {
'constructor': _constructor,
'toString': _to_string,
'valueOf': _value_of
}
2018-06-01 01:45:35 +02:00
true = JSBooleanPrototype(True)
false = JSBooleanPrototype(False)
2017-02-21 22:02:56 +01:00
class JSBoolean(JSObject):
@staticmethod
def call(value=None):
return to_boolean(to_js(value))
@staticmethod
def construct(value=None):
return JSBooleanPrototype(to_boolean(to_js(value)))
name = JSBooleanPrototype.jsclass
own = {
'length': 1,
'prototype': JSBooleanPrototype()
}