Debian Codegraph @

rhino/src/org/mozilla/javascript/InterpretedFunction.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 final class InterpretedFunction extends NativeFunction implements Script
12 {
13 static final long serialVersionUID = 541475680333911468L;
14
15 InterpreterData idata;
16 SecurityController securityController;
17 Object securityDomain;
18
19 private InterpretedFunction(InterpreterData idata,
20 Object staticSecurityDomain)
21 {
22 this.idata = idata;
23
24 // Always get Context from the current thread to
25 // avoid security breaches via passing mangled Context instances
26 // with bogus SecurityController
27 Context cx = Context.getContext();
28 SecurityController sc = cx.getSecurityController();
29 Object dynamicDomain;
30 if (sc != null) {
31 dynamicDomain = sc.getDynamicSecurityDomain(staticSecurityDomain);
32 } else {
33 if (staticSecurityDomain != null) {
34 throw new IllegalArgumentException();
35 }
36 dynamicDomain = null;
37 }
38
39 this.securityController = sc;
40 this.securityDomain = dynamicDomain;
41 }
42
43 private InterpretedFunction(InterpretedFunction parent, int index)
44 {
45 this.idata = parent.idata.itsNestedFunctions[index];
46 this.securityController = parent.securityController;
47 this.securityDomain = parent.securityDomain;
48 }
49
50 /**
51 * Create script from compiled bytecode.
52 */
53 static InterpretedFunction createScript(InterpreterData idata,
54 Object staticSecurityDomain)
55 {
56 InterpretedFunction f;
57 f = new InterpretedFunction(idata, staticSecurityDomain);
58 return f;
59 }
60
61 /**
62 * Create function compiled from Function(...) constructor.
63 */
64 static InterpretedFunction createFunction(Context cx,Scriptable scope,
65 InterpreterData idata,
66 Object staticSecurityDomain)
67 {
68 InterpretedFunction f;
69 f = new InterpretedFunction(idata, staticSecurityDomain);
70 f.initScriptFunction(cx, scope);
71 return f;
72 }
73
74 /**
75 * Create function embedded in script or another function.
76 */
77 static InterpretedFunction createFunction(Context cx, Scriptable scope,
78 InterpretedFunction parent,
79 int index)
80 {
81 InterpretedFunction f = new InterpretedFunction(parent, index);
82 f.initScriptFunction(cx, scope);
83 return f;
84 }
85
86
87 @Override
88 public String getFunctionName()
89 {
90 return (idata.itsName == null) ? "" : idata.itsName;
91 }
92
93 /**
94 * Calls the function.
95 * @param cx the current context
96 * @param scope the scope used for the call
97 * @param thisObj the value of "this"
98 * @param args function arguments. Must not be null. You can use
99 * {@link ScriptRuntime#emptyArgs} to pass empty arguments.
100 * @return the result of the function call.
101 */
102 @Override
103 public Object call(Context cx, Scriptable scope, Scriptable thisObj,
104 Object[] args)
105 {
106 if (!ScriptRuntime.hasTopCall(cx)) {
107 return ScriptRuntime.doTopCall(this, cx, scope, thisObj, args, idata.isStrict);
108 }
109 return Interpreter.interpret(this, cx, scope, thisObj, args);
110 }
111
112 public Object exec(Context cx, Scriptable scope)
113 {
114 if (!isScript()) {
115 // Can only be applied to scripts
116 throw new IllegalStateException();
117 }
118 if (!ScriptRuntime.hasTopCall(cx)) {
119 // It will go through "call" path. but they are equivalent
120 return ScriptRuntime.doTopCall(
121 this, cx, scope, scope, ScriptRuntime.emptyArgs, idata.isStrict);
122 }
123 return Interpreter.interpret(
124 this, cx, scope, scope, ScriptRuntime.emptyArgs);
125 }
126
127 public boolean isScript() {
128 return idata.itsFunctionType == 0;
129 }
130
131 @Override
132 public String getEncodedSource()
133 {
134 return Interpreter.getEncodedSource(idata);
135 }
136
137 @Override
138 public DebuggableScript getDebuggableView()
139 {
140 return idata;
141 }
142
143 @Override
144 public Object resumeGenerator(Context cx, Scriptable scope, int operation,
145 Object state, Object value)
146 {
147 return Interpreter.resumeGenerator(cx, scope, operation, state, value);
148 }
149
150 @Override
151 protected int getLanguageVersion()
152 {
153 return idata.languageVersion;
154 }
155
156 @Override
157 protected int getParamCount()
158 {
159 return idata.argCount;
160 }
161
162 @Override
163 protected int getParamAndVarCount()
164 {
165 return idata.argNames.length;
166 }
167
168 @Override
169 protected String getParamOrVarName(int index)
170 {
171 return idata.argNames[index];
172 }
173
174 @Override
175 protected boolean getParamOrVarConst(int index)
176 {
177 return idata.argIsConst[index];
178 }
179 }
180