зеркало из https://github.com/mozilla/pjs.git
This patch changes NativeError.java to store message and name properties
in fields of the object itself instead of using the standard property hashtable in ScriptableObject.java. This saves 3 object instances per NativeError (2 slot entries and hashtable array itself) and given the fact that NativeGlobal defines a few permanent Error instances, it is visible saving even after taking into account code size increase. The change also gives a good test of IdScriptable implementation. ----- This patch introduces the uniform decompile method for NativeFunction and IdFunction with the signature: public String decompile(Context cx, int indent, boolean justbody) instead of NativeFunction.decompile(int indent, boolean toplevel, boolean justbody) and IdScriptable.toStringForScript(Context cx) and replaces the special treatment of NativeJavaMethod in NativeFunction.jsFunction_toString by overriding decompile in NativeJavaMethod ----- This patch adds getFunctionName to NativeFunction to return function name and replaces few places with jsGet_name usage by getFunctionName The patch was made via diff -ru javascript.0 javascript > name_patch from org/mozilla directory
This commit is contained in:
Родитель
c6fed8f237
Коммит
59a66dee02
|
@ -916,7 +916,7 @@ public class Context {
|
|||
{
|
||||
NativeScript ns = (NativeScript) script;
|
||||
ns.initScript(scope);
|
||||
return ns.decompile(indent, true, false);
|
||||
return ns.decompile(this, indent, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -934,7 +934,9 @@ public class Context {
|
|||
*/
|
||||
public String decompileFunction(Function fun, int indent) {
|
||||
if (fun instanceof NativeFunction)
|
||||
return ((NativeFunction)fun).decompile(indent, true, false);
|
||||
return ((NativeFunction)fun).decompile(this, indent, false);
|
||||
else if (fun instanceof IdFunction)
|
||||
return ((IdFunction)fun).decompile(this, indent, false);
|
||||
else
|
||||
return "function " + fun.getClassName() +
|
||||
"() {\n\t[native code]\n}\n";
|
||||
|
@ -955,7 +957,9 @@ public class Context {
|
|||
*/
|
||||
public String decompileFunctionBody(Function fun, int indent) {
|
||||
if (fun instanceof NativeFunction)
|
||||
return ((NativeFunction)fun).decompile(indent, true, true);
|
||||
return ((NativeFunction)fun).decompile(this, indent, true);
|
||||
else if (fun instanceof IdFunction)
|
||||
return ((IdFunction)fun).decompile(this, indent, true);
|
||||
else
|
||||
// not sure what the right response here is. JSRef currently
|
||||
// dumps core.
|
||||
|
|
|
@ -164,11 +164,14 @@ public class IdFunction extends ScriptableObject implements Function
|
|||
return (Scriptable) protoVal;
|
||||
}
|
||||
|
||||
protected Object toStringForScript(Context cx) {
|
||||
public String decompile(Context cx, int indent, boolean justbody) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("function ");
|
||||
sb.append(methodName);
|
||||
sb.append("() { [native code for ");
|
||||
if (!justbody) {
|
||||
sb.append("function ");
|
||||
sb.append(methodName);
|
||||
sb.append("() { ");
|
||||
}
|
||||
sb.append("[native code for ");
|
||||
if (master instanceof Scriptable) {
|
||||
Scriptable smaster = (Scriptable)master;
|
||||
sb.append(smaster.getClassName());
|
||||
|
@ -177,7 +180,7 @@ public class IdFunction extends ScriptableObject implements Function
|
|||
sb.append(methodName);
|
||||
sb.append(", arity=");
|
||||
sb.append(getArity());
|
||||
sb.append("] }");
|
||||
sb.append(justbody ? "]\n" : "] }\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -48,11 +48,40 @@ public class NativeError extends IdScriptable {
|
|||
public static void init(Context cx, Scriptable scope, boolean sealed) {
|
||||
NativeError obj = new NativeError();
|
||||
obj.prototypeFlag = true;
|
||||
obj.defineProperty("message", "", ScriptableObject.EMPTY);
|
||||
obj.defineProperty("name", "Error", ScriptableObject.EMPTY);
|
||||
obj.messageValue = "";
|
||||
obj.nameValue = "Error";
|
||||
obj.addAsPrototype(MAX_PROTOTYPE_ID, cx, scope, sealed);
|
||||
}
|
||||
|
||||
protected int getIdDefaultAttributes(int id) {
|
||||
if (id == Id_message || id == Id_name) { return EMPTY; }
|
||||
return super.getIdDefaultAttributes(id);
|
||||
}
|
||||
|
||||
protected boolean hasIdValue(int id) {
|
||||
if (id == Id_message) { return messageValue != NOT_FOUND; }
|
||||
if (id == Id_name) { return nameValue != NOT_FOUND; }
|
||||
return super.hasIdValue(id);
|
||||
}
|
||||
|
||||
protected Object getIdValue(int id) {
|
||||
if (id == Id_message) { return messageValue; }
|
||||
if (id == Id_name) { return nameValue; }
|
||||
return super.getIdValue(id);
|
||||
}
|
||||
|
||||
protected void setIdValue(int id, Object value) {
|
||||
if (id == Id_message) { messageValue = value; return; }
|
||||
if (id == Id_name) { nameValue = value; return; }
|
||||
super.setIdValue(id, value);
|
||||
}
|
||||
|
||||
protected void deleteIdValue(int id) {
|
||||
if (id == Id_message) { messageValue = NOT_FOUND; return; }
|
||||
if (id == Id_name) { nameValue = NOT_FOUND; return; }
|
||||
super.deleteIdValue(id);
|
||||
}
|
||||
|
||||
public int methodArity(int methodId) {
|
||||
if (prototypeFlag) {
|
||||
if (methodId == Id_constructor) return 1;
|
||||
|
@ -71,7 +100,7 @@ public class NativeError extends IdScriptable {
|
|||
return jsConstructor(cx, args, f, thisObj == null);
|
||||
}
|
||||
else if (methodId == Id_toString) {
|
||||
return realThis(thisObj, f).jsFunction_toString();
|
||||
return realThis(thisObj, f).toString();
|
||||
}
|
||||
}
|
||||
return super.execMethod(methodId, f, cx, scope, thisObj, args);
|
||||
|
@ -89,7 +118,7 @@ public class NativeError extends IdScriptable {
|
|||
{
|
||||
NativeError result = new NativeError();
|
||||
if (args.length >= 1)
|
||||
result.put("message", result, cx.toString(args[0]));
|
||||
result.messageValue = ScriptRuntime.toString(args[0]);
|
||||
result.setPrototype(getClassPrototype(funObj, "Error"));
|
||||
return result;
|
||||
}
|
||||
|
@ -102,21 +131,23 @@ public class NativeError extends IdScriptable {
|
|||
return getName() + ": " + getMessage();
|
||||
}
|
||||
|
||||
private String jsFunction_toString() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return ScriptRuntime.toString(
|
||||
ScriptRuntime.getProp(this, "name", this));
|
||||
Object val = nameValue;
|
||||
return ScriptRuntime.toString(val != NOT_FOUND ? val
|
||||
: Undefined.instance);
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return ScriptRuntime.toString(
|
||||
ScriptRuntime.getProp(this, "message", this));
|
||||
Object val = messageValue;
|
||||
return ScriptRuntime.toString(val != NOT_FOUND ? val
|
||||
: Undefined.instance);
|
||||
}
|
||||
|
||||
protected int maxInstanceId() { return MAX_INSTANCE_ID; }
|
||||
|
||||
protected String getIdName(int id) {
|
||||
if (id == Id_message) { return "message"; }
|
||||
if (id == Id_name) { return "name"; }
|
||||
if (prototypeFlag) {
|
||||
if (id == Id_constructor) return "constructor";
|
||||
if (id == Id_toString) return "toString";
|
||||
|
@ -126,10 +157,28 @@ public class NativeError extends IdScriptable {
|
|||
|
||||
// #string_id_map#
|
||||
|
||||
private static final int
|
||||
Id_message = 1,
|
||||
Id_name = 2,
|
||||
|
||||
MAX_INSTANCE_ID = 2;
|
||||
|
||||
protected int mapNameToId(String s) {
|
||||
if (!prototypeFlag) { return 0; }
|
||||
int id;
|
||||
// #generated# Last update: 2001-04-23 10:03:24 CEST
|
||||
// #generated# Last update: 2001-05-19 21:55:23 CEST
|
||||
L0: { id = 0; String X = null;
|
||||
int s_length = s.length();
|
||||
if (s_length==4) { X="name";id=Id_name; }
|
||||
else if (s_length==7) { X="message";id=Id_message; }
|
||||
if (X!=null && X!=s && !X.equals(s)) id = 0;
|
||||
}
|
||||
// #/generated#
|
||||
// #/string_id_map#
|
||||
|
||||
if (id != 0 || !prototypeFlag) { return id; }
|
||||
|
||||
// #string_id_map#
|
||||
// #generated# Last update: 2001-05-19 21:55:23 CEST
|
||||
L0: { id = 0; String X = null;
|
||||
int s_length = s.length();
|
||||
if (s_length==8) { X="toString";id=Id_toString; }
|
||||
|
@ -141,11 +190,15 @@ public class NativeError extends IdScriptable {
|
|||
}
|
||||
|
||||
private static final int
|
||||
Id_constructor = 1,
|
||||
Id_toString = 2,
|
||||
MAX_PROTOTYPE_ID = 2;
|
||||
Id_constructor = MAX_INSTANCE_ID + 1,
|
||||
Id_toString = MAX_INSTANCE_ID + 2,
|
||||
|
||||
MAX_PROTOTYPE_ID = MAX_INSTANCE_ID + 2;
|
||||
|
||||
// #/string_id_map#
|
||||
|
||||
private Object messageValue = NOT_FOUND;
|
||||
private Object nameValue = NOT_FOUND;
|
||||
|
||||
private boolean prototypeFlag;
|
||||
}
|
||||
|
|
|
@ -186,20 +186,36 @@ public class NativeFunction extends ScriptableObject implements Function {
|
|||
* between function header and function body that rhino
|
||||
* decompilation does not.
|
||||
*
|
||||
* @param indent How much to indent the decompiled result
|
||||
* @param cx Cuirrent context
|
||||
*
|
||||
* @param toplevel Whether this is the first call or a recursive
|
||||
* call of decompile. (Not whether the function is defined at the
|
||||
* top level of script evaluation.)
|
||||
* @param indent How much to indent the decompiled result
|
||||
*
|
||||
* @param justbody Whether the decompilation should omit the
|
||||
* function header and trailing brace.
|
||||
*/
|
||||
|
||||
public String decompile(int indent, boolean toplevel, boolean justbody) {
|
||||
if (source == null)
|
||||
return "function " + jsGet_name() +
|
||||
"() {\n\t[native code]\n}\n";
|
||||
public String decompile(Context cx, int indent, boolean justbody) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
decompile(indent, true, justbody, result);
|
||||
return result.toString();
|
||||
|
||||
}
|
||||
|
||||
private void decompile(int indent, boolean toplevel, boolean justbody,
|
||||
StringBuffer result)
|
||||
{
|
||||
if (source == null) {
|
||||
if (!justbody) {
|
||||
result.append("function ");
|
||||
result.append(getFunctionName());
|
||||
result.append("() {\n\t");
|
||||
}
|
||||
result.append("[native code]\n");
|
||||
if (!justbody) {
|
||||
result.append("}\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Spew tokens in source, for debugging.
|
||||
// as TYPE number char
|
||||
|
@ -224,8 +240,6 @@ public class NativeFunction extends ScriptableObject implements Function {
|
|||
System.err.println();
|
||||
}
|
||||
|
||||
StringBuffer result = new StringBuffer();
|
||||
|
||||
int i = 0;
|
||||
|
||||
if (source.length() > 0) {
|
||||
|
@ -408,9 +422,8 @@ public class NativeFunction extends ScriptableObject implements Function {
|
|||
}
|
||||
throw Context.reportRuntimeError(message);
|
||||
}
|
||||
result.append(nestedFunctions[functionNumber].decompile(indent,
|
||||
false,
|
||||
false));
|
||||
nestedFunctions[functionNumber].
|
||||
decompile(indent, false, false, result);
|
||||
break;
|
||||
}
|
||||
case TokenStream.COMMA:
|
||||
|
@ -827,29 +840,20 @@ public class NativeFunction extends ScriptableObject implements Function {
|
|||
// add that trailing newline if it's an outermost function.
|
||||
if (toplevel && !justbody)
|
||||
result.append('\n');
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static Object jsFunction_toString(Context cx, Scriptable thisObj,
|
||||
Object[] args, Function funObj)
|
||||
{
|
||||
int indent = ScriptRuntime.toInt32(args, 0);
|
||||
Object val = thisObj.getDefaultValue(ScriptRuntime.FunctionClass);
|
||||
if (val instanceof NativeFunction) {
|
||||
NativeFunction funVal = (NativeFunction)val;
|
||||
if (funVal instanceof NativeJavaMethod) {
|
||||
return "\nfunction " + funVal.jsGet_name() +
|
||||
"() {/*\n" + val.toString() + "*/}\n";
|
||||
}
|
||||
int indent = 0;
|
||||
if (args.length > 0)
|
||||
indent = (int)ScriptRuntime.toNumber(args[0]);
|
||||
|
||||
return funVal.decompile(indent, true, false);
|
||||
return funVal.decompile(cx, indent, false);
|
||||
}
|
||||
else if (val instanceof IdFunction) {
|
||||
IdFunction funVal = (IdFunction)val;
|
||||
return funVal.toStringForScript(cx);
|
||||
return funVal.decompile(cx, indent, false);
|
||||
}
|
||||
throw NativeGlobal.typeError1
|
||||
("msg.incompat.call", "toString", thisObj);
|
||||
|
@ -970,7 +974,7 @@ public class NativeFunction extends ScriptableObject implements Function {
|
|||
return argCount;
|
||||
}
|
||||
|
||||
public String jsGet_name() {
|
||||
public String getFunctionName() {
|
||||
if (functionName == null)
|
||||
return "";
|
||||
if (functionName.equals("anonymous")) {
|
||||
|
@ -981,6 +985,10 @@ public class NativeFunction extends ScriptableObject implements Function {
|
|||
return functionName;
|
||||
}
|
||||
|
||||
public String jsGet_name() {
|
||||
return getFunctionName();
|
||||
}
|
||||
|
||||
private NativeCall getActivation(Context cx) {
|
||||
NativeCall activation = cx.currentActivation;
|
||||
while (activation != null) {
|
||||
|
|
|
@ -153,15 +153,32 @@ public class NativeJavaMethod extends NativeFunction implements Function {
|
|||
}
|
||||
}
|
||||
|
||||
public String decompile(Context cx, int indent, boolean justbody) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (!justbody) {
|
||||
sb.append("function ");
|
||||
sb.append(getFunctionName());
|
||||
sb.append("() {");
|
||||
}
|
||||
sb.append("/*\n");
|
||||
toString(sb);
|
||||
sb.append(justbody ? "*/\n" : "*/}\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
toString(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void toString(StringBuffer sb) {
|
||||
for (int i=0; i < methods.length; i++) {
|
||||
sb.append(javaSignature(methods[i].getReturnType()));
|
||||
sb.append(' ');
|
||||
sb.append(signature(methods[i]));
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
|
||||
|
|
|
@ -134,10 +134,14 @@ public class NativeScript extends NativeFunction implements Script {
|
|||
return cx.decompileScript(thisScript, scope, 0);
|
||||
}
|
||||
|
||||
public String jsGet_name() {
|
||||
return getFunctionName();
|
||||
}
|
||||
|
||||
/*
|
||||
* Override method in NativeFunction to avoid ever returning "anonymous"
|
||||
*/
|
||||
public String jsGet_name() {
|
||||
public String getFunctionName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
|
|
@ -1968,14 +1968,14 @@ public class ScriptRuntime {
|
|||
result.setPrototype(ScriptableObject.getClassPrototype(scope, "Function"));
|
||||
result.setParentScope(scope);
|
||||
|
||||
String fnName = result.jsGet_name();
|
||||
String fnName = result.getFunctionName();
|
||||
if (setName && fnName != null && fnName.length() != 0 &&
|
||||
!fnName.equals("anonymous"))
|
||||
{
|
||||
setProp(scope, fnName, result, scope);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
static void checkDeprecated(Context cx, String name) {
|
||||
|
|
|
@ -273,7 +273,7 @@ public class NativeRegExp extends ScriptableObject implements Function {
|
|||
Object[] args, Scriptable scopeObj,
|
||||
int matchType, Function funObj) {
|
||||
if (!(thisObj instanceof NativeRegExp)) {
|
||||
Object[] errArgs = { ((NativeFunction) funObj).jsGet_name() };
|
||||
Object[] errArgs = { ((NativeFunction) funObj).getFunctionName() };
|
||||
throw NativeGlobal.constructError(
|
||||
cx, "TypeError",
|
||||
ScriptRuntime.getMessage(
|
||||
|
|
Загрузка…
Ссылка в новой задаче