From 254e7faf342b4e68ebc87fcef29c93e95d6d9db3 Mon Sep 17 00:00:00 2001 From: Athijegannathan Sundararajan Date: Thu, 16 Dec 2010 20:52:09 +0530 Subject: [PATCH] 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3 Updating Rhino javascript engine with version 1.7R3. Issue 6427783 (E4X support is missing from Sun's Mozilla JavaScript implementation) is also fixed as a side-effect. Reviewed-by: alanb, jjh --- .../script/javascript/RhinoScriptEngine.java | 37 ++++++++--- .../javascript/RhinoScriptEngineFactory.java | 6 +- .../sun/script/javascript/RhinoTopLevel.java | 12 ---- .../com/sun/tools/script/shell/init.js | 4 +- jdk/test/javax/script/E4XErrorTest.java | 64 ------------------- jdk/test/javax/script/VersionTest.java | 4 +- 6 files changed, 36 insertions(+), 91 deletions(-) delete mode 100644 jdk/test/javax/script/E4XErrorTest.java diff --git a/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java b/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java index d370673f2c3..4c21fdb0c80 100644 --- a/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java +++ b/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java @@ -61,26 +61,43 @@ public final class RhinoScriptEngine extends AbstractScriptEngine private ScriptEngineFactory factory; private InterfaceImplementor implementor; + private static final int languageVersion = getLanguageVersion(); + private static final int optimizationLevel = getOptimizationLevel(); static { ContextFactory.initGlobal(new ContextFactory() { protected Context makeContext() { Context cx = super.makeContext(); + cx.setLanguageVersion(languageVersion); + cx.setOptimizationLevel(optimizationLevel); cx.setClassShutter(RhinoClassShutter.getInstance()); cx.setWrapFactory(RhinoWrapFactory.getInstance()); return cx; } - - public boolean hasFeature(Context cx, int feature) { - // we do not support E4X (ECMAScript for XML)! - if (feature == Context.FEATURE_E4X) { - return false; - } else { - return super.hasFeature(cx, feature); - } - } }); } + private static final String RHINO_JS_VERSION = "rhino.js.version"; + private static int getLanguageVersion() { + int version; + String tmp = java.security.AccessController.doPrivileged( + new sun.security.action.GetPropertyAction(RHINO_JS_VERSION)); + if (tmp != null) { + version = Integer.parseInt((String)tmp); + } else { + version = Context.VERSION_1_8; + } + return version; + } + + private static final String RHINO_OPT_LEVEL = "rhino.opt.level"; + private static int getOptimizationLevel() { + int optLevel = -1; + // disable optimizer under security manager, for now. + if (System.getSecurityManager() == null) { + optLevel = Integer.getInteger(RHINO_OPT_LEVEL, -1); + } + return optLevel; + } /** * Creates a new instance of RhinoScriptEngine @@ -333,6 +350,7 @@ public final class RhinoScriptEngine extends AbstractScriptEngine return result instanceof Undefined ? null : result; } + /* public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("No file specified"); @@ -347,4 +365,5 @@ public final class RhinoScriptEngine extends AbstractScriptEngine engine.eval(r); System.out.println(engine.get("x")); } + */ } diff --git a/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngineFactory.java b/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngineFactory.java index 15937c3b8f8..3936a1abb75 100644 --- a/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngineFactory.java +++ b/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngineFactory.java @@ -58,11 +58,11 @@ public class RhinoScriptEngineFactory extends ScriptEngineFactoryBase { } else if (key.equals(ScriptEngine.ENGINE)) { return "Mozilla Rhino"; } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { - return "1.6 release 2"; + return "1.7 release 3 PRERELEASE"; } else if (key.equals(ScriptEngine.LANGUAGE)) { return "ECMAScript"; } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { - return "1.6"; + return "1.8"; } else if (key.equals("THREADING")) { return "MULTITHREADED"; } else { @@ -128,10 +128,12 @@ public class RhinoScriptEngineFactory extends ScriptEngineFactoryBase { return ret; } + /* public static void main(String[] args) { RhinoScriptEngineFactory fact = new RhinoScriptEngineFactory(); System.out.println(fact.getParameter(ScriptEngine.ENGINE_VERSION)); } + */ private static List names; private static List mimeTypes; diff --git a/jdk/src/share/classes/com/sun/script/javascript/RhinoTopLevel.java b/jdk/src/share/classes/com/sun/script/javascript/RhinoTopLevel.java index 44e5f95cd89..a6b435a6521 100644 --- a/jdk/src/share/classes/com/sun/script/javascript/RhinoTopLevel.java +++ b/jdk/src/share/classes/com/sun/script/javascript/RhinoTopLevel.java @@ -37,15 +37,6 @@ import javax.script.*; * @since 1.6 */ public final class RhinoTopLevel extends ImporterTopLevel { - - // variables defined always to help Java access from JavaScript - private static final String builtinVariables = - "var com = Packages.com; \n" + - "var edu = Packages.edu; \n" + - "var javax = Packages.javax; \n" + - "var net = Packages.net; \n" + - "var org = Packages.org; \n"; - RhinoTopLevel(Context cx, RhinoScriptEngine engine) { super(cx); this.engine = engine; @@ -67,9 +58,6 @@ public final class RhinoTopLevel extends ImporterTopLevel { String names[] = { "bindings", "scope", "sync" }; defineFunctionProperties(names, RhinoTopLevel.class, ScriptableObject.DONTENUM); - - // define built-in variables - cx.evaluateString(this, builtinVariables, "", 1, null); } /** diff --git a/jdk/src/share/classes/com/sun/tools/script/shell/init.js b/jdk/src/share/classes/com/sun/tools/script/shell/init.js index 6abaf37857e..9b31ea47339 100644 --- a/jdk/src/share/classes/com/sun/tools/script/shell/init.js +++ b/jdk/src/share/classes/com/sun/tools/script/shell/init.js @@ -311,9 +311,9 @@ function load(str) { try { engine.eval(reader); } finally { - engine.put(engine.FILENAME, oldFilename); + engine.put(engine.FILENAME, oldFilename); + streamClose(stream); } - streamClose(stream); } // file system utilities diff --git a/jdk/test/javax/script/E4XErrorTest.java b/jdk/test/javax/script/E4XErrorTest.java deleted file mode 100644 index 97eb7aabd7e..00000000000 --- a/jdk/test/javax/script/E4XErrorTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2005, 2008, 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 6346734 6705893 - * @summary We do *not* support E4X (ECMAScript for XML) in our - * implementation. We want to throw error on XML literals - * as early as possible rather than at "runtime" - i.e., when - * engine looks for "XML" constructor. - */ - -import javax.script.*; -import java.util.Locale; - -public class E4XErrorTest { - - public static void main(String[] args) throws Exception { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine jsengine = Helper.getJsEngine(manager); - if (jsengine == null) { - System.out.println("Warning: No js engine found; test vacuously passes."); - return; - } - - // The test below depends on the error message content - // that is loaded from resource bundles. So, we force - // English Locale to compare correct value.. - Locale.setDefault(Locale.US); - - try { - jsengine.eval("var v = ;"); - } catch (ScriptException se) { - String msg = se.getMessage(); - if (msg.indexOf("syntax error") == -1) { - throw new RuntimeException("syntax error expected, got " + - msg); - } - return; - } - // should not reach here.. exception should have been thrown. - throw new RuntimeException("Huh! E4X is supported??"); - } -} diff --git a/jdk/test/javax/script/VersionTest.java b/jdk/test/javax/script/VersionTest.java index 13ed9c05115..ff28c2c2e17 100644 --- a/jdk/test/javax/script/VersionTest.java +++ b/jdk/test/javax/script/VersionTest.java @@ -32,8 +32,8 @@ import java.io.*; public class VersionTest { - private static final String JS_LANG_VERSION = "1.6"; - private static final String JS_ENGINE_VERSION = "1.6 release 2"; + private static final String JS_LANG_VERSION = "1.8"; + private static final String JS_ENGINE_VERSION = "1.7 release 3 PRERELEASE"; public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager();