1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\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
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
13 * The Original Code is Rhino code, released
\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
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
38 package org.mozilla.javascript;
\r
40 public class IdFunction extends BaseFunction
\r
42 public static final int FUNCTION_ONLY = 0;
\r
44 public static final int CONSTRUCTOR_ONLY = 1;
\r
46 public static final int FUNCTION_AND_CONSTRUCTOR = 2;
\r
48 public IdFunction(IdFunctionMaster master, String name, int id) {
\r
49 this.functionName = name;
\r
50 this.master = master;
\r
54 public final int functionType() {
\r
55 return functionType;
\r
58 public void setFunctionType(int type) {
\r
59 functionType = type;
\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
73 public Object call(Context cx, Scriptable scope, Scriptable thisObj,
\r
75 throws JavaScriptException
\r
77 if (functionType != CONSTRUCTOR_ONLY) {
\r
78 return master.execMethod(methodId, this, cx, scope, thisObj, args);
\r
81 return Undefined.instance;
\r
85 public Scriptable construct(Context cx, Scriptable scope, Object[] args)
\r
86 throws JavaScriptException
\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
93 postConstruction(result);
\r
97 return Undefined.instance;
\r
101 public String decompile(Context cx, int indent, boolean justbody) {
\r
102 StringBuffer sb = new StringBuffer();
\r
104 sb.append("function ");
\r
105 sb.append(getFunctionName());
\r
106 sb.append("() { ");
\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
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
121 public int getArity() {
\r
122 int arity = master.methodArity(methodId);
\r
124 throw onBadMethodId(master, methodId);
\r
129 public int getLength() { return getArity(); }
\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
141 static RuntimeException onBadMethodId(IdFunctionMaster master, int id) {
\r
142 // It is program error to call id-like methods for unknown or
\r
144 return new RuntimeException("BAD FUNCTION ID="+id+" MASTER="+master);
\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
152 if (newObj.getParentScope() == null) {
\r
153 Scriptable parent = getParentScope();
\r
154 if (newObj != parent) {
\r
155 newObj.setParentScope(parent);
\r
160 protected IdFunctionMaster master;
\r
161 protected int methodId;
\r
163 protected int functionType = FUNCTION_ONLY;
\r