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 js.*;
025 import js.core.*;
026 
027 final class JscFunction<T> extends JsFunction<T> implements
028     Iterable<JsFunction.Mid>, Like<Function<T>>, LikeObject, Unwrappable<JscObject>
029 {
030     public JscFunction(JscObject var) {
031         super((Object)var);
032     }
033     public JscFunction(Function<T> var) {
034         this(
035                 new JscObject(
036                         var,
037                         Js.object()
038                 )
039         );
040         JsFunction<?> go = JsGlobal.Object.with();
041         if (Js.be(go)) {
042             JsFunction.prototype.with(this, go.create());
043         }
044         JsFunction<?> gf = JsGlobal.Function.with();
045         if (Js.be(gf)) {
046             JsObject.constructor.with(this, gf);
047         }
048     }
049 
050     public final JscObject unwrap() {
051         return (JscObject)var;
052     }
053 
054     @SuppressWarnings("unchecked")
055     public final Function<T> like() {
056         return (Function<T>)unwrap().like();
057     }
058 
059     public final ObjectLike likeObject() {
060         return unwrap().likeObject();
061     }
062 
063     @Override
064     public final JscFunction<T> var() {
065         return this;
066     }
067 
068     public final Iterator<Mid> iterator() {
069         return unwrap().iterator();
070     }
071 
072     @Override
073     public final String typeof() {
074         return undefined() ? "undefined" : "object";
075     }
076 
077     @Override
078     public final String toString() {
079         return toString.with(this).call(this);
080     }
081 
082     @SuppressWarnings("unchecked")
083     @Override
084     public final JsFunction<T> valueOf() {
085         return (JsFunction<T>)valueOf.with(this).call(this);
086     }
087 
088     @Override
089     public final boolean undefined() {
090         return unwrap().undefined();
091     }
092 
093     @Override
094     public final boolean delete() {
095         return unwrap().delete();
096     }
097 
098     @Override
099     public final boolean delete(Mid mid) {
100         return unwrap().delete();
101     }
102 
103     @Override
104     public final Object var(Mid mid) {
105         return unwrap().var(mid);
106     }
107     @Override
108     public final <S> S var(Mid mid, S v) {
109         return unwrap().var(mid, v);
110     }
111 
112     @Override
113     public final JsObject create() {
114         return like().create();
115     }
116     @Override
117     public final JsObject create(Object arg) {
118         return like().create(arg);
119     }
120     @Override
121     public final JsObject create(Vars<?> args) {
122         return like().create(args);
123     }
124     @Override
125     public final T invoke() {
126         return like().invoke();
127     }
128     @Override
129     public final T invoke(Object arg) {
130         return like().invoke(arg);
131     }
132     @Override
133     public final T invoke(Vars<?> args) {
134         return like().invoke(args);
135     }
136     @Override
137     public final T call(Object thisobj) {
138         return like().call(thisobj);
139     }
140     @Override
141     public final T call(Object thisobj, Object arg) {
142         return like().call(thisobj, arg);
143     }
144     @Override
145     public final T call(Object thisobj, Vars<?> args) {
146         return like().call(thisobj, args);
147     }
148     @Override
149     public final T apply(Object thisobj) {
150         return like().apply(thisobj);
151     }
152     @Override
153     public final T apply(Object thisobj, Object args) {
154         return like().call(thisobj, args);
155     }
156 
157     @Override
158     protected final <S> S call(JsFunction.Member<S> member) {
159         return member.with(this).call(this);
160     }
161     @Override
162     protected final <S> S call(JsFunction.Member<S> member, Object arg) {
163         return member.with(this).call(this, arg);
164     }
165     @Override
166     protected final <S> S call(JsFunction.Member<S> member, Vars<?> args) {
167         return member.with(this).call(this, args);
168     }
169 }