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
Родитель 409cbe7e5f
Коммит 22cf07ef28
10 изменённых файлов: 71 добавлений и 44 удалений

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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