Author: Stephan <[email protected]>
Branch: 
Changeset: r192:5ac599e5a97b
Date: 2012-05-21 12:38 +0200
http://bitbucket.org/pypy/lang-js/changeset/5ac599e5a97b/

Log:    11.4.6

diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -132,49 +132,8 @@
     js.builtins_array.setup(global_object)
 
     #Math
-    from js.jsobj import W_Math
-    # 15.8
-    w_Math = W_Math()
-    put_property(global_object, 'Math', w_Math)
-
-    #w_math.Put('__proto__',  w_ObjPrototype)
-
-    import js.builtins_math as math_builtins
-    put_native_function(w_Math, 'abs', math_builtins.abs)
-    put_native_function(w_Math, 'floor', math_builtins.floor)
-    put_native_function(w_Math, 'round', math_builtins.round)
-    put_native_function(w_Math, 'random', math_builtins.random)
-    put_native_function(w_Math, 'min', math_builtins.min)
-    put_native_function(w_Math, 'max', math_builtins.max)
-    put_native_function(w_Math, 'pow', math_builtins.pow)
-    put_native_function(w_Math, 'sqrt', math_builtins.sqrt)
-    put_native_function(w_Math, 'log', math_builtins.log)
-
-    # 15.8.1
-
-    # 15.8.1.1
-    put_property(w_Math, 'E', _w(math_builtins.E), writable = False, 
enumerable = False, configurable = False)
-
-    # 15.8.1.2
-    put_property(w_Math, 'LN10', _w(math_builtins.LN10), writable = False, 
enumerable = False, configurable = False)
-
-    # 15.8.1.3
-    put_property(w_Math, 'LN2', _w(math_builtins.LN2), writable = False, 
enumerable = False, configurable = False)
-
-    # 15.8.1.4
-    put_property(w_Math, 'LOG2E', _w(math_builtins.LOG2E), writable = False, 
enumerable = False, configurable = False)
-
-    # 15.8.1.5
-    put_property(w_Math, 'LOG10E', _w(math_builtins.LOG10E), writable = False, 
enumerable = False, configurable = False)
-
-    # 15.8.1.6
-    put_property(w_Math, 'PI', _w(math_builtins.PI), writable = False, 
enumerable = False, configurable = False)
-
-    # 15.8.1.7
-    put_property(w_Math, 'SQRT1_2', _w(math_builtins.SQRT1_2), writable = 
False, enumerable = False, configurable = False)
-
-    # 15.8.1.8
-    put_property(w_Math, 'SQRT2', _w(math_builtins.SQRT2), writable = False, 
enumerable = False, configurable = False)
+    import js.builtins_math
+    js.builtins_math.setup(global_object)
 
     import js.builtins_date
     js.builtins_date.setup(global_object)
diff --git a/js/builtins_math.py b/js/builtins_math.py
--- a/js/builtins_math.py
+++ b/js/builtins_math.py
@@ -1,7 +1,51 @@
 import math
-from js.jsobj import W_IntNumber
+from js.jsobj import _w
 
 from pypy.rlib.rfloat import NAN, INFINITY, isnan, isinf
+from js.builtins import get_arg
+
+def setup(global_object):
+    from js.builtins import put_native_function, put_property
+    from js.jsobj import W_Math
+    # 15.8
+    w_Math = W_Math()
+    put_property(global_object, 'Math', w_Math)
+
+    put_native_function(w_Math, 'abs', js_abs)
+    put_native_function(w_Math, 'floor', floor)
+    put_native_function(w_Math, 'round', js_round)
+    put_native_function(w_Math, 'random', random)
+    put_native_function(w_Math, 'min', js_min)
+    put_native_function(w_Math, 'max', js_max)
+    put_native_function(w_Math, 'pow', pow)
+    put_native_function(w_Math, 'sqrt', sqrt)
+    put_native_function(w_Math, 'log', log)
+
+    # 15.8.1
+
+    # 15.8.1.1
+    put_property(w_Math, 'E', _w(E), writable = False, enumerable = False, 
configurable = False)
+
+    # 15.8.1.2
+    put_property(w_Math, 'LN10', _w(LN10), writable = False, enumerable = 
False, configurable = False)
+
+    # 15.8.1.3
+    put_property(w_Math, 'LN2', _w(LN2), writable = False, enumerable = False, 
configurable = False)
+
+    # 15.8.1.4
+    put_property(w_Math, 'LOG2E', _w(LOG2E), writable = False, enumerable = 
False, configurable = False)
+
+    # 15.8.1.5
+    put_property(w_Math, 'LOG10E', _w(LOG10E), writable = False, enumerable = 
False, configurable = False)
+
+    # 15.8.1.6
+    put_property(w_Math, 'PI', _w(PI), writable = False, enumerable = False, 
configurable = False)
+
+    # 15.8.1.7
+    put_property(w_Math, 'SQRT1_2', _w(SQRT1_2), writable = False, enumerable 
= False, configurable = False)
+
+    # 15.8.1.8
+    put_property(w_Math, 'SQRT2', _w(SQRT2), writable = False, enumerable = 
False, configurable = False)
 
 # 15.8.2.9
 def floor(this, args):
