Work on bug 264637: BaseFunction.functionName is removed as was suggested by Attila Szegedi <szegedia@freemail.hu> :

I assume "functionName" in BaseFunction could also undergo a similar treatment
of being replaced with an abstract getFunctionName() method, couldn't it? The
function name is either calculable from other data (FieldAndMethods, overloaded
case of NativeJavaMethod*, NativeJavaConstructor, InterpetedFunction) or fixed
(NativeRegExpCtor) in lots of subclasses.
This commit is contained in:
igor%mir2.org 2005-01-17 13:06:34 +00:00
parent d30068e627
commit bcf9f00787
10 changed files with 71 additions and 44 deletions

View File

@ -51,7 +51,6 @@ public class BaseFunction extends IdScriptableObject implements Function
static void init(Context cx, Scriptable scope, boolean sealed)
{
BaseFunction obj = new BaseFunction();
obj.functionName = "";
obj.isPrototypePropertyImmune = true;
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
}
@ -91,7 +90,7 @@ public class BaseFunction extends IdScriptableObject implements Function
return ScriptRuntime.jsDelegatesTo(instance, (Scriptable)protoProp);
}
throw ScriptRuntime.typeError1("msg.instanceof.bad.prototype",
functionName);
getFunctionName());
}
// #string_id_map#
@ -263,7 +262,8 @@ public class BaseFunction extends IdScriptableObject implements Function
if (x instanceof BaseFunction) {
return (BaseFunction)x;
}
throw ScriptRuntime.typeError1("msg.incompat.call", f.functionName);
throw ScriptRuntime.typeError1("msg.incompat.call",
f.getFunctionName());
}
/**
@ -312,7 +312,7 @@ public class BaseFunction extends IdScriptableObject implements Function
// the call method if createObject returns null.
throw new IllegalStateException(
"Bad implementaion of call as constructor, name="
+functionName+" in "+getClass().getName());
+getFunctionName()+" in "+getClass().getName());
}
result = (Scriptable)val;
if (result.getPrototype() == null) {
@ -385,10 +385,9 @@ public class BaseFunction extends IdScriptableObject implements Function
public int getLength() { return 0; }
public String getFunctionName() {
if (functionName == null)
return "";
return functionName;
public String getFunctionName()
{
return "";
}
final Object getPrototypeProperty() {
@ -532,8 +531,6 @@ public class BaseFunction extends IdScriptableObject implements Function
// #/string_id_map#
protected String functionName;
private Object prototypeProperty;
private boolean isPrototypePropertyImmune;
}

View File

@ -259,6 +259,11 @@ public class FunctionObject extends BaseFunction
return getArity();
}
public String getFunctionName()
{
return (functionName == null) ? "" : functionName;
}
/**
* Get Java method or constructor this function represent.
*/
@ -550,6 +555,7 @@ public class FunctionObject extends BaseFunction
public static final int JAVA_OBJECT_TYPE = 6;
MemberBox member;
private String functionName;
private transient byte[] typeTags;
private int parmsLength;
private transient boolean hasVoidReturn;

View File

@ -162,6 +162,11 @@ public class IdFunctionObject extends BaseFunction
public int getLength() { return getArity(); }
public String getFunctionName()
{
return (functionName == null) ? "" : functionName;
}
public final RuntimeException unknown()
{
// It is program error to call id-like methods for unknown function
@ -174,4 +179,5 @@ public class IdFunctionObject extends BaseFunction
private final int methodId;
private int arity;
private boolean useCallAsConstructor;
private String functionName;
}

View File

