001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import java.util.Random;
032:
033: /**
034: * JavaScript object
035: */
036: class NativeMath extends Native {
037: static final int ABS = 1;
038: static final int ACOS = 2;
039: static final int ASIN = 3;
040: static final int ATAN = 4;
041: static final int ATAN2 = 5;
042: static final int CEIL = 6;
043: static final int COS = 7;
044: static final int EXP = 8;
045: static final int FLOOR = 9;
046: static final int LOG = 10;
047: static final int MAX = 11;
048: static final int MIN = 12;
049: static final int POW = 13;
050: static final int RANDOM = 14;
051: static final int ROUND = 15;
052: static final int SET_SEED = 16;
053: static final int SIN = 17;
054: static final int SQRT = 18;
055: static final int TAN = 19;
056:
057: Random random; // XXX: s/b in global?
058:
059: /**
060: * Create a new object based on a prototype
061: */
062: private NativeMath(String name, int n, int len) {
063: super (name, len);
064:
065: this .n = n;
066: }
067:
068: /**
069: * Creates the native Object object
070: */
071: static ESObject create(Global resin) {
072: ESObject math = new ESObject("Math", null);
073:
074: put(math, "abs", ABS, 1);
075: put(math, "asin", ASIN, 1);
076: put(math, "acos", ACOS, 1);
077: put(math, "atan", ATAN, 1);
078: put(math, "atan2", ATAN2, 2);
079: put(math, "ceil", CEIL, 1);
080: put(math, "cos", COS, 1);
081: put(math, "exp", EXP, 1);
082: put(math, "floor", FLOOR, 1);
083: put(math, "log", LOG, 1);
084: put(math, "max", MAX, 2);
085: put(math, "min", MIN, 2);
086: put(math, "pow", POW, 2);
087: put(math, "round", ROUND, 1);
088: put(math, "sin", SIN, 1);
089: put(math, "sqrt", SQRT, 1);
090: put(math, "tan", TAN, 1);
091: put(math, "random", RANDOM, 0);
092: put(math, "setSeed", SET_SEED, 1);
093:
094: int flags = DONT_ENUM | DONT_DELETE | READ_ONLY;
095: math.put("E", ESNumber.create(Math.E), flags);
096: math.put("LN10", ESNumber.create(Math.log(10.0)), flags);
097: math.put("LN2", ESNumber.create(Math.log(2.0)), flags);
098: math.put("LOG2E", ESNumber.create(1.0 / Math.log(2.0)), flags);
099: math
100: .put("LOG10E", ESNumber.create(1.0 / Math.log(10.0)),
101: 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 random = new Random();
107: try {
108: ((NativeMath) math.getProperty("random")).random = random;
109: ((NativeMath) math.getProperty("setSeed")).random = random;
110: } catch (Throwable e) {
111: }
112:
113: math.setClean();
114:
115: return math;
116: }
117:
118: private static void put(ESObject obj, String name, int n, int length) {
119: obj.put(name, new NativeMath(name, n, length), DONT_ENUM);
120: }
121:
122: public ESBase call(Call eval, int length) throws Throwable {
123: double arg;
124: double value;
125:
126: switch (n) {
127: case ABS:
128: if (length == 0)
129: return esUndefined;
130: arg = eval.getArg(0).toNum();
131: return ESNumber.create(arg == 0 ? 0
132: : (arg < 0 ? -arg : arg));
133:
134: case ACOS:
135: if (length == 0)
136: return esUndefined;
137: return ESNumber.create(Math.acos(eval.getArg(0).toNum()));
138:
139: case ASIN:
140: if (length == 0)
141: return esUndefined;
142: return ESNumber.create(Math.asin(eval.getArg(0).toNum()));
143:
144: case ATAN:
145: if (length == 0)
146: return esUndefined;
147: return ESNumber.create(Math.atan(eval.getArg(0).toNum()));
148:
149: case ATAN2:
150: if (length < 2)
151: return esUndefined;
152: return ESNumber.create(Math.atan2(eval.getArg(0).toNum(),
153: eval.getArg(1).toNum()));
154:
155: case CEIL:
156: if (length == 0)
157: return esUndefined;
158: return ESNumber.create(Math.ceil(eval.getArg(0).toNum()));
159:
160: case COS:
161: if (length == 0)
162: return esUndefined;
163: return ESNumber.create(Math.cos(eval.getArg(0).toNum()));
164:
165: case EXP:
166: if (length == 0)
167: return esUndefined;
168: return ESNumber.create(Math.exp(eval.getArg(0).toNum()));
169:
170: case FLOOR:
171: if (length == 0)
172: return esUndefined;
173: return ESNumber.create(Math.floor(eval.getArg(0).toNum()));
174:
175: case LOG:
176: if (length == 0)
177: return esUndefined;
178: return ESNumber.create(Math.log(eval.getArg(0).toNum()));
179:
180: case MAX:
181: arg = -1.0 / 0.0;
182: for (int i = 0; i < length; i++) {
183: double v = eval.getArg(i).toNum();
184:
185: arg = Math.max(v, arg);
186: }
187: return ESNumber.create(arg);
188:
189: case MIN:
190: arg = 1.0 / 0.0;
191: for (int i = 0; i < length; i++) {
192: double v = eval.getArg(i).toNum();
193:
194: arg = Math.min(v, arg);
195: }
196: return ESNumber.create(arg);
197:
198: case POW:
199: if (length < 2)
200: return esUndefined;
201: return ESNumber.create(Math.pow(eval.getArg(0).toNum(),
202: eval.getArg(1).toNum()));
203:
204: case RANDOM: {
205: if (length > 1) {
206: int n = eval.getArg(0).toInt32();
207: if (n > 0)
208: return ESNumber
209: .create((int) (random.nextDouble() * n));
210: } else {
211: return ESNumber.create(random.nextDouble());
212: }
213: }
214:
215: case ROUND:
216: if (length == 0)
217: return esUndefined;
218:
219: arg = eval.getArg(0).toNum();
220: if (arg >= -0.5 && arg < 0.5)
221: return ESNumber.create(Math.rint(arg));
222: else
223: return ESNumber.create(Math.floor(arg + 0.5));
224:
225: case SET_SEED:
226: if (length != 0)
227: random.setSeed((long) eval.getArg(0).toNum());
228: return esUndefined;
229:
230: case SIN:
231: if (length == 0)
232: return esUndefined;
233: return ESNumber.create(Math.sin(eval.getArg(0).toNum()));
234:
235: case SQRT:
236: if (length == 0)
237: return esUndefined;
238: return ESNumber.create(Math.sqrt(eval.getArg(0).toNum()));
239:
240: case TAN:
241: if (length == 0)
242: return esUndefined;
243: return ESNumber.create(Math.tan(eval.getArg(0).toNum()));
244:
245: default:
246: throw new ESException("Undefined math function");
247: }
248: }
249: }
|