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.JSException;
023 import netscape.javascript.JSObject;
024 import js.*;
025 import js.core.JsObject;
026 
027 class JseObjectFF extends JseObject
028 {
029     static class JseoFunction<T> extends JseObjectFF implements HasFun<T>
030     {
031         private JSObject jsof;
032 
033         public JseoFunction(Function<T> var) {
034             super(var);
035         }
036 
037         @SuppressWarnings("unchecked")
038         public final Function<T> getFunction() {
039             return (Function<T>)var;
040         }
041 
042         @Override
043         public final synchronized JSObject getJSObject() {
044             if (jsof == null) {
045                 JSObject win = ((Live)((JsObject)Js.win()).var()).getJSObject();
046                 try {
047                     jsof = (JSObject)win.call("$f", new Object[]{getFunction()});
048                 } catch (JSException e) {
049                     throw e;
050                 }
051             }
052             return jsof;
053         }
054     }
055 
056     public JseObjectFF(Object var) {
057         super(var);
058     }
059 
060     public static final Object wrap(Object v) {
061         return v instanceof JSObject ? new JseObjectFF((JSObject)v) : v;
062     }
063 
064     @Override
065     public final Object callMember(Mid mid) {
066         return wrap(getJSObject().call(mid.toString(), Embeded.noargs));
067     }
068     @Override
069     public final Object callMember(Mid mid, Object arg) {
070         return wrap(getJSObject().call(
071                 mid.toString(),
072                 Js.undefined(arg) ? Embeded.noargs : new Object[]{unwrap(arg)}
073         ));
074     }
075     @Override
076     public final Object callMember(Mid mid, Vars<?> args) {
077         return wrap(getJSObject().call(mid.toString(), unwrap(args)));
078     }
079 
080     @Override
081     public final Object eval(String s) {
082         return wrap(getJSObject().eval(s));
083     }
084 
085     @Override
086     public final Object var(Mid mid) {
087         try {
088             JSObject o = getJSObject();
089             if (o == null) return null;
090             return wrap(getJSObject().getMember(mid.toString()));
091         } catch (JSException jse) {
092             return null;
093         }
094     }
095     @Override
096     public final Object getSlot(int index) {
097         try {
098             JSObject o = getJSObject();
099             if (o == null) return null;
100             return wrap(getJSObject().getSlot(index));
101         } catch (JSException jse) {
102             return null;
103         }
104     }
105     @Override
106     public final void removeMember(Mid mid) {
107         getJSObject().removeMember(mid.toString());
108     }
109 
110     @Override
111     public final <T> T var(Mid mid, T v) {
112         getJSObject().setMember(mid.toString(), unwrap(v));
113         return v;
114     }
115     @Override
116     public final void setSlot(int index, Object v) {
117         getJSObject().setSlot(index, unwrap(v));
118     }
119 }