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 
024 import js.*;
025 import js.core.*;
026 
027 class JseFunction<T> extends JsFunction<T> implements Unwrappable<JseObject>, Live
028 {
029     protected static abstract class Members extends JsFunction.Members
030     {
031         public final static Mid function = id("$f"    );
032         public final static Mid apply    = id("apply" );
033         public final static Mid call     = id("call"  );
034         public final static Mid Object   = id("Object");
035     }
036 
037     public JseFunction(JseObject var) {
038         super((Object)var);
039     }
040 
041     @Override
042     public JseFunction<T> valueOf() {
043         return new JseFunction<T>((JseObject)unwrap().valueOf());
044     }
045     @Override
046     public final JseFunction<T> var() {
047         return this;
048     }
049     public final JseObject unwrap() {
050         return (JseObject)var;
051     }
052     public final JSObject getJSObject() {
053         return unwrap().getJSObject();
054     }
055 
056     @Override
057     public final String toString() {
058         return var == null ? "undefined" : unwrap().toString();
059     }
060 
061     @SuppressWarnings("unchecked")
062     protected T cast(Object o) {
063         return (T)o;
064     }
065     @Override
066     public final JsObject create() {
067         return unwrap() instanceof HasFun<?> ?
068             ((HasFun<?>)unwrap()).getFunction().create() :
069             (JseObject)((JseObject)Js.core().var()).callMember(Members.Object, invoke());
070     }
071     @Override
072     public final JsObject create(Object arg) {
073         return unwrap() instanceof HasFun<?> ?
074             ((HasFun<?>)unwrap()).getFunction().create(arg) :
075             (JseObject)((JseObject)Js.core().var()).callMember(Members.Object, invoke(arg));
076     }
077     @Override
078     public final JsObject create(Vars<?> args) {
079         return unwrap() instanceof HasFun<?> ?
080             ((HasFun<?>)unwrap()).getFunction().create(args) :
081             (JseObject)((JseObject)Js.core().var()).callMember(Members.Object, invoke(args));
082     }
083 
084     @SuppressWarnings("unchecked")
085     @Override
086     public final T call(Object jsthis) {
087         if (unwrap() instanceof HasFun<?>) {
088             return ((HasFun<T>)unwrap()).getFunction().call(jsthis);
089         }
090         Object f = unwrap().var(Members.function);
091         return f instanceof Function<?> ?  ((Function<T>)f).call(jsthis) : cast(unwrap().callMember(Members.call, jsthis));
092     }
093     @SuppressWarnings("unchecked")
094     @Override
095     public final T call(Object jsthis, Object arg) {
096         if (unwrap() instanceof HasFun<?>) {
097             return ((HasFun<T>)unwrap()).getFunction().call(jsthis, arg);
098         }
099         Object f = unwrap().var(Members.function);
100         return f instanceof Function<?> ?  ((Function<T>)f).call(jsthis, arg) : cast(unwrap().callMember(Members.call, new Vars<Object>().add(jsthis).add(arg)));
101     }
102     @SuppressWarnings("unchecked")
103     @Override
104     public final T call(Object jsthis, Vars<?> args) {
105         if (unwrap() instanceof HasFun<?>) {
106             return ((HasFun<T>)unwrap()).getFunction().call(jsthis, args);
107         }
108         Object f = unwrap().var(Members.function);
109         return f instanceof Function<?> ? ((Function<T>)f).call(jsthis, args) : cast(unwrap().callMember(Members.call, new Vars<Object>().add(jsthis).addAll(args)));
110     }
111     @SuppressWarnings("unchecked")
112     @Override
113     public final T invoke() {
114         if (unwrap() instanceof HasFun<?>) {
115             return ((HasFun<T>)unwrap()).getFunction().invoke();
116         }
117         Object f = unwrap().var(Members.function);
118         return f instanceof Function<?> ? ((Function<T>)f).invoke() : cast(unwrap().callMember(Members.call, Js.core()));
119     }
120     @SuppressWarnings("unchecked")
121     @Override
122     public final T invoke(Object arg) {
123         if (unwrap() instanceof HasFun<?>) {
124             return ((HasFun<T>)unwrap()).getFunction().invoke(arg);
125         }
126         Object f = unwrap().var(Members.function);
127         return f instanceof Function<?> ? ((Function<T>)f).invoke(arg) : cast(unwrap().callMember(Members.call, new Vars<Object>().add(Js.core()).add(arg)));
128     }
129     @SuppressWarnings("unchecked")
130     @Override
131     public final T invoke(Vars<?> args) {
132         if (unwrap() instanceof HasFun<?>) {
133             return ((HasFun<T>)unwrap()).getFunction().invoke(args);
134         }
135         Object f = unwrap().var(Members.function);
136         return f instanceof Function<?> ? ((Function<T>)f).invoke(args) : cast(unwrap().callMember(Members.call, new Vars<Object>().add(Js.core()).addAll(args)));
137     }
138     @SuppressWarnings("unchecked")
139     @Override
140     public final T apply(Object jsthis) {
141         if (unwrap() instanceof HasFun<?>) {
142             return ((HasFun<T>)unwrap()).getFunction().apply(jsthis);
143         }
144         Object f = unwrap().var(Members.function);
145         return f instanceof Function<?> ? ((Function<T>)f).apply(jsthis) : cast(unwrap().callMember(Members.apply, jsthis));
146     }
147     @SuppressWarnings("unchecked")
148     @Override
149     public final T apply(Object jsthis, Object arg) {
150         if (unwrap() instanceof HasFun<?>) {
151             return ((HasFun<T>)unwrap()).getFunction().apply(jsthis, arg);
152         }
153         Object f = unwrap().var(Members.function);
154         return f instanceof Function<?> ? ((Function<T>)f).apply(jsthis, arg) : cast(unwrap().callMember(Members.apply, new Vars<Object>().add(jsthis).add(arg)));
155     }
156 
157     @Override
158     public final boolean undefined() {
159         return var == null || unwrap().undefined();
160     }
161 
162     @Override
163     public final boolean delete() {
164         if (undefined()) return false;
165         return unwrap().delete();
166     }
167 
168     @Override
169     public final boolean delete(Mid mid) {
170         return undefined() ? false : unwrap().delete(mid);
171     }
172 
173     public final boolean in(Object o) {
174         return true;
175     }
176 
177     @Override
178     public final Object var(Mid mid) {
179         return unwrap().var(mid);
180     }
181     @Override
182     public final <S> S var(Mid mid, S v) {
183         return unwrap().var(mid, v);
184     }
185 
186     @Override
187     protected final <S> S call(JsFunction.Member<S> member) {
188         return unwrap().call(member);
189     }
190     @Override
191     protected final <S> S call(JsFunction.Member<S> member, Object arg) {
192         return unwrap().call(member, arg);
193     }
194     @Override
195     protected final <S> S call(JsFunction.Member<S> member, Vars<?> args) {
196         return unwrap().call(member, args);
197     }
198 }