1. I fixed a recent regression that caused "arguments" array to be constructed before initialization of standard objects: Global.init() should be called in procesFiles as well.

2. I replaced a couple of anonymous classes implementing ContextAction by Use single IProxy class to shrink compiled code size.
This commit is contained in:
igor%mir2.org 2004-09-29 05:41:00 +00:00
Родитель d6f015e13a
Коммит 2194596ed9
2 изменённых файлов: 69 добавлений и 42 удалений

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

@ -940,7 +940,7 @@ class Runner implements Runnable, ContextAction {
public void run() public void run()
{ {
Main.withContext(this); Main.shellContextFactory.call(this);
} }
public Object run(Context cx) public Object run(Context cx)

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

@ -60,6 +60,38 @@ public class Main
public static final ShellContextFactory public static final ShellContextFactory
shellContextFactory = new ShellContextFactory(); shellContextFactory = new ShellContextFactory();
/**
* Proxy class to avoid proliferation of anonymous classes.
*/
private static class IProxy implements ContextAction
{
private static final int PROCESS_FILES = 1;
private static final int EVAL_INLINE_SCRIPT = 2;
private int type;
String[] args;
String scriptText;
IProxy(int type)
{
this.type = type;
}
public Object run(Context cx)
{
if (type == PROCESS_FILES) {
processFiles(cx, args);
} else if (type == EVAL_INLINE_SCRIPT) {
evaluateScript(cx, getGlobal(), null, scriptText,
"<command>", 1, null);
} else {
throw Kit.codeBug();
}
return null;
}
}
/** /**
* Main entry point. * Main entry point.
* *
@ -97,45 +129,42 @@ public class Main
} }
} }
global = getGlobal();
errorReporter = new ToolErrorReporter(false, global.getErr()); errorReporter = new ToolErrorReporter(false, global.getErr());
shellContextFactory.setErrorReporter(errorReporter); shellContextFactory.setErrorReporter(errorReporter);
final String[] args = processOptions(origArgs); String[] args = processOptions(origArgs);
if (processStdin) if (processStdin)
fileList.addElement(null); fileList.addElement(null);
withContext(new ContextAction() { IProxy iproxy = new IProxy(IProxy.PROCESS_FILES);
public Object run(Context cx) iproxy.args = args;
{ shellContextFactory.call(iproxy);
// define "arguments" array in the top-level object:
// need to allocate new array since newArray requires instances
// of exactly Object[], not ObjectSubclass[]
Object[] array = new Object[args.length];
System.arraycopy(args, 0, array, 0, args.length);
Scriptable argsObj = cx.newArray(global, array);
global.defineProperty("arguments", argsObj,
ScriptableObject.DONTENUM);
for (int i=0; i < fileList.size(); i++) {
processSource(cx, (String) fileList.elementAt(i));
}
return null;
}
});
return exitCode; return exitCode;
} }
public static Global getGlobal() { static void processFiles(Context cx, String[] args)
if (global == null) { {
global = new Global(); if (!global.initialized) {
global.init(cx);
} }
return global; // define "arguments" array in the top-level object:
// need to allocate new array since newArray requires instances
// of exactly Object[], not ObjectSubclass[]
Object[] array = new Object[args.length];
System.arraycopy(args, 0, array, 0, args.length);
Scriptable argsObj = cx.newArray(global, array);
global.defineProperty("arguments", argsObj,
ScriptableObject.DONTENUM);
for (int i=0; i < fileList.size(); i++) {
processSource(cx, (String) fileList.elementAt(i));
}
} }
static Object withContext(ContextAction action) { public static Global getGlobal()
return shellContextFactory.call(action); {
return global;
} }
/** /**
@ -216,15 +245,9 @@ public class Main
usageError = arg; usageError = arg;
break goodUsage; break goodUsage;
} }
final String scriptText = args[i]; IProxy iproxy = new IProxy(IProxy.EVAL_INLINE_SCRIPT);
withContext(new ContextAction() { iproxy.scriptText = args[i];
public Object run(Context cx) shellContextFactory.call(iproxy);
{
evaluateScript(cx, global, null, scriptText,
"<command>", 1, null);
return null;
}
});
continue; continue;
} }
if (arg.equals("-w")) { if (arg.equals("-w")) {
@ -254,7 +277,8 @@ public class Main
return null; return null;
} }
private static void initJavaPolicySecuritySupport() { private static void initJavaPolicySecuritySupport()
{
Throwable exObj; Throwable exObj;
try { try {
Class cl = Class.forName Class cl = Class.forName
@ -282,7 +306,8 @@ public class Main
* @param filename the name of the file to compile, or null * @param filename the name of the file to compile, or null
* for interactive mode. * for interactive mode.
*/ */
public static void processSource(Context cx, String filename) { public static void processSource(Context cx, String filename)
{
if (filename == null || filename.equals("-")) { if (filename == null || filename.equals("-")) {
if (filename == null) { if (filename == null) {
// print implementation version // print implementation version
@ -335,7 +360,9 @@ public class Main
h.put((int)h.getLength(), h, source); h.put((int)h.getLength(), h, source);
} }
global.getErr().println(); global.getErr().println();
} else processFile(cx, global, filename); } else {
processFile(cx, global, filename);
}
System.gc(); System.gc();
} }
@ -344,7 +371,7 @@ public class Main
{ {
if (securityImpl == null) { if (securityImpl == null) {
processFileSecure(cx, scope, filename, null); processFileSecure(cx, scope, filename, null);
}else { } else {
securityImpl.callProcessFileSecure(cx, scope, filename); securityImpl.callProcessFileSecure(cx, scope, filename);
} }
} }
@ -490,8 +517,8 @@ public class Main
Global.getInstance(getGlobal()).setErr(err); Global.getInstance(getGlobal()).setErr(err);
} }
static protected final Global global = new Global();
static protected ToolErrorReporter errorReporter; static protected ToolErrorReporter errorReporter;
static protected Global global;
static protected int exitCode = 0; static protected int exitCode = 0;
static private final int EXITCODE_RUNTIME_ERROR = 3; static private final int EXITCODE_RUNTIME_ERROR = 3;
static private final int EXITCODE_FILE_NOT_FOUND = 4; static private final int EXITCODE_FILE_NOT_FOUND = 4;