java.lang.Object
org.bzdev.scripting.ScriptingContext
org.bzdev.scripting.DefaultScriptingContext
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
can be used instead of the faster, but less readable equivalentimport 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(); } }
The equivalent for ECMAScript isimport 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(); } }
and for ESP isscripting.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()
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()
-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.scripting.ScriptingContext
ScriptingContext.BindingSwapper
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor.DefaultScriptingContext
(String languageName) Constructor given a scripting-language name. -
Method Summary
Modifier and TypeMethodDescriptionprotected Bindings
Get the default script bindings.protected ScriptEngine
Get the script engine.protected String
Get the scripting language.Methods inherited from class org.bzdev.scripting.ScriptingContext
callScriptFunction, callScriptFunction, callScriptMethod, callScriptMethod, containsScriptObject, createBindings, createBindingSwapper, createScriptEngine, doScriptPrivileged, evalScript, evalScript, evalScript, evalScript, getErrorWriter, getNames, getReader, getScriptLanguage, getScriptObject, getWriter, hasScriptEngine, invokePrivateFunction, invokePrivateFunction, putScriptObject, setErrorWriter, setReader, setWriter, usingGraalVM
-
Constructor Details
-
DefaultScriptingContext
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
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 classScriptingContext
- Returns:
- the script engine for this scripting context; null if there is none
-
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 classScriptingContext
- Returns:
- the default bindings for this scripting context; null if there are none
-
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 classScriptingContext
- Returns:
- the script engine for this scripting context; null if there is none
-