Class DefaultScriptingContext

java.lang.Object
org.bzdev.scripting.ScriptingContext
org.bzdev.scripting.DefaultScriptingContext

public class DefaultScriptingContext extends ScriptingContext
Default implementation of a scripting context. If no scripting language is specified, or the language passed to DefaultScriptingContext(java.lang.String) is null, the language ESP * is chosen by default. ESP was developed because the default scripting engine for Java-11 has been deprecated and will go away in future releases. Meanwhile the GraalVM implementation of ECMAScript (at least, at the point this documentation was written) will not allow a objects from a script evaluated in one thread to be used in a different thread, which is problematic for classes such as Simulation, where code such as

 import org.bzdev.devqsim.Simulation;
 import org.bzdev.devqsim.TaskThread;

 public class TestSim1 {
     public static void main(String argv[]) throws Exception {

         Simulation sim = new Simulation();
         sim.scheduleTask(() -> {
             for(int i = 0; i < 5; i++) {
                System.out.println("task is at simulation time "
                                   + sim.currentTicks()
                                   + " [i= " + i +"]");
                 TaskThread.pause(10);
              }
          }, 10);
          sim.run();
     }
 }
 
can be used instead of the faster, but less readable equivalent

 import org.bzdev.devqsim.Simulation;
 import org.bzdev.lang.Callable;

 public class TestSim2 {

    static Callable nextCall(final Simulation sim, final int i) {
        return () -> {
            System.out.println("task is at simulation time "
                               + sim.currentTicks()
                               + " [i= " + i +"]");
             if (i < 4) {
                 sim.scheduleCall(nextCall(sim, i+1), 10);
             }
    };

    public static void main(String argv[]) throws Exception {
          Simulation sim = new Simulation();
        sim.scheduleCall(nextCall(sim, 0), 10);
       sim.run();
    }
 }
 
The equivalent for ECMAScript is

 scripting.importClass("org.bzdev.devqsim.Simulation");
 scripting.importClass("org.bzdev.devqsim.TaskThread");

 var sim = new Simulation(scripting);
 sim.scheduleTaskObject({run: function() {
       for (var i = 0; i < 5; i++) {
          scripting.getWriter().println("task is at simulation time "
                                        + sim.currentTicks()
                                        + " [i= " + i +"]");
          TaskThread.pause(10);
      }
  }
}, 10);
sim.run()
 
and for ESP is

 import(org.bzdev.devqsim, [Simulation, TaskThread]);

 var sim = new Simulation(scripting);
 sim.scheduleTaskObject({run: function() {
     IntStream.range(0,5).forEachOrdered(function(i) {
         global.getWriter().println("task is at simulation time "
                                    + sim.currentTicks()
                                    + " [i= " + i +"]");
         TaskThread.pause(10);
         });
      }
 }, 10);
 sim.run()
 
  • Constructor Details

    • DefaultScriptingContext

      public DefaultScriptingContext(String languageName)
      Constructor given a scripting-language name.
      Parameters:
      languageName - the name of the scripting language; null for the default (ECMAScript)
    • DefaultScriptingContext

      public DefaultScriptingContext()
      Constructor. The default language, ESP, will be used. Please see ExpressionParser and the ESP scripting language for a description of ESP.
  • Method Details

    • doGetScriptEngine

      protected ScriptEngine doGetScriptEngine()
      Description copied from class: ScriptingContext
      Get the script engine. Subclasses providing a scripting environment must override this method. It will typically be called once, but could be called multiple times, so the method must be idempotent in case it is called multiple times. It will not be called by this class if the constructor provided a parent.
      Overrides:
      doGetScriptEngine in class ScriptingContext
      Returns:
      the script engine for this scripting context; null if there is none
    • doGetDefaultBindings

      protected Bindings doGetDefaultBindings()
      Description copied from class: ScriptingContext
      Get the default script bindings. Subclasses providing a scripting environment must override this method. It will typically be called once, but could be called multiple times, so the method must be idempotent. It will not be called by this class if the constructor provided a parent.
      Overrides:
      doGetDefaultBindings in class ScriptingContext
      Returns:
      the default bindings for this scripting context; null if there are none
    • doGetScriptLanguage

      protected String doGetScriptLanguage()
      Description copied from class: ScriptingContext
      Get the scripting language. Subclasses providing a scripting environment must override this method. It will typically be called once, but could be called multiple times, so the method must be idempotent. It will not be called by this class if the constructor provided a parent.
      Overrides:
      doGetScriptLanguage in class ScriptingContext
      Returns:
      the script engine for this scripting context; null if there is none