107 lines
2.8 KiB
Python
Raw Normal View History

2017-02-21 22:02:56 +01:00
from __future__ import unicode_literals
from .internals import jstype, number_type, to_number
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 JSNumberPrototype(JSObjectPrototype):
2017-03-02 21:22:11 +01:00
def __init__(self, value=None):
super(JSNumberPrototype, self).__init__()
if value is None:
# prototype
value = 0
else:
self.value = value
self.own = {}
def __add__(self, other):
if isinstance(other, JSNumberPrototype):
other = other.value
return JSNumberPrototype(self.value + other)
def __sub__(self, other):
if isinstance(other, JSNumberPrototype):
other = other.value
return JSNumberPrototype(self.value - other)
def __mul__(self, other):
if isinstance(other, JSNumberPrototype):
other = other.value
return JSNumberPrototype(self.value * other)
def __div__(self, other):
if isinstance(other, JSNumberPrototype):
other = other.value
return JSNumberPrototype(self.value / other)
def __neg__(self):
return JSNumberPrototype(-self.value)
def __pos__(self):
return JSNumberPrototype(+self.value)
# __invert__?
2017-02-21 22:02:56 +01:00
@staticmethod
def _constructor(value=None):
return JSNumber.construct(value)
def _to_string(self, radix=None):
pass
def _to_locale_string(self):
pass
def _value_of(self):
if jstype(self.value) is not number_type or isinstance(self.value, JSNumberPrototype):
# TODO find way to test it in other interpreters
raise Exception('TypeError')
return self.value
def _to_fixed(self, frac_digits):
return 'Number toFixed'
def _to_exponential(self, frac_digits):
return 'Number toExponential'
def _to_precision(self, prec):
return 'Number toPrecision'
jsclass = 'Number'
own = {
'constructor': _constructor,
'toString': _to_string,
'toLocaleString': _to_locale_string,
'valueOf': _value_of,
'toFixed': _to_fixed,
'toExponential': _to_exponential,
'toPrecision': _to_precision
}
nan = JSNumberPrototype(float('nan'))
infinity = JSNumberPrototype(float('inf'))
2017-02-21 22:02:56 +01:00
class JSNumber(JSObject):
@staticmethod
def call(value=None):
return to_number(to_js(value)) if value is not None else 0
@staticmethod
def construct(value=None):
return JSNumberPrototype(to_number(to_js(value)) if value is not None else 0)
name = JSNumberPrototype.jsclass
own = {
'length': 1,
'prototype': JSNumberPrototype(),
'MAX_VALUE': 1.7976931348623157 * 10 ** 308,
'MIN_VALUE': 5 * 10 ** (-324),
'NAN': nan,
'NEGATIVE_INFINITY': infinity * -1,
'POSITIVE_INFINITY': infinity,
2017-02-21 22:02:56 +01:00
}