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.client;
021 
022 import netscape.javascript.JSObject;
023 import js.*;
024 import js.core.*;
025 
026 abstract class JseObject extends JsObject implements Unwrappable<JseObject>, Live
027 {
028     protected JseObject(Object var) {
029         super(var);
030     }
031 
032     @Override
033     public final JseObject var() {
034         return this;
035     }
036     public final JseObject unwrap() {
037         return this;
038     }
039     public JSObject getJSObject() {
040         return (JSObject)var;
041     }
042 
043     public static final Object unwrap(Object v) {
044         v = v instanceof Var<?> ? ((Var<?>)v).var() : v;
045         return v instanceof JsObject ?
046                 ((Live)((JsObject)v).var()).getJSObject() : v;
047     }
048     public static final Object[] unwrap(Vars<?> args) {
049         ArrayLike<?> a = args.var();
050         if (a == null) return Embeded.noargs;
051         int len = a.length();
052         while(len > 0 && Js.undefined(a.get(len - 1))) len--;
053         if (len < 1) return Embeded.noargs;
054         Object[] arr = new Object[len];
055         for (int i = 0; i < len; i++) {
056             arr[i] = unwrap(a.get(i));
057         }
058         return arr;
059     }
060 
061     public abstract Object callMember(Mid mid);
062     public abstract Object callMember(Mid mid, Object arg);
063     public abstract Object callMember(Mid mid, Vars<?> args);
064     public abstract Object eval(String s);
065     @Override
066     public abstract Object var(Mid mid);
067     public abstract Object getSlot(int index);
068     public abstract void removeMember(Mid mid);
069     @Override
070     public abstract <T> T var(Mid mid, T v);
071     public abstract void setSlot(int index, Object v);
072 
073     @Override
074     public final String toString() {
075         return var == null ? "undefined" : getJSObject().toString();
076     }
077 
078     @Override
079     public final Object valueOf() {
080         return this;
081     }
082 
083     @Override
084     public final String typeof() {
085         return var == null ? "undefined" : (String)getJSObject().eval("typeof this");
086     }
087 
088     @Override
089     public final boolean undefined() {
090         return var == null;
091     }
092 
093     @Override
094     public final boolean delete() {
095         if (undefined()) return false;
096         var = null;
097         return true;
098     }
099 
100     @Override
101     public final boolean delete(Mid mid) {
102         return undefined() ? false : (Boolean)getJSObject().eval("");
103     }
104 
105     public final boolean in(Object o) {
106         return undefined() ? false : (Boolean)getJSObject().eval("");
107     }
108 
109     @SuppressWarnings("unchecked")
110     @Override
111     protected final <T> T call(JsFunction.Member<T> member) {
112         return (T)callMember(member.mid());
113     }
114     @SuppressWarnings("unchecked")
115     @Override
116     protected final <T> T call(JsFunction.Member<T> member, Object arg) {
117         return (T)callMember(member.mid(), arg);
118     }
119     @SuppressWarnings("unchecked")
120     @Override
121     protected final <T> T call(JsFunction.Member<T> member, Vars<?> args) {
122         return (T)callMember(member.mid(), args);
123     }
124 }