sulyi 93c0bb53a6 [jsinterp] Fixing types and operators
- Adds `jsbuilt_ins.nan` and `jsbuilt_ins.infinity`
- Adds arithmetic operator overload to
  `jsbuilt_ins.jsnumber.JSNumberPrototype`
- Adds equality operator overload to `jsinterp.Reference`
- Adds better strict equality and typeof operator in `tstream`
2018-06-11 07:47:53 +02:00

107 lines
2.8 KiB
Python

from __future__ import unicode_literals
from .internals import jstype, number_type, to_number
from .utils import to_js
from .jsobject import JSObject, JSObjectPrototype
class JSNumberPrototype(JSObjectPrototype):
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__?
@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'))
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,
}