LMD ScriptPack Guide


Delphi Type Mapping


As described above, because Delphi and COM have different type systems, we need to convert Delphi types to/from OleVariant. Here resulting variant type-codes, which correspond to specific Delphi type conversion, will be specified:

 

Delphi type

Converted to OleVariant type.

Simple types:

Integer, Byte, Smallint, Shortint, Word, LongWord, Currency, Date, Double, Float, String, Boolean.

Representing by variant value with corresponding type code. For example, an Integer value will have varInteger variant type code.

Int64

Since Delphi 5 does not support int64 variant values, it will be truncated to Integer, and have resulting variant type code – varInteger. For all other Delphi version – varInt64.

Enumerations:

TMyEnum = (meA, meB, meC);

Represented as Integers, variant type code – varInteger.

Subranges:

TMySubrng = (meA..meB);

Represented as Integers, variant type code – varInteger.

Sets:

TMySet = set of TMyEnum;

Represented as Integers, variant type code –varInteger.

Records:

TMyRec = record
  X: Integer;
  Y: Integer;
end;

Represented as a reference to special IDispatch object that stores record value in its field. Variant type code – varDispatch.

 

No indexes are maintained, so it is incorrect to test two variant records for equality or un-equality.

Classes:

TMyObject = class
public
  procedure MyMethod;
end;

Represented as a reference to special IDispatch object that stores a reference to original class instance. Variant type code – varDispatch.

 

LMD-ScriptPack maintains a hash-index of all existing class-type wrapper objects. So, ToVar method always returns a reference to the same wrapper for any given instance.

Metaclasses:

TMyClass = class of TMyObject;

Represented as a reference to special IDispatch object that stores a reference to original meta-class. Variant type code – varDispatch. Script Pack maintains a hash-index of all existing meta-class-type wrapper objects. So, ToVar method always returns a reference to the same wrapper for any given meta-class.

Procedural-types (events):

TMyEvent = procedure of object;

Represented as a reference to special IDispatch object. Two cases possible:

If this is a reference to script-handler, created with MakeEvent or EventToVariantS method, then this is a special wrapper object that stores a reference to script control and script procedure name.

If this is reference to Delphi event handler, created with ToVar method, then this is another special wrapper object that stores a reference to Delphi method (event handler).

No indexes are maintained, so it is incorrect to test two variant event handlers for equality or un-equality.

Wrappers for types itself, not its values.

Types itself are available for script too and represented by special IDispatch objects.

Example:

 

Set fcls  = Unit1.TForm1
Set form  = fcls.Create(Application)
Set ptcls = TPoint
Set pt    = RecNew(ptcls)

 

Wrappers for types itself – are singletons. So, testing for equality is allowed.

Wrappers for units itself.

Units itself are also available for script and represented by special IDispatch objects.

Example:

 

Set unvar = Unit1
Set form  = unvar.Form1
Set form2 = unvar.TForm1.Create(…)

 

Wrappers for units itself – are singletons. So, testing for equality is allowed.

 

Additional mapping utilities

 

There some utilities hard-coded in System.pas wrapper unit to support Delphi to OleVariant mapping in scrip code.

 

Script utilities for working with Delphi sets

 

Following utilities are used for working with Delphi 'set' types:

 

S = SetEmpty
Include S, fsBold
Exclude S, fsBold
S = SetMake(fsBold, fsItalic)
If SetIn(fsBold, s) then...

 

SetEmpty function returns empty set that is a set without any element included.

Include and Exclude functions works like corresponding Delphi functions. Note that first argument – is ByRef argument.

SetMake works like Delphi set contructor [fsBold, fsItalic]. The function takes variable numbers of parameters.

SetIn function works like Delphi in operator. The returned value type is Boolean.

 

Though set are represented as integers (varInteger variant type code), it is not convenient to use integer operations, like +, - with sets.

 

Note: our PasScript language supports working with sets natively. It uses the Delphi syntax, like:

 

S := [];
Include(S, fsBold);
Exclude(S, fsBold);
S := [fsBold, fsItalic];
if fsBold in s then...

 

Script utilities for working with Delphi records

 

Following utilities are used for working with Delphi records:

 

Set R  = TPoint.Create
R.X    = 7
R.Y    = 9
Set R2 = R.Copy
If RecIs(R, TPoint) Then...

 

Because records are represented as IDispatch objects, they actually have reference type semantic in script code.

So, you have to use Create pseudo-constructor to create new record instance before you can use it. Also, simply assigning R to R2 you will just copy a reference to record; this will not create two different records; so, you have to use Copy pseudo-function to make real record copy.

RecIs function just helps you to test record type. Function return value type is Boolean.

 

Note: our NativeVb language supports working with records naturally with its New and TypeOf/Is syntax:

 

Set R = New TPoint
If TypeOf R Is TPoint Then...

 

PasScript language also supports testing of record type using its is operator:

 

R := TPoint.Create;
if R is TPoint then...

 

Script utilities for working with event handlers

 

Another one function available in System.pas wrapper unit for creating script event handlers – is EventMake:

 

Sub Button1Click(Sender)
  MsgBox “Hellow World!”
End Sub
 
Button1.OnClick = EventMake(“Button1Click”)

 

Use EventMake function to create a reference to script event handler inside script code. Note, that from Delphi code you can use EventToVariantS global function, providing a reference to script control as a context (AContext parameter).

 

Note: out native languages supports event handler syntax naturally:

 

PasScript: Button1.OnClick := @Button1Click;

NativeVB: Button1.OnClick = GetRef("Button1Click"), where GetRef is a standard VbScript intrinsic function.