Dynamic object methods

Prof A Olowofoyeku (The African Chief) chiefsoft at bigfoot.com
Tue Apr 29 08:57:45 CEST 2003

Hi Frank

Actually, there is a documented way of accessing the dynamic method 
index. I found it in the Delphi help file, and you won't like it - the 
access method is via asm. In any case, below is the documentation - and 
you can do with it (or not) as you deem fit. In the example given, 
"message MYMESSAGE" is the same thing as the BP "virtual methodindex".

Here it goes:

"Two additional directives allow assembly code to access dynamic and 
virtual methods: VMTOFFSET and DMTINDEX.

VMTOFFSET retrieves the offset in bytes of the virtual method pointer 
table entry of the virtual method argument from the beginning of the 
virtual method table (VMT). This directive needs a fully specified 
class name with a method name as a parameter (for example, 
TExample.VirtualMethod), or an interface name and an interface method 

DMTINDEX retrieves the dynamic method table index of the passed dynamic 
method. This directive also needs a fully specified class name with a 
method name as a parameter, for example, TExample.DynamicMethod. To 
invoke the dynamic method, call System. at CallDynaInst with the (E)SI 
register containing the value obtained from DMTINDEX.


Methods with the message directive are implemented as dynamic methods 
and can also be called using the DMTINDEX technique. For example:

    TMyClass = class
      procedure x; message MYMESSAGE;

The following example uses both DMTINDEX and VMTOFFSET to access 
dynamic and virtual methods:

program Project2;

  TExample = class
    procedure DynamicMethod; dynamic;
    procedure VirtualMethod; virtual;

procedure TExample.DynamicMethod;


procedure TExample.VirtualMethod;


procedure CallDynamicMethod(e: TExample);
  // Save ESI register

  // Instance pointer needs to be in EAX
  MOV     EAX, e

  // DMT entry index needs to be in (E)SI
  MOV     ESI, DMTINDEX TExample.DynamicMethod
  // Now call the method
  CALL    System. at CallDynaInst

  // Restore ESI register

procedure CallVirtualMethod(e: TExample);
  // Instance pointer needs to be in EAX
  MOV     EAX, e

  // Retrieve VMT table entry
  MOV     EDX, [EAX]

  // Now call the method at offset VMTOFFSET
  CALL    DWORD PTR [EDX + VMTOFFSET TExample.VirtualMethod]


  e: TExample;

  e := TExample.Create;

Then the Delphi help file provides extra information about the features 
of dynamic methods, as follows:

1. "Dynamic methods are virtual methods with a slightly different 
dispatch mechanism. Because dynamic methods don't have entries in the 
object's virtual method table, they can reduce the amount of memory 
that objects consume. However, dispatching dynamic methods is somewhat 
slower than dispatching regular virtual methods. If a method is called 
frequently, or if its execution is time-critical, you should probably 
declare it as virtual rather than dynamic.

Objects must store the addresses of their dynamic methods. But instead 
of receiving entries in the virtual method table, dynamic methods are 
listed separately. The dynamic method list contains entries only for 
methods introduced or overridden by a particular class. (The virtual 
method table, in contrast, includes all of the object's virtual 
methods, both inherited and introduced.) Inherited dynamic methods are 
dispatched by searching each ancestor's dynamic method list, working 
backwards through the inheritance tree.

To make a method dynamic, add the directive dynamic after the method 

2. "Virtual versus dynamic

Virtual and dynamic methods are semantically equivalent. They differ 
only in the implementation of method-call dispatching at runtime. 
Virtual methods optimize for speed, while dynamic methods optimize for 
code size.

In general, virtual methods are the most efficient way to implement 
polymorphic behavior. Dynamic methods are useful when a base class 
declares many overridable methods which are inherited by many 
descendant classes in an application, but only occasionally overridden.

Only use dynamic methods if there is a clear, observable benefit. 
Generally, use virtual methods."

Best regards, The Chief
Prof. Abimbola A. Olowofoyeku (The African Chief) 
web:  http://www.bigfoot.com/~african_chief/

More information about the Gpc mailing list