updated Global, Main and ImporterTopLevel
   Date:
        Mon, 23 Oct 2000 14:37:45 +0100
   From:
        Matthias Radestock <matthias@lshift.net>
     To:
        nboyd@atg.com




Norris,

I've made some more changes to shell.Main and shell.Global in order to
reduce their mutual dependency, enable "quit" and get "load" to operate
in the local scope.

see attachments for updated .diffs.


Matthias.
This commit is contained in:
nboyd%atg.com 2000-10-23 15:55:42 +00:00
Родитель 26cffce30e
Коммит 006574d547
6 изменённых файлов: 174 добавлений и 136 удалений

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

@ -20,6 +20,7 @@
*
* Contributor(s):
* Norris Boyd
* Matthias Radestock
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
@ -88,57 +89,79 @@ public class ImporterTopLevel extends ScriptableObject {
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result == NOT_FOUND && importedPackages != null) {
for (int i=0; i < importedPackages.size(); i++) {
Object o = importedPackages.elementAt(i);
NativeJavaPackage p = (NativeJavaPackage) o;
Object v = p.getPkgProperty(name, start, false);
if (v != null && !(v instanceof NativeJavaPackage)) {
if (result == NOT_FOUND) {
result = v;
} else {
String[] args = { result.toString(), v.toString() };
throw Context.reportRuntimeError(
Context.getMessage("msg.ambig.import",
args));
}
if (result != NOT_FOUND)
return result;
if (name.equals("_packages_"))
return result;
Object plist = ScriptableObject.getProperty(start,"_packages_");
if (plist == NOT_FOUND)
return result;
Context cx = Context.enter();
Object[] elements = cx.getElements((Scriptable)plist);
Context.exit();
for (int i=0; i < elements.length; i++) {
NativeJavaPackage p = (NativeJavaPackage) elements[i];
Object v = p.getPkgProperty(name, start, false);
if (v != null && !(v instanceof NativeJavaPackage)) {
if (result == NOT_FOUND) {
result = v;
} else {
String[] args = { result.toString(), v.toString() };
throw Context.reportRuntimeError(
Context.getMessage("msg.ambig.import",
args));
}
}
}
return result;
return result;
}
public void importClass(Object cl) {
if (!(cl instanceof NativeJavaClass)) {
String[] args = { Context.toString(cl) };
throw Context.reportRuntimeError(
Context.getMessage("msg.not.class", args));
public static void importClass(Context cx, Scriptable thisObj,
Object[] args, Function funObj) {
for (int i=0; i<args.length; i++) {
Object cl = args[i];
if (!(cl instanceof NativeJavaClass)) {
String[] eargs = { Context.toString(cl) };
throw Context.reportRuntimeError(Context.getMessage("msg.not.class", eargs));
}
String s = ((NativeJavaClass) cl).getClassObject().getName();
String n = s.substring(s.lastIndexOf('.')+1);
Object val = thisObj.get(n, thisObj);
if (val != NOT_FOUND && val != cl) {
String[] eargs = { n };
throw Context.reportRuntimeError(Context.getMessage("msg.prop.defined", eargs));
}
//thisObj.defineProperty(n, cl, DONTENUM);
thisObj.put(n,thisObj,cl);
}
String s = ((NativeJavaClass) cl).getClassObject().getName();
String n = s.substring(s.lastIndexOf('.')+1);
Object val = this.get(n, this);
if (val != NOT_FOUND && val != cl) {
String[] args = { n };
throw Context.reportRuntimeError(
Context.getMessage("msg.prop.defined", args));
}
this.defineProperty(n, cl, DONTENUM);
}
public void importPackage(Object pkg) {
if (importedPackages == null)
importedPackages = new Vector();
if (!(pkg instanceof NativeJavaPackage)) {
String[] args = { Context.toString(pkg) };
throw Context.reportRuntimeError(
Context.getMessage("msg.not.pkg", args));
public static void importPackage(Context cx, Scriptable thisObj,
Object[] args, Function funObj) {
Scriptable importedPackages;
Object plist = thisObj.get("_packages_", thisObj);
if (plist == NOT_FOUND) {
importedPackages = cx.newArray(thisObj,0);
thisObj.put("_packages_", thisObj, importedPackages);
}
for (int i=0; i < importedPackages.size(); i++) {
if (pkg == importedPackages.elementAt(i))
return; // allready in list
else {
importedPackages = (Scriptable)plist;
}
for (int i=0; i<args.length; i++) {
Object pkg = args[i];
if (!(pkg instanceof NativeJavaPackage)) {
String[] eargs = { Context.toString(pkg) };
throw Context.reportRuntimeError(Context.getMessage("msg.not.pkg", eargs));
}
Object[] elements = cx.getElements(importedPackages);
for (int j=0; j < elements.length; j++) {
if (pkg == elements[j]) {
pkg = null;
break;
}
}
if (pkg != null)
importedPackages.put(elements.length,importedPackages,pkg);
}
importedPackages.addElement(pkg);
}
private Vector importedPackages;
}

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

@ -23,6 +23,7 @@
* Norris Boyd
* Rob Ginda
* Kurt Westerfeld
* Matthias Radestock
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
@ -132,12 +133,8 @@ public class Global extends ImporterTopLevel {
Object[] args, Function funObj)
{
Main.global.exitCode = 0;
if (args.length > 0)
Main.global.exitCode = (int) Context.toNumber(args[0]);
Main.global.quitting = true;
System.exit((args.length > 0) ?
((int) Context.toNumber(args[0])) : 0);
}
/**
@ -166,7 +163,7 @@ public class Global extends ImporterTopLevel {
Object[] args, Function funObj)
{
for (int i=0; i < args.length; i++) {
Main.processSource(cx, cx.toString(args[i]));
Main.processFile(cx, thisObj, cx.toString(args[i]));
}
}
@ -194,7 +191,7 @@ public class Global extends ImporterTopLevel {
PropertyException
{
Class clazz = getClass(args);
ScriptableObject.defineClass(Main.global, clazz);
ScriptableObject.defineClass(thisObj, clazz);
}
/**
@ -227,7 +224,7 @@ public class Global extends ImporterTopLevel {
"msg.must.implement.Script"));
}
Script script = (Script) clazz.newInstance();
script.exec(cx, Main.global);
script.exec(cx, thisObj);
}
private static Class getClass(Object[] args)
@ -284,8 +281,6 @@ public class Global extends ImporterTopLevel {
}
boolean processStdin = true;
boolean quitting;
int exitCode = 0;
NativeArray history;
}

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

@ -110,7 +110,7 @@ public class Main {
processSource(cx, args.length == 0 ? null : args[0]);
cx.exit();
return global.exitCode;
return exitCode;
}
/**
@ -201,7 +201,7 @@ public class Main {
(new InputStreamReader(Main.getIn()));
int lineno = 1;
boolean hitEOF = false;
while (!hitEOF && !global.quitting) {
while (!hitEOF) {
int startline = lineno;
if (filename == null)
getErr().print("js> ");
@ -244,13 +244,15 @@ public class Main {
}
NativeArray h = global.history;
h.put((int)h.jsGet_length(), h, source);
if (global.quitting) {
// The user executed the quit() function.
break;
}
}
getErr().println();
} else {
} else processFile(cx, global, filename);
System.gc();
}
public static void processFile(Context cx, Scriptable scope,
String filename)
{
Reader in = null;
try {
in = new PushbackReader(new FileReader(filename));
@ -278,7 +280,7 @@ public class Main {
Context.reportError(ToolErrorReporter.getMessage(
"msg.couldnt.open",
filename));
global.exitCode = EXITCODE_FILE_NOT_FOUND;
exitCode = EXITCODE_FILE_NOT_FOUND;
return;
} catch (IOException ioe) {
getErr().println(ioe.toString());
@ -287,11 +289,9 @@ public class Main {
// Here we evalute the entire contents of the file as
// a script. Text is printed only if the print() function
// is called.
evaluateReader(cx, global, in, filename, 1);
}
System.gc();
evaluateReader(cx, scope, in, filename, 1);
}
public static Object evaluateReader(Context cx, Scriptable scope,
Reader in, String sourceName,
int lineno)
@ -307,7 +307,7 @@ public class Main {
catch (EcmaError ee) {
String msg = ToolErrorReporter.getMessage(
"msg.uncaughtJSException", ee.toString());
global.exitCode = EXITCODE_RUNTIME_ERROR;
exitCode = EXITCODE_RUNTIME_ERROR;
if (ee.getSourceName() != null) {
Context.reportError(msg, ee.getSourceName(),
ee.getLineNumber(),
@ -319,14 +319,14 @@ public class Main {
}
catch (EvaluatorException ee) {
// Already printed message.
global.exitCode = EXITCODE_RUNTIME_ERROR;
exitCode = EXITCODE_RUNTIME_ERROR;
}
catch (JavaScriptException jse) {
// Need to propagate ThreadDeath exceptions.
Object value = jse.getValue();
if (value instanceof ThreadDeath)
throw (ThreadDeath) value;
global.exitCode = EXITCODE_RUNTIME_ERROR;
exitCode = EXITCODE_RUNTIME_ERROR;
Context.reportError(ToolErrorReporter.getMessage(
"msg.uncaughtJSException",
jse.getMessage()));
@ -381,6 +381,7 @@ public class Main {
static protected ToolErrorReporter errorReporter;
static protected Global global;
static protected int exitCode = 0;
static public InputStream inStream;
static public PrintStream outStream;
static public PrintStream errStream;

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

@ -20,6 +20,7 @@
*
* Contributor(s):
* Norris Boyd
* Matthias Radestock
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
@ -88,57 +89,79 @@ public class ImporterTopLevel extends ScriptableObject {
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result == NOT_FOUND && importedPackages != null) {
for (int i=0; i < importedPackages.size(); i++) {
Object o = importedPackages.elementAt(i);
NativeJavaPackage p = (NativeJavaPackage) o;
Object v = p.getPkgProperty(name, start, false);
if (v != null && !(v instanceof NativeJavaPackage)) {
if (result == NOT_FOUND) {
result = v;
} else {
String[] args = { result.toString(), v.toString() };
throw Context.reportRuntimeError(
Context.getMessage("msg.ambig.import",
args));
}
if (result != NOT_FOUND)
return result;
if (name.equals("_packages_"))
return result;
Object plist = ScriptableObject.getProperty(start,"_packages_");
if (plist == NOT_FOUND)
return result;
Context cx = Context.enter();
Object[] elements = cx.getElements((Scriptable)plist);
Context.exit();
for (int i=0; i < elements.length; i++) {
NativeJavaPackage p = (NativeJavaPackage) elements[i];
Object v = p.getPkgProperty(name, start, false);
if (v != null && !(v instanceof NativeJavaPackage)) {
if (result == NOT_FOUND) {
result = v;
} else {
String[] args = { result.toString(), v.toString() };
throw Context.reportRuntimeError(
Context.getMessage("msg.ambig.import",
args));
}
}
}
return result;
return result;
}
public void importClass(Object cl) {
if (!(cl instanceof NativeJavaClass)) {
String[] args = { Context.toString(cl) };
throw Context.reportRuntimeError(
Context.getMessage("msg.not.class", args));
public static void importClass(Context cx, Scriptable thisObj,
Object[] args, Function funObj) {
for (int i=0; i<args.length; i++) {
Object cl = args[i];
if (!(cl instanceof NativeJavaClass)) {
String[] eargs = { Context.toString(cl) };
throw Context.reportRuntimeError(Context.getMessage("msg.not.class", eargs));
}
String s = ((NativeJavaClass) cl).getClassObject().getName();
String n = s.substring(s.lastIndexOf('.')+1);
Object val = thisObj.get(n, thisObj);
if (val != NOT_FOUND && val != cl) {
String[] eargs = { n };
throw Context.reportRuntimeError(Context.getMessage("msg.prop.defined", eargs));
}
//thisObj.defineProperty(n, cl, DONTENUM);
thisObj.put(n,thisObj,cl);
}
String s = ((NativeJavaClass) cl).getClassObject().getName();
String n = s.substring(s.lastIndexOf('.')+1);
Object val = this.get(n, this);
if (val != NOT_FOUND && val != cl) {
String[] args = { n };
throw Context.reportRuntimeError(
Context.getMessage("msg.prop.defined", args));
}
this.defineProperty(n, cl, DONTENUM);
}
public void importPackage(Object pkg) {
if (importedPackages == null)
importedPackages = new Vector();
if (!(pkg instanceof NativeJavaPackage)) {
String[] args = { Context.toString(pkg) };
throw Context.reportRuntimeError(
Context.getMessage("msg.not.pkg", args));
public static void importPackage(Context cx, Scriptable thisObj,
Object[] args, Function funObj) {
Scriptable importedPackages;
Object plist = thisObj.get("_packages_", thisObj);
if (plist == NOT_FOUND) {
importedPackages = cx.newArray(thisObj,0);
thisObj.put("_packages_", thisObj, importedPackages);
}
for (int i=0; i < importedPackages.size(); i++) {
if (pkg == importedPackages.elementAt(i))
return; // allready in list
else {
importedPackages = (Scriptable)plist;
}
for (int i=0; i<args.length; i++) {
Object pkg = args[i];
if (!(pkg instanceof NativeJavaPackage)) {
String[] eargs = { Context.toString(pkg) };
throw Context.reportRuntimeError(Context.getMessage("msg.not.pkg", eargs));
}
Object[] elements = cx.getElements(importedPackages);
for (int j=0; j < elements.length; j++) {
if (pkg == elements[j]) {
pkg = null;
break;
}
}
if (pkg != null)
importedPackages.put(elements.length,importedPackages,pkg);
}
importedPackages.addElement(pkg);
}
private Vector importedPackages;
}

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

@ -23,6 +23,7 @@
* Norris Boyd
* Rob Ginda
* Kurt Westerfeld
* Matthias Radestock
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
@ -132,12 +133,8 @@ public class Global extends ImporterTopLevel {
Object[] args, Function funObj)
{
Main.global.exitCode = 0;
if (args.length > 0)
Main.global.exitCode = (int) Context.toNumber(args[0]);
Main.global.quitting = true;
System.exit((args.length > 0) ?
((int) Context.toNumber(args[0])) : 0);
}
/**
@ -166,7 +163,7 @@ public class Global extends ImporterTopLevel {
Object[] args, Function funObj)
{
for (int i=0; i < args.length; i++) {
Main.processSource(cx, cx.toString(args[i]));
Main.processFile(cx, thisObj, cx.toString(args[i]));
}
}
@ -194,7 +191,7 @@ public class Global extends ImporterTopLevel {
PropertyException
{
Class clazz = getClass(args);
ScriptableObject.defineClass(Main.global, clazz);
ScriptableObject.defineClass(thisObj, clazz);
}
/**
@ -227,7 +224,7 @@ public class Global extends ImporterTopLevel {
"msg.must.implement.Script"));
}
Script script = (Script) clazz.newInstance();
script.exec(cx, Main.global);
script.exec(cx, thisObj);
}
private static Class getClass(Object[] args)
@ -284,8 +281,6 @@ public class Global extends ImporterTopLevel {
}
boolean processStdin = true;
boolean quitting;
int exitCode = 0;
NativeArray history;
}

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

@ -110,7 +110,7 @@ public class Main {
processSource(cx, args.length == 0 ? null : args[0]);
cx.exit();
return global.exitCode;
return exitCode;
}
/**
@ -201,7 +201,7 @@ public class Main {
(new InputStreamReader(Main.getIn()));
int lineno = 1;
boolean hitEOF = false;
while (!hitEOF && !global.quitting) {
while (!hitEOF) {
int startline = lineno;
if (filename == null)
getErr().print("js> ");
@ -244,13 +244,15 @@ public class Main {
}
NativeArray h = global.history;
h.put((int)h.jsGet_length(), h, source);
if (global.quitting) {
// The user executed the quit() function.
break;
}
}
getErr().println();
} else {
} else processFile(cx, global, filename);
System.gc();
}
public static void processFile(Context cx, Scriptable scope,
String filename)
{
Reader in = null;
try {
in = new PushbackReader(new FileReader(filename));
@ -278,7 +280,7 @@ public class Main {
Context.reportError(ToolErrorReporter.getMessage(
"msg.couldnt.open",
filename));
global.exitCode = EXITCODE_FILE_NOT_FOUND;
exitCode = EXITCODE_FILE_NOT_FOUND;
return;
} catch (IOException ioe) {
getErr().println(ioe.toString());
@ -287,11 +289,9 @@ public class Main {
// Here we evalute the entire contents of the file as
// a script. Text is printed only if the print() function
// is called.
evaluateReader(cx, global, in, filename, 1);
}
System.gc();
evaluateReader(cx, scope, in, filename, 1);
}
public static Object evaluateReader(Context cx, Scriptable scope,
Reader in, String sourceName,
int lineno)
@ -307,7 +307,7 @@ public class Main {
catch (EcmaError ee) {
String msg = ToolErrorReporter.getMessage(
"msg.uncaughtJSException", ee.toString());
global.exitCode = EXITCODE_RUNTIME_ERROR;
exitCode = EXITCODE_RUNTIME_ERROR;
if (ee.getSourceName() != null) {
Context.reportError(msg, ee.getSourceName(),
ee.getLineNumber(),
@ -319,14 +319,14 @@ public class Main {
}
catch (EvaluatorException ee) {
// Already printed message.
global.exitCode = EXITCODE_RUNTIME_ERROR;
exitCode = EXITCODE_RUNTIME_ERROR;
}
catch (JavaScriptException jse) {
// Need to propagate ThreadDeath exceptions.
Object value = jse.getValue();
if (value instanceof ThreadDeath)
throw (ThreadDeath) value;
global.exitCode = EXITCODE_RUNTIME_ERROR;
exitCode = EXITCODE_RUNTIME_ERROR;
Context.reportError(ToolErrorReporter.getMessage(
"msg.uncaughtJSException",
jse.getMessage()));
@ -381,6 +381,7 @@ public class Main {
static protected ToolErrorReporter errorReporter;
static protected Global global;
static protected int exitCode = 0;
static public InputStream inStream;
static public PrintStream outStream;
static public PrintStream errStream;