01 
02 /*
03  *  JScripter Simulation 1.0 - For Java To Script
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
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.jscripter.sim.core;
21 
22 import js.Js;
23 import js.Vars;
24 import js.core.JsBoolean;
25 import js.core.JsFunction;
26 import js.core.JsGlobal;
27 import js.core.JsObject;
28 
29 final class CreatorBoolean extends CoreFunction<Boolean>
30 {
31     private CreatorBoolean() {
32         super(JsGlobal.Boolean.toString());
33     }
34 
35     @Override
36     protected Boolean function(Object jsthis, Call<Boolean> callee) {
37         return Js.be(callee.arguments.get(0));
38     }
39     @Override
40     public final JsBoolean create() {
41         return (JsBoolean)construct(new JscBoolean(invoke()));
42     }
43     @Override
44     public final JsBoolean create(Object arg) {
45         return (JsBoolean)construct(new JscBoolean(invoke(arg)));
46     }
47     @Override
48     public final JsBoolean create(Vars<?> args) {
49         return (JsBoolean)construct(new JscBoolean(invoke(args)));
50     }
51 
52     private static CreatorBoolean singleton;
53 
54     public final static synchronized CreatorBoolean get() {
55         if (singleton == null) {
56             singleton = new CreatorBoolean();
57             JsObject p = JsFunction.prototype.with(singleton.var());
58             p.var(
59                     JsBoolean.toString,
60                     new CoreFunction<String>(JsBoolean.toString.toString()) {
61                         @Override
62                         protected String function(Object jsthis, Call<String> callee) {
63                             return Js.toString(like(jsthis));
64                         }
65                     }
66             );
67             p.var(
68                     JsBoolean.valueOf,
69                     new CoreFunction<Boolean>(JsBoolean.valueOf.toString()) {
70                         @Override
71                         protected Boolean function(Object jsthis, Call<Boolean> callee) {
72                             return (Boolean)like(jsthis);
73                         }
74                     }
75             );
76         }
77         return singleton;
78     }
79 }