| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 116 |
throw new IllegalStateException(); |
| 117 |
} |
| 118 |
if (!ScriptRuntime.hasTopCall(cx)) { |
| 119 |
|
| 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 |
|