01 
02 /*
03  *  Descripter 1.0 - Java Script Engines
04  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package org.descripter.js.api.global;
21 
22 import org.descripter.js.api.Core;
23 import org.descripter.js.api.Function;
24 import org.descripter.js.api.Functor;
25 import org.descripter.js.api.Script;
26 import org.descripter.js.api.core.CBoolean;
27 
28 /**
29  * <p>Emulates the global Boolean function in JavaScript.</p>
30  * 
31  * @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>
32  * @since Descripter 1.0
33  */
34 public class Boolean_ extends Function<Core>
35 {
36     /**
37      * <p>Creates the singleton of the global {@link Function} object of this type.</p>
38      * @param core The {@link Core} global script context
39      * @since Descripter 1.0
40      */
41     public static final void create(Core core) {
42         if (core.get(core._Boolean) == null) {
43             core.put(core._Boolean, new Boolean_(core));
44         }
45     }
46 
47     private Boolean_(Core core) {
48         super(core);
49     }
50 
51     /**
52      * <p>Allocates a new object with this {@link Function}.</p>
53      * @param script The script context that invoked this service
54      * @param args An array of the arguments passed by the invocation
55      * @return The newly allocated object
56      * @since Descripter 1.0
57      */
58     @Override
59     public <S extends Script<?>> CBoolean alloc(S script, Object ...args) {
60         return new CBoolean(this, args != null & args.length > 0 && bool(args[0]));
61     }
62 
63     /**
64      * <p>Gets the {@link Functor} of the {@link Function} object.</p>
65      * <p>This is a concrete implementation of the super abstract method.</p>
66      * @return The {@link Functor} of the {@link Function} object
67      * @since Descripter 1.0
68      */
69     @Override
70     protected Functor<Core> functor() {
71         return new Functor<Core>(this) {
72             @Override
73             public Object function() {
74                 return new CBoolean(Boolean_.this, bool(arguments().get(0)));
75             }
76         };
77     }
78 }