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.Js;
023 import js.NumberLike;
024 import js.Value;
025 import js.Vars;
026 import js.core.JsFunction;
027 import js.core.JsGlobal;
028 import js.core.JsNumber;
029 import js.core.JsObject;
030 
031 final class CreatorNumber extends CoreFunction<Number>
032 {
033     private CreatorNumber() {
034         super(JsGlobal.Number.toString());
035     }
036 
037     @Override
038     protected java.lang.Number function(Object jsthis, Call<Number> callee) {
039         Object a0 = callee.arguments.get(0);
040         a0 = a0 instanceof Value<?> ? ((Value<?>)a0).var() : a0;
041         return a0 instanceof Number    ? (Number)a0 :
042                a0 instanceof Character ? (short)((Character)a0).charValue() :
043                Js.be(a0) ? 1 : 0;
044     }
045     @Override
046     public JsNumber create() {
047         return (JsNumber)construct(new JscNumber(Core.get().numberLike(invoke())));
048     }
049     @Override
050     public JsNumber create(Object arg) {
051         return (JsNumber)construct(new JscNumber(Core.get().numberLike(invoke(arg))));
052     }
053     @Override
054     public JsNumber create(Vars<?> args) {
055         return (JsNumber)construct(new JscNumber(Core.get().numberLike(invoke(args))));
056     }
057 
058     private static CreatorNumber singleton;
059 
060     public final static synchronized CreatorNumber get() {
061         if (singleton == null) {
062             singleton = new CreatorNumber();
063             JsObject p = JsFunction.prototype.with(singleton.var());
064             p.var(
065                     JsNumber.toExponential,
066                     new CoreFunction<String>(JsNumber.toExponential.toString()) {
067                         @Override
068                         protected String function(Object jsthis, Call<String> callee) {
069                             return callee.arguments.length() < 1 ?
070                                     ((NumberLike<?>)like(jsthis)).toExponential():
071                                     ((NumberLike<?>)like(jsthis)).toExponential(
072                                             callee.arguments.get(0)
073                                     );
074                         }
075                     }
076             );
077             p.var(
078                     JsNumber.toFixed,
079                     new CoreFunction<String>(JsNumber.toFixed.toString()) {
080                         @Override
081                         protected String function(Object jsthis, Call<String> callee) {
082                             return callee.arguments.length() < 1 ?
083                                     ((NumberLike<?>)like(jsthis)).toFixed():
084                                     ((NumberLike<?>)like(jsthis)).toFixed(
085                                             callee.arguments.get(0)
086                                     );
087                         }
088                     }
089             );
090             p.var(
091                     JsNumber.toPrecision,
092                     new CoreFunction<String>(JsNumber.toPrecision.toString()) {
093                         @Override
094                         protected String function(Object jsthis, Call<String> callee) {
095                             return callee.arguments.length() < 1 ?
096                                     ((NumberLike<?>)like(jsthis)).toPrecision():
097                                     ((NumberLike<?>)like(jsthis)).toPrecision(
098                                             callee.arguments.get(0)
099                                     );
100                         }
101                     }
102             );
103             p.var(
104                     JsNumber.toString,
105                     new CoreFunction<String>(JsNumber.toString.toString()) {
106                         @Override
107                         protected String function(Object jsthis, Call<String> callee) {
108                             return Js.toString(like(jsthis));
109                         }
110                     }
111             );
112             p.var(
113                     JsNumber.valueOf,
114                     new CoreFunction<Number>(JsNumber.valueOf.toString()) {
115                         @Override
116                         protected Number function(Object jsthis, Call<Number> callee) {
117                             return ((NumberLike<?>)like(jsthis)).valueOf();
118                         }
119                     }
120             );
121         }
122         return singleton;
123     }
124 }