Leaps and bounds of DWScript

A DWS 2.2 preview zip (199 kB) has been posted, it is an SVN snapshot, and features recent additions to DWS:

  • Contracts are now partially supported: ensure, require and old are available and use the same syntax as Prism contracts, inheritance is supported, class invariants are not in just yet, mostly for syntax indecision reasons, see this thread.
  • the implies operator is now supported, “a implies b” is equivalent to “(not a) or b”, or in other words, it is false only if a is true and b is false.
  • Assert() is supported, and will trigger EAssertionFailed in case of, well, an assertion failure. A compiler option has been added to control the generation of assertions.
  • Exception now has a StackTrace method, which will return a textual stacktrace script-side. Runtime errors now also include a stack trace, and new Delphi-side methods provide access to even more details.
  • the in operator has been extended to allow class overloading, f.i. you can now use “if (aString in someStringList) then…”, the not in form is also supported.

A fair chunk of the error and exception code has been refactored, if you were relying on it, all the preexisting functionality is still there, though it has been shuffled around to (hopefully) more relevant locations.

Threaded script execution

The SVN trunk version of DWS now supports threaded script execution.

Things went quite smoothly given that practically all classes got affected, yet all tests pass, including a special threaded torture test, so I’m reasonably confident ;-). The changes are however breaking backward compatibility, hopefully for the best.

Here are the major changes that will affect your code:

  • Scripts used to compile to a TdwsProgram, they now compile to an IdwsProgram
  • An IdwsProgram is a factory for IdwsProgramExecution
  • Script executions now happen via IdwsProgramExecution
  • Messages are now split between IdwsProgram (compile time messages) and IdwsProgramExecution (runtime messages)

You can now have as many program executions for a given IdwsProgram as you wish, each execution will use memory for its heap & stack only, the compiled expression tree is shared. Both new interfaces use reference-counted memory management.

An IdwsProgramExecution is single threaded, but isn’t attached to any particular thread, meaning that it can be moved across threads freely, and re-run as many times as desired. A given TDelphiWebScript compiler component can still only compile one TdwsProgram at once. That said, you can have multiple TDelphiWebScript components, each compiling a different IdwsProgram in a different thread .

It goes without saying that of course, all the above is only valid if all the functions, classes and events you expose to the scripts are themselves thread-safe!

DWScript 2.1 branched

DWScript 2.1 RC2 has been promoted to stable 2.1.0 status, if you already have 2.1 RC2, you don’t need to download anything new.

If you use SVN, there is a now a “stable-2.1” branch which will see only fixes, evolutions and additions will now resume under “trunk”.

What can be expected for DWS 2.2?

In addition to the previously listed goals, which are still part of the background work, there are a few specific goals for 2.2:

  • separating execution context from the expression tree. Currently a compiled script can be run multiple times, but from only one thread at a a time, if you need to run two, you need to compile two instances (thus using up twice the memory). The goal is to allow the same program to be run from multiple threads at the same time.
  • language support for contracts programming.
  • 85% testing code coverage psychological threshold.
  • more demos/samples, at least a couple showcases of IDE/Debugger support features.