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.ObjectLike;
24 import js.Var;
25 import js.Vars;
26 import js.core.JsFunction;
27 import js.core.JsGlobal;
28 import js.core.JsObject;
29 
30 final class CreatorObject extends CoreFunction<JsObject>
31 {
32     private CreatorObject() {
33         super(JsGlobal.Object.toString());
34     }
35 
36     @Override
37     protected final JsObject function(java.lang.Object jsthis, Call<JsObject> callee) {
38         return callee.arguments.length() < 1 ? create() :
39             create(callee.arguments.get(0));
40     }
41 
42     @Override
43     protected final JsObject construct(Object o) {
44         JsObject ret = (JsObject)o;
45         JsObject.constructor.with(ret, var());
46         JsFunction.prototype.with(ret, JsFunction.prototype.with(var()));
47         return ret;
48     }
49     @Override
50     public final JsObject create() {
51         return construct(new JscObject());
52     }
53     @Override
54     public final JsObject create(java.lang.Object arg) {
55         if (Js.undefined(arg)) {
56             return create();
57         } else if (arg instanceof JsObject) {
58             return (JsObject)arg;
59         } else if (arg instanceof ObjectLike) {
60             return create((ObjectLike)arg);
61         } else if (arg instanceof Var<?>) {
62             return create(((Var<?>)arg).var());
63         } else if (arg instanceof ObjectLike) {
64             return create((ObjectLike)arg);
65         } else if (arg instanceof java.lang.Boolean) {
66             return JsGlobal.Boolean.with().create(arg);
67         } else if (arg instanceof java.lang.Number) {
68             return JsGlobal.Number.with().create(arg);
69         } else if (arg instanceof java.lang.String) {
70             return JsGlobal.String.with().create(arg);
71         } else {
72             return new JscObject(arg, create());
73         }
74     }
75     private final JsObject create(ObjectLike arg) {
76         return arg instanceof JsObject ? arg.var() : construct(new JscObject(arg));
77     }
78     @Override
79     public final JsObject create(Vars<?> args) {
80         return args != null && args.var().length() > 0 ?
81                 create(args.var().get(0)) : create();
82     }
83 
84     private static CreatorObject singleton;
85 
86     public final static synchronized CreatorObject get() {
87         if (singleton == null) {
88             singleton = new CreatorObject();
89         }
90         return singleton;
91     }
92 }