Wednesday, March 15, 2006

Polymorphism ad nauseum

As all object oriented languages Delphi (née Object Pascal) supports the concept of polymorphism. Polymorphism is a Greek word that literally means many-shapes or many-forms. In programming-speak it refers to the idea that invoking a conceptual operation is detached from the actual implementation of that operation. In fact, at run-time the actual implemented operation may change radically depending on the type of the object being used to perform the operation.

One of the driving factors for OOP were graphical user-interfaces (GUIs). The archetypical schoolbook example of polymorphism follows the translation slavishly by using an abstract TShape base class with TRectangle and TEllipse descendants that override a virtual Draw routine. The main drawing routine loops over a list of TShape instances, calling the virtual Draw method - each call will be handled by a descendant and draw a rectangle or ellipse (or any other) shape.

In its most basic concept polymorphism is about changing the target address of a CALL instruction dynamically at runtime. From this angle the numbers of mechanisms that can provide polymorphism are many.

At the Delphi language level:
procedure pointer
event (procedure of object)

By using dirty hacks:
overwrite virtual method pointer
overwrite VMT pointer
overwrite message/dynamic slot
overwrite call-site address
overwrite callee instructions
overwrite DLL imports slots

I'll try to cover some of these in upcoming articles, so stay tuned!

No comments:

Copyright © 2004-2007 by Hallvard Vassbotn