Script Engine
Review
A script engine is an algorithm for interpreting scripts as subroutines.
Each script engine instance is a separate virtual machine process.
A script, as a high-level declaration of an algorithm, consists of components.
The script processor, as a lower-level interpretation algorithm, includes algorithms for implementing the functionality of components.
The scripting engine process is a state machine and handles events.
Events can come in the form of messages from the outside, or generated by executable components.
Process
The workflow of the script handler can be represented in the following sequence of actions:
-
loads the script,
-
creates data areas for storing variables declared by the script,
-
searches for a component in the script Start with the main branch type,
-
compares the algorithm for its interpretation with the declaration of the component,
-
transfers control to the component algorithm, passing the parameters for the component from the script and the accumulated context as input,
-
directs events received during operation of the component to its processing queue and transfers control to it for processing this queue,
-
processes the component response:
-
continuation of the component’s operation, then waits for events to arrive;
-
deferring processing of the next event, then moves the event to another queue; **component shutdown. When the component completes its work, it returns:
-
identifier of the next component, then the process is repeated with the new component starting from the stage of searching for it in the script, expanding however by using deferred events;
-
code for the following script, then it is loaded and the process is repeated from the very beginning;
-
error, then the script ends and possibly a post-processing branch is started;
-
command to return to the previous script on the stack from a nested one, then the context of the previous script and the position in it are restored;
-
stop command, then the script ends, and perhaps the post-processing branch is started;
-
-
Thus, each component is executed in the script engine process, taking as input the accumulated context and the component declaration from the script.
Component execution can occur synchronously or asynchronously.
In the asynchronous case, all events received by the scripting engine (from within and from outside) are passed to the currently executing component for processing.
He, in turn, can:
-
process the message,
-
throw it away
-
set aside for next component.
At any time, the handler can receive an external command to terminate, in which case the currently running component is terminated.
If there is a corresponding post-processing branch, control is transferred to it, and if not, the script ends.
Auto-closing transition branches
When the component completes its work, based on the result obtained, it selects one or another transition branch to transfer control.
Most components separate successful and unsuccessful results by assigning them to different transition branches.
If the alternative transition branch is not specified (the property is not selected), then the components usually transfer control along the main branch.
Thus, the script has the ability to continue if the component fails, but the corresponding transition branch is not specified.
This behavior of branching components is called transition branch closure.
Branching components are divided into closing and interrupting along an unspecified branch.
Post processing
Script termination can occur for several reasons (described in component Start).
However, the script engine can continue executing the script in the same context on an alternate branch created by the administrator in case the script terminates for one reason or another.
Executing a post-processing branch when completed no longer involves switching to another branch.
To avoid cluttering up processor power and RAM, a limit is imposed on the execution time of the post-processing branch.
It can be strengthened in the Start component, which initiates the corresponding branch.
Script substitution
The script engine exposes a script substitution interface to the outside. Using it, at any point in execution and at any point in time, a new script can be loaded into the current context, which will begin executing from its beginning.
The component running at the time of replacement is terminated.
Default values for variables
Local variables can be given default values when the script is created. The initial values are supplied externally by the owner of the script engine, that is, by the process that launched it.
The script engine itself can also fill in the values of local variables if they have not previously been set.
This occurs immediately after loading if the script entity in options (opts.variables) contains default values for the variable names declared by the script. This operation occurs when loading each script, including nested ones.
Next, the Start component can also fill in the values of local variables if the values for them have not been previously set. This is done with the appropriate component settings.
A script may contain an Value Assignment component immediately after the Start component or later.
In any other case, local variables are left blank and cannot be used as value providers.