1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* The contents of this file are subject to the Netscape Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/NPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is Rhino code, released
16
* The Initial Developer of the Original Code is Netscape
17
* Communications Corporation. Portions created by Netscape are
18
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
24
* Alternatively, the contents of this file may be used under the
25
* terms of the GNU Public License (the "GPL"), in which case the
26
* provisions of the GPL are applicable instead of those above.
27
* If you wish to allow use of your version of this file only
28
* under the terms of the GPL and not to allow others to use your
29
* version of this file under the NPL, indicate your decision by
30
* deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL. If you do not delete
32
* the provisions above, a recipient may use your version of this
33
* file under either the NPL or the GPL.
36
package org.mozilla.javascript;
39
import org.mozilla.javascript.debug.DebuggableScript;
41
class InterpretedFunction extends NativeFunction implements DebuggableScript {
43
InterpretedFunction(Context cx,
44
InterpreterData theData,
45
String[] argNames, short argCount)
48
this.argNames = argNames;
49
this.argCount = argCount;
55
functionName = itsData.itsName;
56
source = itsData.itsSource;
57
nestedFunctions = itsData.itsNestedFunctions;
59
version = (short)cx.getLanguageVersion();
62
InterpretedFunction(InterpretedFunction theOther,
63
Scriptable theScope, Context cx)
65
itsData = theOther.itsData;
66
this.argNames = theOther.argNames;
67
this.argCount = theOther.argCount;
68
itsClosure = theScope;
72
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
74
throws JavaScriptException
76
if (itsClosure != null)
78
else if (!itsData.itsUseDynamicScope)
79
scope = getParentScope();
81
if (itsData.itsCheckThis)
82
thisObj = ScriptRuntime.getThis(thisObj);
84
if (itsData.itsNeedsActivation) {
85
scope = ScriptRuntime.initVarObj(cx, scope, this, thisObj, args);
88
return Interpreter.interpret(cx, scope, thisObj, args, this,
92
if (itsData.itsNeedsActivation) {
93
ScriptRuntime.popActivation(cx);
98
public boolean isFunction() {
102
public Scriptable getScriptable() {
106
public String getSourceName() {
107
return itsData.itsSourceFile;
110
public int[] getLineNumbers() {
111
return itsData.itsLineNumberTable.getKeys();
114
public boolean placeBreakpoint(int line) { // XXX throw exn?
115
return itsData.placeBreakpoint(line);
118
public boolean removeBreakpoint(int line) {
119
return itsData.removeBreakpoint(line);
122
InterpreterData itsData;
123
Scriptable itsClosure;
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* The contents of this file are subject to the Netscape Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/NPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is Rhino code, released
16
* The Initial Developer of the Original Code is Netscape
17
* Communications Corporation. Portions created by Netscape are
18
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
25
* Alternatively, the contents of this file may be used under the
26
* terms of the GNU Public License (the "GPL"), in which case the
27
* provisions of the GPL are applicable instead of those above.
28
* If you wish to allow use of your version of this file only
29
* under the terms of the GPL and not to allow others to use your
30
* version of this file under the NPL, indicate your decision by
31
* deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL. If you do not delete
33
* the provisions above, a recipient may use your version of this
34
* file under either the NPL or the GPL.
37
package org.mozilla.javascript;
39
import org.mozilla.javascript.debug.DebuggableScript;
41
final class InterpretedFunction extends NativeFunction implements Script
43
InterpreterData idata;
44
SecurityController securityController;
45
Object securityDomain;
46
Scriptable[] functionRegExps;
48
private InterpretedFunction(InterpreterData idata,
49
Object staticSecurityDomain)
53
// Always get Context from the current thread to
54
// avoid security breaches via passing mangled Context instances
55
// with bogus SecurityController
56
Context cx = Context.getContext();
57
SecurityController sc = cx.getSecurityController();
60
dynamicDomain = sc.getDynamicSecurityDomain(staticSecurityDomain);
62
if (staticSecurityDomain != null) {
63
throw new IllegalArgumentException();
68
this.securityController = sc;
69
this.securityDomain = dynamicDomain;
72
private InterpretedFunction(InterpretedFunction parent, int index)
74
this.idata = parent.idata.itsNestedFunctions[index];
75
this.securityController = parent.securityController;
76
this.securityDomain = parent.securityDomain;
80
* Create script from compiled bytecode.
82
static InterpretedFunction createScript(InterpreterData idata,
83
Object staticSecurityDomain)
85
InterpretedFunction f;
86
f = new InterpretedFunction(idata, staticSecurityDomain);
92
* Create function compiled from Function(...) constructor.
94
static InterpretedFunction createFunction(Context cx,Scriptable scope,
95
InterpreterData idata,
96
Object staticSecurityDomain)
98
InterpretedFunction f;
99
f = new InterpretedFunction(idata, staticSecurityDomain);
100
f.initInterpretedFunction(cx, scope);
105
* Create function embedded in script or another function.
107
static InterpretedFunction createFunction(Context cx, Scriptable scope,
108
InterpretedFunction parent,
111
InterpretedFunction f = new InterpretedFunction(parent, index);
112
f.initInterpretedFunction(cx, scope);
116
Scriptable[] createRegExpWraps(Context cx, Scriptable scope)
118
if (idata.itsRegExpLiterals == null) Kit.codeBug();
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, idata.itsRegExpLiterals[i]);
129
private void initInterpretedFunction(Context cx, Scriptable scope)
131
initScriptFunction(cx, scope, idata.itsName);
132
if (idata.itsRegExpLiterals != null) {
133
functionRegExps = createRegExpWraps(cx, scope);
137
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
140
return Interpreter.interpret(this, cx, scope, thisObj, args);
143
public Object exec(Context cx, Scriptable scope)
145
if (idata.itsFunctionType != 0) {
146
// Can only be applied to scripts
147
throw new IllegalStateException();
149
return call(cx, scope, scope, ScriptRuntime.emptyArgs);
152
public String getEncodedSource()
154
return Interpreter.getEncodedSource(idata);
157
public DebuggableScript getDebuggableView()
162
protected int getLanguageVersion()
164
return idata.languageVersion;
167
protected int getParamCount()
169
return idata.argCount;
172
protected int getParamAndVarCount()
174
return idata.argNames.length;
177
protected String getParamOrVarName(int index)
179
return idata.argNames[index];