~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to src/org/mozilla/javascript/Delegator.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 
 * The contents of this file are subject to the Mozilla Public License
3
 
 * Version 1.1 (the "License"); you may not use this file except in
4
 
 * compliance with the License. You may obtain a copy of the License at
5
 
 * http://www.mozilla.org/MPL/
6
 
 *
7
 
 * Software distributed under the License is distributed on an "AS IS"
8
 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9
 
 * License for the specific language governing rights and limitations
10
 
 * under the License.
11
 
 *
12
 
 * The Original Code is Delegator.java, released Sep 27, 2000.
13
 
 *
14
 
 * The Initial Developer of the Original Code is Matthias Radestock.
15
 
 * <matthias@sorted.org>. Portions created by Matthias Radestock are
16
 
 * Copyright (C) 2000 Matthias Radestock. All Rights Reserved.
17
 
 *
18
 
 * Contributor(s):
19
 
 *      Redfig Ltd (http://www.redfig.com)
20
 
 *      LShift Ltd (http://www.lshift.net)
21
 
 *
22
 
 * Alternatively, the contents of this file may be used under the terms
23
 
 * of the GNU Public License (the  "GPL License"), in which case the
24
 
 * provisions of the GPL License are applicable instead of those
25
 
 * above.  If you wish to allow use of your version of this file only
26
 
 * under the terms of the GPL License and not to allow others to use
27
 
 * your version of this file under the MPL, indicate your decision by
28
 
 * deleting  the provisions above and replace  them with the notice and
29
 
 * other provisions required by the GPL License.  If you do not delete
30
 
 * the provisions above, a recipient may use your version of this file
31
 
 * under either the MPL or the GPL License.
32
 
 */
33
 
 
34
 
// API class
35
 
 
36
 
package org.mozilla.javascript;
37
 
 
38
 
/**
39
 
 * This is a helper class for implementing wrappers around Scriptable
40
 
 * objects. It implements the Function interface and delegates all
41
 
 * invocations to a delegee Scriptable object. The normal use of this
42
 
 * class involves creating a sub-class and overriding one or more of
43
 
 * the methods.
44
 
 *
45
 
 * A useful application is the implementation of interceptors,
46
 
 * pre/post conditions, debugging.
47
 
 *
48
 
 * @see Function
49
 
 * @see Scriptable
50
 
 * @author Matthias Radestock
51
 
 */
52
 
 
53
 
public class Delegator implements Function {
54
 
 
55
 
    protected Scriptable obj = null;
56
 
 
57
 
    /**
58
 
     * Create a Delegator prototype.
59
 
     *
60
 
     * This constructor should only be used for creating prototype
61
 
     * objects of Delegator.
62
 
     *
63
 
     * @see org.mozilla.javascript.Delegator#construct
64
 
     */
65
 
    public Delegator() {
66
 
    }
67
 
 
68
 
    /**
69
 
     * Create a new Delegator that forwards requests to a delegee
70
 
     * Scriptable object.
71
 
     *
72
 
     * @param obj the delegee
73
 
     * @see org.mozilla.javascript.Scriptable
74
 
     */
75
 
    public Delegator(Scriptable obj) {
76
 
        this.obj = obj;
77
 
    }
78
 
 
79
 
    /**
80
 
     * Retrieve the delegee.
81
 
     *
82
 
     * @return the delegee
83
 
     */
84
 
    public Scriptable getDelegee() {
85
 
        return obj;
86
 
    }
87
 
    /**
88
 
     * Set the delegee.
89
 
     *
90
 
     * @param obj the delegee
91
 
     * @see org.mozilla.javascript.Scriptable
92
 
     */
93
 
    public void setDelegee(Scriptable obj) {
94
 
        this.obj = obj;
95
 
    }
96
 
    /**
97
 
     * @see org.mozilla.javascript.Scriptable#getClassName
98
 
     */
99
 
    public String getClassName() {
100
 
        return obj.getClassName();
101
 
    }
102
 
    /**
103
 
     * @see org.mozilla.javascript.Scriptable#get
104
 
     */
105
 
    public Object get(String name, Scriptable start) {
106
 
        return obj.get(name,start);
107
 
    }
108
 
    /**
109
 
     * @see org.mozilla.javascript.Scriptable#get
110
 
     */
111
 
    public Object get(int index, Scriptable start) {
112
 
        return obj.get(index,start);
113
 
        }
114
 
    /**
115
 
     * @see org.mozilla.javascript.Scriptable#has
116
 
     */
117
 
    public boolean has(String name, Scriptable start) {
118
 
        return obj.has(name,start);
119
 
        }
120
 
    /**
121
 
     * @see org.mozilla.javascript.Scriptable#has
122
 
     */
123
 
    public boolean has(int index, Scriptable start) {
124
 
        return obj.has(index,start);
125
 
        }
126
 
    /**
127
 
     * @see org.mozilla.javascript.Scriptable#put
128
 
     */
129
 
    public void put(String name, Scriptable start, Object value) {
130
 
        obj.put(name,start,value);
131
 
    }
132
 
    /**
133
 
     * @see org.mozilla.javascript.Scriptable#put
134
 
     */
135
 
    public void put(int index, Scriptable start, Object value) {
136
 
        obj.put(index,start,value);
137
 
    }
138
 
    /**
139
 
     * @see org.mozilla.javascript.Scriptable#delete
140
 
     */
141
 
    public void delete(String name) {
142
 
        obj.delete(name);
143
 
    }
144
 
    /**
145
 
     * @see org.mozilla.javascript.Scriptable#delete
146
 
     */
147
 
    public void delete(int index) {
148
 
        obj.delete(index);
149
 
    }
150
 
    /**
151
 
     * @see org.mozilla.javascript.Scriptable#getPrototype
152
 
     */
153
 
    public Scriptable getPrototype() {
154
 
        return obj.getPrototype();
155
 
    }
156
 
    /**
157
 
     * @see org.mozilla.javascript.Scriptable#setPrototype
158
 
     */
159
 
    public void setPrototype(Scriptable prototype) {
160
 
        obj.setPrototype(prototype);
161
 
    }
162
 
    /**
163
 
     * @see org.mozilla.javascript.Scriptable#getParentScope
164
 
     */
165
 
    public Scriptable getParentScope() {
166
 
        return obj.getParentScope();
167
 
    }
168
 
    /**
169
 
     * @see org.mozilla.javascript.Scriptable#setParentScope
170
 
     */
171
 
    public void setParentScope(Scriptable parent) {
172
 
        obj.setParentScope(parent);
173
 
    }
174
 
    /**
175
 
     * @see org.mozilla.javascript.Scriptable#getIds
176
 
     */
177
 
    public Object[] getIds() {
178
 
        return obj.getIds();
179
 
    }
180
 
    /**
181
 
     * Note that this method does not get forwarded to the delegee if
182
 
     * the <code>hint</code> parameter is null,
183
 
     * <code>ScriptRuntime.ScriptableClass</code> or
184
 
     * <code>ScriptRuntime.FunctionClass</code>. Instead the object
185
 
     * itself is returned.
186
 
     *
187
 
     * @param hint the type hint
188
 
     * @return the default value
189
 
     *
190
 
     * @see org.mozilla.javascript.Scriptable#getDefaultValue
191
 
     */
192
 
    public Object getDefaultValue(Class hint) {
193
 
        return (hint == null ||
194
 
                hint == ScriptRuntime.ScriptableClass ||
195
 
                hint == ScriptRuntime.FunctionClass) ?
196
 
            this : obj.getDefaultValue(hint);
197
 
    }
198
 
    /**
199
 
     * @see org.mozilla.javascript.Scriptable#hasInstance
200
 
     */
201
 
    public boolean hasInstance(Scriptable instance) {
202
 
        return obj.hasInstance(instance);
203
 
    }
204
 
    /**
205
 
     * @see org.mozilla.javascript.Function#call
206
 
     */
207
 
    public Object call(Context cx, Scriptable scope, Scriptable thisObj,
208
 
                       Object[] args)
209
 
        throws JavaScriptException {
210
 
        return ((Function)obj).call(cx,scope,thisObj,args);
211
 
    }
212
 
 
213
 
    /**
214
 
     * Note that if the <code>delegee</code> is <code>null</code>,
215
 
     * this method creates a new instance of the Delegator itself
216
 
     * rathert than forwarding the call to the
217
 
     * <code>delegee</code>. This permits the use of Delegator
218
 
     * prototypes.
219
 
     *
220
 
     * @param cx the current Context for this thread
221
 
     * @param scope an enclosing scope of the caller except
222
 
     *              when the function is called from a closure.
223
 
     * @param args the array of arguments
224
 
     * @return the allocated object
225
 
     * @exception JavaScriptException if an uncaught exception
226
 
     *            occurred while executing the constructor
227
 
     *
228
 
     * @see org.mozilla.javascript.Function#construct
229
 
     */
230
 
    public Scriptable construct(Context cx, Scriptable scope, Object[] args)
231
 
        throws JavaScriptException {
232
 
        if (obj == null) {
233
 
            //this little trick allows us to declare prototype objects for
234
 
            //Delegators
235
 
            try {
236
 
                Delegator n = (Delegator)this.getClass().newInstance();
237
 
                n.setDelegee((Scriptable)args[0]);
238
 
                return n;
239
 
            }
240
 
            catch (Exception e) {
241
 
                e.printStackTrace();
242
 
                System.exit(0);
243
 
            }
244
 
            return null;
245
 
        }
246
 
        else {
247
 
            return ((Function)obj).construct(cx,scope,args);
248
 
        }
249
 
    }
250
 
}
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Netscape Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is Delegator.java, released Sep 27, 2000.
 
14
 *
 
15
 * The Initial Developer of the Original Code is Matthias Radestock.
 
16
 * <matthias@sorted.org>. Portions created by Matthias Radestock are
 
17
 * Copyright (C) 2000 Matthias Radestock. All Rights Reserved.
 
18
 *
 
19
 * Contributor(s):
 
20
 *
 
21
 * Matthias Radestock, Redfig Ltd (http://www.redfig.com)
 
22
 * Matthias Radestock, LShift Ltd (http://www.lshift.net)
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the
 
25
 * terms of the GNU Public License (the "GPL"), in which case the
 
26
 * provisions of the GPL are applicable instead of those above.
 
27
 * If you wish to allow use of your version of this file only
 
28
 * under the terms of the GPL and not to allow others to use your
 
29
 * version of this file under the NPL, indicate your decision by
 
30
 * deleting the provisions above and replace them with the notice
 
31
 * and other provisions required by the GPL.  If you do not delete
 
32
 * the provisions above, a recipient may use your version of this
 
33
 * file under either the NPL or the GPL.
 
34
 */
 
35
 
 
36
// API class
 
37
 
 
38
package org.mozilla.javascript;
 
39
 
 
40
/**
 
41
 * This is a helper class for implementing wrappers around Scriptable
 
42
 * objects. It implements the Function interface and delegates all
 
43
 * invocations to a delegee Scriptable object. The normal use of this
 
44
 * class involves creating a sub-class and overriding one or more of
 
45
 * the methods.
 
46
 *
 
47
 * A useful application is the implementation of interceptors,
 
48
 * pre/post conditions, debugging.
 
49
 *
 
50
 * @see Function
 
51
 * @see Scriptable
 
52
 * @author Matthias Radestock
 
53
 */
 
54
 
 
55
public class Delegator implements Function {
 
56
 
 
57
    protected Scriptable obj = null;
 
58
 
 
59
    /**
 
60
     * Create a Delegator prototype.
 
61
     *
 
62
     * This constructor should only be used for creating prototype
 
63
     * objects of Delegator.
 
64
     *
 
65
     * @see org.mozilla.javascript.Delegator#construct
 
66
     */
 
67
    public Delegator() {
 
68
    }
 
69
 
 
70
    /**
 
71
     * Create a new Delegator that forwards requests to a delegee
 
72
     * Scriptable object.
 
73
     *
 
74
     * @param obj the delegee
 
75
     * @see org.mozilla.javascript.Scriptable
 
76
     */
 
77
    public Delegator(Scriptable obj) {
 
78
        this.obj = obj;
 
79
    }
 
80
 
 
81
    /**
 
82
     * Crete new Delegator instance.
 
83
     * The default implementation calls this.getClass().newInstance().
 
84
     *
 
85
     * @see #construct(Context cx, Scriptable scope, Object[] args)
 
86
     */
 
87
    protected Delegator newInstance()
 
88
    {
 
89
        try {
 
90
            return (Delegator)this.getClass().newInstance();
 
91
        } catch (Exception ex) {
 
92
            throw Context.throwAsScriptRuntimeEx(ex);
 
93
        }
 
94
    }
 
95
 
 
96
    /**
 
97
     * Retrieve the delegee.
 
98
     *
 
99
     * @return the delegee
 
100
     */
 
101
    public Scriptable getDelegee() {
 
102
        return obj;
 
103
    }
 
104
    /**
 
105
     * Set the delegee.
 
106
     *
 
107
     * @param obj the delegee
 
108
     * @see org.mozilla.javascript.Scriptable
 
109
     */
 
110
    public void setDelegee(Scriptable obj) {
 
111
        this.obj = obj;
 
112
    }
 
113
    /**
 
114
     * @see org.mozilla.javascript.Scriptable#getClassName
 
115
     */
 
116
    public String getClassName() {
 
117
        return obj.getClassName();
 
118
    }
 
119
    /**
 
120
     * @see org.mozilla.javascript.Scriptable#get
 
121
     */
 
122
    public Object get(String name, Scriptable start) {
 
123
        return obj.get(name,start);
 
124
    }
 
125
    /**
 
126
     * @see org.mozilla.javascript.Scriptable#get
 
127
     */
 
128
    public Object get(int index, Scriptable start) {
 
129
        return obj.get(index,start);
 
130
        }
 
131
    /**
 
132
     * @see org.mozilla.javascript.Scriptable#has
 
133
     */
 
134
    public boolean has(String name, Scriptable start) {
 
135
        return obj.has(name,start);
 
136
        }
 
137
    /**
 
138
     * @see org.mozilla.javascript.Scriptable#has
 
139
     */
 
140
    public boolean has(int index, Scriptable start) {
 
141
        return obj.has(index,start);
 
142
        }
 
143
    /**
 
144
     * @see org.mozilla.javascript.Scriptable#put
 
145
     */
 
146
    public void put(String name, Scriptable start, Object value) {
 
147
        obj.put(name,start,value);
 
148
    }
 
149
    /**
 
150
     * @see org.mozilla.javascript.Scriptable#put
 
151
     */
 
152
    public void put(int index, Scriptable start, Object value) {
 
153
        obj.put(index,start,value);
 
154
    }
 
155
    /**
 
156
     * @see org.mozilla.javascript.Scriptable#delete
 
157
     */
 
158
    public void delete(String name) {
 
159
        obj.delete(name);
 
160
    }
 
161
    /**
 
162
     * @see org.mozilla.javascript.Scriptable#delete
 
163
     */
 
164
    public void delete(int index) {
 
165
        obj.delete(index);
 
166
    }
 
167
    /**
 
168
     * @see org.mozilla.javascript.Scriptable#getPrototype
 
169
     */
 
170
    public Scriptable getPrototype() {
 
171
        return obj.getPrototype();
 
172
    }
 
173
    /**
 
174
     * @see org.mozilla.javascript.Scriptable#setPrototype
 
175
     */
 
176
    public void setPrototype(Scriptable prototype) {
 
177
        obj.setPrototype(prototype);
 
178
    }
 
179
    /**
 
180
     * @see org.mozilla.javascript.Scriptable#getParentScope
 
181
     */
 
182
    public Scriptable getParentScope() {
 
183
        return obj.getParentScope();
 
184
    }
 
185
    /**
 
186
     * @see org.mozilla.javascript.Scriptable#setParentScope
 
187
     */
 
188
    public void setParentScope(Scriptable parent) {
 
189
        obj.setParentScope(parent);
 
190
    }
 
191
    /**
 
192
     * @see org.mozilla.javascript.Scriptable#getIds
 
193
     */
 
194
    public Object[] getIds() {
 
195
        return obj.getIds();
 
196
    }
 
197
    /**
 
198
     * Note that this method does not get forwarded to the delegee if
 
199
     * the <code>hint</code> parameter is null,
 
200
     * <code>ScriptRuntime.ScriptableClass</code> or
 
201
     * <code>ScriptRuntime.FunctionClass</code>. Instead the object
 
202
     * itself is returned.
 
203
     *
 
204
     * @param hint the type hint
 
205
     * @return the default value
 
206
     *
 
207
     * @see org.mozilla.javascript.Scriptable#getDefaultValue
 
208
     */
 
209
    public Object getDefaultValue(Class hint) {
 
210
        return (hint == null ||
 
211
                hint == ScriptRuntime.ScriptableClass ||
 
212
                hint == ScriptRuntime.FunctionClass) ?
 
213
            this : obj.getDefaultValue(hint);
 
214
    }
 
215
    /**
 
216
     * @see org.mozilla.javascript.Scriptable#hasInstance
 
217
     */
 
218
    public boolean hasInstance(Scriptable instance) {
 
219
        return obj.hasInstance(instance);
 
220
    }
 
221
    /**
 
222
     * @see org.mozilla.javascript.Function#call
 
223
     */
 
224
    public Object call(Context cx, Scriptable scope, Scriptable thisObj,
 
225
                       Object[] args)
 
226
    {
 
227
        return ((Function)obj).call(cx,scope,thisObj,args);
 
228
    }
 
229
 
 
230
    /**
 
231
     * Note that if the <code>delegee</code> is <code>null</code>,
 
232
     * this method creates a new instance of the Delegator itself
 
233
     * rathert than forwarding the call to the
 
234
     * <code>delegee</code>. This permits the use of Delegator
 
235
     * prototypes.
 
236
     *
 
237
     * @param cx the current Context for this thread
 
238
     * @param scope an enclosing scope of the caller except
 
239
     *              when the function is called from a closure.
 
240
     * @param args the array of arguments
 
241
     * @return the allocated object
 
242
     *
 
243
     * @see Function#construct(Context, Scriptable, Object[])
 
244
     */
 
245
    public Scriptable construct(Context cx, Scriptable scope, Object[] args)
 
246
    {
 
247
        if (obj == null) {
 
248
            //this little trick allows us to declare prototype objects for
 
249
            //Delegators
 
250
            Delegator n = newInstance();
 
251
            Scriptable delegee;
 
252
            if (args.length == 0) {
 
253
                delegee = Undefined.instance;
 
254
            } else {
 
255
                delegee = ScriptRuntime.toObject(cx, scope, args[0]);
 
256
            }
 
257
            n.setDelegee(delegee);
 
258
            return n;
 
259
        }
 
260
        else {
 
261
            return ((Function)obj).construct(cx,scope,args);
 
262
        }
 
263
    }
 
264
}