8198816: AbstractScriptEngine.getScriptContext creation of SimpleScriptContext is inefficient
Reviewed-by: sundar, jlaskey
--- a/src/java.scripting/share/classes/javax/script/AbstractScriptEngine.java Mon Apr 23 10:59:39 2018 -0500
+++ b/src/java.scripting/share/classes/javax/script/AbstractScriptEngine.java Mon Apr 23 18:14:35 2018 +0200
@@ -287,7 +287,7 @@
*/
protected ScriptContext getScriptContext(Bindings nn) {
- SimpleScriptContext ctxt = new SimpleScriptContext();
+ SimpleScriptContext ctxt = new SimpleScriptContext(context.getReader(), context.getWriter(), context.getErrorWriter());
Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
if (gs != null) {
@@ -301,10 +301,6 @@
throw new NullPointerException("Engine scope Bindings may not be null.");
}
- ctxt.setReader(context.getReader());
- ctxt.setWriter(context.getWriter());
- ctxt.setErrorWriter(context.getErrorWriter());
-
return ctxt;
}
--- a/src/java.scripting/share/classes/javax/script/CompiledScript.java Mon Apr 23 10:59:39 2018 -0500
+++ b/src/java.scripting/share/classes/javax/script/CompiledScript.java Mon Apr 23 18:14:35 2018 +0200
@@ -79,13 +79,10 @@
ScriptContext ctxt = getEngine().getContext();
if (bindings != null) {
- SimpleScriptContext tempctxt = new SimpleScriptContext();
+ SimpleScriptContext tempctxt = new SimpleScriptContext(ctxt.getReader(), ctxt.getWriter(), ctxt.getErrorWriter());
tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),
ScriptContext.GLOBAL_SCOPE);
- tempctxt.setWriter(ctxt.getWriter());
- tempctxt.setReader(ctxt.getReader());
- tempctxt.setErrorWriter(ctxt.getErrorWriter());
ctxt = tempctxt;
}
--- a/src/java.scripting/share/classes/javax/script/SimpleScriptContext.java Mon Apr 23 10:59:39 2018 -0500
+++ b/src/java.scripting/share/classes/javax/script/SimpleScriptContext.java Mon Apr 23 18:14:35 2018 +0200
@@ -86,11 +86,25 @@
* Create a {@code SimpleScriptContext}.
*/
public SimpleScriptContext() {
+ this(new InputStreamReader(System.in),
+ new PrintWriter(System.out , true),
+ new PrintWriter(System.err, true));
engineScope = new SimpleBindings();
globalScope = null;
- reader = new InputStreamReader(System.in);
- writer = new PrintWriter(System.out , true);
- errorWriter = new PrintWriter(System.err, true);
+ }
+
+ /**
+ * Package-private constructor to avoid needless creation of reader and writers.
+ * It is the caller's responsability to initialize the engine scope.
+ *
+ * @param reader the reader
+ * @param writer the writer
+ * @param errorWriter the error writer
+ */
+ SimpleScriptContext(Reader reader, Writer writer, Writer errorWriter) {
+ this.reader = reader;
+ this.writer = writer;
+ this.errorWriter = errorWriter;
}
/**