@@ -17,22 +61,71 @@
     return pos
 
 # 15.8.2.1
-py_abs = abs
-def abs(this, args):
+def js_abs(this, args):
     val = args[0]
-    #if isinstance(val, W_IntNumber):
-        #if val.ToInteger() > 0:
-            #return val # fast path
-        #return -val.ToInteger()
-    return py_abs(val.ToNumber())
+    return abs(val.ToNumber())
 
 # 15.8.2.15
-def round(this, args):
+def js_round(this, args):
     return floor(this, args)
 
+def isodd(i):
+    isinstance(i, int) and i % 2 == 1
+
 # 15.8.2.13
 def pow(this, args):
-    return math.pow(args[0].ToNumber(), args[1].ToNumber())
+    w_x = get_arg(args, 0)
+    w_y = get_arg(args, 1)
+    x = w_x.ToNumber()
+    y = w_y.ToNumber()
+
+    if isnan(y):
+        return NAN
+    if y == 0:
+        return 1
+    if isnan(x):
+        return NAN
+    if abs(x) > 1 and y == INFINITY:
+        return INFINITY
+    if abs(x) > 1 and y == -INFINITY:
+        return 0
+    if abs(x) == 1 and isinf(y):
+        return NAN
+    if abs(x) < 1 and y == INFINITY:
+        return 0
+    if abs(x) < 1 and y == -INFINITY:
+        return INFINITY
+    if x == INFINITY and y > 0:
+        return INFINITY
+    if x == INFINITY and y < 0:
+        return 0
+    if x == -INFINITY and y > 0 and isodd(y):
+        return -INFINITY
+    if x == -INFINITY and y > 0 and not isodd(y):
+        return INFINITY
+    if x == -INFINITY and y < 0 and isodd(y):
+        return -0
+    if x == -INFINITY and y < 0 and not isodd(y):
+        return 0
+    if x == 0 and y > 0:
+        return 0
+    if x == 0 and y < 0:
+        return INFINITY
+    if x == -0 and y > 0 and isodd(y):
+        return -0
+    if x == -0 and y > 0 and not isodd(y):
+        return +0
+    if x == -0 and y < 0 and isodd(y):
+        return -INFINITY
+    if x == -0 and y < 0 and not isodd(y):
+        return INFINITY
+    if x < 0 and not isinstance(y, int):
+        return NAN
+
+    try:
+        return math.pow(x, y)
+    except OverflowError:
+        return INFINITY
 
 # 15.8.2.17
 def sqrt(this, args):
@@ -43,18 +136,16 @@
     return math.log(args[0].ToNumber())
 
 # 15.8.2.11
-py_min = min
-def min(this, args):
+def js_min(this, args):
     a = args[0].ToNumber()
     b = args[1].ToNumber()
-    return py_min(a, b)
+    return min(a, b)
 
 # 15.8.2.12
-py_max = max
-def max(this, args):
+def js_max(this, args):
     a = args[0].ToNumber()
     b = args[1].ToNumber()
-    return py_max(a, b)
+    return max(a, b)
 
 import time
 from pypy.rlib import rrandom
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -1158,6 +1158,8 @@
     def ToNumber(self):
         if not self._strval_:
             return 0.0
+        if self._strval_.isspace():
+            return 0.0
         try:
             return float(self._strval_)
         except ValueError:
diff --git a/js/opcodes.py b/js/opcodes.py
--- a/js/opcodes.py
+++ b/js/opcodes.py
@@ -298,11 +298,10 @@
 
 class UPLUS(BaseUnaryOperation):
     def eval(self, ctx):
-        if isinstance(ctx.stack_top(), W_IntNumber):
-            return
-        if isinstance(ctx.stack_top(), W_FloatNumber):
-            return
-        ctx.stack_append(W_FloatNumber(ctx.stack_pop().ToNumber()))
+        expr = ctx.stack_pop()
+        num = expr.ToNumber()
+        res = _w(num)
+        ctx.stack_append(res)
 
 class UMINUS(BaseUnaryOperation):
     def eval(self, ctx):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to