From 3814c7ccaa0fdc78109b95b49b101dae39eaaa18 Mon Sep 17 00:00:00 2001 From: sundar Date: Mon, 17 Jan 2011 13:29:12 +0530 Subject: [PATCH] 6508943: small typo in the documentation 6759414: javascript engine can not write to StringWriter 6869617: RhinoScriptEngine bug : ScriptException cause not set (with fix) 7012701: Add a test to check that Rhino's RegExp parser accepts unescaped '[' Reviewed-by: alanb, jjh --- .../script/javascript/RhinoScriptEngine.java | 6 ++- .../classes/javax/script/CompiledScript.java | 2 +- test/javax/script/CauseExceptionTest.java | 49 +++++++++++++++++++ test/javax/script/StringWriterPrintTest.java | 44 +++++++++++++++++ .../script/UnescapedBracketRegExTest.java | 40 +++++++++++++++ 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 test/javax/script/CauseExceptionTest.java create mode 100644 test/javax/script/StringWriterPrintTest.java create mode 100644 test/javax/script/UnescapedBracketRegExTest.java diff --git a/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java b/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java index 4c21fdb0c..6688ec65a 100644 --- a/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java +++ b/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java @@ -223,7 +223,9 @@ public final class RhinoScriptEngine extends AbstractScriptEngine } catch (RhinoException re) { if (DEBUG) re.printStackTrace(); int line = (line = re.lineNumber()) == 0 ? -1 : line; - throw new ScriptException(re.toString(), re.sourceName(), line); + ScriptException se = new ScriptException(re.toString(), re.sourceName(), line); + se.initCause(re); + throw se; } finally { cx.exit(); } @@ -257,6 +259,8 @@ public final class RhinoScriptEngine extends AbstractScriptEngine " str = 'null'; \n" + " } \n" + " var out = context.getWriter(); \n" + + " if (!(out instanceof java.io.PrintWriter))\n" + + " out = new java.io.PrintWriter(out); \n" + " out.print(String(str)); \n" + " if (newline) out.print('\\n'); \n" + " out.flush(); \n" + diff --git a/src/share/classes/javax/script/CompiledScript.java b/src/share/classes/javax/script/CompiledScript.java index da31be3bb..4a9a4ba05 100644 --- a/src/share/classes/javax/script/CompiledScript.java +++ b/src/share/classes/javax/script/CompiledScript.java @@ -107,7 +107,7 @@ public abstract class CompiledScript { } /** - * Returns the ScriptEngine wbose compile method created this CompiledScript. + * Returns the ScriptEngine whose compile method created this CompiledScript. * The CompiledScript will execute in this engine. * * @return The ScriptEngine that created this CompiledScript diff --git a/test/javax/script/CauseExceptionTest.java b/test/javax/script/CauseExceptionTest.java new file mode 100644 index 000000000..535717609 --- /dev/null +++ b/test/javax/script/CauseExceptionTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6869617 + * @summary RhinoScriptEngine bug : ScriptException cause not set (with fix) + */ + +import javax.script.*; +import java.io.*; + +public class CauseExceptionTest { + public static void main(String[] args) throws ScriptException, NoSuchMethodException { + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine engine = sem.getEngineByName("js"); + engine.eval("function hello_world() { println('hello world'); throw 'out of here'; } "); + Invocable invocable = (Invocable) engine; + try { + invocable.invokeFunction("hello_world", (Object[])null); + } catch (ScriptException se) { + Throwable cause = se.getCause(); + if (cause == null) { + throw new RuntimeException("null cause"); + } + System.out.println(cause); + } + } +}; diff --git a/test/javax/script/StringWriterPrintTest.java b/test/javax/script/StringWriterPrintTest.java new file mode 100644 index 000000000..65b401ce6 --- /dev/null +++ b/test/javax/script/StringWriterPrintTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6759414 + * @summary javascript engine can not write to StringWriter. + */ + +import javax.script.*; +import java.io.*; + +public class StringWriterPrintTest { + public static void main(String[] args) throws ScriptException { + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine engine = sem.getEngineByName("js"); + StringWriter sw = new StringWriter(); + engine.eval("print(\"hello world 1\\n\")"); + engine.getContext().setWriter(sw); + // the following "print" call throws exception + engine.eval("print(\"hello world 2\\n\")"); + System.out.println(sw.toString()); + } +}; diff --git a/test/javax/script/UnescapedBracketRegExTest.java b/test/javax/script/UnescapedBracketRegExTest.java new file mode 100644 index 000000000..d444ec74a --- /dev/null +++ b/test/javax/script/UnescapedBracketRegExTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 7012701 + * @summary 7012701 Add a test to check that Rhino's RegExp parser accepts unescaped '[' + */ + +import javax.script.*; +import java.io.*; + +public class UnescapedBracketRegExTest { + public static void main(String[] args) throws ScriptException { + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine engine = sem.getEngineByName("js"); + // the following throws exception + engine.eval("var x = /[a-zA-Z+/=]/;"); + } +}; -- GitLab