2002/10/16 06:22:38
[org.ibex.core.git] / src / org / mozilla / javascript / Arguments.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 /**\r
39  * This class implements the "arguments" object.\r
40  *\r
41  * See ECMA 10.1.8\r
42  *\r
43  * @see org.mozilla.javascript.NativeCall\r
44  * @author Norris Boyd\r
45  */\r
46 class Arguments extends ScriptableObject {\r
47 \r
48     public Arguments(NativeCall activation) {\r
49         this.activation = activation;\r
50 \r
51         Scriptable parent = activation.getParentScope();\r
52         setParentScope(parent);\r
53         setPrototype(ScriptableObject.getObjectPrototype(parent));\r
54 \r
55         args = activation.getOriginalArguments();\r
56         int length = args.length;\r
57         Object callee = activation.funObj;\r
58 \r
59         defineProperty("length", new Integer(length),\r
60                        ScriptableObject.DONTENUM);\r
61         defineProperty("callee", callee, ScriptableObject.DONTENUM);\r
62 \r
63         hasCaller = (activation.funObj.version <= Context.VERSION_1_3 &&\r
64                      activation.funObj.version != Context.VERSION_DEFAULT);\r
65     }\r
66 \r
67     public String getClassName() {\r
68         return "Arguments";\r
69     }\r
70 \r
71     public boolean has(String name, Scriptable start) {\r
72         return (hasCaller && name.equals("caller")) || super.has(name, start);\r
73     }\r
74 \r
75     public boolean has(int index, Scriptable start) {\r
76         Object[] args = activation.getOriginalArguments();\r
77         return (0 <= index && index < args.length) || super.has(index, start);\r
78     }\r
79 \r
80     public Object get(String name, Scriptable start) {\r
81         if (hasCaller && name.equals("caller")) {\r
82             NativeCall caller = activation.caller;\r
83             if (caller == null || caller.originalArgs == null) return null;\r
84             return caller.get("arguments", caller);\r
85 \r
86         } else if (name.equals("cascade")) {\r
87             return org.xwt.Trap.cascadeFunction;\r
88 \r
89         } else if (name.equals("trapee")) {\r
90             return org.xwt.Trap.currentTrapee();\r
91 \r
92         } else if (name.equals("trapname")) {\r
93             return org.xwt.Trap.currentTrapname();\r
94 \r
95         }\r
96 \r
97         return super.get(name, start);\r
98     }\r
99 \r
100     public Object get(int index, Scriptable start) {\r
101         if (0 <= index && index < args.length) {\r
102             NativeFunction f = activation.funObj;\r
103             if (index < f.argCount)\r
104                 return activation.get(f.argNames[index], activation);\r
105             return args[index];\r
106         }\r
107         return super.get(index, start);\r
108     }\r
109 \r
110     public void put(String name, Scriptable start, Object value) {\r
111         if (name.equals("caller")) {\r
112             // Set "hasCaller" to false so that we won't look up a  \r
113             // computed value.\r
114             hasCaller = false;\r
115         }\r
116         super.put(name, start, value);\r
117     }\r
118 \r
119     public void put(int index, Scriptable start, Object value) {\r
120         if (0 <= index && index < args.length) {\r
121             NativeFunction f = activation.funObj;\r
122             if (index < f.argCount)\r
123                 activation.put(f.argNames[index], activation, value);\r
124             else\r
125                 args[index] = value;\r
126             return;\r
127         }\r
128         super.put(index, start, value);\r
129     }\r
130 \r
131     public void delete(String name) {\r
132         if (name.equals("caller"))\r
133             hasCaller = false;\r
134         super.delete(name);\r
135     }\r
136 \r
137     public void delete(int index) {\r
138         if (0 <= index && index < args.length) {\r
139             NativeFunction f = activation.funObj;\r
140             if (index < f.argCount)\r
141                 activation.delete(f.argNames[index]);\r
142             else\r
143                 args[index] = Undefined.instance;\r
144         }\r
145     }\r
146 \r
147     private NativeCall activation;\r
148     private Object[] args;\r
149     private boolean hasCaller;\r
150 }\r