@ -84,7 +84,6 @@ final class InterpretedFunction extends NativeFunction implements Script
{
InterpretedFunction f;
f = new InterpretedFunction(idata, staticSecurityDomain);
f.initScriptObject();
return f;
}
@ -128,12 +127,17 @@ final class InterpretedFunction extends NativeFunction implements Script
private void initInterpretedFunction(Context cx, Scriptable scope)
{
initScriptFunction(cx, scope, idata.itsName);
initScriptFunction(cx, scope);
if (idata.itsRegExpLiterals != null) {
functionRegExps = createRegExpWraps(cx, scope);
}
}
public String getFunctionName()
{
return (idata.itsName == null) ? "" : idata.itsName;
}
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
Object[] args)
{

View File

@ -48,19 +48,11 @@ import org.mozilla.javascript.debug.DebuggableScript;
public abstract class NativeFunction extends BaseFunction
{
public final void initScriptFunction(Context cx, Scriptable scope,
String functionName)
public final void initScriptFunction(Context cx, Scriptable scope)
{
this.functionName = functionName;
ScriptRuntime.setFunctionProtoAndParent(this, scope);
}
public final void initScriptObject()
{
this.functionName = "";
}
/**
* @param indent How much to indent the decompiled result
*

View File

@ -56,11 +56,11 @@ import java.io.*;
public class NativeJavaConstructor extends BaseFunction
{
MemberBox ctor;
public NativeJavaConstructor(MemberBox ctor)
{
this.ctor = ctor;
String sig = JavaMembers.liveConnectSignature(ctor.argTypes);
this.functionName = "<init>".concat(sig);
}
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
@ -69,11 +69,15 @@ public class NativeJavaConstructor extends BaseFunction
return NativeJavaClass.constructSpecific(cx, scope, args, ctor);
}
public String getFunctionName()
{
String sig = JavaMembers.liveConnectSignature(ctor.argTypes);
return "<init>".concat(sig);
}
public String toString()
{
return "[JavaConstructor " + ctor.getName() + "]";
}
MemberBox ctor;
}

View File

@ -70,6 +70,11 @@ public class NativeJavaMethod extends BaseFunction
this(new MemberBox(method, null), name);
}
public String getFunctionName()
{
return functionName;
}
static String scriptSignature(Object[] values)
{
StringBuffer sig = new StringBuffer();
@ -148,7 +153,7 @@ public class NativeJavaMethod extends BaseFunction
int index = findFunction(cx, methods, args);
if (index < 0) {
Class c = methods[0].method().getDeclaringClass();
String sig = c.getName() + '.' + functionName + '(' +
String sig = c.getName() + '.' + getFunctionName() + '(' +
scriptSignature(args) + ')';
throw Context.reportRuntimeError1("msg.java.no_such_method", sig);
}
@ -177,7 +182,7 @@ public class NativeJavaMethod extends BaseFunction
for (;;) {
if (o == null) {
throw Context.reportRuntimeError3(
"msg.nonjava.method", functionName,
"msg.nonjava.method", getFunctionName(),
ScriptRuntime.toString(thisObj), c.getName());
}
if (o instanceof Wrapper) {
@ -467,5 +472,6 @@ public class NativeJavaMethod extends BaseFunction
}
MemberBox[] methods;
private String functionName;
}

View File

@ -3034,7 +3034,7 @@ public class ScriptRuntime {
boolean fromEvalCode)
{
if (type == FunctionNode.FUNCTION_STATEMENT) {
String name = function.functionName;
String name = function.getFunctionName();
if (name != null && name.length() != 0) {
if (!fromEvalCode) {
// ECMA specifies that functions defined in global and
@ -3046,7 +3046,7 @@ public class ScriptRuntime {
}
}
} else if (type == FunctionNode.FUNCTION_EXPRESSION_STATEMENT) {
String name = function.functionName;
String name = function.getFunctionName();
if (name != null && name.length() != 0) {
// Always put function expression statements into initial
// activation object ignoring the with statement to follow

View File

@ -553,13 +553,6 @@ public class Codegen extends Interpreter
cfw.addPush(0);
cfw.add(ByteCode.PUTFIELD, cfw.getClassName(), ID_FIELD_NAME, "I");
// Call
// NativeFunction.initScriptObject(version, varNamesArray)
cfw.addLoadThis();
cfw.addInvoke(ByteCode.INVOKEVIRTUAL,
"org/mozilla/javascript/NativeFunction",
"initScriptObject", "()V");
cfw.add(ByteCode.RETURN);
// 1 parameter = this
cfw.stopMethod((short)1);
@ -635,13 +628,11 @@ public class Codegen extends Interpreter
cfw.addLoadThis();
cfw.addALoad(CONTEXT_ARG);
cfw.addALoad(SCOPE_ARG);
cfw.addPush(ofn.fnode.getFunctionName());
cfw.addInvoke(ByteCode.INVOKEVIRTUAL,
"org/mozilla/javascript/NativeFunction",
"initScriptFunction",
"(Lorg/mozilla/javascript/Context;"
+"Lorg/mozilla/javascript/Scriptable;"
+"Ljava/lang/String;"
+")V");
// precompile all regexp literals
@ -676,11 +667,12 @@ public class Codegen extends Interpreter
// The rest of NativeFunction overrides require specific code for each
// script/function id
final int Do_getParamCount = 0;
final int Do_getParamAndVarCount = 1;
final int Do_getParamOrVarName = 2;
final int Do_getEncodedSource = 3;
final int SWITCH_COUNT = 4;
final int Do_getFunctionName = 0;
final int Do_getParamCount = 1;
final int Do_getParamAndVarCount = 2;
final int Do_getParamOrVarName = 3;
final int Do_getEncodedSource = 4;
final int SWITCH_COUNT = 5;
for (int methodIndex = 0; methodIndex != SWITCH_COUNT; ++methodIndex) {
if (methodIndex == Do_getEncodedSource && encodedSource == null) {
@ -694,6 +686,11 @@ public class Codegen extends Interpreter
short metodLocals;
switch (methodIndex) {
case Do_getFunctionName:
metodLocals = 1; // Only this
cfw.startMethod("getFunctionName", "()Ljava/lang/String;",
ClassFileWriter.ACC_PUBLIC);
break;
case Do_getParamCount:
metodLocals = 1; // Only this
cfw.startMethod("getParamCount", "()I",
@ -748,6 +745,17 @@ public class Codegen extends Interpreter
// Impelemnet method-specific switch code
switch (methodIndex) {
case Do_getFunctionName:
// Push function name
if (n.getType() == Token.SCRIPT) {
cfw.addPush("");
} else {
String name = ((FunctionNode)n).getFunctionName();
cfw.addPush(name);
}
cfw.add(ByteCode.ARETURN);
break;
case Do_getParamCount:
// Push number of defined parameters
cfw.addPush(n.getParamCount());

View File

@ -57,7 +57,11 @@ class NativeRegExpCtor extends BaseFunction {
NativeRegExpCtor()
{
functionName = "RegExp";
}
public String getFunctionName()
{
return "RegExp";
}
public Object call(Context cx, Scriptable scope, Scriptable thisObj,