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.DateLike;
023 import js.Js;
024 import js.Vars;
025 import js.core.JsDate;
026 import js.core.JsFunction;
027 import js.core.JsGlobal;
028 import js.core.JsObject;
029 
030 final class CreatorDate extends CoreFunction<JsDate>
031 {
032     private CreatorDate() {
033         super(JsGlobal.Date.toString());
034     }
035 
036     @Override
037     protected JsDate function(Object jsthis, Call<JsDate> callee) {
038         Object arg0 = callee.arguments.get(0);
039         return arg0 instanceof JsDate ? (JsDate)arg0 :
040             (JsDate)construct(new JscDate(
041                     arg0 instanceof Number ? Js.date((Number)arg0) :
042                     arg0 instanceof String ? Js.date((String)arg0) :
043                     Js.date()
044             ));
045     }
046     @Override
047     protected final JsDate construct(Object o) {
048         JsDate ret = (JsDate)o;
049         JsObject.constructor.with(ret, var());
050         JsFunction.prototype.with(ret, JsFunction.prototype.with(var()));
051         return ret;
052     }
053     @Override
054     public final JsDate create() {
055         return invoke();
056     }
057     @Override
058     public final JsDate create(Object arg) {
059         return invoke(arg);
060     }
061     @Override
062     public final JsDate create(Vars<?> args) {
063         return invoke(args);
064     }
065 
066     private static CreatorDate singleton;
067 
068     public final static synchronized CreatorDate get() {
069         if (singleton == null) {
070             singleton = new CreatorDate();
071             JsObject p = JsFunction.prototype.with(singleton.var());
072             p.var(
073                     JsDate.getTime,
074                     new CoreFunction<Number>(JsDate.getTime.toString()) {
075                         @Override
076                         protected Number function(Object jsthis, Call<Number> callee) {
077                             return ((DateLike)like(jsthis)).getTime();
078                         }
079                     }
080             );
081             p.var(
082                     JsDate.getFullYear,
083                     new CoreFunction<Number>(JsDate.getFullYear.toString()) {
084                         @Override
085                         protected Number function(Object jsthis, Call<Number> callee) {
086                             return ((DateLike)like(jsthis)).getFullYear();
087                         }
088                     }
089             );
090             p.var(
091                     JsDate.getUTCFullYear,
092                     new CoreFunction<Number>(JsDate.getUTCFullYear.toString()) {
093                         @Override
094                         protected Number function(Object jsthis, Call<Number> callee) {
095                             return ((DateLike)like(jsthis)).getUTCFullYear();
096                         }
097                     }
098             );
099             p.var(
100                     JsDate.getMonth,
101                     new CoreFunction<Number>(JsDate.getMonth.toString()) {
102                         @Override
103                         protected Number function(Object jsthis, Call<Number> callee) {
104                             return ((DateLike)like(jsthis)).getMonth();
105                         }
106                     }
107             );
108             p.var(
109                     JsDate.getUTCMonth,
110                     new CoreFunction<Number>(JsDate.getUTCMonth.toString()) {
111                         @Override
112                         protected Number function(Object jsthis, Call<Number> callee) {
113                             return ((DateLike)like(jsthis)).getUTCMonth();
114                         }
115                     }
116             );
117             p.var(
118                     JsDate.getDate,
119                     new CoreFunction<Number>(JsDate.getDate.toString()) {
120                         @Override
121                         protected Number function(Object jsthis, Call<Number> callee) {
122                             return ((DateLike)like(jsthis)).getDate();
123                         }
124                     }
125             );
126             p.var(
127                     JsDate.getUTCDate,
128                     new CoreFunction<Number>(JsDate.getUTCDate.toString()) {
129                         @Override
130                         protected Number function(Object jsthis, Call<Number> callee) {
131                             return ((DateLike)like(jsthis)).getUTCDate();
132                         }
133                     }
134             );
135             p.var(
136                     JsDate.getDay,
137                     new CoreFunction<Number>(JsDate.getDay.toString()) {
138                         @Override
139                         protected Number function(Object jsthis, Call<Number> callee) {
140                             return ((DateLike)like(jsthis)).getDay();
141                         }
142                     }
143             );
144             p.var(
145                     JsDate.getUTCDay,
146                     new CoreFunction<Number>(JsDate.getUTCDay.toString()) {
147                         @Override
148                         protected Number function(Object jsthis, Call<Number> callee) {
149                             return ((DateLike)like(jsthis)).getUTCDay();
150                         }
151                     }
152             );
153             p.var(
154                     JsDate.getHours,
155                     new CoreFunction<Number>(JsDate.getHours.toString()) {
156                         @Override
157                         protected Number function(Object jsthis, Call<Number> callee) {
158                             return ((DateLike)like(jsthis)).getHours();
159                         }
160                     }
161             );
162             p.var(
163                     JsDate.getUTCHours,
164                     new CoreFunction<Number>(JsDate.getUTCHours.toString()) {
165                         @Override
166                         protected Number function(Object jsthis, Call<Number> callee) {
167                             return ((DateLike)like(jsthis)).getUTCHours();
168                         }
169                     }
170             );
171             p.var(
172                     JsDate.getMinutes,
173                     new CoreFunction<Number>(JsDate.getMinutes.toString()) {
174                         @Override
175                         protected Number function(Object jsthis, Call<Number> callee) {
176                             return ((DateLike)like(jsthis)).getMinutes();
177                         }
178                     }
179             );
180             p.var(
181                     JsDate.getUTCMinutes,
182                     new CoreFunction<Number>(JsDate.getUTCMinutes.toString()) {
183                         @Override
184                         protected Number function(Object jsthis, Call<Number> callee) {
185                             return ((DateLike)like(jsthis)).getUTCMinutes();
186                         }
187                     }
188             );
189             p.var(
190                     JsDate.getSeconds,
191                     new CoreFunction<Number>(JsDate.getSeconds.toString()) {
192                         @Override
193                         protected Number function(Object jsthis, Call<Number> callee) {
194                             return ((DateLike)like(jsthis)).getSeconds();
195                         }
196                     }
197             );
198             p.var(
199                     JsDate.getUTCSeconds,
200                     new CoreFunction<Number>(JsDate.getUTCSeconds.toString()) {
201                         @Override
202                         protected Number function(Object jsthis, Call<Number> callee) {
203                             return ((DateLike)like(jsthis)).getUTCSeconds();
204                         }
205                     }
206             );
207             p.var(
208                     JsDate.getMilliseconds,
209                     new CoreFunction<Number>(JsDate.getMilliseconds.toString()) {
210                         @Override
211                         protected Number function(Object jsthis, Call<Number> callee) {
212                             return ((DateLike)like(jsthis)).getMilliseconds();
213                         }
214                     }
215             );
216             p.var(
217                     JsDate.getUTCMilliseconds,
218                     new CoreFunction<Number>(JsDate.getUTCMilliseconds.toString()) {
219                         @Override
220                         protected Number function(Object jsthis, Call<Number> callee) {
221                             return ((DateLike)like(jsthis)).getUTCMilliseconds();
222                         }
223                     }
224             );
225             p.var(
226                     JsDate.getTimezoneOffset,
227                     new CoreFunction<Number>(JsDate.getTimezoneOffset.toString()) {
228                         @Override
229                         protected Number function(Object jsthis, Call<Number> callee) {
230                             return ((DateLike)like(jsthis)).getTimezoneOffset();
231                         }
232                     }
233             );
234             p.var(
235                     JsDate.setTime,
236                     new CoreFunction<Number>(JsDate.setTime.toString()) {
237                         @Override
238                         protected Number function(Object jsthis, Call<Number> callee) {
239                             return ((DateLike)like(jsthis)).setTime(
240                                     callee.arguments.get(0)
241                             );
242                         }
243                     }
244             );
245             p.var(
246                     JsDate.setMilliseconds,
247                     new CoreFunction<Number>(JsDate.setMilliseconds.toString()) {
248                         @Override
249                         protected Number function(Object jsthis, Call<Number> callee) {
250                             return ((DateLike)like(jsthis)).setMilliseconds(
251                                     callee.arguments.get(0)
252                             );
253                         }
254                     }
255             );
256             p.var(
257                     JsDate.setUTCMilliseconds,
258                     new CoreFunction<Number>(JsDate.setUTCMilliseconds.toString()) {
259                         @Override
260                         protected Number function(Object jsthis, Call<Number> callee) {
261                             return ((DateLike)like(jsthis)).setUTCMilliseconds(
262                                     callee.arguments.get(0)
263                             );
264                         }
265                     }
266             );
267             p.var(
268                     JsDate.setSeconds,
269                     new CoreFunction<Number>(JsDate.setSeconds.toString()) {
270                         @Override
271                         protected Number function(Object jsthis, Call<Number> callee) {
272                             return callee.arguments.length() < 2 ?
273                                     ((DateLike)like(jsthis)).setSeconds(
274                                             callee.arguments.get(0)
275                                     ) : ((DateLike)like(jsthis)).setSeconds(
276                                             callee.arguments.get(0),
277                                             callee.arguments.get(1)
278                                     );
279                         }
280                     }
281             );
282             p.var(
283                     JsDate.setUTCSeconds,
284                     new CoreFunction<Number>(JsDate.setUTCSeconds.toString()) {
285                         @Override
286                         protected Number function(Object jsthis, Call<Number> callee) {
287                             return callee.arguments.length() < 2 ?
288                                     ((DateLike)like(jsthis)).setUTCSeconds(
289                                             callee.arguments.get(0)
290                                     ) : ((DateLike)like(jsthis)).setUTCSeconds(
291                                             callee.arguments.get(0),
292                                             callee.arguments.get(1)
293                                     );
294                         }
295                     }
296             );
297             p.var(
298                     JsDate.setMinutes,
299                     new CoreFunction<Number>(JsDate.setMinutes.toString()) {
300                         @Override
301                         protected Number function(Object jsthis, Call<Number> callee) {
302                             return callee.arguments.length() < 2 ? ((DateLike)like(jsthis)).setMinutes(
303                                             callee.arguments.get(0)
304                                     ) : callee.arguments.length() < 3 ? ((DateLike)like(jsthis)).setMinutes(
305                                             callee.arguments.get(0),
306                                             callee.arguments.get(1)
307                                     ) : ((DateLike)like(jsthis)).setMinutes(
308                                             callee.arguments.get(0),
309                                             callee.arguments.get(1),
310                                             callee.arguments.get(2)
311                                     );
312                         }
313                     }
314             );
315             p.var(
316                     JsDate.setUTCMinutes,
317                     new CoreFunction<Number>(JsDate.setUTCMinutes.toString()) {
318                         @Override
319                         protected Number function(Object jsthis, Call<Number> callee) {
320                             return callee.arguments.length() < 2 ? ((DateLike)like(jsthis)).setUTCMinutes(
321                                             callee.arguments.get(0)
322                                     ) : callee.arguments.length() < 3 ? ((DateLike)like(jsthis)).setUTCMinutes(
323                                             callee.arguments.get(0),
324                                             callee.arguments.get(1)
325                                     ) : ((DateLike)like(jsthis)).setUTCMinutes(
326                                             callee.arguments.get(0),
327                                             callee.arguments.get(1),
328                                             callee.arguments.get(2)
329                                     );
330                         }
331                     }
332             );
333             p.var(
334                     JsDate.setHours,
335                     new CoreFunction<Number>(JsDate.setHours.toString()) {
336                         @Override
337                         protected Number function(Object jsthis, Call<Number> callee) {
338                             return callee.arguments.length() < 2 ? ((DateLike)like(jsthis)).setHours(
339                                             callee.arguments.get(0)
340                                     ) : callee.arguments.length() < 3 ? ((DateLike)like(jsthis)).setHours(
341                                             callee.arguments.get(0),
342                                             callee.arguments.get(1)
343                                     ) : callee.arguments.length() < 4 ? ((DateLike)like(jsthis)).setHours(
344                                             callee.arguments.get(0),
345                                             callee.arguments.get(1),
346                                             callee.arguments.get(2)
347                                     ) : ((DateLike)like(jsthis)).setHours(
348                                             callee.arguments.get(0),
349                                             callee.arguments.get(1),
350                                             callee.arguments.get(2),
351                                             callee.arguments.get(3)
352                                     );
353                         }
354                     }
355             );
356             p.var(
357                     JsDate.setUTCHours,
358                     new CoreFunction<Number>(JsDate.setUTCHours.toString()) {
359                         @Override
360                         protected Number function(Object jsthis, Call<Number> callee) {
361                             return callee.arguments.length() < 2 ? ((DateLike)like(jsthis)).setUTCHours(
362                                             callee.arguments.get(0)
363                                     ) : callee.arguments.length() < 3 ? ((DateLike)like(jsthis)).setUTCHours(
364                                             callee.arguments.get(0),
365                                             callee.arguments.get(1)
366                                     ) : callee.arguments.length() < 4 ? ((DateLike)like(jsthis)).setUTCHours(
367                                             callee.arguments.get(0),
368                                             callee.arguments.get(1),
369                                             callee.arguments.get(2)
370                                     ) : ((DateLike)like(jsthis)).setUTCHours(
371                                             callee.arguments.get(0),
372                                             callee.arguments.get(1),
373                                             callee.arguments.get(2),
374                                             callee.arguments.get(3)
375                                     );
376                         }
377                     }
378             );
379             p.var(
380                     JsDate.setDate,
381                     new CoreFunction<Number>(JsDate.setDate.toString()) {
382                         @Override
383                         protected Number function(Object jsthis, Call<Number> callee) {
384                             return ((DateLike)like(jsthis)).setDate(
385                                     callee.arguments.get(0)
386                             );
387                         }
388                     }
389             );
390             p.var(
391                     JsDate.setUTCDate,
392                     new CoreFunction<Number>(JsDate.setUTCDate.toString()) {
393                         @Override
394                         protected Number function(Object jsthis, Call<Number> callee) {
395                             return ((DateLike)like(jsthis)).setUTCDate(
396                                     callee.arguments.get(0)
397                             );
398                         }
399                     }
400             );
401             p.var(
402                     JsDate.setMonth,
403                     new CoreFunction<Number>(JsDate.setMonth.toString()) {
404                         @Override
405                         protected Number function(Object jsthis, Call<Number> callee) {
406                             return callee.arguments.length() < 2 ?
407                                     ((DateLike)like(jsthis)).setMonth(
408                                             callee.arguments.get(0)
409                                     ) : ((DateLike)like(jsthis)).setMonth(
410                                             callee.arguments.get(0),
411                                             callee.arguments.get(1)
412                                     );
413                         }
414                     }
415             );
416             p.var(
417                     JsDate.setUTCMonth,
418                     new CoreFunction<Number>(JsDate.setUTCMonth.toString()) {
419                         @Override
420                         protected Number function(Object jsthis, Call<Number> callee) {
421                             return callee.arguments.length() < 2 ?
422                                     ((DateLike)like(jsthis)).setUTCMonth(
423                                             callee.arguments.get(0)
424                                     ) : ((DateLike)like(jsthis)).setUTCMonth(
425                                             callee.arguments.get(0),
426                                             callee.arguments.get(1)
427                                     );
428                         }
429                     }
430             );
431             p.var(
432                     JsDate.setFullYear,
433                     new CoreFunction<Number>(JsDate.setFullYear.toString()) {
434                         @Override
435                         protected Number function(Object jsthis, Call<Number> callee) {
436                             return callee.arguments.length() < 2 ? ((DateLike)like(jsthis)).setFullYear(
437                                             callee.arguments.get(0)
438                                     ) : callee.arguments.length() < 3 ? ((DateLike)like(jsthis)).setFullYear(
439                                             callee.arguments.get(0),
440                                             callee.arguments.get(1)
441                                     ) : ((DateLike)like(jsthis)).setFullYear(
442                                             callee.arguments.get(0),
443                                             callee.arguments.get(1),
444                                             callee.arguments.get(2)
445                                     );
446                         }
447                     }
448             );
449             p.var(
450                     JsDate.setUTCFullYear,
451                     new CoreFunction<Number>(JsDate.setUTCFullYear.toString()) {
452                         @Override
453                         protected Number function(Object jsthis, Call<Number> callee) {
454                             return callee.arguments.length() < 2 ? ((DateLike)like(jsthis)).setUTCFullYear(
455                                             callee.arguments.get(0)
456                                     ) : callee.arguments.length() < 3 ? ((DateLike)like(jsthis)).setUTCFullYear(
457                                             callee.arguments.get(0),
458                                             callee.arguments.get(1)
459                                     ) : ((DateLike)like(jsthis)).setUTCFullYear(
460                                             callee.arguments.get(0),
461                                             callee.arguments.get(1),
462                                             callee.arguments.get(2)
463                                     );
464                         }
465                     }
466             );
467             p.var(
468                     JsDate.toDateString,
469                     new CoreFunction<String>(JsDate.toDateString.toString()) {
470                         @Override
471                         protected String function(Object jsthis, Call<String> callee) {
472                             return ((DateLike)like(jsthis)).toDateString();
473                         }
474                     }
475             );
476             p.var(
477                     JsDate.toTimeString,
478                     new CoreFunction<String>(JsDate.toTimeString.toString()) {
479                         @Override
480                         protected String function(Object jsthis, Call<String> callee) {
481                             return ((DateLike)like(jsthis)).toTimeString();
482                         }
483                     }
484             );
485             p.var(
486                     JsDate.toLocaleDateString,
487                     new CoreFunction<String>(JsDate.toLocaleDateString.toString()) {
488                         @Override
489                         protected String function(Object jsthis, Call<String> callee) {
490                             return ((DateLike)like(jsthis)).toLocaleDateString();
491                         }
492                     }
493             );
494             p.var(
495                     JsDate.toLocaleTimeString,
496                     new CoreFunction<String>(JsDate.toLocaleTimeString.toString()) {
497                         @Override
498                         protected String function(Object jsthis, Call<String> callee) {
499                             return ((DateLike)like(jsthis)).toLocaleTimeString();
500                         }
501                     }
502             );
503             p.var(
504                     JsDate.toUTCString,
505                     new CoreFunction<String>(JsDate.toUTCString.toString()) {
506                         @Override
507                         protected String function(Object jsthis, Call<String> callee) {
508                             return ((DateLike)like(jsthis)).toUTCString();
509                         }
510                     }
511             );
512             p.var(
513                     JsDate.toString,
514                     new CoreFunction<String>(JsDate.toString.toString()) {
515                         @Override
516                         protected String function(Object jsthis, Call<String> callee) {
517                             return Js.toString(like(jsthis));
518                         }
519                     }
520             );
521             p.var(
522                     JsDate.valueOf,
523                     new CoreFunction<Number>(JsDate.valueOf.toString()) {
524                         @Override
525                         protected Number function(Object jsthis, Call<Number> callee) {
526                             return ((DateLike)like(jsthis)).valueOf();
527                         }
528                     }
529             );
530         }
531         return singleton;
532     }
533 }