updated Makefile.common
[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 class JSMath extends JS {
7     private static final JS E       = JS.N(java.lang.Math.E);
8     private static final JS PI      = JS.N(java.lang.Math.PI);
9     private static final JS LN10    = JS.N(java.lang.Math.log(10));
10     private static final JS LN2     = JS.N(java.lang.Math.log(2));
11     private static final JS LOG10E  = JS.N(1/java.lang.Math.log(10));
12     private static final JS LOG2E   = JS.N(1/java.lang.Math.log(2));
13     private static final JS SQRT1_2 = JS.N(1/java.lang.Math.sqrt(2));
14     private static final JS SQRT2   = JS.N(java.lang.Math.sqrt(2));
15
16     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
17         switch(nargs) {
18             case 0: {
19                 //#jswitch(method)
20                 case "random": return JS.N(java.lang.Math.random());
21                 //#end
22                 break;
23             }
24             case 1: {
25                 //#jswitch(method)
26                 case "ceil": return JS.N((long)java.lang.Math.ceil(toDouble(a0)));
27                 case "floor": return JS.N((long)java.lang.Math.floor(toDouble(a0)));
28                 case "round": return JS.N((long)java.lang.Math.round(toDouble(a0)));
29                 case "abs": return JS.N(java.lang.Math.abs(toDouble(a0)));
30                 case "sin": return JS.N(java.lang.Math.sin(toDouble(a0)));
31                 case "cos": return JS.N(java.lang.Math.cos(toDouble(a0)));
32                 case "tan": return JS.N(java.lang.Math.tan(toDouble(a0)));
33                 case "asin": return JS.N(java.lang.Math.asin(toDouble(a0)));
34                 case "acos": return JS.N(java.lang.Math.acos(toDouble(a0)));
35                 case "atan": return JS.N(java.lang.Math.atan(toDouble(a0)));
36                 case "sqrt": return JS.N(java.lang.Math.sqrt(toDouble(a0)));
37                 case "exp": return JS.N(java.lang.Math.exp(toDouble(a0)));
38                 case "log": return JS.N(java.lang.Math.log(toDouble(a0)));
39                 //#end
40                 break;
41             }
42             case 2: {
43                 //#jswitch(method)
44                 case "min": return JS.N(java.lang.Math.min(toDouble(a0), toDouble(a1)));
45                 case "max": return JS.N(java.lang.Math.max(toDouble(a0), toDouble(a1)));
46                 case "pow": return JS.N(java.lang.Math.pow(toDouble(a0), toDouble(a1)));
47                 case "atan2": return JS.N(java.lang.Math.atan2(toDouble(a0), toDouble(a1)));
48                 //#end
49                 break;
50             }
51         }
52         return super.callMethod(method, a0, a1, a2, rest, nargs);
53     }
54
55     public JS get(JS key) throws JSExn {
56         //#jswitch(key)
57         case "E": return E;
58         case "LN10": return LN10;
59         case "LN2": return LN2;
60         case "LOG10E": return LOG10E;
61         case "LOG2E": return LOG2E;
62         case "PI": return PI;
63         case "SQRT1_2": return SQRT1_2;
64         case "SQRT2": return SQRT2;
65         case "ceil": return METHOD;
66         case "floor": return METHOD;
67         case "round": return METHOD;
68         case "min": return METHOD;
69         case "max": return METHOD;
70         case "pow": return METHOD;
71         case "atan2": return METHOD;
72         case "abs": return METHOD;
73         case "sin": return METHOD;
74         case "cos": return METHOD;
75         case "tan": return METHOD;
76         case "asin": return METHOD;
77         case "acos": return METHOD;
78         case "atan": return METHOD;
79         case "sqrt": return METHOD;
80         case "exp": return METHOD;
81         case "log": return METHOD;
82         case "random": return METHOD;
83         //#end
84         return super.get(key);
85     }
86 }