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.Js;
023 import js.RegExpLike;
024 import js.StringLike;
025 import js.Vars;
026 import js.core.JsArray;
027 import js.core.JsFunction;
028 import js.core.JsGlobal;
029 import js.core.JsObject;
030 import js.core.JsString;
031 
032 final class CreatorString extends CoreFunction<String>
033 {
034     private CreatorString() {
035         super(JsGlobal.String.toString());
036     }
037 
038     @Override
039     protected String function(Object jsthis, Call<String> callee) {
040         if (callee.arguments.length() < 1) return "";
041         return Js.toString(callee.arguments.get(0));
042     }
043     @Override
044     public JsString create() {
045         return (JsString)construct(new JscString(Core.get().stringLike(invoke())));
046     }
047     @Override
048     public JsString create(Object arg) {
049         return (JsString)construct(new JscString(Core.get().stringLike(invoke(arg))));
050     }
051     @Override
052     public JsString create(Vars<?> args) {
053         return (JsString)construct(new JscString(Core.get().stringLike(invoke(args))));
054     }
055 
056     private static CreatorString singleton;
057 
058     public final static synchronized CreatorString get() {
059         if (singleton == null) {
060             singleton = new CreatorString();
061             JsObject p = JsFunction.prototype.with(singleton.var());
062             p.var(
063                     JsString.charAt,
064                     new CoreFunction<String>(JsString.charAt.toString()) {
065                         @Override
066                         protected String function(Object jsthis, Call<String> callee) {
067                             if (!(jsthis instanceof StringLike)) {
068                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
069                             }
070                             return ((StringLike)like(jsthis)).charAt(
071                                     callee.arguments.get(0)
072                             );
073                         }
074                     }
075             );
076             p.var(
077                     JsString.charCodeAt,
078                     new CoreFunction<Character>(JsString.charCodeAt.toString()) {
079                         @Override
080                         protected Character function(Object jsthis, Call<Character> callee) {
081                             if (!(jsthis instanceof StringLike)) {
082                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
083                             }
084                             return ((StringLike)like(jsthis)).charCodeAt(
085                                     callee.arguments.get(0)
086                             );
087                         }
088                     }
089             );
090             p.var(
091                     JsString.concat,
092                     new CoreFunction<String>(JsString.concat.toString()) {
093                         @Override
094                         protected String function(Object jsthis, Call<String> callee) {
095                             if (!(jsthis instanceof StringLike)) {
096                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
097                             }
098                             return ((StringLike)like(jsthis)).concat(callee.arguments.get(0));
099                         }
100                     }
101             );
102             p.var(
103                     JsString.indexOf,
104                     new CoreFunction<Integer>(JsString.indexOf.toString()) {
105                         @Override
106                         protected Integer function(Object jsthis, Call<Integer> callee) {
107                             if (!(jsthis instanceof StringLike)) {
108                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
109                             }
110                             return callee.arguments.length() < 2 ?
111                                     ((StringLike)like(jsthis)).indexOf(
112                                             callee.arguments.get(0)
113                                     ) : ((StringLike)like(jsthis)).indexOf(
114                                             callee.arguments.get(0),
115                                             callee.arguments.get(1)
116                                     );
117                         }
118                     }
119             );
120             p.var(
121                     JsString.lastIndexOf,
122                     new CoreFunction<Integer>(JsString.lastIndexOf.toString()) {
123                         @Override
124                         protected Integer function(Object jsthis, Call<Integer> callee) {
125                             if (!(jsthis instanceof StringLike)) {
126                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
127                             }
128                             return callee.arguments.length() < 2 ?
129                                     ((StringLike)like(jsthis)).lastIndexOf(
130                                             callee.arguments.get(0)
131                                     ) : ((StringLike)like(jsthis)).lastIndexOf(
132                                             callee.arguments.get(0),
133                                             callee.arguments.get(1)
134                                     );
135                         }
136                     }
137             );
138             p.var(
139                     JsString.localeCompare,
140                     new CoreFunction<Integer>(JsString.localeCompare.toString()) {
141                         @Override
142                         protected Integer function(Object jsthis, Call<Integer> callee) {
143                             if (!(jsthis instanceof StringLike)) {
144                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
145                             }
146                             return ((StringLike)like(jsthis)).localeCompare(
147                                     callee.arguments.get(0)
148                             );
149                         }
150                     }
151             );
152             p.var(
153                     JsString.match,
154                     new CoreFunction<JsArray>(JsString.match.toString()) {
155                         @Override
156                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
157                             if (!(jsthis instanceof StringLike)) {
158                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
159                             }
160                             return JsGlobal.Array.with().create(
161                                     ((StringLike)like(jsthis)).match(
162                                             (RegExpLike)like(callee.arguments.get(0))
163                                     )
164                             );
165                         }
166                     }
167             );
168             p.var(
169                     JsString.replace,
170                     new CoreFunction<String>(JsString.replace.toString()) {
171                         @Override
172                         protected String function(Object jsthis, Call<String> callee) {
173                             if (!(jsthis instanceof StringLike)) {
174                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
175                             }
176                             return ((StringLike)like(jsthis)).replace(
177                                     (RegExpLike)like(callee.arguments.get(0)),
178                                     callee.arguments.get(1).toString()
179                             );
180                         }
181                     }
182             );
183             p.var(
184                     JsString.search,
185                     new CoreFunction<Integer>(JsString.search.toString()) {
186                         @Override
187                         protected Integer function(Object jsthis, Call<Integer> callee) {
188                             if (!(jsthis instanceof StringLike)) {
189                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
190                             }
191                             return ((StringLike)like(jsthis)).search(
192                                     (RegExpLike)like(callee.arguments.get(0))
193                             );
194                         }
195                     }
196             );
197             p.var(
198                     JsString.slice,
199                     new CoreFunction<String>(JsString.slice.toString()) {
200                         @Override
201                         protected String function(Object jsthis, Call<String> callee) {
202                             if (!(jsthis instanceof StringLike)) {
203                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
204                             }
205                             return callee.arguments.length() < 2 ?
206                                     ((StringLike)like(jsthis)).slice(
207                                             callee.arguments.get(0)
208                                     ) : ((StringLike)like(jsthis)).slice(
209                                             callee.arguments.get(0),
210                                             callee.arguments.get(1)
211                                     );
212                         }
213                     }
214             );
215             p.var(
216                     JsString.split,
217                     new CoreFunction<JsArray>(JsString.split.toString()) {
218                         @Override
219                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
220                             if (!(jsthis instanceof StringLike)) {
221                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
222                             }
223                             return callee.arguments.length() < 2 ?
224                                     JsGlobal.Array.with().create(
225                                             ((StringLike)like(jsthis)).split(
226                                                     callee.arguments.get(0)
227                                             )
228                                     ) : JsGlobal.Array.with().create(
229                                             ((StringLike)like(jsthis)).split(
230                                                     callee.arguments.get(0),
231                                                     callee.arguments.get(1)
232                                             )
233                                     );
234                         }
235                     }
236             );
237             p.var(
238                     JsString.substr,
239                     new CoreFunction<String>(JsString.substr.toString()) {
240                         @Override
241                         protected String function(Object jsthis, Call<String> callee) {
242                             if (!(jsthis instanceof StringLike)) {
243                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
244                             }
245                             return callee.arguments.length() < 2 ?
246                                     ((StringLike)like(jsthis)).substr(
247                                             callee.arguments.get(0)
248                                     ) : ((StringLike)like(jsthis)).substr(
249                                             callee.arguments.get(0),
250                                             callee.arguments.get(1)
251                                     );
252                         }
253                     }
254             );
255             p.var(
256                     JsString.substring,
257                     new CoreFunction<String>(JsString.substring.toString()) {
258                         @Override
259                         protected String function(Object jsthis, Call<String> callee) {
260                             if (!(jsthis instanceof StringLike)) {
261                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
262                             }
263                             return ((StringLike)like(jsthis)).substring(
264                                     callee.arguments.get(0),
265                                     callee.arguments.get(1)
266                             );
267                         }
268                     }
269             );
270             p.var(
271                     JsString.toLowerCase,
272                     new CoreFunction<String>(JsString.toLowerCase.toString()) {
273                         @Override
274                         protected String function(Object jsthis, Call<String> callee) {
275                             if (!(jsthis instanceof StringLike)) {
276                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
277                             }
278                             return ((StringLike)like(jsthis)).toLowerCase();
279                         }
280                     }
281             );
282             p.var(
283                     JsString.toUpperCase,
284                     new CoreFunction<String>(JsString.toUpperCase.toString()) {
285                         @Override
286                         protected String function(Object jsthis, Call<String> callee) {
287                             if (!(jsthis instanceof StringLike)) {
288                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
289                             }
290                             return ((StringLike)like(jsthis)).toUpperCase();
291                         }
292                     }
293             );
294             p.var(
295                     JsString.toLocaleLowerCase,
296                     new CoreFunction<String>(JsString.toLocaleLowerCase.toString()) {
297                         @Override
298                         protected String function(Object jsthis, Call<String> callee) {
299                             if (!(jsthis instanceof StringLike)) {
300                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
301                             }
302                             return ((StringLike)like(jsthis)).toLocaleLowerCase();
303                         }
304                     }
305             );
306             p.var(
307                     JsString.toLocaleUpperCase,
308                     new CoreFunction<String>(JsString.toLocaleUpperCase.toString()) {
309                         @Override
310                         protected String function(Object jsthis, Call<String> callee) {
311                             if (!(jsthis instanceof StringLike)) {
312                                 jsthis = Core.get().stringLike(Js.toString(jsthis));
313                             }
314                             return ((StringLike)like(jsthis)).toLocaleUpperCase();
315                         }
316                     }
317             );
318             p.var(
319                     JsString.toString,
320                     new CoreFunction<String>(JsString.toString.toString()) {
321                         @Override
322                         protected String function(Object jsthis, Call<String> callee) {
323                             return Js.toString(like(jsthis));
324                         }
325                     }
326             );
327             p.var(
328                     JsString.valueOf,
329                     new CoreFunction<String>(JsString.valueOf.toString()) {
330                         @Override
331                         protected String function(Object jsthis, Call<String> callee) {
332                             return ((StringLike)like(jsthis)).valueOf();
333                         }
334                     }
335             );
336         }
337         return singleton;
338     }
339 }