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-2000
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: * Bob Jervis
027: * Roger Lawrence
028: *
029: * Alternatively, the contents of this file may be used under the terms of
030: * the GNU General Public License Version 2 or later (the "GPL"), in which
031: * case the provisions of the GPL are applicable instead of those above. If
032: * you wish to allow use of your version of this file only under the terms of
033: * the GPL and not to allow others to use your version of this file under the
034: * MPL, indicate your decision by deleting the provisions above and replacing
035: * them with the notice and other provisions required by the GPL. If you do
036: * not delete the provisions above, a recipient may use your version of this
037: * file under either the MPL or the GPL.
038: *
039: * ***** END LICENSE BLOCK ***** */
040:
041: package org.mozilla.javascript;
042:
043: import org.mozilla.javascript.debug.DebuggableScript;
044:
045: final class InterpretedFunction extends NativeFunction implements
046: Script {
047: static final long serialVersionUID = 541475680333911468L;
048:
049: InterpreterData idata;
050: SecurityController securityController;
051: Object securityDomain;
052: Scriptable[] functionRegExps;
053:
054: private InterpretedFunction(InterpreterData idata,
055: Object staticSecurityDomain) {
056: this .idata = idata;
057:
058: // Always get Context from the current thread to
059: // avoid security breaches via passing mangled Context instances
060: // with bogus SecurityController
061: Context cx = Context.getContext();
062: SecurityController sc = cx.getSecurityController();
063: Object dynamicDomain;
064: if (sc != null) {
065: dynamicDomain = sc
066: .getDynamicSecurityDomain(staticSecurityDomain);
067: } else {
068: if (staticSecurityDomain != null) {
069: throw new IllegalArgumentException();
070: }
071: dynamicDomain = null;
072: }
073:
074: this .securityController = sc;
075: this .securityDomain = dynamicDomain;
076: }
077:
078: private InterpretedFunction(InterpretedFunction parent, int index) {
079: this .idata = parent.idata.itsNestedFunctions[index];
080: this .securityController = parent.securityController;
081: this .securityDomain = parent.securityDomain;
082: }
083:
084: /**
085: * Create script from compiled bytecode.
086: */
087: static InterpretedFunction createScript(InterpreterData idata,
088: Object staticSecurityDomain) {
089: InterpretedFunction f;
090: f = new InterpretedFunction(idata, staticSecurityDomain);
091: return f;
092: }
093:
094: /**
095: * Create function compiled from Function(...) constructor.
096: */
097: static InterpretedFunction createFunction(Context cx,
098: Scriptable scope, InterpreterData idata,
099: Object staticSecurityDomain) {
100: InterpretedFunction f;
101: f = new InterpretedFunction(idata, staticSecurityDomain);
102: f.initInterpretedFunction(cx, scope);
103: return f;
104: }
105:
106: /**
107: * Create function embedded in script or another function.
108: */
109: static InterpretedFunction createFunction(Context cx,
110: Scriptable scope, InterpretedFunction parent, int index) {
111: InterpretedFunction f = new InterpretedFunction(parent, index);
112: f.initInterpretedFunction(cx, scope);
113: return f;
114: }
115:
116: Scriptable[] createRegExpWraps(Context cx, Scriptable scope) {
117: if (idata.itsRegExpLiterals == null)
118: Kit.codeBug();
119:
120: RegExpProxy rep = ScriptRuntime.checkRegExpProxy(cx);
121: int N = idata.itsRegExpLiterals.length;
122: Scriptable[] array = new Scriptable[N];
123: for (int i = 0; i != N; ++i) {
124: array[i] = rep.wrapRegExp(cx, scope,
125: idata.itsRegExpLiterals[i]);
126: }
127: return array;
128: }
129:
130: private void initInterpretedFunction(Context cx, Scriptable scope) {
131: initScriptFunction(cx, scope);
132: if (idata.itsRegExpLiterals != null) {
133: functionRegExps = createRegExpWraps(cx, scope);
134: }
135: }
136:
137: public String getFunctionName() {
138: return (idata.itsName == null) ? "" : idata.itsName;
139: }
140:
141: /**
142: * Calls the function.
143: * @param cx the current context
144: * @param scope the scope used for the call
145: * @param thisObj the value of "this"
146: * @param args function arguments. Must not be null. You can use
147: * {@link ScriptRuntime#emptyArgs} to pass empty arguments.
148: * @return the result of the function call.
149: */
150: public Object call(Context cx, Scriptable scope,
151: Scriptable this Obj, Object[] args) {
152: if (!ScriptRuntime.hasTopCall(cx)) {
153: return ScriptRuntime.doTopCall(this , cx, scope, this Obj,
154: args);
155: }
156: return Interpreter.interpret(this , cx, scope, this Obj, args);
157: }
158:
159: public Object exec(Context cx, Scriptable scope) {
160: if (idata.itsFunctionType != 0) {
161: // Can only be applied to scripts
162: throw new IllegalStateException();
163: }
164: if (!ScriptRuntime.hasTopCall(cx)) {
165: // It will go through "call" path. but they are equivalent
166: return ScriptRuntime.doTopCall(this , cx, scope, scope,
167: ScriptRuntime.emptyArgs);
168: }
169: return Interpreter.interpret(this , cx, scope, scope,
170: ScriptRuntime.emptyArgs);
171: }
172:
173: public String getEncodedSource() {
174: return Interpreter.getEncodedSource(idata);
175: }
176:
177: public DebuggableScript getDebuggableView() {
178: return idata;
179: }
180:
181: public Object resumeGenerator(Context cx, Scriptable scope,
182: int operation, Object state, Object value) {
183: return Interpreter.resumeGenerator(cx, scope, operation, state,
184: value);
185: }
186:
187: protected int getLanguageVersion() {
188: return idata.languageVersion;
189: }
190:
191: protected int getParamCount() {
192: return idata.argCount;
193: }
194:
195: protected int getParamAndVarCount() {
196: return idata.argNames.length;
197: }
198:
199: protected String getParamOrVarName(int index) {
200: return idata.argNames[index];
201: }
202:
203: protected boolean getParamOrVarConst(int index) {
204: return idata.argIsConst[index];
205: }
206: }
|