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/
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
12
* The Original Code is Delegator.java, released Sep 27, 2000.
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.
19
* Redfig Ltd (http://www.redfig.com)
20
* LShift Ltd (http://www.lshift.net)
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.
36
package org.mozilla.javascript;
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
45
* A useful application is the implementation of interceptors,
46
* pre/post conditions, debugging.
50
* @author Matthias Radestock
53
public class Delegator implements Function {
55
protected Scriptable obj = null;
58
* Create a Delegator prototype.
60
* This constructor should only be used for creating prototype
61
* objects of Delegator.
63
* @see org.mozilla.javascript.Delegator#construct
69
* Create a new Delegator that forwards requests to a delegee
72
* @param obj the delegee
73
* @see org.mozilla.javascript.Scriptable
75
public Delegator(Scriptable obj) {
80
* Retrieve the delegee.
84
public Scriptable getDelegee() {
90
* @param obj the delegee
91
* @see org.mozilla.javascript.Scriptable
93
public void setDelegee(Scriptable obj) {
97
* @see org.mozilla.javascript.Scriptable#getClassName
99
public String getClassName() {
100
return obj.getClassName();
103
* @see org.mozilla.javascript.Scriptable#get
105
public Object get(String name, Scriptable start) {
106
return obj.get(name,start);
109
* @see org.mozilla.javascript.Scriptable#get
111
public Object get(int index, Scriptable start) {
112
return obj.get(index,start);
115
* @see org.mozilla.javascript.Scriptable#has
117
public boolean has(String name, Scriptable start) {
118
return obj.has(name,start);
121
* @see org.mozilla.javascript.Scriptable#has
123
public boolean has(int index, Scriptable start) {
124
return obj.has(index,start);
127
* @see org.mozilla.javascript.Scriptable#put
129
public void put(String name, Scriptable start, Object value) {
130
obj.put(name,start,value);
133
* @see org.mozilla.javascript.Scriptable#put
135
public void put(int index, Scriptable start, Object value) {
136
obj.put(index,start,value);
139
* @see org.mozilla.javascript.Scriptable#delete
141
public void delete(String name) {
145
* @see org.mozilla.javascript.Scriptable#delete
147
public void delete(int index) {
151
* @see org.mozilla.javascript.Scriptable#getPrototype
153
public Scriptable getPrototype() {
154
return obj.getPrototype();
157
* @see org.mozilla.javascript.Scriptable#setPrototype
159
public void setPrototype(Scriptable prototype) {
160
obj.setPrototype(prototype);
163
* @see org.mozilla.javascript.Scriptable#getParentScope
165
public Scriptable getParentScope() {
166
return obj.getParentScope();
169
* @see org.mozilla.javascript.Scriptable#setParentScope
171
public void setParentScope(Scriptable parent) {
172
obj.setParentScope(parent);
175
* @see org.mozilla.javascript.Scriptable#getIds
177
public Object[] getIds() {
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.
187
* @param hint the type hint
188
* @return the default value
190
* @see org.mozilla.javascript.Scriptable#getDefaultValue
192
public Object getDefaultValue(Class hint) {
193
return (hint == null ||
194
hint == ScriptRuntime.ScriptableClass ||
195
hint == ScriptRuntime.FunctionClass) ?
196
this : obj.getDefaultValue(hint);
199
* @see org.mozilla.javascript.Scriptable#hasInstance
201
public boolean hasInstance(Scriptable instance) {
202
return obj.hasInstance(instance);
205
* @see org.mozilla.javascript.Function#call
207
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
209
throws JavaScriptException {
210
return ((Function)obj).call(cx,scope,thisObj,args);
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
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
228
* @see org.mozilla.javascript.Function#construct
230
public Scriptable construct(Context cx, Scriptable scope, Object[] args)
231
throws JavaScriptException {
233
//this little trick allows us to declare prototype objects for
236
Delegator n = (Delegator)this.getClass().newInstance();
237
n.setDelegee((Scriptable)args[0]);
240
catch (Exception e) {
247
return ((Function)obj).construct(cx,scope,args);
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/
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.
13
* The Original Code is Delegator.java, released Sep 27, 2000.
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.
21
* Matthias Radestock, Redfig Ltd (http://www.redfig.com)
22
* Matthias Radestock, LShift Ltd (http://www.lshift.net)
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.
38
package org.mozilla.javascript;
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
47
* A useful application is the implementation of interceptors,
48
* pre/post conditions, debugging.
52
* @author Matthias Radestock
55
public class Delegator implements Function {
57
protected Scriptable obj = null;
60
* Create a Delegator prototype.
62
* This constructor should only be used for creating prototype
63
* objects of Delegator.
65
* @see org.mozilla.javascript.Delegator#construct
71
* Create a new Delegator that forwards requests to a delegee
74
* @param obj the delegee
75
* @see org.mozilla.javascript.Scriptable
77
public Delegator(Scriptable obj) {
82
* Crete new Delegator instance.
83
* The default implementation calls this.getClass().newInstance().
85
* @see #construct(Context cx, Scriptable scope, Object[] args)
87
protected Delegator newInstance()
90
return (Delegator)this.getClass().newInstance();
91
} catch (Exception ex) {
92
throw Context.throwAsScriptRuntimeEx(ex);
97
* Retrieve the delegee.
101
public Scriptable getDelegee() {
107
* @param obj the delegee
108
* @see org.mozilla.javascript.Scriptable
110
public void setDelegee(Scriptable obj) {
114
* @see org.mozilla.javascript.Scriptable#getClassName
116
public String getClassName() {
117
return obj.getClassName();
120
* @see org.mozilla.javascript.Scriptable#get
122
public Object get(String name, Scriptable start) {
123
return obj.get(name,start);
126
* @see org.mozilla.javascript.Scriptable#get
128
public Object get(int index, Scriptable start) {
129
return obj.get(index,start);
132
* @see org.mozilla.javascript.Scriptable#has
134
public boolean has(String name, Scriptable start) {
135
return obj.has(name,start);
138
* @see org.mozilla.javascript.Scriptable#has
140
public boolean has(int index, Scriptable start) {
141
return obj.has(index,start);
144
* @see org.mozilla.javascript.Scriptable#put
146
public void put(String name, Scriptable start, Object value) {
147
obj.put(name,start,value);
150
* @see org.mozilla.javascript.Scriptable#put
152
public void put(int index, Scriptable start, Object value) {
153
obj.put(index,start,value);
156
* @see org.mozilla.javascript.Scriptable#delete
158
public void delete(String name) {
162
* @see org.mozilla.javascript.Scriptable#delete
164
public void delete(int index) {
168
* @see org.mozilla.javascript.Scriptable#getPrototype
170
public Scriptable getPrototype() {
171
return obj.getPrototype();
174
* @see org.mozilla.javascript.Scriptable#setPrototype
176
public void setPrototype(Scriptable prototype) {
177
obj.setPrototype(prototype);
180
* @see org.mozilla.javascript.Scriptable#getParentScope
182
public Scriptable getParentScope() {
183
return obj.getParentScope();
186
* @see org.mozilla.javascript.Scriptable#setParentScope
188
public void setParentScope(Scriptable parent) {
189
obj.setParentScope(parent);
192
* @see org.mozilla.javascript.Scriptable#getIds
194
public Object[] getIds() {
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.
204
* @param hint the type hint
205
* @return the default value
207
* @see org.mozilla.javascript.Scriptable#getDefaultValue
209
public Object getDefaultValue(Class hint) {
210
return (hint == null ||
211
hint == ScriptRuntime.ScriptableClass ||
212
hint == ScriptRuntime.FunctionClass) ?
213
this : obj.getDefaultValue(hint);
216
* @see org.mozilla.javascript.Scriptable#hasInstance
218
public boolean hasInstance(Scriptable instance) {
219
return obj.hasInstance(instance);
222
* @see org.mozilla.javascript.Function#call
224
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
227
return ((Function)obj).call(cx,scope,thisObj,args);
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
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
243
* @see Function#construct(Context, Scriptable, Object[])
245
public Scriptable construct(Context cx, Scriptable scope, Object[] args)
248
//this little trick allows us to declare prototype objects for
250
Delegator n = newInstance();
252
if (args.length == 0) {
253
delegee = Undefined.instance;
255
delegee = ScriptRuntime.toObject(cx, scope, args[0]);
257
n.setDelegee(delegee);
261
return ((Function)obj).construct(cx,scope,args);