66da72dd77467e877711853c09a5f59f90beca07
[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 \r
93         return super.get(name, start);\r
94     }\r
95 \r
96     public Object get(int index, Scriptable start) {\r
97         if (0 <= index && index < args.length) {\r
98             NativeFunction f = activation.funObj;\r
99             if (index < f.argCount)\r
100                 return activation.get(f.argNames[index], activation);\r
101             return args[index];\r
102         }\r
103         return super.get(index, start);\r
104     }\r
105 \r
106     public void put(String name, Scriptable start, Object value) {\r
107         if (name.equals("caller")) {\r
108             // Set "hasCaller" to false so that we won't look up a  \r
109             // computed value.\r
110             hasCaller = false;\r
111         }\r
112         super.put(name, start, value);\r
113     }\r
114 \r
115     public void put(int index, Scriptable start, Object value) {\r
116         if (0 <= index && index < args.length) {\r
117             NativeFunction f = activation.funObj;\r
118             if (index < f.argCount)\r
119                 activation.put(f.argNames[index], activation, value);\r
120             else\r
121                 args[index] = value;\r
122             return;\r
123         }\r
124         super.put(index, start, value);\r
125     }\r
126 \r
127     public void delete(String name) {\r
128         if (name.equals("caller"))\r
129             hasCaller = false;\r
130         super.delete(name);\r
131     }\r
132 \r
133     public void delete(int index) {\r
134         if (0 <= index && index < args.length) {\r
135             NativeFunction f = activation.funObj;\r
136             if (index < f.argCount)\r
137                 activation.delete(f.argNames[index]);\r
138             else\r
139                 args[index] = Undefined.instance;\r
140         }\r
141     }\r
142 \r
143     private NativeCall activation;\r
144     private Object[] args;\r
145     private boolean hasCaller;\r
146 }\r