DWScript has a “Globals” facility that allows storing and sharing data across scripts living in the same executable.

The first are Global Vars, and is a Key-Value storage, the second are Global Queues, which are LIFO/FIFO named queues. Both are thread-safe and high performance.

Enabling Support for Globals

To support them, just have dwsGlobalVarsFunctions in one of your project’s uses clause.

From that point on, all your scripts will have access to the Global Vars and Queues.

Delphi-side utilities are also exposed in that unit, and provide ability for the host application to debug, backup, etc.

Global Vars

Globals Vars storage is accessed through the ReadGlobalVar(name) and WriteGlobalVar(name, value) functions. They allow reading and writing a value by name, in an in-memory dictionary. Read & write operations are atomic.

While they’re quite self-sufficient on their own, you’ll usually want to access them through a minimal wrapper functions that’ll handle namespaces.

Here is the complete list of functions:

ReadGlobalVar (name) : reads the variable of given name, returns Null if not defined

ReadGlobalVarDef (name, default) : reads the variable of given name, returns default if not defined

(name, default) : reads the variable of given name, returns default if not defined TryReadGlobalVar (name, var value) : attempts to read variable of given name, if defined places the value in value and returns True, otherwise returns False

(name, var value) : attempts to read variable of given name, if defined places the value in value and returns True, otherwise returns False WriteGlobalVar (name, value, [expiration]) : writes value into the variable of given name, with an optional auto-expiration (in seconds)

(name, value, [expiration]) : writes value into the variable of given name, with an optional auto-expiration (in seconds) IncrementGlobalVar (name, delta = 1) : adds delta to the variable of given name and returns the resulting value

(name, delta = 1) : adds delta to the variable of given name and returns the resulting value DeleteGlobalVar (name) : deletes the variable of given name

(name) : deletes the variable of given name CleanupGlobalVars (filter = ‘*’) : delete variables matching the specified filter (wildcards ‘*’ and ‘?’ are supported). Memory is compacted if relevant. An empty string as filter will essentially just run a compaction operation.

(filter = ‘*’) : delete variables matching the specified filter (wildcards ‘*’ and ‘?’ are supported). Memory is compacted if relevant. An empty string as filter will essentially just run a compaction operation. GlobalVarNames : returns an array of string that holds the names of all variables

: returns an array of string that holds the names of all variables SaveGlobalVarsToString : returns a streamed persistence of all global vars

: returns a streamed persistence of all global vars LoadGlobalVarsFromString : loads a streamed persistence of all global vars

Global Queues

Global Queues are named chains of values which can be used both as a LIFO and FIFO queues. Writing or reading from a queue is thread-safe and atomic.

While they’re quite self-sufficient on their own, you’ll usually want to access them through a minimal wrapper functions that’ll handle namespaces.

Here is the complete list of functions: