Store optimization level and language version in ShellContextFactory

so all Context instances created on other threads or from JavaAdapter
calls would have proper setup.
This commit is contained in:
igor%mir2.org 2004-09-28 21:58:01 +00:00
Родитель 1f3b183f24
Коммит 31962091d1
2 изменённых файлов: 106 добавлений и 54 удалений

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

@ -86,7 +86,8 @@ public class Main
/** /**
* Execute the given arguments, but don't System.exit at the end. * Execute the given arguments, but don't System.exit at the end.
*/ */
public static int exec(final String origArgs[]) { public static int exec(String origArgs[])
{
for (int i=0; i < origArgs.length; i++) { for (int i=0; i < origArgs.length; i++) {
String arg = origArgs[i]; String arg = origArgs[i];
@ -96,19 +97,16 @@ public class Main
} }
} }
global = getGlobal();
errorReporter = new ToolErrorReporter(false, global.getErr());
shellContextFactory.setErrorReporter(errorReporter);
final String[] args = processOptions(origArgs);
if (processStdin)
fileList.addElement(null);
withContext(new ContextAction() { withContext(new ContextAction() {
public Object run(Context cx) public Object run(Context cx)
{ {
// Create the top-level scope object.
global = getGlobal();
errorReporter = new ToolErrorReporter(false, global.getErr());
cx.setErrorReporter(errorReporter);
String[] args = processOptions(cx, origArgs);
if (processStdin)
fileList.addElement(null);
// define "arguments" array in the top-level object: // define "arguments" array in the top-level object:
// need to allocate new array since newArray requires instances // need to allocate new array since newArray requires instances
// of exactly Object[], not ObjectSubclass[] // of exactly Object[], not ObjectSubclass[]
@ -136,27 +134,20 @@ public class Main
return global; return global;
} }
static Object withContext(final ContextAction action) { static Object withContext(ContextAction action) {
ContextAction wrap;
if (securityImpl == null) {
wrap = action;
} else {
wrap = new ContextAction() {
public Object run(Context cx)
{
cx.setSecurityController(securityImpl);
return action.run(cx);
}
};
}
return shellContextFactory.call(action); return shellContextFactory.call(action);
} }
/** /**
* Parse arguments. * Parse arguments.
*/ */
public static String[] processOptions(Context cx, String args[]) { public static String[] processOptions(String args[])
for (int i=0; i < args.length; i++) { {
String usageError;
goodUsage: for (int i = 0; ; ++i) {
if (i == args.length) {
return new String[0];
}
String arg = args[i]; String arg = args[i];
if (!arg.startsWith("-")) { if (!arg.startsWith("-")) {
processStdin = false; processStdin = false;
@ -166,27 +157,45 @@ public class Main
return result; return result;
} }
if (arg.equals("-version")) { if (arg.equals("-version")) {
if (++i == args.length) if (++i == args.length) {
usage(arg); usageError = arg;
double d = cx.toNumber(args[i]); break goodUsage;
if (d != d) }
usage(arg); int version;
cx.setLanguageVersion((int) d); try {
version = Integer.parseInt(args[i]);
} catch (NumberFormatException ex) {
usageError = args[i];
break goodUsage;
}
if (!Context.isValidLanguageVersion(version)) {
usageError = args[i];
break goodUsage;
}
shellContextFactory.setLanguageVersion(version);
continue; continue;
} }
if (arg.equals("-opt") || arg.equals("-O")) { if (arg.equals("-opt") || arg.equals("-O")) {
if (++i == args.length) if (++i == args.length) {
usage(arg); usageError = arg;
double d = cx.toNumber(args[i]); break goodUsage;
if (d != d) }
usage(arg); int opt;
int opt = (int)d; try {
opt = Integer.parseInt(args[i]);
} catch (NumberFormatException ex) {
usageError = args[i];
break goodUsage;
}
if (opt == -2) { if (opt == -2) {
// Compatibility with Cocoon Rhino fork // Compatibility with Cocoon Rhino fork
shellContextFactory.setEnableContinuations(true); shellContextFactory.setEnableContinuations(true);
opt = -1; opt = -1;
} else if (!Context.isValidOptimizationLevel(opt)) {
usageError = args[i];
break goodUsage;
} }
cx.setOptimizationLevel(opt); shellContextFactory.setOptimizationLevel(opt);
if (opt >= 0) { if (opt >= 0) {
shellContextFactory.setEnableContinuations(false); shellContextFactory.setEnableContinuations(false);
} }
@ -198,14 +207,24 @@ public class Main
} }
if (arg.equals("-continuations")) { if (arg.equals("-continuations")) {
shellContextFactory.setEnableContinuations(true); shellContextFactory.setEnableContinuations(true);
cx.setOptimizationLevel(-1); shellContextFactory.setOptimizationLevel(-1);
continue; continue;
} }
if (arg.equals("-e")) { if (arg.equals("-e")) {
processStdin = false; processStdin = false;
if (++i == args.length) if (++i == args.length) {
usage(arg); usageError = arg;
evaluateScript(cx, global, null, args[i], "<command>", 1, null); break goodUsage;
}
final String scriptText = args[i];
withContext(new ContextAction() {
public Object run(Context cx)
{
evaluateScript(cx, global, null, scriptText,
"<command>", 1, null);
return null;
}
});
continue; continue;
} }
if (arg.equals("-w")) { if (arg.equals("-w")) {
@ -214,8 +233,10 @@ public class Main
} }
if (arg.equals("-f")) { if (arg.equals("-f")) {
processStdin = false; processStdin = false;
if (++i == args.length) if (++i == args.length) {
usage(arg); usageError = arg;
break goodUsage;
}
fileList.addElement(args[i].equals("-") ? null : args[i]); fileList.addElement(args[i].equals("-") ? null : args[i]);
continue; continue;
} }
@ -224,17 +245,13 @@ public class Main
if (!sealedStdLib) Kit.codeBug(); if (!sealedStdLib) Kit.codeBug();
continue; continue;
} }
usage(arg); usageError = arg;
break goodUsage;
} }
return new String[0]; // print usage message
} p(ToolErrorReporter.getMessage("msg.shell.usage", usageError));
/**
* Print a usage message.
*/
public static void usage(String s) {
p(ToolErrorReporter.getMessage("msg.shell.usage", s));
System.exit(1); System.exit(1);
return null;
} }
private static void initJavaPolicySecuritySupport() { private static void initJavaPolicySecuritySupport() {
@ -243,6 +260,7 @@ public class Main
Class cl = Class.forName Class cl = Class.forName
("org.mozilla.javascript.tools.shell.JavaPolicySecurity"); ("org.mozilla.javascript.tools.shell.JavaPolicySecurity");
securityImpl = (SecurityProxy)cl.newInstance(); securityImpl = (SecurityProxy)cl.newInstance();
SecurityController.initGlobal(securityImpl);
return; return;
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
exObj = ex; exObj = ex;

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

@ -36,11 +36,15 @@
package org.mozilla.javascript.tools.shell; package org.mozilla.javascript.tools.shell;
import org.mozilla.javascript.*; import org.mozilla.javascript.*;
import org.mozilla.javascript.tools.ToolErrorReporter;
public class ShellContextFactory extends ContextFactory public class ShellContextFactory extends ContextFactory
{ {
private boolean enableContinuations; private boolean enableContinuations;
private boolean strictMode; private boolean strictMode;
private int languageVersion;
private int optimizationLevel;
private ErrorReporter errorReporter;
protected boolean hasFeature(Context cx, int featureIndex) protected boolean hasFeature(Context cx, int featureIndex)
{ {
@ -53,6 +57,16 @@ public class ShellContextFactory extends ContextFactory
return super.hasFeature(cx, featureIndex); return super.hasFeature(cx, featureIndex);
} }
protected void onContextCreated(Context cx)
{
cx.setLanguageVersion(languageVersion);
cx.setOptimizationLevel(optimizationLevel);
if (errorReporter != null) {
cx.setErrorReporter(errorReporter);
}
super.onContextCreated(cx);
}
public void setEnableContinuations(boolean flag) public void setEnableContinuations(boolean flag)
{ {
checkNotSealed(); checkNotSealed();
@ -65,4 +79,24 @@ public class ShellContextFactory extends ContextFactory
this.strictMode = flag; this.strictMode = flag;
} }
public void setLanguageVersion(int version)
{
Context.checkLanguageVersion(version);
checkNotSealed();
this.languageVersion = version;
}
public void setOptimizationLevel(int optimizationLevel)
{
Context.checkOptimizationLevel(optimizationLevel);
checkNotSealed();
this.optimizationLevel = optimizationLevel;
}
public void setErrorReporter(ErrorReporter errorReporter)
{
if (errorReporter == null) throw new IllegalArgumentException();
this.errorReporter = errorReporter;
}
} }