1    // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL ]
2    
3    package org.xwt.js; 
4    import org.xwt.util.*; 
5    import java.io.*;
6    import java.util.*;
7    
8    /** The JavaScript Math object */
9    public class JSMath extends JS {
10   
11       public static JSMath singleton = new JSMath();
12   
13       private static final Double E       = new Double(java.lang.Math.E);
14       private static final Double PI      = new Double(java.lang.Math.PI);
15       private static final Double LN10    = new Double(java.lang.Math.log(10));
16       private static final Double LN2     = new Double(java.lang.Math.log(2));
17       private static final Double LOG10E  = new Double(1/java.lang.Math.log(10));
18       private static final Double LOG2E   = new Double(1/java.lang.Math.log(2));
19       private static final Double SQRT1_2 = new Double(1/java.lang.Math.sqrt(2));
20       private static final Double SQRT2   = new Double(java.lang.Math.sqrt(2));
21   
22       public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
23           switch(nargs) {
24               case 0: {
25                   //#switch(method)
26                   case "random": return new Double(java.lang.Math.random());
27                   //#end
28                   break;
29               }
30               case 1: {
31                   //#switch(method)
32                   case "ceil": return new Long((long)java.lang.Math.ceil(toDouble(a0)));
33                   case "floor": return new Long((long)java.lang.Math.floor(toDouble(a0)));
34                   case "round": return new Long((long)java.lang.Math.round(toDouble(a0)));
35                   case "abs": return new Double(java.lang.Math.abs(toDouble(a0)));
36                   case "sin": return new Double(java.lang.Math.sin(toDouble(a0)));
37                   case "cos": return new Double(java.lang.Math.cos(toDouble(a0)));
38                   case "tan": return new Double(java.lang.Math.tan(toDouble(a0)));
39                   case "asin": return new Double(java.lang.Math.asin(toDouble(a0)));
40                   case "acos": return new Double(java.lang.Math.acos(toDouble(a0)));
41                   case "atan": return new Double(java.lang.Math.atan(toDouble(a0)));
42                   case "sqrt": return new Double(java.lang.Math.sqrt(toDouble(a0)));
43                   case "exp": return new Double(java.lang.Math.exp(toDouble(a0)));
44                   case "log": return new Double(java.lang.Math.log(toDouble(a0)));
45                   //#end
46                   break;
47               }
48               case 2: {
49                   //#switch(method)
50                   case "min": return new Double(java.lang.Math.min(toDouble(a0), toDouble(a1)));
51                   case "max": return new Double(java.lang.Math.max(toDouble(a0), toDouble(a1)));
52                   case "pow": return new Double(java.lang.Math.pow(toDouble(a0), toDouble(a1)));
53                   case "atan2": return new Double(java.lang.Math.atan2(toDouble(a0), toDouble(a1)));
54                   //#end
55                   break;
56               }
57           }
58           return super.callMethod(method, a0, a1, a2, rest, nargs);
59       }
60   
61       public void put(Object key, Object val) { return; }
62   
63       public Object get(Object key) throws JSExn {
64           //#switch(key)
65           case "E": return E;
66           case "LN10": return LN10;
67           case "LN2": return LN2;
68           case "LOG10E": return LOG10E;
69           case "LOG2E": return LOG2E;
70           case "PI": return PI;
71           case "SQRT1_2": return SQRT1_2;
72           case "SQRT2": return SQRT2;
73           case "ceil": return METHOD;
74           case "floor": return METHOD;
75           case "round": return METHOD;
76           case "min": return METHOD;
77           case "max": return METHOD;
78           case "pow": return METHOD;
79           case "atan2": return METHOD;
80           case "abs": return METHOD;
81           case "sin": return METHOD;
82           case "cos": return METHOD;
83           case "tan": return METHOD;
84           case "asin": return METHOD;
85           case "acos": return METHOD;
86           case "atan": return METHOD;
87           case "sqrt": return METHOD;
88           case "exp": return METHOD;
89           case "log": return METHOD;
90           case "random": return METHOD;
91           //#end
92           return super.get(key);
93       }
94   }
95