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.Var;
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.JsRegExp;
031 
032 final class CreatorRegExp extends CoreFunction<JsRegExp>
033 {
034     private CreatorRegExp() {
035         super(JsGlobal.RegExp.toString());
036     }
037 
038     @Override
039     protected JsRegExp function(Object jsthis, Call<JsRegExp> callee) {
040         Object arg0 = callee.arguments.get(0);
041         Object arg1 = callee.arguments.get(1);
042         return arg0 instanceof JsRegExp ? (JsRegExp)arg0 :
043             (JsRegExp)construct(new JscRegExp(
044                     arg0 instanceof String ? arg1 instanceof String ?
045                     Js.re((String)arg0, (String)arg1) : 
046                     Js.re((String)arg0) : null
047             ));
048     }
049     @Override
050     protected final JsRegExp construct(Object o) {
051         JsRegExp ret = (JsRegExp)o;
052         JsObject.constructor.with(ret, var());
053         JsFunction.prototype.with(ret, JsFunction.prototype.with(var()));
054         return ret;
055     }
056     @Override
057     public final JsRegExp create() {
058         return invoke();
059     }
060     @Override
061     public final JsRegExp create(Object arg) {
062         return invoke(arg);
063     }
064     @Override
065     public final JsRegExp create(Vars<?> args) {
066         return invoke(args);
067     }
068 
069     private static CreatorRegExp singleton;
070 
071     public final static synchronized CreatorRegExp get() {
072         if (singleton == null) {
073             singleton = new CreatorRegExp();
074             JsObject p = JsFunction.prototype.with(singleton.var());
075             p.var(
076                     JsRegExp.exec,
077                     new CoreFunction<JsArray>(JsRegExp.exec.toString()) {
078                         @Override
079                         protected JsArray function(Object jsthis, Call<JsArray> callee) {
080                             return jsthis instanceof RegExpLike ?
081                                    new JscArray(
082                                         ((RegExpLike)jsthis).exec(callee.arguments.get(0))
083                                    ) :
084                                    new JscArray(
085                                         ((RegExpLike)((Var<?>)jsthis).var()).exec(callee.arguments.get(0))
086                                    );
087                         }
088                     }
089             );
090             p.var(
091                     JsRegExp.test,
092                     new CoreFunction<Boolean>(JsRegExp.test.toString()) {
093                         @Override
094                         protected Boolean function(Object jsthis, Call<Boolean> callee) {
095                             return jsthis instanceof RegExpLike ?
096                                    ((RegExpLike)jsthis).test(callee.arguments.get(0)) :
097                                    ((RegExpLike)((Var<?>)jsthis).var()).test(callee.arguments.get(0));
098                         }
099                     }
100             );
101             p.var(
102                     JsRegExp.toString,
103                     new CoreFunction<String>(JsRegExp.toString.toString()) {
104                         @Override
105                         protected String function(Object jsthis, Call<String> callee) {
106                             return Js.toString(like(jsthis));
107                         }
108                     }
109             );
110             p.var(
111                     JsRegExp.valueOf,
112                     new CoreFunction<JsRegExp>(JsRegExp.valueOf.toString()) {
113                         @Override
114                         protected JsRegExp function(Object jsthis, Call<JsRegExp> callee) {
115                             return new JscRegExp(((RegExpLike)like(jsthis)).valueOf());
116                         }
117                     }
118             );
119         }
120         return singleton;
121     }
122 }