KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > ScoperFunctionObject


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import org.mozilla.javascript.Context;
41 import org.mozilla.javascript.Function;
42 import org.mozilla.javascript.Scriptable;
43
44 /**
45  * Object f in "f = new Function("...");" has not the current execution scope as its
46  * parent scope and can't find objects in this scope when it is runned.
47  * This class wrapps the native object for "Function" and fixes the problem by
48  * seting the parent scope of the object created in {@link #construct(Context, Scriptable, Object[])}.
49  * <p>
50  * This class should disappear when the bug is fixed by Rhino:
51  * <a HREF="https://bugzilla.mozilla.org/show_bug.cgi?id=277462">bug 277462</a>
52  * </p>
53  * <p>
54  * The problem is tested by
55  * {@link com.gargoylesoftware.htmlunit.javascript.JavaScriptEngineTest#testScopeOfNestedNewFunction()}
56  * </p>
57  * @version $Revision: 100 $
58  * @author Marc Guillemot
59  * @author Mike Bowler
60  */

61 class ScoperFunctionObject implements Function {
62     private final Function wrapped_;
63     private final Scriptable scope_;
64
65     /**
66      * Create an instance.
67      * @param wrapped The wrapped function.
68      * @param scope The scope.
69      */

70     public ScoperFunctionObject(Function wrapped, final Scriptable scope) {
71         wrapped_ = wrapped;
72         scope_ = scope;
73     }
74     
75     /** @see Function#call(Context,Scriptable,Scriptable,Object[]) */
76     public Object JavaDoc call(
77             final Context cx, final Scriptable scope, final Scriptable thisObj,
78             final Object JavaDoc[] args) {
79         return wrapped_.call(cx, scope, thisObj, args);
80     }
81
82     /** @see Function#construct(Context,Scriptable,Object[]) */
83     public Scriptable construct(final Context cx, final Scriptable scope, final Object JavaDoc[] args) {
84         final Scriptable o = wrapped_.construct(cx, scope, args);
85         o.setParentScope(scope_);
86
87         return o;
88     }
89
90     /** @see Function#delete(int) */
91     public void delete(final int index) {
92         wrapped_.delete(index);
93     }
94     
95     /** @see Function#delete(String) */
96     public void delete(final String JavaDoc name) {
97         wrapped_.delete(name);
98     }
99     
100     /** @see Object#equals(Object) */
101     public boolean equals(final Object JavaDoc obj) {
102         return wrapped_.equals(obj);
103     }
104     
105     /** @see Function#get(int,Scriptable) */
106     public Object JavaDoc get(final int index, final Scriptable start) {
107         return wrapped_.get(index, start);
108     }
109     
110     /** @see Function#get(String,Scriptable) */
111     public Object JavaDoc get(final String JavaDoc name, final Scriptable start) {
112         return wrapped_.get(name, start);
113     }
114     
115     /** @see Function#getClassName() */
116     public String JavaDoc getClassName() {
117         return wrapped_.getClassName();
118     }
119     
120     /** @see Function#getDefaultValue(Class) */
121     public Object JavaDoc getDefaultValue(final Class JavaDoc hint) {
122         return wrapped_.getDefaultValue(hint);
123     }
124
125     /** @see Function#getIds() */
126     public Object JavaDoc[] getIds() {
127         return wrapped_.getIds();
128     }
129
130     /** @see Function#getParentScope() */
131     public Scriptable getParentScope() {
132         return wrapped_.getParentScope();
133     }
134
135     /** @see Function#getPrototype() */
136     public Scriptable getPrototype() {
137         return wrapped_.getPrototype();
138     }
139
140     /** @see Function#has(int,Scriptable) */
141     public boolean has(final int index, final Scriptable start) {
142         return wrapped_.has(index, start);
143     }
144
145     /** @see Function#has(String,Scriptable) */
146     public boolean has(final String JavaDoc name, final Scriptable start) {
147         return wrapped_.has(name, start);
148     }
149
150     /** @see Object#hashCode() */
151     public int hashCode() {
152         return wrapped_.hashCode();
153     }
154
155     /** @see Function#hasInstance(Scriptable) */
156     public boolean hasInstance(final Scriptable instance) {
157         return wrapped_.hasInstance(instance);
158     }
159
160     /** @see Function#put(int,Scriptable,Object) */
161     public void put(final int index, final Scriptable start, final Object JavaDoc value) {
162         wrapped_.put(index, start, value);
163     }
164
165     /** @see Function#put(String,Scriptable,Object) */
166     public void put(final String JavaDoc name, final Scriptable start, final Object JavaDoc value) {
167         wrapped_.put(name, start, value);
168     }
169
170     /** @see Function#setParentScope(Scriptable) */
171     public void setParentScope(final Scriptable parent) {
172         wrapped_.setParentScope(parent);
173     }
174
175     /** @see Function#setPrototype(Scriptable) */
176     public void setPrototype(final Scriptable prototype) {
177         wrapped_.setPrototype(prototype);
178     }
179
180     /** @see Object#toString() */
181     public String JavaDoc toString() {
182         return wrapped_.toString();
183     }
184 }
185
Popular Tags