KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > NativeMath


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import java.util.Random JavaDoc;
32
33 /**
34  * JavaScript object
35  */

36 class NativeMath extends Native {
37   static final int ABS = 1;
38   static final int ACOS = 2;
39   static final int ASIN = 3;
40   static final int ATAN = 4;
41   static final int ATAN2 = 5;
42   static final int CEIL = 6;
43   static final int COS = 7;
44   static final int EXP = 8;
45   static final int FLOOR = 9;
46   static final int LOG = 10;
47   static final int MAX = 11;
48   static final int MIN = 12;
49   static final int POW = 13;
50   static final int RANDOM = 14;
51   static final int ROUND = 15;
52   static final int SET_SEED = 16;
53   static final int SIN = 17;
54   static final int SQRT = 18;
55   static final int TAN = 19;
56
57   Random JavaDoc random; // XXX: s/b in global?
58

59   /**
60    * Create a new object based on a prototype
61    */

62   private NativeMath(String JavaDoc name, int n, int len)
63   {
64     super(name, len);
65
66     this.n = n;
67   }
68   
69   /**
70    * Creates the native Object object
71    */

72   static ESObject create(Global resin)
73   {
74     ESObject math = new ESObject("Math", null);
75
76     put(math, "abs", ABS, 1);
77     put(math, "asin", ASIN, 1);
78     put(math, "acos", ACOS, 1);
79     put(math, "atan", ATAN, 1);
80     put(math, "atan2", ATAN2, 2);
81     put(math, "ceil", CEIL, 1);
82     put(math, "cos", COS, 1);
83     put(math, "exp", EXP, 1);
84     put(math, "floor", FLOOR, 1);
85     put(math, "log", LOG, 1);
86     put(math, "max", MAX, 2);
87     put(math, "min", MIN, 2);
88     put(math, "pow", POW, 2);
89     put(math, "round", ROUND, 1);
90     put(math, "sin", SIN, 1);
91     put(math, "sqrt", SQRT, 1);
92     put(math, "tan", TAN, 1);
93     put(math, "random", RANDOM, 0);
94     put(math, "setSeed", SET_SEED, 1);
95
96     int flags = DONT_ENUM|DONT_DELETE|READ_ONLY;
97     math.put("E", ESNumber.create(Math.E), flags);
98     math.put("LN10", ESNumber.create(Math.log(10.0)), flags);
99     math.put("LN2", ESNumber.create(Math.log(2.0)), flags);
100     math.put("LOG2E", ESNumber.create(1.0 / Math.log(2.0)), flags);
101     math.put("LOG10E", ESNumber.create(1.0 / Math.log(10.0)), flags);
102     math.put("PI", ESNumber.create(Math.PI), flags);
103     math.put("SQRT1_2", ESNumber.create(Math.sqrt(0.5)), flags);
104     math.put("SQRT2", ESNumber.create(Math.sqrt(2.0)), flags);
105     // XXX: potential problems.
106
Random JavaDoc random = new Random JavaDoc();
107     try {
108       ((NativeMath) math.getProperty("random")).random = random;
109       ((NativeMath) math.getProperty("setSeed")).random = random;
110     } catch (Throwable JavaDoc e) {
111     }
112
113     math.setClean();
114
115     return math;
116   }
117
118   private static void put(ESObject obj, String JavaDoc name, int n, int length)
119   {
120     obj.put(name, new NativeMath(name, n, length), DONT_ENUM);
121   }
122
123   public ESBase call(Call eval, int length) throws Throwable JavaDoc
124   {
125     double arg;
126     double value;
127
128     switch (n) {
129     case ABS:
130       if (length == 0)
131     return esUndefined;
132       arg = eval.getArg(0).toNum();
133       return ESNumber.create(arg == 0 ? 0 : (arg < 0 ? -arg : arg));
134
135     case ACOS:
136       if (length == 0)
137     return esUndefined;
138       return ESNumber.create(Math.acos(eval.getArg(0).toNum()));
139
140     case ASIN:
141       if (length == 0)
142     return esUndefined;
143       return ESNumber.create(Math.asin(eval.getArg(0).toNum()));
144
145     case ATAN:
146       if (length == 0)
147     return esUndefined;
148       return ESNumber.create(Math.atan(eval.getArg(0).toNum()));
149
150     case ATAN2:
151       if (length < 2)
152     return esUndefined;
153       return ESNumber.create(Math.atan2(eval.getArg(0).toNum(),
154                     eval.getArg(1).toNum()));
155
156     case CEIL:
157       if (length == 0)
158     return esUndefined;
159       return ESNumber.create(Math.ceil(eval.getArg(0).toNum()));
160
161     case COS:
162       if (length == 0)
163     return esUndefined;
164       return ESNumber.create(Math.cos(eval.getArg(0).toNum()));
165
166     case EXP:
167       if (length == 0)
168     return esUndefined;
169       return ESNumber.create(Math.exp(eval.getArg(0).toNum()));
170
171     case FLOOR:
172       if (length == 0)
173     return esUndefined;
174       return ESNumber.create(Math.floor(eval.getArg(0).toNum()));
175
176     case LOG:
177       if (length == 0)
178     return esUndefined;
179       return ESNumber.create(Math.log(eval.getArg(0).toNum()));
180
181     case MAX:
182       arg = -1.0/0.0;
183       for (int i = 0; i < length; i++) {
184     double v = eval.getArg(i).toNum();
185
186     arg = Math.max(v, arg);
187       }
188       return ESNumber.create(arg);
189
190     case MIN:
191       arg = 1.0/0.0;
192       for (int i = 0; i < length; i++) {
193     double v = eval.getArg(i).toNum();
194
195     arg = Math.min(v, arg);
196       }
197       return ESNumber.create(arg);
198
199     case POW:
200       if (length < 2)
201     return esUndefined;
202       return ESNumber.create(Math.pow(eval.getArg(0).toNum(),
203                       eval.getArg(1).toNum()));
204
205     case RANDOM:
206       {
207     if (length > 1) {
208       int n = eval.getArg(0).toInt32();
209       if (n > 0)
210         return ESNumber.create((int) (random.nextDouble() * n));
211     } else {
212       return ESNumber.create(random.nextDouble());
213     }
214       }
215
216     case ROUND:
217       if (length == 0)
218     return esUndefined;
219
220       arg = eval.getArg(0).toNum();
221       if (arg >= -0.5 && arg < 0.5)
222     return ESNumber.create(Math.rint(arg));
223       else
224     return ESNumber.create(Math.floor(arg + 0.5));
225
226     case SET_SEED:
227       if (length != 0)
228     random.setSeed((long) eval.getArg(0).toNum());
229       return esUndefined;
230
231     case SIN:
232       if (length == 0)
233     return esUndefined;
234       return ESNumber.create(Math.sin(eval.getArg(0).toNum()));
235
236     case SQRT:
237       if (length == 0)
238     return esUndefined;
239       return ESNumber.create(Math.sqrt(eval.getArg(0).toNum()));
240
241     case TAN:
242       if (length == 0)
243     return esUndefined;
244       return ESNumber.create(Math.tan(eval.getArg(0).toNum()));
245
246     default:
247       throw new ESException("Undefined math function");
248     }
249   }
250 }
251
Popular Tags