Registeging Delphi classes and methods

<< Click to Display Table of Contents >>

Navigation:  Using Delphi classes and functions >

Registeging Delphi classes and methods

Registering Delphi classes is necessary (for Delphi 2009+) only in following cases

object of this class will be created in script via constructor

class is used in 'is' operator

object of this class is used in for-in statement.

RTTI is disabled for the class.

 

Classes can be registered for global usage (in any script) or for single Scripter instance.

To register global class use HtScriptGlobal instance for registration.

 

Delphi class registration:

 

 Scripter.RegisterClass(<constructor declaration>, <constructor>, <class>);

 

Example:

 

  HtScriptGlobal.RegisterClass('Create()', @TStringList.Create, 

   TStringList);

 

Registering methods and properties:

 

For Delphi 2009+ and classes with RTTI enabled, registering properties and methods is optional. All properties and methods that has RTTI or Extended RTTI will be available in script without registration.

 

Example of registering class with properties and methods:

 

  TStringsHack = class(TStrings);
 
  with HtScriptGlobal.RegisterClass('Create()', @TStrings.Create, TStrings) do
  begin
   RegisterProperty('Count', 'integer', @TStringsHack.GetCount, nil);
   RegisterProperty('Items', 'string', @TStringsHack.Get, @TStringsHack.Put,

     'integer', true);
   RegisterProperty('Objects', 'TObject', @TStringsHack.GetObject, 

     @TStringsHack.PutObject, 'integer');
   RegisterProperty('Text', 'string', @TStringsHack.GetTextStr, 

     @TStringsHack.SetTextStr);
   RegisterMethod('Add(const s: string)', @TStrings.Add);
   RegisterMethod('AddObject(const s: string; O: TObject)', 

     @TStrings.AddObject);
   RegisterMethod('Delete(Index: integer)', 

     @TStrings.Delete);
   RegisterMethod('Exchange(Index1, Index2: integer)', 

     @TStrings.Exchange);
   RegisterMethod('Insert(Index: Integer; const s: string)', 

     @TStrings.Insert);
end;