001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: *
027: * Alternatively, the contents of this file may be used under the terms of
028: * the GNU General Public License Version 2 or later (the "GPL"), in which
029: * case the provisions of the GPL are applicable instead of those above. If
030: * you wish to allow use of your version of this file only under the terms of
031: * the GPL and not to allow others to use your version of this file under the
032: * MPL, indicate your decision by deleting the provisions above and replacing
033: * them with the notice and other provisions required by the GPL. If you do
034: * not delete the provisions above, a recipient may use your version of this
035: * file under either the MPL or the GPL.
036: *
037: * ***** END LICENSE BLOCK ***** */
038:
039: // API class
040: package org.mozilla.javascript;
041:
042: public class IdFunctionObject extends BaseFunction {
043:
044: static final long serialVersionUID = -5332312783643935019L;
045:
046: public IdFunctionObject(IdFunctionCall idcall, Object tag, int id,
047: int arity) {
048: if (arity < 0)
049: throw new IllegalArgumentException();
050:
051: this .idcall = idcall;
052: this .tag = tag;
053: this .methodId = id;
054: this .arity = arity;
055: if (arity < 0)
056: throw new IllegalArgumentException();
057: }
058:
059: public IdFunctionObject(IdFunctionCall idcall, Object tag, int id,
060: String name, int arity, Scriptable scope) {
061: super (scope, null);
062:
063: if (arity < 0)
064: throw new IllegalArgumentException();
065: if (name == null)
066: throw new IllegalArgumentException();
067:
068: this .idcall = idcall;
069: this .tag = tag;
070: this .methodId = id;
071: this .arity = arity;
072: this .functionName = name;
073: }
074:
075: public void initFunction(String name, Scriptable scope) {
076: if (name == null)
077: throw new IllegalArgumentException();
078: if (scope == null)
079: throw new IllegalArgumentException();
080: this .functionName = name;
081: setParentScope(scope);
082: }
083:
084: public final boolean hasTag(Object tag) {
085: return this .tag == tag;
086: }
087:
088: public final int methodId() {
089: return methodId;
090: }
091:
092: public final void markAsConstructor(Scriptable prototypeProperty) {
093: useCallAsConstructor = true;
094: setImmunePrototypeProperty(prototypeProperty);
095: }
096:
097: public final void addAsProperty(Scriptable target) {
098: ScriptableObject.defineProperty(target, functionName, this ,
099: ScriptableObject.DONTENUM);
100: }
101:
102: public void exportAsScopeProperty() {
103: addAsProperty(getParentScope());
104: }
105:
106: public Scriptable getPrototype() {
107: // Lazy initialization of prototype: for native functions this
108: // may not be called at all
109: Scriptable proto = super .getPrototype();
110: if (proto == null) {
111: proto = getFunctionPrototype(getParentScope());
112: setPrototype(proto);
113: }
114: return proto;
115: }
116:
117: public Object call(Context cx, Scriptable scope,
118: Scriptable this Obj, Object[] args) {
119: return idcall.execIdCall(this , cx, scope, this Obj, args);
120: }
121:
122: public Scriptable createObject(Context cx, Scriptable scope) {
123: if (useCallAsConstructor) {
124: return null;
125: }
126: // Throw error if not explicitly coded to be used as constructor,
127: // to satisfy ECMAScript standard (see bugzilla 202019).
128: // To follow current (2003-05-01) SpiderMonkey behavior, change it to:
129: // return super.createObject(cx, scope);
130: throw ScriptRuntime.typeError1("msg.not.ctor", functionName);
131: }
132:
133: String decompile(int indent, int flags) {
134: StringBuffer sb = new StringBuffer();
135: boolean justbody = (0 != (flags & Decompiler.ONLY_BODY_FLAG));
136: if (!justbody) {
137: sb.append("function ");
138: sb.append(getFunctionName());
139: sb.append("() { ");
140: }
141: sb.append("[native code for ");
142: if (idcall instanceof Scriptable) {
143: Scriptable sobj = (Scriptable) idcall;
144: sb.append(sobj.getClassName());
145: sb.append('.');
146: }
147: sb.append(getFunctionName());
148: sb.append(", arity=");
149: sb.append(getArity());
150: sb.append(justbody ? "]\n" : "] }\n");
151: return sb.toString();
152: }
153:
154: public int getArity() {
155: return arity;
156: }
157:
158: public int getLength() {
159: return getArity();
160: }
161:
162: public String getFunctionName() {
163: return (functionName == null) ? "" : functionName;
164: }
165:
166: public final RuntimeException unknown() {
167: // It is program error to call id-like methods for unknown function
168: return new IllegalArgumentException("BAD FUNCTION ID="
169: + methodId + " MASTER=" + idcall);
170: }
171:
172: private final IdFunctionCall idcall;
173: private final Object tag;
174: private final int methodId;
175: private int arity;
176: private boolean useCallAsConstructor;
177: private String functionName;
178: }
|