Debian Codegraph @

rhino/src/org/mozilla/javascript/NativeFunction.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import org.mozilla.javascript.debug.DebuggableScript;
10
11 /**
12 * This class implements the Function native object.
13 * See ECMA 15.3.
14 * @author Norris Boyd
15 */
16 public abstract class NativeFunction extends BaseFunction
17 {
18
19 static final long serialVersionUID = 8713897114082216401L;
20
21 public final void initScriptFunction(Context cx, Scriptable scope)
22 {
23 ScriptRuntime.setFunctionProtoAndParent(this, scope);
24 }
25
26 /**
27 * @param indent How much to indent the decompiled result
28 *
29 * @param flags Flags specifying format of decompilation output
30 */
31 @Override
32 final String decompile(int indent, int flags)
33 {
34 String encodedSource = getEncodedSource();
35 if (encodedSource == null) {
36 return super.decompile(indent, flags);
37 } else {
38 UintMap properties = new UintMap(1);
39 properties.put(Decompiler.INITIAL_INDENT_PROP, indent);
40 return Decompiler.decompile(encodedSource, flags, properties);
41 }
42 }
43
44 @Override
45 public int getLength()
46 {
47 int paramCount = getParamCount();
48 if (getLanguageVersion() != Context.VERSION_1_2) {
49 return paramCount;
50 }
51 Context cx = Context.getContext();
52 NativeCall activation = ScriptRuntime.findFunctionActivation(cx, this);
53 if (activation == null) {
54 return paramCount;
55 }
56 return activation.originalArgs.length;
57 }
58
59 @Override
60 public int getArity()
61 {
62 return getParamCount();
63 }
64
65 /**
66 * @deprecated Use {@link BaseFunction#getFunctionName()} instead.
67 * For backwards compatibility keep an old method name used by
68 * Batik and possibly others.
69 */
70 @Deprecated
71 public String jsGet_name()
72 {
73 return getFunctionName();
74 }
75
76 /**
77 * Get encoded source string.
78 */
79 public String getEncodedSource()
80 {
81 return null;
82 }
83
84 public DebuggableScript getDebuggableView()
85 {
86 return null;
87 }
88
89 /**
90 * Resume execution of a suspended generator.
91 * @param cx The current context
92 * @param scope Scope for the parent generator function
93 * @param operation The resumption operation (next, send, etc.. )
94 * @param state The generator state (has locals, stack, etc.)
95 * @param value The return value of yield (if required).
96 * @return The next yielded value (if any)
97 */
98 public Object resumeGenerator(Context cx, Scriptable scope,
99 int operation, Object state, Object value)
100 {
101 throw new EvaluatorException("resumeGenerator() not implemented");
102 }
103
104
105 protected abstract int getLanguageVersion();
106
107 /**
108 * Get number of declared parameters. It should be 0 for scripts.
109 */
110 protected abstract int getParamCount();
111
112 /**
113 * Get number of declared parameters and variables defined through var
114 * statements.
115 */
116 protected abstract int getParamAndVarCount();
117
118 /**
119 * Get parameter or variable name.
120 * If <tt>index < {@link #getParamCount()}</tt>, then return the name of the
121 * corresponding parameter. Otherwise return the name of variable.
122 */
123 protected abstract String getParamOrVarName(int index);
124
125 /**
126 * Get parameter or variable const-ness.
127 * If <tt>index < {@link #getParamCount()}</tt>, then return the const-ness
128 * of the corresponding parameter. Otherwise return whether the variable is
129 * const.
130 */
131 protected boolean getParamOrVarConst(int index)
132 {
133 // By default return false to preserve compatibility with existing
134 // classes subclassing this class, which are mostly generated by jsc
135 // from earlier Rhino versions. See Bugzilla #396117.
136 return false;
137 }
138 }
139