Native Languages Tech Info

Top  Previous  Next

Natively implemented PasScript and NativeVb languages included as a part of LMD-ScriptPack. They are implemented 100% in Delphi with full source code provided. These languages compiles directly into application exe, no external dll is required to deploy with the application.


Here are some interesting facts about the implementation of the native scripting languages:


Languages use shared virtual machine (VM) that is able to interpret byte-code, generated by the language compilers. VM is excessively optimized to provide the best efficiency and performance. The optimization covers both: accurate low level implementation as well as the VM architecture design. The byte-code instruction set is also designed carefully to allow language compilers to emit optimized code.
Despite the fact that the script control is not thread-safe and the underlaying VM too, VM actually has some internal thread-safe parts to allow supporting debugger related things like call-stack and also thread name-search caches for very fast name-search at run-time. Tricky architecture is used here to minimize the count of interlocked low-level operations required for threads synchronization, thus improving performance.
When one script-control is added to another one, VMs use much more efficient, than IDispatch, internal way to interoperate. Moreover, this way provides compatibility with Delphi exception system, so, no exception conversions are required.
Variant operation routines, used in VM, such as addition or multiplication of two Variants has been optimized too. We wrote our own functions that runs much faster than standard Delphi Variant handling routines. These function optimizes a wide set of common cases, like addition of two integers; and acts just like a proxy to Delphi routines for all other (more complex) cases - thus carefully preserving Delphi Variant handling semantic.
The debugger breakpoints and stepping (step-over, step-into) are implemented as in real languages using code-patching technique, which introduces zero overhead to running program. The ability to break script at any time (without breapoints, e.g. in unknown place) is implemented tricky with very low overhead.
Both compilers, for PasScript and NativeVb languages, are optimizing compilers, that means that they attempts to emit more efficient byte-code where possible:
Despite the dynamic nature of the scripting languages, compilers try to omit run-time name-search where possible. For example, almost always the access to local variables and parameters is emit without name-search, because compilers knows a set of local variables/parameters at compile-time. Thus the access to local variables/parameters, which is the most frequent data access, is performed much faster at run-time.
In unconditional loops, commonly written like while True do or until False, compilers emit simple unconditional jumps. Compilers also has the ability to ignore empty except/finally sections, converting try statements to simple statement list.
Double jumps are removed. E.g. when the first jump points to second unconditional jump, the first jump is re-targeted to second jump target.
Special, more efficient, byte-code instructions are used to working with common constants, like 0, 1, 2, Unassigned (Empty).
Special, more efficient, byte-code instructions are used when compiling expressions in if, while, until. For example, for if x = y then statement, the special single instruction will be used to compare two values for equality and jump, instead of two distinct compare and jump instructions. In byte-code architecture this can save considerable amount of time.
And more...
VM is fully compatible with Delphi's exceptions, so both languages provides try/except/finally functionality. PasScript uses Delphi like syntax, while NativeVB uses Vb .Net like syntax.
PasScript syntax natively supports the following things to work with imported Delphi types:
oset constructors, for example [biSystemMenu, biMinimize], and in operator to work with Delphi set types;
ois operator to work with class instances as well as with record instances;
o@ operator to assign event handlers: Button1.OnClick := @Button1Click;
NativeVB syntax natively supports the following things to work with imported Delphi types:
oVB .Net like New operator to create class instances as well as record instances;
oVB .Net like TypeOf/Is operator to work with class instances as well as with record instances;
oMS VBScript like GetRef intrinsic function to assign event handlers, like Button1.OnClick = GetRef("Button1Click")