| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | // API class |
| 8 | |
| 9 | package org.mozilla.javascript; |
| 10 | |
| 11 | /** |
| 12 | * This is a helper class for implementing wrappers around Scriptable |
| 13 | * objects. It implements the Function interface and delegates all |
| 14 | * invocations to a delegee Scriptable object. The normal use of this |
| 15 | * class involves creating a sub-class and overriding one or more of |
| 16 | * the methods. |
| 17 | * |
| 18 | * A useful application is the implementation of interceptors, |
| 19 | * pre/post conditions, debugging. |
| 20 | * |
| 21 | * @see Function |
| 22 | * @see Scriptable |
| 23 | * @author Matthias Radestock |
| 24 | */ |
| 25 | |
| 26 | public class Delegator implements Function { |
| 27 | |
| 28 | protected Scriptable obj = null; |
| 29 | |
| 30 | /** |
| 31 | * Create a Delegator prototype. |
| 32 | * |
| 33 | * This constructor should only be used for creating prototype |
| 34 | * objects of Delegator. |
| 35 | * |
| 36 | * @see org.mozilla.javascript.Delegator#construct |
| 37 | */ |
| 38 | public Delegator() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Create a new Delegator that forwards requests to a delegee |
| 43 | * Scriptable object. |
| 44 | * |
| 45 | * @param obj the delegee |
| 46 | * @see org.mozilla.javascript.Scriptable |
| 47 | */ |
| 48 | public Delegator(Scriptable obj) { |
| 49 | this.obj = obj; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Crete new Delegator instance. |
| 54 | * The default implementation calls this.getClass().newInstance(). |
| 55 | * |
| 56 | * @see #construct(Context cx, Scriptable scope, Object[] args) |
| 57 | */ |
| 58 | protected Delegator newInstance() |
| 59 | { |
| 60 | try { |
| 61 | return this.getClass().newInstance(); |
| 62 | } catch (Exception ex) { |
| 63 | throw Context.throwAsScriptRuntimeEx(ex); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Retrieve the delegee. |
| 69 | * |
| 70 | * @return the delegee |
| 71 | */ |
| 72 | public Scriptable getDelegee() { |
| 73 | return obj; |
| 74 | } |
| 75 | /** |
| 76 | * Set the delegee. |
| 77 | * |
| 78 | * @param obj the delegee |
| 79 | * @see org.mozilla.javascript.Scriptable |
| 80 | */ |
| 81 | public void setDelegee(Scriptable obj) { |
| 82 | this.obj = obj; |
| 83 | } |
| 84 | /** |
| 85 | * @see org.mozilla.javascript.Scriptable#getClassName |
| 86 | */ |
| 87 | public String getClassName() { |
| 88 | return obj.getClassName(); |
| 89 | } |
| 90 | /** |
| 91 | * @see org.mozilla.javascript.Scriptable#get(String, Scriptable) |
| 92 | */ |
| 93 | public Object get(String name, Scriptable start) { |
| 94 | return obj.get(name,start); |
| 95 | } |
| 96 | /** |
| 97 | * @see org.mozilla.javascript.Scriptable#get(int, Scriptable) |
| 98 | */ |
| 99 | public Object get(int index, Scriptable start) { |
| 100 | return obj.get(index,start); |
| 101 | } |
| 102 | /** |
| 103 | * @see org.mozilla.javascript.Scriptable#has(String, Scriptable) |
| 104 | */ |
| 105 | public boolean has(String name, Scriptable start) { |
| 106 | return obj.has(name,start); |
| 107 | } |
| 108 | /** |
| 109 | * @see org.mozilla.javascript.Scriptable#has(int, Scriptable) |
| 110 | */ |
| 111 | public boolean has(int index, Scriptable start) { |
| 112 | return obj.has(index,start); |
| 113 | } |
| 114 | /** |
| 115 | * @see org.mozilla.javascript.Scriptable#put(String, Scriptable, Object) |
| 116 | */ |
| 117 | public void put(String name, Scriptable start, Object value) { |
| 118 | obj.put(name,start,value); |
| 119 | } |
| 120 | /** |
| 121 | * @see org.mozilla.javascript.Scriptable#put(int, Scriptable, Object) |
| 122 | */ |
| 123 | public void put(int index, Scriptable start, Object value) { |
| 124 | obj.put(index,start,value); |
| 125 | } |
| 126 | /** |
| 127 | * @see org.mozilla.javascript.Scriptable#delete(String) |
| 128 | */ |
| 129 | public void delete(String name) { |
| 130 | obj.delete(name); |
| 131 | } |
| 132 | /** |
| 133 | * @see org.mozilla.javascript.Scriptable#delete(int) |
| 134 | */ |
| 135 | public void delete(int index) { |
| 136 | obj.delete(index); |
| 137 | } |
| 138 | /** |
| 139 | * @see org.mozilla.javascript.Scriptable#getPrototype |
| 140 | */ |
| 141 | public Scriptable getPrototype() { |
| 142 | return obj.getPrototype(); |
| 143 | } |
| 144 | /** |
| 145 | * @see org.mozilla.javascript.Scriptable#setPrototype |
| 146 | */ |
| 147 | public void setPrototype(Scriptable prototype) { |
| 148 | obj.setPrototype(prototype); |
| 149 | } |
| 150 | /** |
| 151 | * @see org.mozilla.javascript.Scriptable#getParentScope |
| 152 | */ |
| 153 | public Scriptable getParentScope() { |
| 154 | return obj.getParentScope(); |
| 155 | } |
| 156 | /** |
| 157 | * @see org.mozilla.javascript.Scriptable#setParentScope |
| 158 | */ |
| 159 | public void setParentScope(Scriptable parent) { |
| 160 | obj.setParentScope(parent); |
| 161 | } |
| 162 | /** |
| 163 | * @see org.mozilla.javascript.Scriptable#getIds |
| 164 | */ |
| 165 | public Object[] getIds() { |
| 166 | return obj.getIds(); |
| 167 | } |
| 168 | /** |
| 169 | * Note that this method does not get forwarded to the delegee if |
| 170 | * the <code>hint</code> parameter is null, |
| 171 | * <code>ScriptRuntime.ScriptableClass</code> or |
| 172 | * <code>ScriptRuntime.FunctionClass</code>. Instead the object |
| 173 | * itself is returned. |
| 174 | * |
| 175 | * @param hint the type hint |
| 176 | * @return the default value |
| 177 | * |
| 178 | * @see org.mozilla.javascript.Scriptable#getDefaultValue |
| 179 | */ |
| 180 | public Object getDefaultValue(Class<?> hint) { |
| 181 | return (hint == null || |
| 182 | hint == ScriptRuntime.ScriptableClass || |
| 183 | hint == ScriptRuntime.FunctionClass) ? |
| 184 | this : obj.getDefaultValue(hint); |
| 185 | } |
| 186 | /** |
| 187 | * @see org.mozilla.javascript.Scriptable#hasInstance |
| 188 | */ |
| 189 | public boolean hasInstance(Scriptable instance) { |
| 190 | return obj.hasInstance(instance); |
| 191 | } |
| 192 | /** |
| 193 | * @see org.mozilla.javascript.Function#call |
| 194 | */ |
| 195 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 196 | Object[] args) |
| 197 | { |
| 198 | return ((Function)obj).call(cx,scope,thisObj,args); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Note that if the <code>delegee</code> is <code>null</code>, |
| 203 | * this method creates a new instance of the Delegator itself |
| 204 | * rathert than forwarding the call to the |
| 205 | * <code>delegee</code>. This permits the use of Delegator |
| 206 | * prototypes. |
| 207 | * |
| 208 | * @param cx the current Context for this thread |
| 209 | * @param scope an enclosing scope of the caller except |
| 210 | * when the function is called from a closure. |
| 211 | * @param args the array of arguments |
| 212 | * @return the allocated object |
| 213 | * |
| 214 | * @see Function#construct(Context, Scriptable, Object[]) |
| 215 | */ |
| 216 | public Scriptable construct(Context cx, Scriptable scope, Object[] args) |
| 217 | { |
| 218 | if (obj == null) { |
| 219 | //this little trick allows us to declare prototype objects for |
| 220 | //Delegators |
| 221 | Delegator n = newInstance(); |
| 222 | Scriptable delegee; |
| 223 | if (args.length == 0) { |
| 224 | delegee = new NativeObject(); |
| 225 | } else { |
| 226 | delegee = ScriptRuntime.toObject(cx, scope, args[0]); |
| 227 | } |
| 228 | n.setDelegee(delegee); |
| 229 | return n; |
| 230 | } |
| 231 | else { |
| 232 | return ((Function)obj).construct(cx,scope,args); |
| 233 | } |
| 234 | } |
| 235 | } |