2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / NativeWith.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\r
3  * The contents of this file are subject to the Netscape Public\r
4  * License Version 1.1 (the "License"); you may not use this file\r
5  * except in compliance with the License. You may obtain a copy of\r
6  * the License at http://www.mozilla.org/NPL/\r
7  *\r
8  * Software distributed under the License is distributed on an "AS\r
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
10  * implied. See the License for the specific language governing\r
11  * rights and limitations under the License.\r
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\r
16  * The Initial Developer of the Original Code is Netscape\r
17  * Communications Corporation.  Portions created by Netscape are\r
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Norris Boyd\r
23  *\r
24  * Alternatively, the contents of this file may be used under the\r
25  * terms of the GNU Public License (the "GPL"), in which case the\r
26  * provisions of the GPL are applicable instead of those above.\r
27  * If you wish to allow use of your version of this file only\r
28  * under the terms of the GPL and not to allow others to use your\r
29  * version of this file under the NPL, indicate your decision by\r
30  * deleting the provisions above and replace them with the notice\r
31  * and other provisions required by the GPL.  If you do not delete\r
32  * the provisions above, a recipient may use your version of this\r
33  * file under either the NPL or the GPL.\r
34  */\r
35 \r
36 package org.mozilla.javascript;\r
37 \r
38 import java.lang.reflect.Method;\r
39 \r
40 /**\r
41  * This class implements the object lookup required for the\r
42  * <code>with</code> statement.\r
43  * It simply delegates every action to its prototype except\r
44  * for operations on its parent.\r
45  */\r
46 public class NativeWith implements Scriptable, IdFunctionMaster { \r
47     \r
48     public static void init(Context cx, Scriptable scope, boolean sealed) {\r
49         NativeWith obj = new NativeWith();\r
50         obj.prototypeFlag = true;\r
51 \r
52         IdFunction ctor = new IdFunction(obj, "constructor", Id_constructor);\r
53         ctor.initAsConstructor(scope, obj);\r
54         if (sealed) { ctor.sealObject(); }\r
55         \r
56         obj.setParentScope(ctor);\r
57         obj.setPrototype(ScriptableObject.getObjectPrototype(scope));\r
58         \r
59         ScriptableObject.defineProperty(scope, "With", ctor,\r
60                                         ScriptableObject.DONTENUM);\r
61     }\r
62 \r
63     public NativeWith() {\r
64     }\r
65 \r
66     public NativeWith(Scriptable parent, Scriptable prototype) {\r
67         this.parent = parent;\r
68         this.prototype = prototype;\r
69     }\r
70 \r
71     public String getClassName() {\r
72         return "With";\r
73     }\r
74 \r
75     public boolean has(String id, Scriptable start) {\r
76         if (start == this)\r
77             start = prototype;\r
78         return prototype.has(id, start);\r
79     }\r
80 \r
81     public boolean has(int index, Scriptable start) {\r
82         if (start == this)\r
83             start = prototype;\r
84         return prototype.has(index, start);\r
85     }\r
86 \r
87     public Object get(String id, Scriptable start) {\r
88         if (start == this)\r
89             start = prototype;\r
90         return prototype.get(id, start);\r
91     }\r
92 \r
93     public Object get(int index, Scriptable start) {\r
94         if (start == this)\r
95             start = prototype;\r
96         return prototype.get(index, start);\r
97     }\r
98 \r
99     public void put(String id, Scriptable start, Object value) {\r
100         if (start == this)\r
101             start = prototype;\r
102         prototype.put(id, start, value);\r
103     }\r
104 \r
105     public void put(int index, Scriptable start, Object value) {\r
106         if (start == this)\r
107             start = prototype;\r
108         prototype.put(index, start, value);\r
109     }\r
110 \r
111     public void delete(String id) {\r
112         prototype.delete(id);\r
113     }\r
114 \r
115     public void delete(int index) {\r
116         prototype.delete(index);\r
117     }\r
118 \r
119     public Scriptable getPrototype() {\r
120         return prototype;\r
121     }\r
122 \r
123     public void setPrototype(Scriptable prototype) {\r
124         this.prototype = prototype;\r
125     }\r
126 \r
127     public Scriptable getParentScope() {\r
128         return parent;\r
129     }\r
130 \r
131     public void setParentScope(Scriptable parent) {\r
132         this.parent = parent;\r
133     }\r
134 \r
135     public Object[] getIds() {\r
136         return prototype.getIds();\r
137     }\r
138 \r
139     public Object getDefaultValue(Class typeHint) {\r
140         return prototype.getDefaultValue(typeHint);\r
141     }\r
142 \r
143     public boolean hasInstance(Scriptable value) {\r
144         return prototype.hasInstance(value);\r
145     }\r
146 \r
147     public Object execMethod(int methodId, IdFunction function, Context cx,\r
148                              Scriptable scope, Scriptable thisObj, \r
149                              Object[] args)\r
150         throws JavaScriptException\r
151     {\r
152         if (prototypeFlag) {\r
153             if (methodId == Id_constructor) {\r
154                 throw Context.reportRuntimeError1\r
155                     ("msg.cant.call.indirect", "With");\r
156             }\r
157         }\r
158         throw IdFunction.onBadMethodId(this, methodId);\r
159     }\r
160     \r
161     public int methodArity(int methodId) {\r
162         if (prototypeFlag) {\r
163             if (methodId == Id_constructor) { return 0; }\r
164         }\r
165         return -1;\r
166     }\r
167 \r
168     public static Object newWithSpecial(Context cx, Object[] args, \r
169                                         Function ctorObj, boolean inNewExpr)\r
170     {\r
171         if (!inNewExpr) {\r
172             throw Context.reportRuntimeError1("msg.only.from.new", "With");\r
173         }\r
174         \r
175         ScriptRuntime.checkDeprecated(cx, "With");\r
176         \r
177         Scriptable scope = ScriptableObject.getTopLevelScope(ctorObj);\r
178         NativeWith thisObj = new NativeWith();\r
179         thisObj.setPrototype(args.length == 0\r
180                              ? ScriptableObject.getClassPrototype(scope,\r
181                                                                   "Object")\r
182                              : ScriptRuntime.toObject(scope, args[0]));\r
183         thisObj.setParentScope(scope);\r
184         return thisObj;\r
185     }\r
186     \r
187     private static final int    \r
188         Id_constructor = 1;\r
189 \r
190     private Scriptable prototype;\r
191     private Scriptable parent;\r
192     private Scriptable constructor;\r
193 \r
194     private boolean prototypeFlag;\r
195 }\r