Praising changes: remove direct access to VariableTable in ScriptOrFnNode and instead provide methods to access its functionality. In this way VariableTable can be changed without affecting the rest of code.

This commit is contained in:
igor%mir2.org 2003-02-20 09:18:12 +00:00
Родитель f7ae206b83
Коммит dc46d02971
7 изменённых файлов: 56 добавлений и 52 удалений

Просмотреть файл

@ -92,10 +92,6 @@ public class FunctionNode extends ScriptOrFnNode {
itsFunctionType = functionType;
}
public int getParameterCount() {
return getVariableTable().getParameterCount();
}
protected void finishParsing(IRFactory irFactory) {
super.finishParsing(irFactory);
int functionCount = getFunctionCount();

Просмотреть файл

@ -103,7 +103,6 @@ public class Interpreter {
generateRegExpLiterals(cx, scope);
itsVariableTable = scriptOrFn.getVariableTable();
generateICodeFromTree(scriptOrFn);
if (Context.printICode) dumpICode(itsData);
@ -129,7 +128,6 @@ public class Interpreter {
itsData.itsNeedsActivation = theFunction.requiresActivation();
itsVariableTable = theFunction.getVariableTable();
generateICodeFromTree(theFunction.getLastChild());
itsData.itsName = theFunction.getFunctionName();
@ -220,7 +218,7 @@ public class Interpreter {
itsData.itsDoubleTable = tmp;
}
itsData.itsMaxVars = itsVariableTable.size();
itsData.itsMaxVars = scriptOrFn.getParameterAndVarCount();
// itsMaxFrameArray: interpret method needs this amount for its
// stack and sDbl arrays
itsData.itsMaxFrameArray = itsData.itsMaxVars
@ -228,8 +226,8 @@ public class Interpreter {
+ itsData.itsMaxTryDepth
+ itsData.itsMaxStack;
itsData.argNames = itsVariableTable.getAllVariables();
itsData.argCount = itsVariableTable.getParameterCount();
itsData.argNames = scriptOrFn.getParameterAndVarNames();
itsData.argCount = scriptOrFn.getParameterCount();
}
private int updateLineNumber(Node node, int iCodeTop) {
@ -676,7 +674,7 @@ public class Interpreter {
// use typeofname if an activation frame exists
// since the vars all exist there instead of in jregs
if (itsInFunctionFlag && !itsData.itsNeedsActivation)
index = itsVariableTable.getOrdinal(name);
index = scriptOrFn.getParameterOrVarIndex(name);
if (index == -1) {
iCodeTop = addByte(TokenStream.TYPEOFNAME, iCodeTop);
iCodeTop = addString(name, iCodeTop);
@ -726,7 +724,7 @@ public class Interpreter {
iCodeTop);
itsStackDepth--;
} else {
int i = itsVariableTable.getOrdinal(name);
int i = scriptOrFn.getParameterOrVarIndex(name);
iCodeTop = addByte(type == TokenStream.INC
? TokenStream.VARINC
: TokenStream.VARDEC,
@ -932,7 +930,7 @@ public class Interpreter {
iCodeTop = addByte(TokenStream.GETPROP, iCodeTop);
itsStackDepth--;
} else {
int index = itsVariableTable.getOrdinal(name);
int index = scriptOrFn.getParameterOrVarIndex(name);
iCodeTop = addByte(TokenStream.GETVAR, iCodeTop);
iCodeTop = addByte(index, iCodeTop);
itsStackDepth++;
@ -951,7 +949,7 @@ public class Interpreter {
String name = child.getString();
child = child.getNext();
iCodeTop = generateICode(child, iCodeTop);
int index = itsVariableTable.getOrdinal(name);
int index = scriptOrFn.getParameterOrVarIndex(name);
iCodeTop = addByte(TokenStream.SETVAR, iCodeTop);
iCodeTop = addByte(index, iCodeTop);
}
@ -2886,7 +2884,6 @@ public class Interpreter {
private InterpreterData itsData;
private ScriptOrFnNode scriptOrFn;
private VariableTable itsVariableTable;
private int itsTryDepth = 0;
private int itsStackDepth = 0;
private String itsSourceFile;

Просмотреть файл

@ -305,8 +305,7 @@ public class JavaAdapter extends ScriptableObject {
ScriptableObject.getProperty(p, "length"));
} else if (f instanceof FunctionNode) {
// This is used only by optimizer/Codegen
length = ((FunctionNode) f).getVariableTable()
.getParameterCount();
length = ((FunctionNode)f).getParameterCount();
} else {
continue;
}

Просмотреть файл

@ -41,11 +41,6 @@ public class ScriptOrFnNode extends Node {
super(nodeType);
}
public final VariableTable getVariableTable() {
if (variableTable == null) { variableTable = new VariableTable(); }
return variableTable;
}
public final String getSourceName() { return sourceName; }
public final void setSourceName(String sourceName) {
@ -127,6 +122,30 @@ public class ScriptOrFnNode extends Node {
return variableTable.hasVariable(name);
}
public final int getParameterOrVarIndex(String name) {
if (variableTable == null) { return -1; }
return variableTable.getOrdinal(name);
}
public final String getParameterOrVarName(int index) {
return variableTable.getVariable(index);
}
public final int getParameterCount() {
if (variableTable == null) { return 0; }
return variableTable.getParameterCount();
}
public final int getParameterAndVarCount() {
if (variableTable == null) { return 0; }
return variableTable.size();
}
public final String[] getParameterAndVarNames() {
if (variableTable == null) { return new String[0]; }
return variableTable.getAllVariables();
}
public final void addParameter(String name) {
if (variableTable == null) { variableTable = new VariableTable(); }
variableTable.addParameter(name);

Просмотреть файл

@ -413,7 +413,6 @@ public class Codegen extends Interpreter {
if (inFunction) {
fnCurrent = (OptFunctionNode)scriptOrFn;
inDirectCallFunction = fnCurrent.isTargetOfDirectCall();
vars = fnCurrent.getVariableTable();
this.name = fnCurrent.getClassName();
classFile = new ClassFileWriter(name, superClassName, itsSourceFile);
String name = fnCurrent.getFunctionName();
@ -430,7 +429,7 @@ public class Codegen extends Interpreter {
addByteCode(ByteCode.ALOAD_1);
addByteCode(ByteCode.ALOAD_2);
addByteCode(ByteCode.ALOAD_3);
for (int i = 0; i < vars.getParameterCount(); i++) {
for (int i = 0; i < scriptOrFn.getParameterCount(); i++) {
push(i);
addByteCode(ByteCode.ALOAD, 4);
addByteCode(ByteCode.ARRAYLENGTH);
@ -483,7 +482,7 @@ public class Codegen extends Interpreter {
markLabel(isObjectLabel);
}
}
generatePrologue(cx, true, vars.getParameterCount());
generatePrologue(cx, true, scriptOrFn.getParameterCount());
} else {
startNewMethod("call",
"(Lorg/mozilla/javascript/Context;" +
@ -498,7 +497,6 @@ public class Codegen extends Interpreter {
// better be a script
if (scriptOrFn.getType() != TokenStream.SCRIPT)
badTree();
vars = scriptOrFn.getVariableTable();
boolean isPrimary = nameHelper.getTargetExtends() == null &&
nameHelper.getTargetImplements() == null;
this.name = getScriptClassName(null, isPrimary);
@ -1118,7 +1116,6 @@ public class Codegen extends Interpreter {
{
trivialInit = true;
boolean inCtor = false;
VariableTable vars = scriptOrFn.getVariableTable();
if (methodName.equals("<init>")) {
inCtor = true;
setNonTrivialInit(methodName);
@ -1148,27 +1145,25 @@ public class Codegen extends Interpreter {
"functionName", "Ljava/lang/String;");
}
if (vars != null) {
int N = vars.size();
if (N != 0) {
setNonTrivialInit(methodName);
push(N);
addByteCode(ByteCode.ANEWARRAY, "java/lang/String");
for (int i = 0; i != N; i++) {
addByteCode(ByteCode.DUP);
push(i);
push(vars.getVariable(i));
addByteCode(ByteCode.AASTORE);
}
addByteCode(ByteCode.ALOAD_0);
addByteCode(ByteCode.SWAP);
classFile.add(ByteCode.PUTFIELD,
"org/mozilla/javascript/NativeFunction",
"argNames", "[Ljava/lang/String;");
int N = scriptOrFn.getParameterAndVarCount();
if (N != 0) {
setNonTrivialInit(methodName);
push(N);
addByteCode(ByteCode.ANEWARRAY, "java/lang/String");
for (int i = 0; i != N; i++) {
addByteCode(ByteCode.DUP);
push(i);
push(scriptOrFn.getParameterOrVarName(i));
addByteCode(ByteCode.AASTORE);
}
addByteCode(ByteCode.ALOAD_0);
addByteCode(ByteCode.SWAP);
classFile.add(ByteCode.PUTFIELD,
"org/mozilla/javascript/NativeFunction",
"argNames", "[Ljava/lang/String;");
}
int parmCount = vars == null ? 0 : vars.getParameterCount();
int parmCount = scriptOrFn.getParameterCount();
if (parmCount != 0) {
setNonTrivialInit(methodName);
addByteCode(ByteCode.ALOAD_0);
@ -1378,7 +1373,7 @@ public class Codegen extends Interpreter {
!((OptFunctionNode)scriptOrFn).requiresActivation();
if (hasVarsInRegs) {
// No need to create activation. Pad arguments if need be.
int parmCount = vars.getParameterCount();
int parmCount = scriptOrFn.getParameterCount();
if (inFunction && parmCount > 0 && directParameterCount < 0) {
// Set up args array
// check length of arguments, pad if need be
@ -3771,7 +3766,6 @@ public class Codegen extends Interpreter {
private boolean itsForcedObjectParameters;
private boolean trivialInit;
private short itsLocalAllocationBase;
private VariableTable vars;
private OptLocalVariable[] debugVars;
private int epilogueLabel;
private int optLevel;

Просмотреть файл

@ -48,12 +48,11 @@ class OptFunctionNode extends FunctionNode {
protected void finishParsing(IRFactory irFactory) {
super.finishParsing(irFactory);
VariableTable vars = getVariableTable();
int N = vars.size();
int N = getParameterAndVarCount();
int parameterCount = getParameterCount();
optVars = new OptLocalVariable[N];
for (int i = 0; i != N; ++i) {
String name = vars.getVariable(i);
String name = getParameterOrVarName(i);
optVars[i] = new OptLocalVariable(name, i < parameterCount);
}
}
@ -130,7 +129,7 @@ class OptFunctionNode extends FunctionNode {
}
OptLocalVariable getVar(String name) {
int index = getVariableTable().getOrdinal(name);
int index = getParameterOrVarIndex(name);
if (index < 0) { return null; }
return optVars[index];
}