Two submissions from Kurt Westerfield <kurt@westerfield.com>:

Subject:
        Embedding the shell
   Date:
        Wed, 8 Sep 1999 16:01:44 -0400
   From:
        "Kurt Westerfeld" <kurt@westerfeld.com>
     To:
        "Norris Boyd" <norris@netscape.com>




Norris, please find the attached zip file containing the (minor)
modifications to the Rhino shell module that enables the shell to be
embedded in a host application.

There are two areas of change to be concerned about:

    1. Any and all references to System.in/out/err have been modified to use
Main.in/out/err, which default to System.in/out/err.  Methods to do a setIn,
setOut, and setErr were added.   Note that in/out/err on Main were made
static public, so that the jikes compiler wouldn't complain (I had them as
static protected, but when accessed outside of the package, a warning was
issued).

    2.  The global and sharedGlobal static variables were made protected so
that my app can make use of them (to add extensions after an initial pass
through main()).

That's it.

I have successfully used the facility to drop a remote telnet server into
the shell interpreter, effectively giving our server a remote shell
interpreter.  It's quite nice, as we have a lot of extensions to Rhino
written that blend into our server already.

PS, I am still working on the array issues, but made a lot of progress
today.  I just wanted to get this stuff off my desk.

Thanks!

________________________________________________________________________
  Kurt Westerfeld
  Managed Object Solutions
  2722 Merrilee Drive
  Suite 350
  Fairfax, VA 22031
  Phone: 703.208.3330 x225
  Fax: 703.208.3331
  http://www.mosol.com
  mailto:kurt@mosol.com



   shell.zip

             Name:
                   shell.zip
             Type:
                   Zip Compressed Data (application/x-zip-compressed)
          Encoding:
                   base64


==============================================================================

Subject:
        Rhino Array Source (Fixed)
   Date:
        Thu, 9 Sep 1999 14:12:03 -0400
   From:
        "Kurt Westerfeld" <kurt@mosol.com>
     To:
        "Norris Boyd" <norris@netscape.com>




Attached is NativeJavaObject.java, which seems to now pass the tests supplied to me by you and Scott.  Not a lot of change, but a lot of
testing and thinking was involved. <g>

PS. I also fixed a bug in reportConversionError() which was throwing an IllegalArgumentException inside of the MessageFormat class at times.
It also looks a little nicer (uses formatting from NativeJavaMethod) and closer to the C implementation.




   NativeJavaObject.java

                        Name:
                              NativeJavaObject.java
                         Type:
                              Java Source File (text/java)
                     Encoding:
                              quoted-printable
This commit is contained in:
norris%netscape.com 1999-09-09 18:49:58 +00:00
Родитель 6b2c541366
Коммит 34c9c62f33
7 изменённых файлов: 162 добавлений и 64 удалений

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

