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