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