2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / IdFunction.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  * Igor Bukanov\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 // API class\r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 public class IdFunction extends BaseFunction\r
41 {\r
42     public static final int FUNCTION_ONLY = 0;\r
43 \r
44     public static final int CONSTRUCTOR_ONLY = 1;\r
45 \r
46     public static final int FUNCTION_AND_CONSTRUCTOR = 2;\r
47 \r
48     public IdFunction(IdFunctionMaster master, String name, int id) {\r
49         this.functionName = name;\r
50         this.master = master;\r
51         this.methodId = id;\r
52     }\r
53     \r
54     public final int functionType() {\r
55         return functionType;\r
56     }\r
57     \r
58     public void setFunctionType(int type) {\r
59         functionType = type;\r
60     }\r
61     \r
62     public Scriptable getPrototype() {\r
63         // Lazy initialization of prototype: for native functions this\r
64         // may not be called at all\r
65         Scriptable proto = super.getPrototype(); \r
66         if (proto == null) {\r
67             proto = getFunctionPrototype(getParentScope());\r
68             setPrototype(proto);\r
69         }\r
70         return proto;\r
71     }\r
72 \r
73     public Object call(Context cx, Scriptable scope, Scriptable thisObj, \r
74                        Object[] args)\r
75         throws JavaScriptException\r
76     {\r
77         if (functionType != CONSTRUCTOR_ONLY) {\r
78             return master.execMethod(methodId, this, cx, scope, thisObj, args);\r
79         }\r
80         else {\r
81             return Undefined.instance;\r
82         }\r
83     }\r
84 \r
85     public Scriptable construct(Context cx, Scriptable scope, Object[] args)\r
86         throws JavaScriptException\r
87     {\r
88         if (functionType != FUNCTION_ONLY) {\r
89             // It is program error not to return Scriptable from constructor\r
90             Scriptable result = (Scriptable)master.execMethod(methodId, this,\r
91                                                               cx, scope, \r
92                                                               null, args);\r
93             postConstruction(result);\r
94             return result;\r
95         }\r
96         else {\r
97             return Undefined.instance;\r
98         }\r
99     }\r
100 \r
101     public String decompile(Context cx, int indent, boolean justbody) {\r
102         StringBuffer sb = new StringBuffer();\r
103         if (!justbody) {\r
104             sb.append("function ");\r
105             sb.append(getFunctionName());\r
106             sb.append("() { ");\r
107         }\r
108         sb.append("[native code for ");\r
109         if (master instanceof Scriptable) {\r
110             Scriptable smaster = (Scriptable)master;\r
111             sb.append(smaster.getClassName());\r
112             sb.append('.');\r
113         }\r
114         sb.append(getFunctionName());\r
115         sb.append(", arity=");\r
116         sb.append(getArity());\r
117         sb.append(justbody ? "]\n" : "] }\n");\r
118         return sb.toString();\r
119     }\r
120     \r
121     public int getArity() {\r
122         int arity = master.methodArity(methodId);\r
123         if (arity < 0) { \r
124             throw onBadMethodId(master, methodId);\r
125         }\r
126         return arity;\r
127     }\r
128     \r
129     public int getLength() { return getArity(); }\r
130 \r
131     /** Prepare to be used as constructor .\r
132      ** @param scope constructor scope\r
133      ** @param prototype DontEnum, DontDelete, ReadOnly prototype property \r
134      ** of the constructor */\r
135     public void initAsConstructor(Scriptable scope, Scriptable prototype) {\r
136         setFunctionType(FUNCTION_AND_CONSTRUCTOR);\r
137         setParentScope(scope);\r
138         setImmunePrototypeProperty(prototype);\r
139     }\r
140     \r
141     static RuntimeException onBadMethodId(IdFunctionMaster master, int id) {\r
142         // It is program error to call id-like methods for unknown or \r
143         // non-function id\r
144         return new RuntimeException("BAD FUNCTION ID="+id+" MASTER="+master);\r
145     }\r
146 \r
147     // Copied from NativeFunction.construct\r
148     private void postConstruction(Scriptable newObj) {\r
149         if (newObj.getPrototype() == null) {\r
150             newObj.setPrototype(getClassPrototype());\r
151         }\r
152         if (newObj.getParentScope() == null) {\r
153             Scriptable parent = getParentScope();\r
154             if (newObj != parent) {\r
155                 newObj.setParentScope(parent);\r
156             }\r
157         }\r
158     }\r
159 \r
160     protected IdFunctionMaster master;\r
161     protected int methodId;\r
162 \r
163     protected int functionType = FUNCTION_ONLY;\r
164 }\r