@ -366,6 +366,12 @@ public class NativeJavaObject implements Scriptable, Wrapper {
jsObjectClass.isAssignableFrom(to)) {
result = 1;
}
else if (fromObj instanceof NativeArray && to.isArray()) {
// This is a native array conversion to a java array
// Array conversions are all equal, and preferable to object
// and string conversion, per LC3.
result = 1;
}
else if (to == ScriptRuntime.ObjectClass) {
result = 2;
}
@ -465,7 +471,7 @@ public class NativeJavaObject implements Scriptable, Wrapper {
if (value != null && value.getClass() == type) {
return value;
}
switch (NativeJavaObject.getJSTypeCode(value)) {
case JSTYPE_NULL:
@ -611,6 +617,25 @@ public class NativeJavaObject implements Scriptable, Wrapper {
else if (type.isInstance(value)) {
return value;
}
else if (type.isArray() && value instanceof NativeArray) {
// Make a new java array, and coerce the JS array components
// to the target (component) type.
NativeArray array = (NativeArray) value;
long length = array.jsGet_length();
Class arrayType = type.getComponentType();
Object Result = Array.newInstance(arrayType, (int)length);
for (int i = 0 ; i < length ; ++i) {
try {
Array.set(Result, i, coerceType(arrayType,
array.get(i, array)));
}
catch (EvaluatorException ee) {
reportConversionError(value, type);
}
}
return Result;
}
else {
reportConversionError(value, type);
}
@ -812,8 +837,11 @@ public class NativeJavaObject implements Scriptable, Wrapper {
}
static void reportConversionError(Object value, Class type) {
Object[] args = {value, type};
throw Context.reportRuntimeError(Context.getMessage("msg.conversion.not.allowed", args));
Object[] args = { Context.toString(value),
NativeJavaMethod.javaSignature(type)
};
throw Context.reportRuntimeError(
Context.getMessage("msg.conversion.not.allowed", args));
}
public static void initJSObject() {

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

@ -31,7 +31,12 @@ import java.util.*;
public class ToolErrorReporter implements ErrorReporter {
public ToolErrorReporter(boolean reportWarnings) {
this.reportWarnings = reportWarnings;
this(reportWarnings, System.err);
}
public ToolErrorReporter(boolean reportWarnings, PrintStream err) {
this.reportWarnings = reportWarnings;
this.err = err;
}
/**
@ -79,10 +84,10 @@ public class ToolErrorReporter implements ErrorReporter {
return;
Object[] errArgs = { formatMessage(message, sourceName, line) };
message = getMessage("msg.warning", errArgs);
System.err.println(messagePrefix + message);
err.println(messagePrefix + message);
if (null != lineSource) {
System.err.println(messagePrefix + lineSource);
System.err.println(messagePrefix + buildIndicator(lineOffset));
err.println(messagePrefix + lineSource);
err.println(messagePrefix + buildIndicator(lineOffset));
}
}
@ -91,10 +96,10 @@ public class ToolErrorReporter implements ErrorReporter {
{
hasReportedErrorFlag = true;
message = formatMessage(message, sourceName, line);
System.err.println(messagePrefix + message);
err.println(messagePrefix + message);
if (null != lineSource) {
System.err.println(messagePrefix + lineSource);
System.err.println(messagePrefix + buildIndicator(lineOffset));
err.println(messagePrefix + lineSource);
err.println(messagePrefix + buildIndicator(lineOffset));
}
}
@ -144,4 +149,5 @@ public class ToolErrorReporter implements ErrorReporter {
private final String messagePrefix = "js: ";
private boolean hasReportedErrorFlag;
private boolean reportWarnings;
private PrintStream err;
}

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

@ -53,7 +53,7 @@ public class Main extends ScriptableObject {
public static void main(String args[]) {
Context cx = Context.enter();
errorReporter = new ToolErrorReporter(false);
errorReporter = new ToolErrorReporter(false, err);
cx.setErrorReporter(errorReporter);
// A bit of shorthand: since Main extends ScriptableObject,
@ -104,7 +104,7 @@ public class Main extends ScriptableObject {
global.debug_dm.createdContext(cx);
if (global.showDebuggerUI) {
System.out.println("Launching JSDebugger...");
out.println("Launching JSDebugger...");
try {
Class clazz = Class.forName(
@ -115,11 +115,11 @@ public class Main extends ScriptableObject {
} catch (Exception e) {
// eat it...
System.out.println(e);
System.out.println("Failed to launch the JSDebugger");
out.println(e);
out.println("Failed to launch the JSDebugger");
}
}
System.out.println("Debug level set to "+cx.getDebugLevel());
out.println("Debug level set to "+cx.getDebugLevel());
}
*/
@ -248,7 +248,7 @@ public class Main extends ScriptableObject {
SourceTextManager stm = cx.getSourceTextManager();
if (filename == null || filename.equals("-")) {
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
(new InputStreamReader(Main.in));
if(null != stm)
in = new DebugReader(in, stm, "<stdin>");
int lineno = 1;
@ -256,8 +256,8 @@ public class Main extends ScriptableObject {
while (!hitEOF && !global.quitting) {
int startline = lineno;
if (filename == null)
System.err.print("js> ");
System.err.flush();
err.print("js> ");
err.flush();
try {
String source = "";
@ -278,7 +278,7 @@ public class Main extends ScriptableObject {
"<stdin>", startline,
null);
if (result != cx.getUndefinedValue()) {
System.err.println(cx.toString(result));
err.println(cx.toString(result));
}
NativeArray h = global.history;
h.put((int)h.jsGet_length(), h, source);
@ -286,7 +286,7 @@ public class Main extends ScriptableObject {
catch (WrappedException we) {
// Some form of exception was caught by JavaScript and
// propagated up.
System.err.println(we.getWrappedException().toString());
err.println(we.getWrappedException().toString());
we.printStackTrace();
}
catch (EvaluatorException ee) {
@ -303,14 +303,14 @@ public class Main extends ScriptableObject {
jse.getMessage()));
}
catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
if (global.quitting) {
// The user executed the quit() function.
break;
}
}
System.err.println();
err.println();
} else {
Reader in = null;
try {
@ -343,7 +343,7 @@ public class Main extends ScriptableObject {
filename));
return;
} catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
try {
@ -353,7 +353,7 @@ public class Main extends ScriptableObject {
cx.evaluateReader(global, in, filename, 1, null);
}
catch (WrappedException we) {
System.err.println(we.getWrappedException().toString());
err.println(we.getWrappedException().toString());
we.printStackTrace();
}
catch (EvaluatorException ee) {
@ -369,14 +369,14 @@ public class Main extends ScriptableObject {
jse.getMessage()));
}
catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
finally {
try {
in.close();
}
catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
}
}
@ -384,12 +384,27 @@ public class Main extends ScriptableObject {
}
private static void p(String s) {
System.out.println(s);
out.println(s);
}
static ToolErrorReporter errorReporter;
static Main global;
static SharedGlobal sharedGlobal;
public static void setIn(InputStream _in) {
in = _in;
}
public static void setOut(PrintStream _out) {
out = _out;
}
public static void setErr(PrintStream _err) {
err = _err;
}
static protected ToolErrorReporter errorReporter;
static protected Main global;
static protected SharedGlobal sharedGlobal;
static public InputStream in = System.in;
static public PrintStream out = System.out;
static public PrintStream err = System.err;
boolean quitting;
SourceTextManager debug_stm;

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

@ -61,7 +61,7 @@ public class SharedGlobal extends ImporterTopLevel {
* This method is defined as a JavaScript function.
*/
public static void help(String s) {
System.out.println(ToolErrorReporter.getMessage("msg.help"));
Main.out.println(ToolErrorReporter.getMessage("msg.help"));
}
/**
@ -78,16 +78,16 @@ public class SharedGlobal extends ImporterTopLevel {
{
for (int i=0; i < args.length; i++) {
if (i > 0)
System.out.print(" ");
Main.out.print(" ");
// Convert the
// arbitrary JavaScript value into a string form.
String s = Context.toString(args[i]);
System.out.print(s);
Main.out.print(s);
}
System.out.println();
Main.out.println();
return Context.getUndefinedValue();
}

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

@ -366,6 +366,12 @@ public class NativeJavaObject implements Scriptable, Wrapper {
jsObjectClass.isAssignableFrom(to)) {
result = 1;
}
else if (fromObj instanceof NativeArray && to.isArray()) {
// This is a native array conversion to a java array
// Array conversions are all equal, and preferable to object
// and string conversion, per LC3.
result = 1;
}
else if (to == ScriptRuntime.ObjectClass) {
result = 2;
}
@ -465,7 +471,7 @@ public class NativeJavaObject implements Scriptable, Wrapper {
if (value != null && value.getClass() == type) {
return value;
}
switch (NativeJavaObject.getJSTypeCode(value)) {
case JSTYPE_NULL:
@ -611,6 +617,25 @@ public class NativeJavaObject implements Scriptable, Wrapper {
else if (type.isInstance(value)) {
return value;
}
else if (type.isArray() && value instanceof NativeArray) {
// Make a new java array, and coerce the JS array components
// to the target (component) type.
NativeArray array = (NativeArray) value;
long length = array.jsGet_length();
Class arrayType = type.getComponentType();
Object Result = Array.newInstance(arrayType, (int)length);
for (int i = 0 ; i < length ; ++i) {
try {
Array.set(Result, i, coerceType(arrayType,
array.get(i, array)));
}
catch (EvaluatorException ee) {
reportConversionError(value, type);
}
}
return Result;
}
else {
reportConversionError(value, type);
}
@ -812,8 +837,11 @@ public class NativeJavaObject implements Scriptable, Wrapper {
}
static void reportConversionError(Object value, Class type) {
Object[] args = {value, type};
throw Context.reportRuntimeError(Context.getMessage("msg.conversion.not.allowed", args));
Object[] args = { Context.toString(value),
NativeJavaMethod.javaSignature(type)
};
throw Context.reportRuntimeError(
Context.getMessage("msg.conversion.not.allowed", args));
}
public static void initJSObject() {

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

@ -31,7 +31,12 @@ import java.util.*;
public class ToolErrorReporter implements ErrorReporter {
public ToolErrorReporter(boolean reportWarnings) {
this.reportWarnings = reportWarnings;
this(reportWarnings, System.err);
}
public ToolErrorReporter(boolean reportWarnings, PrintStream err) {
this.reportWarnings = reportWarnings;
this.err = err;
}
/**
@ -79,10 +84,10 @@ public class ToolErrorReporter implements ErrorReporter {
return;
Object[] errArgs = { formatMessage(message, sourceName, line) };
message = getMessage("msg.warning", errArgs);
System.err.println(messagePrefix + message);
err.println(messagePrefix + message);
if (null != lineSource) {
System.err.println(messagePrefix + lineSource);
System.err.println(messagePrefix + buildIndicator(lineOffset));
err.println(messagePrefix + lineSource);
err.println(messagePrefix + buildIndicator(lineOffset));
}
}
@ -91,10 +96,10 @@ public class ToolErrorReporter implements ErrorReporter {
{
hasReportedErrorFlag = true;
message = formatMessage(message, sourceName, line);
System.err.println(messagePrefix + message);
err.println(messagePrefix + message);
if (null != lineSource) {
System.err.println(messagePrefix + lineSource);
System.err.println(messagePrefix + buildIndicator(lineOffset));
err.println(messagePrefix + lineSource);
err.println(messagePrefix + buildIndicator(lineOffset));
}
}
@ -144,4 +149,5 @@ public class ToolErrorReporter implements ErrorReporter {
private final String messagePrefix = "js: ";
private boolean hasReportedErrorFlag;
private boolean reportWarnings;
private PrintStream err;
}

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

@ -53,7 +53,7 @@ public class Main extends ScriptableObject {
public static void main(String args[]) {
Context cx = Context.enter();
errorReporter = new ToolErrorReporter(false);
errorReporter = new ToolErrorReporter(false, err);
cx.setErrorReporter(errorReporter);
// A bit of shorthand: since Main extends ScriptableObject,
@ -104,7 +104,7 @@ public class Main extends ScriptableObject {
global.debug_dm.createdContext(cx);
if (global.showDebuggerUI) {
System.out.println("Launching JSDebugger...");
out.println("Launching JSDebugger...");
try {
Class clazz = Class.forName(
@ -115,11 +115,11 @@ public class Main extends ScriptableObject {
} catch (Exception e) {
// eat it...
System.out.println(e);
System.out.println("Failed to launch the JSDebugger");
out.println(e);
out.println("Failed to launch the JSDebugger");
}
}
System.out.println("Debug level set to "+cx.getDebugLevel());
out.println("Debug level set to "+cx.getDebugLevel());
}
*/
@ -248,7 +248,7 @@ public class Main extends ScriptableObject {
SourceTextManager stm = cx.getSourceTextManager();
if (filename == null || filename.equals("-")) {
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
(new InputStreamReader(Main.in));
if(null != stm)
in = new DebugReader(in, stm, "<stdin>");
int lineno = 1;
@ -256,8 +256,8 @@ public class Main extends ScriptableObject {
while (!hitEOF && !global.quitting) {
int startline = lineno;
if (filename == null)
System.err.print("js> ");
System.err.flush();
err.print("js> ");
err.flush();
try {
String source = "";
@ -278,7 +278,7 @@ public class Main extends ScriptableObject {
"<stdin>", startline,
null);
if (result != cx.getUndefinedValue()) {
System.err.println(cx.toString(result));
err.println(cx.toString(result));
}
NativeArray h = global.history;
h.put((int)h.jsGet_length(), h, source);
@ -286,7 +286,7 @@ public class Main extends ScriptableObject {
catch (WrappedException we) {
// Some form of exception was caught by JavaScript and
// propagated up.
System.err.println(we.getWrappedException().toString());
err.println(we.getWrappedException().toString());
we.printStackTrace();
}
catch (EvaluatorException ee) {
@ -303,14 +303,14 @@ public class Main extends ScriptableObject {
jse.getMessage()));
}
catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
if (global.quitting) {
// The user executed the quit() function.
break;
}
}
System.err.println();
err.println();
} else {
Reader in = null;
try {
@ -343,7 +343,7 @@ public class Main extends ScriptableObject {
filename));
return;
} catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
try {
@ -353,7 +353,7 @@ public class Main extends ScriptableObject {
cx.evaluateReader(global, in, filename, 1, null);
}
catch (WrappedException we) {
System.err.println(we.getWrappedException().toString());
err.println(we.getWrappedException().toString());
we.printStackTrace();
}
catch (EvaluatorException ee) {
@ -369,14 +369,14 @@ public class Main extends ScriptableObject {
jse.getMessage()));
}
catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
finally {
try {
in.close();
}
catch (IOException ioe) {
System.err.println(ioe.toString());
err.println(ioe.toString());
}
}
}
@ -384,12 +384,27 @@ public class Main extends ScriptableObject {
}
private static void p(String s) {
System.out.println(s);
out.println(s);
}
static ToolErrorReporter errorReporter;
static Main global;
static SharedGlobal sharedGlobal;
public static void setIn(InputStream _in) {
in = _in;
}
public static void setOut(PrintStream _out) {
out = _out;
}
public static void setErr(PrintStream _err) {
err = _err;
}
static protected ToolErrorReporter errorReporter;
static protected Main global;
static protected SharedGlobal sharedGlobal;
static public InputStream in = System.in;
static public PrintStream out = System.out;
static public PrintStream err = System.err;
boolean quitting;
SourceTextManager debug_stm;