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;
021 
022 import java.util.HashMap;
023 import java.util.Iterator;
024 import java.util.Map;
025 
026 import org.jscripter.sim.core.Core;
027 
028 
029 import js.Id;
030 import js.Js;
031 import js.ObjectLike;
032 import js.Var;
033 import js.core.JsGlobal;
034 import js.core.JsObject;
035 
036 final class SimObjectLike extends Var<Object> implements ObjectLike
037 {
038     private final Map<Mid, Object> map = new HashMap<Mid, Object>();
039 
040     final boolean has(Mid mid) {
041         return map.containsKey(mid);
042     }
043 
044     //@Override
045     public final String typeof() {
046         return "object";
047     }
048 
049     //@Override
050     public final boolean undefined() {
051         return false;
052     }
053 
054     //@Override
055     public final Object valueOf() {
056         return map;
057     }
058 
059     //@Override
060     public final boolean delete(Id<?> id) {
061         return delete(id.mid());
062     }
063 
064     //@Override
065     public final boolean delete(Member<?> m) {
066         return delete(m.mid());
067     }
068 
069     //@Override
070     public final boolean delete(String name) {
071         return delete(Members.id(name));
072     }
073 
074     //@Override
075     public final synchronized boolean delete(Mid mid) {
076         if (!map.containsKey(mid)) {
077             return false;
078         }
079         map.remove(mid);
080         return true;
081     }
082 
083     @SuppressWarnings("unchecked")
084     //@Override
085     public final <T> T var(Id<T> id) {
086         return (T)var(id.mid());
087     }
088 
089     @SuppressWarnings("unchecked")
090     //@Override
091     public final <T> T var(Var.Member<T> m) {
092         return (T)var(m.mid());
093     }
094 
095     //@Override
096     public final Object var(String name) {
097         return var(Members.id(name));
098     }
099 
100     //@Override
101     public final synchronized Object var(Mid mid) {
102         return map.get(mid);
103     }
104 
105     //@Override
106     public final <T> T var(Id<T> id, T value) {
107         return var(id.mid(), value);
108     }
109 
110     //@Override
111     public final <T> T var(Var.Member<T> m, T value) {
112         return var(m.mid(), value);
113     }
114 
115     //@Override
116     public final <T> T var(String name, T value) {
117         return var(Members.id(name), value);
118     }
119 
120     //@Override
121     public final synchronized <T> T var(Mid mid, T value) {
122         map.put(mid, value);
123         return value;
124     }
125 
126     //@Override
127     public Boolean hasOwnProperty(Object propname) {
128         return has(Members.id(Js.toString(propname)));
129     }
130 
131     //@Override
132     public Boolean isPrototypeOf(Object o) {
133         return false;
134     }
135 
136     //@Override
137     public Boolean propertyIsEnumerable(Object propname) {
138         return hasOwnProperty(propname);
139     }
140 
141     //@Override
142     public String toLocaleString() {
143         return toString();
144     }
145 
146     //@Override
147     public final String toString() {
148         return "[object Object]";
149     }
150 
151     //@Override
152     public final synchronized JsObject var() {
153         if (Core.isRunning()) {
154             return JsGlobal.Object.with().create(this);
155         } else {
156             JsObject o = JsGlobal.Object.with().create();
157             for (Mid mid : map.keySet()) {
158                 o.var(mid, var(mid));
159             }
160             return o;
161         }
162     }
163 
164     final Iterator<Mid> iterator() {
165         return map.keySet().iterator();
166         
167     }
168 }