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