001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
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.descripter.js.api.global;
021 
022 import org.descripter.js.api.Core;
023 import org.descripter.js.api.Function;
024 import org.descripter.js.api.Functor;
025 import org.descripter.js.api.Script;
026 import org.descripter.js.api.core.CArray;
027 import org.descripter.js.api.core.CObject;
028 import org.descripter.js.api.core.CRegExp;
029 
030 /**
031  * <p>Emulates the global RegExp function in JavaScript.</p>
032  * 
033  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
034  * @since Descripter 1.0
035  */
036 public class RegExp_ extends Function<Core>
037 {
038     /**
039      * <p>Creates the singleton of the global {@link Function} object of this type.</p>
040      * @param core The {@link Core} global script context
041      * @since Descripter 1.0
042      */
043     public static final void create(Core core) {
044         if (core.get(core._RegExp) == null) {
045             core.put(core._RegExp, new RegExp_(core));
046         }
047     }
048 
049     private RegExp_(Core core) {
050         super(core);
051         CObject prototype = prototype();
052         prototype.put(core._exec, new Function<Core>(core) {
053             @Override
054             protected Functor<Core> functor() {
055                 return new Functor<Core>(this) {
056                     @Override
057                     public Object function() {
058                         return ((CRegExp)this_()).exec(arguments().get(0));
059                     }
060                 };
061             }
062         });
063         prototype.put(core._test, new Function<Core>(core) {
064             @Override
065             protected Functor<Core> functor() {
066                 return new Functor<Core>(this) {
067                     @Override
068                     public Object function() {
069                         return ((CRegExp)this_()).test(arguments().get(0));
070                     }
071                 };
072             }
073         });
074     }
075 
076     /**
077      * <p>Allocates a new object with this {@link Function}.</p>
078      * @param script The script context that invoked this service
079      * @param args An array of the arguments passed by the invocation
080      * @return The newly allocated object
081      * @since Descripter 1.0
082      */
083     @Override
084     public <S extends Script<?>> CRegExp alloc(S script, Object ...args) {
085         if (args != null) {
086             switch (args.length) {
087                 case 1:
088                     return new CRegExp(this, toString(args[0]));
089                 case 2:
090                     return new CRegExp(this, toString(args[0]), toString(args[1]));
091             }
092         }
093         return null;
094     }
095 
096     /**
097      * <p>Gets the {@link Functor} of the {@link Function} object.</p>
098      * <p>This is a concrete implementation of the super abstract method.</p>
099      * @return The {@link Functor} of the {@link Function} object
100      * @since Descripter 1.0
101      */
102     @Override
103     protected Functor<Core> functor() {
104         return new Functor<Core>(this) {
105             @Override
106             public Object function() {
107                 CArray arguments = arguments();
108                 switch (arguments.length()) {
109                     case 1:
110                         return new CRegExp(RegExp_.this, toString(arguments.get(0)));
111                     case 2:
112                         return new CRegExp(RegExp_.this, toString(arguments.get(0)), toString(arguments.get(1)));
113                     default:
114                         return null;
115                 }
116             }
117         };
118     }
119 }