aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/rhino1_7R1/src/org/mozilla/javascript/Delegator.java
blob: e04486361d577f383631e1b52cb0ddc58a823675 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Delegator.java, released
 * Sep 27, 2000.
 *
 * The Initial Developer of the Original Code is
 * Matthias Radestock. <matthias@sorted.org>.
 * Portions created by the Initial Developer are Copyright (C) 2000
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 * case the provisions of the GPL are applicable instead of those above. If
 * you wish to allow use of your version of this file only under the terms of
 * the GPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replacing
 * them with the notice and other provisions required by the GPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 *
 * ***** END LICENSE BLOCK ***** */

// API class

package org.mozilla.javascript;

/**
 * This is a helper class for implementing wrappers around Scriptable
 * objects. It implements the Function interface and delegates all
 * invocations to a delegee Scriptable object. The normal use of this
 * class involves creating a sub-class and overriding one or more of
 * the methods.
 *
 * A useful application is the implementation of interceptors,
 * pre/post conditions, debugging.
 *
 * @see Function
 * @see Scriptable
 * @author Matthias Radestock
 */

public class Delegator implements Function {

    protected Scriptable obj = null;

    /**
     * Create a Delegator prototype.
     *
     * This constructor should only be used for creating prototype
     * objects of Delegator.
     *
     * @see org.mozilla.javascript.Delegator#construct
     */
    public Delegator() {
    }

    /**
     * Create a new Delegator that forwards requests to a delegee
     * Scriptable object.
     *
     * @param obj the delegee
     * @see org.mozilla.javascript.Scriptable
     */
    public Delegator(Scriptable obj) {
        this.obj = obj;
    }

    /**
     * Crete new Delegator instance.
     * The default implementation calls this.getClass().newInstance().
     *
     * @see #construct(Context cx, Scriptable scope, Object[] args)
     */
    protected Delegator newInstance()
    {
        try {
            return this.getClass().newInstance();
        } catch (Exception ex) {
            throw Context.throwAsScriptRuntimeEx(ex);
        }
    }

    /**
     * Retrieve the delegee.
     *
     * @return the delegee
     */
    public Scriptable getDelegee() {
        return obj;
    }
    /**
     * Set the delegee.
     *
     * @param obj the delegee
     * @see org.mozilla.javascript.Scriptable
     */
    public void setDelegee(Scriptable obj) {
        this.obj = obj;
    }
    /**
     * @see org.mozilla.javascript.Scriptable#getClassName
     */
    public String getClassName() {
        return obj.getClassName();
    }
    /**
     * @see org.mozilla.javascript.Scriptable#get(String, Scriptable)
     */
    public Object get(String name, Scriptable start) {
        return obj.get(name,start);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#get(int, Scriptable)
     */
    public Object get(int index, Scriptable start) {
        return obj.get(index,start);
        }
    /**
     * @see org.mozilla.javascript.Scriptable#has(String, Scriptable)
     */
    public boolean has(String name, Scriptable start) {
        return obj.has(name,start);
        }
    /**
     * @see org.mozilla.javascript.Scriptable#has(int, Scriptable)
     */
    public boolean has(int index, Scriptable start) {
        return obj.has(index,start);
        }
    /**
     * @see org.mozilla.javascript.Scriptable#put(String, Scriptable, Object)
     */
    public void put(String name, Scriptable start, Object value) {
        obj.put(name,start,value);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#put(int, Scriptable, Object)
     */
    public void put(int index, Scriptable start, Object value) {
        obj.put(index,start,value);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#delete(String)
     */
    public void delete(String name) {
        obj.delete(name);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#delete(int)
     */
    public void delete(int index) {
        obj.delete(index);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#getPrototype
     */
    public Scriptable getPrototype() {
        return obj.getPrototype();
    }
    /**
     * @see org.mozilla.javascript.Scriptable#setPrototype
     */
    public void setPrototype(Scriptable prototype) {
        obj.setPrototype(prototype);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#getParentScope
     */
    public Scriptable getParentScope() {
        return obj.getParentScope();
    }
    /**
     * @see org.mozilla.javascript.Scriptable#setParentScope
     */
    public void setParentScope(Scriptable parent) {
        obj.setParentScope(parent);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#getIds
     */
    public Object[] getIds() {
        return obj.getIds();
    }
    /**
     * Note that this method does not get forwarded to the delegee if
     * the <code>hint</code> parameter is null,
     * <code>ScriptRuntime.ScriptableClass</code> or
     * <code>ScriptRuntime.FunctionClass</code>. Instead the object
     * itself is returned.
     *
     * @param hint the type hint
     * @return the default value
     *
     * @see org.mozilla.javascript.Scriptable#getDefaultValue
     */
    public Object getDefaultValue(Class hint) {
        return (hint == null ||
                hint == ScriptRuntime.ScriptableClass ||
                hint == ScriptRuntime.FunctionClass) ?
            this : obj.getDefaultValue(hint);
    }
    /**
     * @see org.mozilla.javascript.Scriptable#hasInstance
     */
    public boolean hasInstance(Scriptable instance) {
        return obj.hasInstance(instance);
    }
    /**
     * @see org.mozilla.javascript.Function#call
     */
    public Object call(Context cx, Scriptable scope, Scriptable thisObj,
                       Object[] args)
    {
        return ((Function)obj).call(cx,scope,thisObj,args);
    }

    /**
     * Note that if the <code>delegee</code> is <code>null</code>,
     * this method creates a new instance of the Delegator itself
     * rathert than forwarding the call to the
     * <code>delegee</code>. This permits the use of Delegator
     * prototypes.
     *
     * @param cx the current Context for this thread
     * @param scope an enclosing scope of the caller except
     *              when the function is called from a closure.
     * @param args the array of arguments
     * @return the allocated object
     *
     * @see Function#construct(Context, Scriptable, Object[])
     */
    public Scriptable construct(Context cx, Scriptable scope, Object[] args)
    {
        if (obj == null) {
            //this little trick allows us to declare prototype objects for
            //Delegators
            Delegator n = newInstance();
            Scriptable delegee;
            if (args.length == 0) {
                delegee = new NativeObject();
            } else {
                delegee = ScriptRuntime.toObject(cx, scope, args[0]);
            }
            n.setDelegee(delegee);
            return n;
        }
        else {
            return ((Function)obj).construct(cx,scope,args);
        }
    }
}