001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
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.descripter.js.api.global;
021 
022 import org.descripter.js.api.Core;
023 import org.descripter.js.api.Function;
024 import org.descripter.js.api.Functor;
025 import org.descripter.js.api.Script;
026 import org.descripter.js.api.core.CArray;
027 import org.descripter.js.api.core.CNumber;
028 import org.descripter.js.api.core.CObject;
029 
030 /**
031  * <p>Emulates the global Number function in JavaScript.</p>
032  * 
033  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
034  * @since Descripter 1.0
035  */
036 public class Number_ extends Function<Core>
037 {
038     /**
039      * <p>Creates the singleton of the global {@link Function} object of this type.</p>
040      * @param core The {@link Core} global script context
041      * @since Descripter 1.0
042      */
043     public static final void create(Core core) {
044         if (core.get(core._Number) == null) {
045             core.put(core._Number, new Number_(core));
046         }
047     }
048 
049     private Number_(Core core) {
050         super(core);
051         CObject prototype = prototype();
052         prototype.put(core._toExponential, new Function<Core>(core) {
053             @Override
054             protected Functor<Core> functor() {
055                 return new Functor<Core>(this) {
056                     @Override
057                     public Object function() {
058                         return ((CNumber)this_()).toExponential(arguments().get(0));
059                     }
060                 };
061             }
062         });
063         prototype.put(core._toFixed, new Function<Core>(core) {
064             @Override
065             protected Functor<Core> functor() {
066                 return new Functor<Core>(this) {
067                     @Override
068                     public Object function() {
069                         return ((CNumber)this_()).toFixed(arguments().get(0));
070                     }
071                 };
072             }
073         });
074         prototype.put(core._toPrecision, new Function<Core>(core) {
075             @Override
076             protected Functor<Core> functor() {
077                 return new Functor<Core>(this) {
078                     @Override
079                     public Object function() {
080                         CArray args = arguments();
081                         CNumber base = (CNumber)this_();
082                         return args.has(0) ? base.toPrecision(intValue(args.get(0))) : base.toPrecision();
083                     }
084                 };
085             }
086         });
087     }
088 
089     /**
090      * <p>Allocates a new object with this {@link Function}.</p>
091      * @param script The script context that invoked this service
092      * @param args An array of the arguments passed by the invocation
093      * @return The newly allocated object
094      * @since Descripter 1.0
095      */
096     @Override
097     public <S extends Script<?>> CNumber alloc(S script, Object ...args) {
098         return new CNumber(this, args != null & args.length > 0 ? toNumber(args[0]) : 0);
099     }
100 
101     /**
102      * <p>Gets the {@link Functor} of the {@link Function} object.</p>
103      * <p>This is a concrete implementation of the super abstract method.</p>
104      * @return The {@link Functor} of the {@link Function} object
105      * @since Descripter 1.0
106      */
107     @Override
108     protected Functor<Core> functor() {
109         return new Functor<Core>(this) {
110             @Override
111             public Object function() {
112                 return new CNumber(Number_.this, toNumber(arguments().get(0)));
113             }
114         };
115     }
116 }