1. Added QuitAction interface to specify the action to perform when quit is called. By default Global does nothing and shell explicitly call initQuitAction with code to call System.exit().
2. Added toint32 to be on pair with SpiderMonkey git-svn-id: svn://10.0.0.236/trunk@181274 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
fe5161dd9f
commit
720dcc0348
@ -57,6 +57,14 @@ public class Global extends ImporterTopLevel
|
||||
{
|
||||
static final long serialVersionUID = 4029130780977538005L;
|
||||
|
||||
NativeArray history;
|
||||
private InputStream inStream;
|
||||
private PrintStream outStream;
|
||||
private PrintStream errStream;
|
||||
private boolean sealedStdLib = false;
|
||||
boolean initialized;
|
||||
private QuitAction quitAction;
|
||||
|
||||
public Global()
|
||||
{
|
||||
}
|
||||
@ -66,6 +74,19 @@ public class Global extends ImporterTopLevel
|
||||
init(cx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the action to call from quit().
|
||||
*/
|
||||
public void initQuitAction(QuitAction quitAction)
|
||||
{
|
||||
if (quitAction == null)
|
||||
throw new IllegalArgumentException("quitAction is null");
|
||||
if (this.quitAction != null)
|
||||
throw new IllegalArgumentException("The method is once-call.");
|
||||
|
||||
this.quitAction = quitAction;
|
||||
}
|
||||
|
||||
public void init(ContextFactory factory)
|
||||
{
|
||||
factory.call(new ContextAction() {
|
||||
@ -82,10 +103,24 @@ public class Global extends ImporterTopLevel
|
||||
// Define some global functions particular to the shell. Note
|
||||
// that these functions are not part of ECMA.
|
||||
initStandardObjects(cx, sealedStdLib);
|
||||
String[] names = { "print", "quit", "version", "load", "help",
|
||||
"loadClass", "defineClass", "spawn", "sync",
|
||||
"serialize", "deserialize", "runCommand",
|
||||
"seal", "readFile", "readUrl" };
|
||||
String[] names = {
|
||||
"defineClass",
|
||||
"deserialize",
|
||||
"help",
|
||||
"load",
|
||||
"loadClass",
|
||||
"print",
|
||||
"quit",
|
||||
"readFile",
|
||||
"readUrl",
|
||||
"runCommand",
|
||||
"seal",
|
||||
"serialize",
|
||||
"spawn",
|
||||
"sync",
|
||||
"toint32",
|
||||
"version",
|
||||
};
|
||||
defineFunctionProperties(names, Global.class,
|
||||
ScriptableObject.DONTENUM);
|
||||
|
||||
@ -140,18 +175,20 @@ public class Global extends ImporterTopLevel
|
||||
}
|
||||
|
||||
/**
|
||||
* Quit the shell.
|
||||
*
|
||||
* This only affects the interactive mode.
|
||||
* Call embedding-specific quit action passing its argument as
|
||||
* int32 exit code.
|
||||
*
|
||||
* This method is defined as a JavaScript function.
|
||||
*/
|
||||
public static void quit(Context cx, Scriptable thisObj,
|
||||
Object[] args, Function funObj)
|
||||
{
|
||||
|
||||
System.exit((args.length > 0) ?
|
||||
((int) Context.toNumber(args[0])) : 0);
|
||||
Global global = getInstance(funObj);
|
||||
if (global.quitAction != null) {
|
||||
int exitCode = (args.length == 0 ? 0
|
||||
: ScriptRuntime.toInt32(args[0]));
|
||||
global.quitAction.quit(cx, exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -590,6 +627,18 @@ public class Global extends ImporterTopLevel
|
||||
return readUrl(url, charCoding, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the argumnet to int32 number.
|
||||
*/
|
||||
public static Object toint32(Context cx, Scriptable thisObj, Object[] args,
|
||||
Function funObj)
|
||||
{
|
||||
Object arg = (args.length != 0 ? args[0] : Undefined.instance);
|
||||
if (arg instanceof Integer)
|
||||
return arg;
|
||||
return ScriptRuntime.wrapInt(ScriptRuntime.toInt32(arg));
|
||||
}
|
||||
|
||||
public InputStream getIn() {
|
||||
return inStream == null ? System.in : inStream;
|
||||
}
|
||||
@ -936,13 +985,6 @@ public class Global extends ImporterTopLevel
|
||||
String message = ToolErrorReporter.getMessage(msgId, msgArg);
|
||||
return Context.reportRuntimeError(message);
|
||||
}
|
||||
|
||||
NativeArray history;
|
||||
private InputStream inStream;
|
||||
private PrintStream outStream;
|
||||
private PrintStream errStream;
|
||||
private boolean sealedStdLib = false;
|
||||
boolean initialized;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -60,13 +60,27 @@ public class Main
|
||||
public static final ShellContextFactory
|
||||
shellContextFactory = new ShellContextFactory();
|
||||
|
||||
static protected final Global global = new Global();
|
||||
static protected ToolErrorReporter errorReporter;
|
||||
static protected int exitCode = 0;
|
||||
static private final int EXITCODE_RUNTIME_ERROR = 3;
|
||||
static private final int EXITCODE_FILE_NOT_FOUND = 4;
|
||||
static boolean processStdin = true;
|
||||
static Vector fileList = new Vector(5);
|
||||
private static SecurityProxy securityImpl;
|
||||
|
||||
static {
|
||||
global.initQuitAction(new IProxy(IProxy.SYSTEM_EXIT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy class to avoid proliferation of anonymous classes.
|
||||
*/
|
||||
private static class IProxy implements ContextAction
|
||||
private static class IProxy implements ContextAction, QuitAction
|
||||
{
|
||||
private static final int PROCESS_FILES = 1;
|
||||
private static final int EVAL_INLINE_SCRIPT = 2;
|
||||
private static final int SYSTEM_EXIT = 3;
|
||||
|
||||
private int type;
|
||||
String[] args;
|
||||
@ -92,6 +106,15 @@ public class Main
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void quit(Context cx, int exitCode)
|
||||
{
|
||||
if (type == SYSTEM_EXIT) {
|
||||
System.exit(exitCode);
|
||||
return;
|
||||
}
|
||||
throw Kit.codeBug();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -586,12 +609,4 @@ public class Main
|
||||
return result;
|
||||
}
|
||||
|
||||
static protected final Global global = new Global();
|
||||
static protected ToolErrorReporter errorReporter;
|
||||
static protected int exitCode = 0;
|
||||
static private final int EXITCODE_RUNTIME_ERROR = 3;
|
||||
static private final int EXITCODE_FILE_NOT_FOUND = 4;
|
||||
static boolean processStdin = true;
|
||||
static Vector fileList = new Vector(5);
|
||||
private static SecurityProxy securityImpl;
|
||||
}
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is Rhino code, released
|
||||
* May 6, 1998.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Igor Bukanov
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* terms of the GNU Public License (the "GPL"), in which case the
|
||||
* provisions of the GPL are applicable instead of those above.
|
||||
* If you wish to allow use of your version of this file only
|
||||
* under the terms of the GPL and not to allow others to use your
|
||||
* version of this file under the NPL, indicate your decision by
|
||||
* deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this
|
||||
* file under either the NPL or the GPL.
|
||||
*/
|
||||
|
||||
package org.mozilla.javascript.tools.shell;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
|
||||
/**
|
||||
* Defines action to perform in response to quit command.
|
||||
*/
|
||||
public interface QuitAction
|
||||
{
|
||||
public void quit(Context cx, int exitCode);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user