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 java.util.Iterator;
023 
024 import org.jscripter.sim.SimUtil;
025 
026 
027 import js.*;
028 import js.core.*;
029 
030 final class JscArray extends JsArray implements
031     Iterable<JsArray.Mid>, Like<ArrayLike<Object>>, LikeObject, Unwrappable<JscObject>
032 {
033     public JscArray(JscObject var) {
034         super((Object)var);
035     }
036     public JscArray(ArrayLike<?> var, ObjectLike objectLike) {
037         this(new JscObject(var, objectLike));
038     }
039     public JscArray(ArrayLike<?> var) {
040         this(var, Js.object());
041     }
042 
043     public final JscObject unwrap() {
044         return (JscObject)var;
045     }
046 
047     @SuppressWarnings("unchecked")
048     public final ArrayLike<Object> like() {
049         return (ArrayLike<Object>)unwrap().like();
050     }
051 
052     public final ObjectLike likeObject() {
053         return unwrap().likeObject();
054     }
055 
056     @Override
057     public final JscArray var() {
058         return this;
059     }
060 
061     public final Iterator<Mid> iterator() {
062         return unwrap().iterator();
063     }
064 
065     @Override
066     public final String toString() {
067         return toString.with(this).call(this);
068     }
069 
070     @Override
071     public final String typeof() {
072         return undefined() ? "undefined" : "object";
073     }
074 
075     @Override
076     public final JsArray valueOf() {
077         return valueOf.with(this).call(this);
078     }
079 
080     @Override
081     public final boolean undefined() {
082         return unwrap().undefined();
083     }
084 
085     @Override
086     public final boolean delete() {
087         return unwrap().delete();
088     }
089 
090     @Override
091     public final boolean delete(Mid mid) {
092         return unwrap().delete(mid);
093     }
094 
095     @Override
096     public final Object var(Mid mid) {
097         if (mid == Members.length) {
098             return like().length();
099         } else if (mid == Members.index) {
100             return like().index();
101         } else if (mid == Members.input) {
102             return like().input();
103         }
104         return unwrap().var(mid);
105     }
106     @Override
107     public final <S> S var(Mid mid, S v) {
108         if (mid == Members.length) {
109             like().length(
110                     SimUtil.getNumber(v).intValue()
111             );
112             return v;
113         }
114         return unwrap().var(mid, v);
115     }
116 
117     @Override
118     public final Object get(int i) {
119         return like().get(i);
120     }
121     @Override
122     public final Object set(int i, Object v) {
123         return like().set(i, v);
124     }
125     @Override
126     public final boolean delete(int i) {
127         return like().delete(i);
128     }
129 
130     @Override
131     protected final <T> T call(JsFunction.Member<T> member) {
132         return member.with(this).call(this);
133     }
134     @Override
135     protected final <T> T call(JsFunction.Member<T> member, Object arg) {
136         return member.with(this).call(this, arg);
137     }
138     @Override
139     protected final <T> T call(JsFunction.Member<T> member, Vars<?> args) {
140         return member.with(this).call(this, args);
141     }
142 }