diff --git a/mozilla/js/rhino/org/mozilla/javascript/NativeJavaObject.java b/mozilla/js/rhino/org/mozilla/javascript/NativeJavaObject.java index bc51fa4a904..efd4ce678a4 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/NativeJavaObject.java +++ b/mozilla/js/rhino/org/mozilla/javascript/NativeJavaObject.java @@ -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() { diff --git a/mozilla/js/rhino/org/mozilla/javascript/tools/ToolErrorReporter.java b/mozilla/js/rhino/org/mozilla/javascript/tools/ToolErrorReporter.java index 597773f259a..bfd4de8a5d1 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/tools/ToolErrorReporter.java +++ b/mozilla/js/rhino/org/mozilla/javascript/tools/ToolErrorReporter.java @@ -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; } diff --git a/mozilla/js/rhino/org/mozilla/javascript/tools/shell/Main.java b/mozilla/js/rhino/org/mozilla/javascript/tools/shell/Main.java index 8284016caa8..8d762c9f8c6 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/tools/shell/Main.java +++ b/mozilla/js/rhino/org/mozilla/javascript/tools/shell/Main.java @@ -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, ""); 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 { "", 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; diff --git a/mozilla/js/rhino/org/mozilla/javascript/tools/shell/SharedGlobal.java b/mozilla/js/rhino/org/mozilla/javascript/tools/shell/SharedGlobal.java index c0f47cd38cd..570d1e5a2da 100644 --- a/mozilla/js/rhino/org/mozilla/javascript/tools/shell/SharedGlobal.java +++ b/mozilla/js/rhino/org/mozilla/javascript/tools/shell/SharedGlobal.java @@ -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(); } diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java b/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java index bc51fa4a904..efd4ce678a4 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java @@ -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() { diff --git a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/ToolErrorReporter.java b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/ToolErrorReporter.java index 597773f259a..bfd4de8a5d1 100644 --- a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/ToolErrorReporter.java +++ b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/ToolErrorReporter.java @@ -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; } diff --git a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java index 8284016caa8..8d762c9f8c6 100644 --- a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java +++ b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java @@ -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, ""); 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 { "", 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;