test/src/jdk/nashorn/internal/test/framework/SharedContextEvaluator.java

Print this page




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.test.framework;
  27 
  28 import static jdk.nashorn.tools.Shell.COMPILATION_ERROR;
  29 import static jdk.nashorn.tools.Shell.RUNTIME_ERROR;
  30 import static jdk.nashorn.tools.Shell.SUCCESS;
  31 
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.io.OutputStream;
  35 import java.io.PrintWriter;
  36 import jdk.nashorn.api.scripting.NashornException;

  37 import jdk.nashorn.internal.runtime.Context;
  38 import jdk.nashorn.internal.runtime.ErrorManager;
  39 import jdk.nashorn.internal.runtime.ScriptFunction;
  40 import jdk.nashorn.internal.runtime.ScriptObject;
  41 import jdk.nashorn.internal.runtime.ScriptRuntime;
  42 import jdk.nashorn.internal.runtime.Source;
  43 import jdk.nashorn.internal.runtime.options.Options;
  44 
  45 /**
  46  * A script evaluator that shares a single Nashorn Context instance to run
  47  * scripts many times on it.
  48  */
  49 public final class SharedContextEvaluator implements ScriptEvaluator {
  50     // The shared Nashorn Context
  51     private final Context context;
  52 
  53     // We can't replace output and error streams after Context is created
  54     // So, we create these delegating streams - so that we can replace underlying
  55     // delegate streams for each script run call
  56     private final DelegatingOutputStream ctxOut;
  57     private final DelegatingOutputStream ctxErr;
  58 
  59     private static class DelegatingOutputStream extends OutputStream {
  60         private OutputStream underlying;


  93         }
  94     }
  95 
  96     /**
  97      * SharedContextEvaluator constructor
  98      * @param args initial script arguments to create shared context
  99      */
 100     public SharedContextEvaluator(final String[] args) {
 101         this.ctxOut = new DelegatingOutputStream(System.out);
 102         this.ctxErr = new DelegatingOutputStream(System.err);
 103         PrintWriter wout = new PrintWriter(ctxOut, true);
 104         PrintWriter werr = new PrintWriter(ctxErr, true);
 105         Options options = new Options("nashorn", werr);
 106         options.process(args);
 107         ErrorManager errors = new ErrorManager(werr);
 108         this.context = new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader());
 109     }
 110 
 111     @Override
 112     public int run(final OutputStream out, final OutputStream err, final String[] args) throws IOException {
 113         final ScriptObject oldGlobal = Context.getGlobal();
 114         try {
 115             ctxOut.setDelegatee(out);
 116             ctxErr.setDelegatee(err);
 117             final ErrorManager errors = context.getErrorManager();
 118             final ScriptObject global = context.createGlobal();
 119             Context.setGlobal(global);
 120 
 121             // For each file on the command line.
 122             for (final String fileName : args) {
 123                 if (fileName.startsWith("-")) {
 124                     // ignore options in shared context mode (which was initialized upfront!)
 125                     continue;
 126                 }
 127                 final File file = new File(fileName);
 128                 ScriptFunction script = context.compileScript(new Source(fileName, file.toURI().toURL()), global);
 129 
 130                 if (script == null || errors.getNumberOfErrors() != 0) {
 131                     return COMPILATION_ERROR;
 132                 }
 133 
 134                 try {
 135                     ScriptRuntime.apply(script, global);
 136                 } catch (final NashornException e) {
 137                     errors.error(e.toString());
 138                     if (context.getEnv()._dump_on_error) {


  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.test.framework;
  27 
  28 import static jdk.nashorn.tools.Shell.COMPILATION_ERROR;
  29 import static jdk.nashorn.tools.Shell.RUNTIME_ERROR;
  30 import static jdk.nashorn.tools.Shell.SUCCESS;
  31 
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.io.OutputStream;
  35 import java.io.PrintWriter;
  36 import jdk.nashorn.api.scripting.NashornException;
  37 import jdk.nashorn.internal.objects.Global;
  38 import jdk.nashorn.internal.runtime.Context;
  39 import jdk.nashorn.internal.runtime.ErrorManager;
  40 import jdk.nashorn.internal.runtime.ScriptFunction;

  41 import jdk.nashorn.internal.runtime.ScriptRuntime;
  42 import jdk.nashorn.internal.runtime.Source;
  43 import jdk.nashorn.internal.runtime.options.Options;
  44 
  45 /**
  46  * A script evaluator that shares a single Nashorn Context instance to run
  47  * scripts many times on it.
  48  */
  49 public final class SharedContextEvaluator implements ScriptEvaluator {
  50     // The shared Nashorn Context
  51     private final Context context;
  52 
  53     // We can't replace output and error streams after Context is created
  54     // So, we create these delegating streams - so that we can replace underlying
  55     // delegate streams for each script run call
  56     private final DelegatingOutputStream ctxOut;
  57     private final DelegatingOutputStream ctxErr;
  58 
  59     private static class DelegatingOutputStream extends OutputStream {
  60         private OutputStream underlying;


  93         }
  94     }
  95 
  96     /**
  97      * SharedContextEvaluator constructor
  98      * @param args initial script arguments to create shared context
  99      */
 100     public SharedContextEvaluator(final String[] args) {
 101         this.ctxOut = new DelegatingOutputStream(System.out);
 102         this.ctxErr = new DelegatingOutputStream(System.err);
 103         PrintWriter wout = new PrintWriter(ctxOut, true);
 104         PrintWriter werr = new PrintWriter(ctxErr, true);
 105         Options options = new Options("nashorn", werr);
 106         options.process(args);
 107         ErrorManager errors = new ErrorManager(werr);
 108         this.context = new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader());
 109     }
 110 
 111     @Override
 112     public int run(final OutputStream out, final OutputStream err, final String[] args) throws IOException {
 113         final Global oldGlobal = Context.getGlobal();
 114         try {
 115             ctxOut.setDelegatee(out);
 116             ctxErr.setDelegatee(err);
 117             final ErrorManager errors = context.getErrorManager();
 118             final Global global = context.createGlobal();
 119             Context.setGlobal(global);
 120 
 121             // For each file on the command line.
 122             for (final String fileName : args) {
 123                 if (fileName.startsWith("-")) {
 124                     // ignore options in shared context mode (which was initialized upfront!)
 125                     continue;
 126                 }
 127                 final File file = new File(fileName);
 128                 ScriptFunction script = context.compileScript(new Source(fileName, file.toURI().toURL()), global);
 129 
 130                 if (script == null || errors.getNumberOfErrors() != 0) {
 131                     return COMPILATION_ERROR;
 132                 }
 133 
 134                 try {
 135                     ScriptRuntime.apply(script, global);
 136                 } catch (final NashornException e) {
 137                     errors.error(e.toString());
 138                     if (context.getEnv()._dump_on_error) {