2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / NativeCall.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 activation object.\r
40  *\r
41  * See ECMA 10.1.6\r
42  *\r
43  * @see org.mozilla.javascript.Arguments\r
44  * @author Norris Boyd\r
45  */\r
46 public final class NativeCall extends IdScriptable {\r
47 \r
48     static void init(Context cx, Scriptable scope, boolean sealed) {\r
49         NativeCall obj = new NativeCall();\r
50         obj.prototypeFlag = true;\r
51         obj.addAsPrototype(MAX_PROTOTYPE_ID, cx, scope, sealed);\r
52     }\r
53 \r
54     NativeCall(Context cx, Scriptable scope, NativeFunction funObj, \r
55                Scriptable thisObj, Object[] args)\r
56     {\r
57         this.funObj = funObj;\r
58         this.thisObj = thisObj;\r
59         \r
60         setParentScope(scope);\r
61         // leave prototype null\r
62         \r
63         // save current activation\r
64         this.caller = cx.currentActivation;\r
65         cx.currentActivation = this;\r
66 \r
67         this.originalArgs = (args == null) ? ScriptRuntime.emptyArgs : args;\r
68         \r
69         // initialize values of arguments\r
70         String[] argNames = funObj.argNames;\r
71         if (argNames != null) {\r
72             for (int i=0; i < funObj.argCount; i++) {\r
73                 Object val = i < args.length ? args[i] \r
74                                              : Undefined.instance;\r
75                 super.put(argNames[i], this, val);\r
76             }\r
77         }\r
78         \r
79         // initialize "arguments" property\r
80         super.put("arguments", this, new Arguments(this));\r
81     }\r
82     \r
83     private NativeCall() {\r
84     }\r
85 \r
86     public String getClassName() {\r
87         return "Call";\r
88     }\r
89     \r
90     private static Object jsConstructor(Context cx, Object[] args, \r
91                                         Function ctorObj, boolean inNewExpr)\r
92     {\r
93         if (!inNewExpr) {\r
94             throw Context.reportRuntimeError1("msg.only.from.new", "Call");\r
95         }\r
96         ScriptRuntime.checkDeprecated(cx, "Call");\r
97         NativeCall result = new NativeCall();\r
98         result.setPrototype(getObjectPrototype(ctorObj));\r
99         return result;\r
100     }\r
101     \r
102     NativeCall getActivation(Function f) {\r
103         NativeCall x = this;\r
104         do {\r
105             if (x.funObj == f)\r
106                 return x;\r
107             x = x.caller;\r
108         } while (x != null);\r
109         return null;\r
110     }\r
111         \r
112     public Function getFunctionObject() {\r
113         return funObj;\r
114     }\r
115 \r
116     public Object[] getOriginalArguments() {\r
117         return originalArgs;\r
118     }\r
119     \r
120     public NativeCall getCaller() {\r
121         return caller;\r
122     }\r
123         \r
124     public Scriptable getThisObj() {\r
125         return thisObj;\r
126     }\r
127     \r
128     public int methodArity(int methodId) {\r
129         if (prototypeFlag) {\r
130             if (methodId == Id_constructor) return 1;\r
131         }\r
132         return super.methodArity(methodId);\r
133     }\r
134 \r
135     public Object execMethod\r
136         (int methodId, IdFunction f,\r
137          Context cx, Scriptable scope, Scriptable thisObj, Object[] args)\r
138         throws JavaScriptException\r
139     {\r
140         if (prototypeFlag) {\r
141             if (methodId == Id_constructor) {\r
142                 return jsConstructor(cx, args, f, thisObj == null);\r
143             }\r
144         }\r
145         return super.execMethod(methodId, f, cx, scope, thisObj, args);\r
146     }\r
147 \r
148     protected String getIdName(int id) {\r
149         if (prototypeFlag) {\r
150             if (id == Id_constructor) return "constructor";\r
151         }\r
152         return null;        \r
153     }\r
154     \r
155     protected int mapNameToId(String s) {\r
156         if (!prototypeFlag) { return 0; }\r
157         return s.equals("constructor") ? Id_constructor : 0;\r
158     }\r
159 \r
160     private static final int\r
161         Id_constructor   = 1,\r
162         MAX_PROTOTYPE_ID = 1;\r
163 \r
164     NativeCall caller;\r
165     NativeFunction funObj;\r
166     Scriptable thisObj;\r
167     Object[] originalArgs;\r
168     public int debugPC;\r
169 \r
170     private boolean prototypeFlag;\r
171 }\r