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.ArrayLike;
023 import js.Js;
024 import js.Vars;
025 import js.core.JsArray;
026 import js.core.JsFunction;
027 import js.core.JsGlobal;
028 import js.core.JsObject;
029 
030 final class CreatorArray extends CoreFunction<JsArray>
031 {
032     private CreatorArray() {
033         super(JsGlobal.Array.toString());
034     }
035 
036     @Override
037     protected JsArray function(Object jsthis, Call<JsArray> callee) {
038         Object arg0 = callee.arguments.get(0);
039         return arg0 instanceof JsArray ? (JsArray)arg0 :
040             (JsArray)construct(new JscArray(Js.array()));
041     }
042     @Override
043     protected final JsArray construct(Object o) {
044         JsArray ret = (JsArray)o;
045         JsObject.constructor.with(ret, var());
046         JsFunction.prototype.with(ret, JsFunction.prototype.with(var()));
047         return ret;
048     }
049     @Override
050     public final JsArray create() {
051         return (JsArray)invoke();
052     }
053     @Override
054     public final JsArray create(Object arg) {
055         return (JsArray)invoke(arg);
056     }
057     @Override
058     public final JsArray create(Vars<?> args) {
059         return (JsArray)invoke(args);
060     }
061 
062     private static CreatorArray singleton;
063 
064     public final static synchronized CreatorArray get() {
065         if (singleton == null) {
066             singleton = new CreatorArray();
067             JsObject p = JsFunction.prototype.with(singleton.var());
068             p.var(
069                     JsArray.concat,
070                     new CoreFunction<JsArray>(JsArray.concat.toString()) {
071                         @Override
072                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
073                             return JsGlobal.Array.with().create(
074                                     ((ArrayLike<?>)like(jsthis)).concat(
075                                             callee.arguments.var()
076                                     )
077                             );
078                         }
079                     }
080             );
081             p.var(
082                     JsArray.join,
083                     new CoreFunction<String>(JsArray.join.toString()) {
084                         @Override
085                         protected String function(Object jsthis, Call<String> callee) {
086                             return callee.arguments.length() < 1 ?
087                                     ((ArrayLike<?>)like(jsthis)).join() :
088                                     ((ArrayLike<?>)like(jsthis)).join(
089                                             callee.arguments.get(0)
090                                     );
091                         }
092                     }
093             );
094             p.var(
095                     JsArray.pop,
096                     new CoreFunction<Object>(JsArray.pop.toString()) {
097                         @Override
098                         protected Object function(Object jsthis, Call<Object> callee) {
099                             return ((ArrayLike<?>)like(jsthis)).pop();
100                         }
101                     }
102             );
103             p.var(
104                     JsArray.push,
105                     new CoreFunction<Integer>(JsArray.push.toString()) {
106                         @SuppressWarnings("unchecked")
107                         @Override
108                         protected Integer function(Object jsthis, Call<Integer> callee) {
109                             return ((ArrayLike<Object>)like(jsthis)).push(
110                                     callee.arguments.var()
111                             );
112                         }
113                     }
114             );
115             p.var(
116                     JsArray.reverse,
117                     new CoreFunction<Void>(JsArray.reverse.toString()) {
118                         @Override
119                         protected Void function(Object jsthis, Call<Void> callee) {
120                             ((ArrayLike<?>)like(jsthis)).reverse();
121                             return (Void)null;
122                         }
123                     }
124             );
125             p.var(
126                     JsArray.shift,
127                     new CoreFunction<Object>(JsArray.shift.toString()) {
128                         @Override
129                         protected Object function(Object jsthis, Call<Object> callee) {
130                             return ((ArrayLike<?>)like(jsthis)).shift();
131                         }
132                     }
133             );
134             p.var(
135                     JsArray.slice,
136                     new CoreFunction<JsArray>(JsArray.slice.toString()) {
137                         @Override
138                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
139                             return JsGlobal.Array.with().create(
140                                     ((ArrayLike<?>)like(jsthis)).slice(
141                                             callee.arguments.var()
142                                     )
143                             );
144                         }
145                     }
146             );
147             p.var(
148                     JsArray.sort,
149                     new CoreFunction<Object>(JsArray.sort.toString()) {
150                         @SuppressWarnings("unchecked")
151                         @Override
152                         protected Object function(Object jsthis, Call<Object> callee) {
153                             return callee.arguments.length() < 1 ?
154                                     ((ArrayLike<?>)like(jsthis)).sort() :
155                                     ((ArrayLike<?>)like(jsthis)).sort(
156                                             (JsFunction<? extends Number>)like(
157                                                     callee.arguments.get(0)
158                                             )
159                                     );
160                         }
161                     }
162             );
163             p.var(
164                     JsArray.splice,
165                     new CoreFunction<JsArray>(JsArray.splice.toString()) {
166                         @Override
167                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
168                             return JsGlobal.Array.with().create(
169                                     ((ArrayLike<?>)like(jsthis)).splice(
170                                             callee.arguments.var()
171                                     )
172                             );
173                         }
174                     }
175             );
176             p.var(
177                     JsArray.unshift,
178                     new CoreFunction<Integer>(JsArray.unshift.toString()) {
179                         @Override
180                         protected Integer function(Object jsthis, Call<Integer> callee) {
181                             return ((ArrayLike<?>)like(jsthis)).unshift(
182                                     callee.arguments.var()
183                             );
184                         }
185                     }
186             );
187             p.var(
188                     JsArray.toString,
189                     new CoreFunction<String>(JsArray.toString.toString()) {
190                         @Override
191                         protected String function(Object jsthis, Call<String> callee) {
192                             return Js.toString(like(jsthis));
193                         }
194                     }
195             );
196             p.var(
197                     JsObject.valueOf,
198                     new CoreFunction<JsArray>(JsArray.valueOf.toString()) {
199                         @Override
200                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
201                             return JsGlobal.Array.with().create((ArrayLike<?>)like(jsthis));
202                         }
203                     }
204             );
205         }
206         return singleton;
207     }
208 }