001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.sim.core;
021 
022 import js.Function;
023 import js.Js;
024 import js.MathLike;
025 import js.ObjectLike;
026 import js.core.JsGlobal;
027 import js.core.JsObject;
028 
029 final class CoreMath extends JscObject
030 {
031     private CoreMath() {
032         super(Js.object());
033     }
034 
035     private static CoreMath singleton;
036 
037     public final static synchronized CoreMath get() {
038         if (singleton == null) {
039             singleton = new CoreMath();
040             JsObject.constructor.with(singleton, JsGlobal.Object);
041             ObjectLike o = singleton.likeObject();
042             o.var(JsGlobal.Math.E       .mid(), MathLike.E      );
043             o.var(JsGlobal.Math.LN2     .mid(), MathLike.LN2    );
044             o.var(JsGlobal.Math.LN10    .mid(), MathLike.LN10   );
045             o.var(JsGlobal.Math.LOG2E   .mid(), MathLike.LOG2E  );
046             o.var(JsGlobal.Math.LOG10E  .mid(), MathLike.LOG10E );
047             o.var(JsGlobal.Math.PI      .mid(), MathLike.PI     );
048             o.var(JsGlobal.Math.SQRT1_2 .mid(), MathLike.SQRT1_2);
049             o.var(JsGlobal.Math.SQRT2   .mid(), MathLike.SQRT2  );
050             o.var(
051                     JsGlobal.Math.abs.mid(),
052                     new Function<Number>() {
053                         @Override
054                         protected Number function(Object jsthis, Call<Number> callee) {
055                             return Js.math().abs(callee.arguments.get(0));
056                         }
057                     }.var()
058             );
059             o.var(
060                     JsGlobal.Math.acos.mid(),
061                     new Function<Number>() {
062                         @Override
063                         protected Number function(Object jsthis, Call<Number> callee) {
064                             return Js.math().acos(callee.arguments.get(0));
065                         }
066                     }.var()
067             );
068             o.var(
069                     JsGlobal.Math.asin.mid(),
070                     new Function<Number>() {
071                         @Override
072                         protected Number function(Object jsthis, Call<Number> callee) {
073                             return Js.math().asin(callee.arguments.get(0));
074                         }
075                     }.var()
076             );
077             o.var(
078                     JsGlobal.Math.atan.mid(),
079                     new Function<Number>() {
080                         @Override
081                         protected Number function(Object jsthis, Call<Number> callee) {
082                             return Js.math().atan(callee.arguments.get(0));
083                         }
084                     }.var()
085             );
086             o.var(
087                     JsGlobal.Math.atan2.mid(),
088                     new Function<Number>() {
089                         @Override
090                         protected Number function(Object jsthis, Call<Number> callee) {
091                             return Js.math().atan2(callee.arguments.get(0), callee.arguments.get(1));
092                         }
093                     }.var()
094             );
095             o.var(
096                     JsGlobal.Math.ceil.mid(),
097                     new Function<Number>() {
098                         @Override
099                         protected Number function(Object jsthis, Call<Number> callee) {
100                             return Js.math().ceil(callee.arguments.get(0));
101                         }
102                     }.var()
103             );
104             o.var(
105                     JsGlobal.Math.cos.mid(),
106                     new Function<Number>() {
107                         @Override
108                         protected Number function(Object jsthis, Call<Number> callee) {
109                             return Js.math().cos(callee.arguments.get(0));
110                         }
111                     }.var()
112             );
113             o.var(
114                     JsGlobal.Math.exp.mid(),
115                     new Function<Number>() {
116                         @Override
117                         protected Number function(Object jsthis, Call<Number> callee) {
118                             return Js.math().exp(callee.arguments.get(0));
119                         }
120                     }.var()
121             );
122             o.var(
123                     JsGlobal.Math.floor.mid(),
124                     new Function<Number>() {
125                         @Override
126                         protected Number function(Object jsthis, Call<Number> callee) {
127                             return Js.math().floor(callee.arguments.get(0));
128                         }
129                     }.var()
130             );
131             o.var(
132                     JsGlobal.Math.log.mid(),
133                     new Function<Number>() {
134                         @Override
135                         protected Number function(Object jsthis, Call<Number> callee) {
136                             return Js.math().log(callee.arguments.get(0));
137                         }
138                     }.var()
139             );
140             o.var(
141                     JsGlobal.Math.max.mid(),
142                     new Function<Number>() {
143                         @Override
144                         protected Number function(Object jsthis, Call<Number> callee) {
145                             return Js.math().max(callee.arguments.var());
146                         }
147                     }.var()
148             );
149             o.var(
150                     JsGlobal.Math.min.mid(),
151                     new Function<Number>() {
152                         @Override
153                         protected Number function(Object jsthis, Call<Number> callee) {
154                             return Js.math().min(callee.arguments.var());
155                         }
156                     }.var()
157             );
158             o.var(
159                     JsGlobal.Math.pow.mid(),
160                     new Function<Number>() {
161                         @Override
162                         protected Number function(Object jsthis, Call<Number> callee) {
163                             return Js.math().pow(callee.arguments.get(0), callee.arguments.get(1));
164                         }
165                     }.var()
166             );
167             o.var(
168                     JsGlobal.Math.random.mid(),
169                     new Function<Number>() {
170                         @Override
171                         protected Number function(Object jsthis, Call<Number> callee) {
172                             return Js.math().random();
173                         }
174                     }.var()
175             );
176             o.var(
177                     JsGlobal.Math.round.mid(),
178                     new Function<Number>() {
179                         @Override
180                         protected Number function(Object jsthis, Call<Number> callee) {
181                             return Js.math().round(callee.arguments.get(0));
182                         }
183                     }.var()
184             );
185             o.var(
186                     JsGlobal.Math.sin.mid(),
187                     new Function<Number>() {
188                         @Override
189                         protected Number function(Object jsthis, Call<Number> callee) {
190                             return Js.math().sin(callee.arguments.get(0));
191                         }
192                     }.var()
193             );
194             o.var(
195                     JsGlobal.Math.sinh.mid(),
196                     new Function<Number>() {
197                         @Override
198                         protected Number function(Object jsthis, Call<Number> callee) {
199                             return Js.math().sinh(callee.arguments.get(0));
200                         }
201                     }.var()
202             );
203             o.var(
204                     JsGlobal.Math.sqrt.mid(),
205                     new Function<Number>() {
206                         @Override
207                         protected Number function(Object jsthis, Call<Number> callee) {
208                             return Js.math().sqrt(callee.arguments.get(0));
209                         }
210                     }.var()
211             );
212             o.var(
213                     JsGlobal.Math.tan.mid(),
214                     new Function<Number>() {
215                         @Override
216                         protected Number function(Object jsthis, Call<Number> callee) {
217                             return Js.math().tan(callee.arguments.get(0));
218                         }
219                     }.var()
220             );
221             o.var(
222                     JsGlobal.Math.tanh.mid(),
223                     new Function<Number>() {
224                         @Override
225                         protected Number function(Object jsthis, Call<Number> callee) {
226                             return Js.math().tanh(callee.arguments.get(0));
227                         }
228                     }.var()
229             );
230         }
231         return singleton;
232     }
233 }