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: * Norris Boyd
026: * Igor Bukanov
027: * Bob Jervis
028: * Roger Lawrence
029: * Mike McCabe
030: *
031: * Alternatively, the contents of this file may be used under the terms of
032: * the GNU General Public License Version 2 or later (the "GPL"), in which
033: * case the provisions of the GPL are applicable instead of those above. If
034: * you wish to allow use of your version of this file only under the terms of
035: * the GPL and not to allow others to use your version of this file under the
036: * MPL, indicate your decision by deleting the provisions above and replacing
037: * them with the notice and other provisions required by the GPL. If you do
038: * not delete the provisions above, a recipient may use your version of this
039: * file under either the MPL or the GPL.
040: *
041: * ***** END LICENSE BLOCK ***** */
042:
043: package org.mozilla.javascript;
044:
045: import org.mozilla.javascript.debug.DebuggableScript;
046:
047: /**
048: * This class implements the Function native object.
049: * See ECMA 15.3.
050: * @author Norris Boyd
051: */
052: public abstract class NativeFunction extends BaseFunction {
053:
054: public final void initScriptFunction(Context cx, Scriptable scope) {
055: ScriptRuntime.setFunctionProtoAndParent(this , scope);
056: }
057:
058: /**
059: * @param indent How much to indent the decompiled result
060: *
061: * @param flags Flags specifying format of decompilation output
062: */
063: final String decompile(int indent, int flags) {
064: String encodedSource = getEncodedSource();
065: if (encodedSource == null) {
066: return super .decompile(indent, flags);
067: } else {
068: UintMap properties = new UintMap(1);
069: properties.put(Decompiler.INITIAL_INDENT_PROP, indent);
070: return Decompiler.decompile(encodedSource, flags,
071: properties);
072: }
073: }
074:
075: public int getLength() {
076: int paramCount = getParamCount();
077: if (getLanguageVersion() != Context.VERSION_1_2) {
078: return paramCount;
079: }
080: Context cx = Context.getContext();
081: NativeCall activation = ScriptRuntime.findFunctionActivation(
082: cx, this );
083: if (activation == null) {
084: return paramCount;
085: }
086: return activation.originalArgs.length;
087: }
088:
089: public int getArity() {
090: return getParamCount();
091: }
092:
093: /**
094: * @deprecated Use {@link BaseFunction#getFunctionName()} instead.
095: * For backwards compatibility keep an old method name used by
096: * Batik and possibly others.
097: */
098: public String jsGet_name() {
099: return getFunctionName();
100: }
101:
102: /**
103: * Get encoded source string.
104: */
105: public String getEncodedSource() {
106: return null;
107: }
108:
109: public DebuggableScript getDebuggableView() {
110: return null;
111: }
112:
113: /**
114: * Resume execution of a suspended generator.
115: * @param cx The current context
116: * @param scope Scope for the parent generator function
117: * @param operation The resumption operation (next, send, etc.. )
118: * @param state The generator state (has locals, stack, etc.)
119: * @param value The return value of yield (if required).
120: * @return The next yielded value (if any)
121: */
122: public Object resumeGenerator(Context cx, Scriptable scope,
123: int operation, Object state, Object value) {
124: throw new EvaluatorException(
125: "resumeGenerator() not implemented");
126: }
127:
128: protected abstract int getLanguageVersion();
129:
130: /**
131: * Get number of declared parameters. It should be 0 for scripts.
132: */
133: protected abstract int getParamCount();
134:
135: /**
136: * Get number of declared parameters and variables defined through var
137: * statements.
138: */
139: protected abstract int getParamAndVarCount();
140:
141: /**
142: * Get parameter or variable name.
143: * If <tt>index < {@link #getParamCount()}</tt>, then return the name of the
144: * corresponding parameter. Otherwise return the name of variable.
145: */
146: protected abstract String getParamOrVarName(int index);
147:
148: /**
149: * Get parameter or variable const-ness.
150: * If <tt>index < {@link #getParamCount()}</tt>, then return the const-ness
151: * of the corresponding parameter. Otherwise return whether the variable is
152: * const.
153: */
154: protected boolean getParamOrVarConst(int index) {
155: // By default return false to preserve compatibility with existing
156: // classes subclassing this class, which are mostly generated by jsc
157: // from earlier Rhino versions. See Bugzilla #396117.
158: return false;
159: }
160: }
|