renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / JSMath.java
index 04828ed..7a805e7 100644 (file)
@@ -1,64 +1,63 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL ]
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
 
 package org.ibex.js; 
 
 /** The JavaScript Math object */
-public class JSMath extends JS {
+class JSMath extends JS.Immutable {
+    private static final JS.Method METHOD = new JS.Method();
 
-    public static JSMath singleton = new JSMath();
+    private static final JS E       = JSU.N(java.lang.Math.E);
+    private static final JS PI      = JSU.N(java.lang.Math.PI);
+    private static final JS LN10    = JSU.N(java.lang.Math.log(10));
+    private static final JS LN2     = JSU.N(java.lang.Math.log(2));
+    private static final JS LOG10E  = JSU.N(1/java.lang.Math.log(10));
+    private static final JS LOG2E   = JSU.N(1/java.lang.Math.log(2));
+    private static final JS SQRT1_2 = JSU.N(1/java.lang.Math.sqrt(2));
+    private static final JS SQRT2   = JSU.N(java.lang.Math.sqrt(2));
 
-    private static final Double E       = new Double(java.lang.Math.E);
-    private static final Double PI      = new Double(java.lang.Math.PI);
-    private static final Double LN10    = new Double(java.lang.Math.log(10));
-    private static final Double LN2     = new Double(java.lang.Math.log(2));
-    private static final Double LOG10E  = new Double(1/java.lang.Math.log(10));
-    private static final Double LOG2E   = new Double(1/java.lang.Math.log(2));
-    private static final Double SQRT1_2 = new Double(1/java.lang.Math.sqrt(2));
-    private static final Double SQRT2   = new Double(java.lang.Math.sqrt(2));
-
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        switch(nargs) {
+    public JS call(JS method, JS[] args) throws JSExn {
+        switch(args.length) {
             case 0: {
-                //#switch(method)
-                case "random": return new Double(java.lang.Math.random());
+                //#switch(JSU.toString(method))
+                case "random": return JSU.N(java.lang.Math.random());
                 //#end
                 break;
             }
             case 1: {
-                //#switch(method)
-                case "ceil": return new Long((long)java.lang.Math.ceil(toDouble(a0)));
-                case "floor": return new Long((long)java.lang.Math.floor(toDouble(a0)));
-                case "round": return new Long((long)java.lang.Math.round(toDouble(a0)));
-                case "abs": return new Double(java.lang.Math.abs(toDouble(a0)));
-                case "sin": return new Double(java.lang.Math.sin(toDouble(a0)));
-                case "cos": return new Double(java.lang.Math.cos(toDouble(a0)));
-                case "tan": return new Double(java.lang.Math.tan(toDouble(a0)));
-                case "asin": return new Double(java.lang.Math.asin(toDouble(a0)));
-                case "acos": return new Double(java.lang.Math.acos(toDouble(a0)));
-                case "atan": return new Double(java.lang.Math.atan(toDouble(a0)));
-                case "sqrt": return new Double(java.lang.Math.sqrt(toDouble(a0)));
-                case "exp": return new Double(java.lang.Math.exp(toDouble(a0)));
-                case "log": return new Double(java.lang.Math.log(toDouble(a0)));
+                //#switch(JSU.toString(method))
+                case "ceil": return JSU.N((long)java.lang.Math.ceil(JSU.toDouble(args[0])));
+                case "floor": return JSU.N((long)java.lang.Math.floor(JSU.toDouble(args[0])));
+                case "round": return JSU.N((long)java.lang.Math.round(JSU.toDouble(args[0])));
+                case "abs": return JSU.N(java.lang.Math.abs(JSU.toDouble(args[0])));
+                case "sin": return JSU.N(java.lang.Math.sin(JSU.toDouble(args[0])));
+                case "cos": return JSU.N(java.lang.Math.cos(JSU.toDouble(args[0])));
+                case "tan": return JSU.N(java.lang.Math.tan(JSU.toDouble(args[0])));
+                case "asin": return JSU.N(java.lang.Math.asin(JSU.toDouble(args[0])));
+                case "acos": return JSU.N(java.lang.Math.acos(JSU.toDouble(args[0])));
+                case "atan": return JSU.N(java.lang.Math.atan(JSU.toDouble(args[0])));
+                case "sqrt": return JSU.N(java.lang.Math.sqrt(JSU.toDouble(args[0])));
+                case "exp": return JSU.N(java.lang.Math.exp(JSU.toDouble(args[0])));
+                case "log": return JSU.N(java.lang.Math.log(JSU.toDouble(args[0])));
                 //#end
                 break;
             }
             case 2: {
-                //#switch(method)
-                case "min": return new Double(java.lang.Math.min(toDouble(a0), toDouble(a1)));
-                case "max": return new Double(java.lang.Math.max(toDouble(a0), toDouble(a1)));
-                case "pow": return new Double(java.lang.Math.pow(toDouble(a0), toDouble(a1)));
-                case "atan2": return new Double(java.lang.Math.atan2(toDouble(a0), toDouble(a1)));
+                //#switch(JSU.toString(method))
+                case "min": return JSU.N(java.lang.Math.min(JSU.toDouble(args[0]), JSU.toDouble(args[1])));
+                case "max": return JSU.N(java.lang.Math.max(JSU.toDouble(args[0]), JSU.toDouble(args[1])));
+                case "pow": return JSU.N(java.lang.Math.pow(JSU.toDouble(args[0]), JSU.toDouble(args[1])));
+                case "atan2": return JSU.N(java.lang.Math.atan2(JSU.toDouble(args[0]), JSU.toDouble(args[1])));
                 //#end
                 break;
             }
         }
-        return super.callMethod(method, a0, a1, a2, rest, nargs);
+        return super.call(method, args);
     }
 
-    public void put(Object key, Object val) { }
-
-    public Object get(Object key) throws JSExn {
-        //#switch(key)
+    public JS get(JS key) throws JSExn {
+        //#switch(JSU.toString(key))
         case "E": return E;
         case "LN10": return LN10;
         case "LN2": return LN2;