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 
029 /**
030  * <p>Emulates the global Array function in JavaScript.</p>
031  * 
032  * @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>
033  * @since Descripter 1.0
034  */
035 public final class Array_ extends Function<Core>
036 {
037     /**
038      * <p>Creates the singleton of the global {@link Function} object of this type.</p>
039      * @param core The {@link Core} global script context
040      * @since Descripter 1.0
041      */
042     public static final void create(Core core) {
043         if (core.get(core._Array) == null) {
044             core.put(core._Array, new Array_(core));
045         }
046     }
047 
048     private Array_(Core core) {
049         super(core);
050         CObject prototype = prototype();
051         prototype.put(core._concat, new Function<Core>(core) {
052             @Override
053             protected Functor<Core> functor() {
054                 return new Functor<Core>(this) {
055                     @Override
056                     public Object function() {
057                         return ((CArray)this_()).concat(arguments());
058                     }
059                 };
060             }
061         });
062         prototype.put(core._join, new Function<Core>(core) {
063             @Override
064             protected Functor<Core> functor() {
065                 return new Functor<Core>(this) {
066                     @Override
067                     public Object function() {
068                         return ((CArray)this_()).join(toString(arguments().get(0)));
069                     }
070                 };
071             }
072         });
073         prototype.put(core._pop, new Function<Core>(core) {
074             @Override
075             protected Functor<Core> functor() {
076                 return new Functor<Core>(this) {
077                     @Override
078                     public Object function() {
079                         return ((CArray)this_()).pop();
080                     }
081                 };
082             }
083         });
084         prototype.put(core._push, new Function<Core>(core) {
085             @Override
086             protected Functor<Core> functor() {
087                 return new Functor<Core>(this) {
088                     @Override
089                     public Object function() {
090                         return ((CArray)this_()).push(arguments());
091                     }
092                 };
093             }
094         });
095         prototype.put(core._reverse, new Function<Core>(core) {
096             @Override
097             protected Functor<Core> functor() {
098                 return new Functor<Core>(this) {
099                     @Override
100                     public Object function() {
101                         return ((CArray)this_()).reverse();
102                     }
103                 };
104             }
105         });
106         prototype.put(core._shift, new Function<Core>(core) {
107             @Override
108             protected Functor<Core> functor() {
109                 return new Functor<Core>(this) {
110                     @Override
111                     public Object function() {
112                         return ((CArray)this_()).shift();
113                     }
114                 };
115             }
116         });
117         prototype.put(core._slice, new Function<Core>(core) {
118             @Override
119             protected Functor<Core> functor() {
120                 return new Functor<Core>(this) {
121                     @Override
122                     public Object function() {
123                         return ((CArray)this_()).slice(arguments());
124                     }
125                 };
126             }
127         });
128         prototype.put(core._sort, new Function<Core>(core) {
129             @Override
130             protected Functor<Core> functor() {
131                 return new Functor<Core>(this) {
132                     @Override
133                     public Object function() {
134                         return ((CArray)this_()).sort((Function<?>)arguments().get(0));
135                     }
136                 };
137             }
138         });
139         prototype.put(core._splice, new Function<Core>(core) {
140             @Override
141             protected Functor<Core> functor() {
142                 return new Functor<Core>(this) {
143                     @Override
144                     public Object function() {
145                         return ((CArray)this_()).splice(arguments());
146                     }
147                 };
148             }
149         });
150         prototype.put(core._unshift, new Function<Core>(core) {
151             @Override
152             protected Functor<Core> functor() {
153                 return new Functor<Core>(this) {
154                     @Override
155                     public Object function() {
156                         return ((CArray)this_()).unshift(arguments());
157                     }
158                 };
159             }
160         });
161     }
162 
163     /**
164      * <p>Allocates a new object with this {@link Function}.</p>
165      * @param script The script context that invoked this service
166      * @param args An array of the arguments passed by the invocation
167      * @return The newly allocated object
168      * @since Descripter 1.0
169      */
170     @Override
171     public <S extends Script<?>> CArray alloc(S script, Object ...args) {
172         CArray o = new CArray(this);
173 //      Function<?> f = function();
174 //      f.evaluate();
175         return o;
176     }
177 
178     /**
179      * <p>Gets the {@link Functor} of the {@link Function} object.</p>
180      * <p>This is a concrete implementation of the super abstract method.</p>
181      * @return The {@link Functor} of the {@link Function} object
182      * @since Descripter 1.0
183      */
184     @Override
185     protected Functor<Core> functor() {
186         return new Functor<Core>(this) {
187             @Override
188             public Object function() {
189                 return new CArray(Array_.this);
190             }
191         };
192     }
193 }