Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-04

Create class descendant at runtime?

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Craig Peterson

Hi guys,

Does Free Pascal or any of the included packages have a way to create a descendant of a class at runtime? Before anyone tells me it's a bad idea, I know. I have an existing class-based registration scheme that I'm adding some dynamically loaded plugins to and the alternatives are all messier.

Craig Peterson Scooter Software
Michael Schnell
to Craig Peterson
Craig Peterson wrote
Does Free Pascal or any of the included packages have a way to create a descendant of a class at runtime?

Only a script interpreter could possibly create a , there is a Pascal script interpreter usable with fpc. And in a script I don't think this is a bad idea (but I am not an expert with scripts).

-Michael

Mark Morgan Lloyd
to Michael Schnell
Michael Schnell wrote 7 lines
On 04/29/2014 03:58 AM, Craig Peterson wrote: > Does Free Pascal or any of the included packages have a way to create a > descendant of a class at runtime? Only a script interpreter could possibly create a Type at runtime. AFAIK, there is a Pascal script interpreter usable with fpc. And in a script I don't think this is a bad idea (but I am not an expert with scripts).

Or possibly building the source for a dll/so on the fly. However the entire custom class would have to be hidden in the library, i.e. couldn't be exposed by the interface since this would have to be known when the main program was compiled.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Schnell
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
Or possibly building the source for a dll/so on the fly.

A nice and funny idea combining the benefits of scripting and compiling. A little bit similar to the "ahead of time" reatlime compiler in a Java or CIL framework.

Did somebody ever indeed put something like this to work ?

-Michael

Mark Morgan Lloyd
to Michael Schnell
Michael Schnell wrote
A little bit similar to the "ahead of time" reatlime

:-)

compiler in a Java

or CIL framework. Did somebody ever indeed put something like this to work ?

I've got something where I can recompile a .so and the main program reloads it automatically (usually) without crashing. To clarify: it fails to reload on occasion possibly because it drops a signal but I've not seen it crash since I'm careful about when the swap is attempted.

I've not tried building something into the main program to reconstruct the source of the library and recompile it. I'd note that I'm being paranoid about protecting the interface- lots of magic numbers- and that the .so is always generated with a unique name.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Martin Frb
to Craig Peterson: Does Free Pascal or any of the included packages have a way to create a descendant of a…
Craig Peterson wrote 8 lines
Hi guys, Does Free Pascal or any of the included packages have a way to create a descendant of a class at runtime? Before anyone tells me it's a bad idea, I know. I have an existing class-based registration scheme that I'm adding some dynamically loaded plugins to and the alternatives are all messier.

Well. Nothing is impossible. But somethings are not advice-able.

It can be done. But it must be checked with every Version of FPC, it may break on every new version.
Same it must be checked with ever compiler setting, and may break with any of them.

Also: inherited calls will not work in such cases, and you need to write very messy code, in order for it to work with such a self build class.

The basic is:

PPointer = ^Pointer

PPointer(SomeObject)^ contains the address of the class data (size and content can be found in the RTL).

You can copy and modify that. You also must copy the VMT, and that means you need to know how many virtual methods you declared (there is no count stored / so you must know). And you must disable any optimizations to class structure/ vmt optimizations.

All in all, you will have really hard to read and easy to break code.

Paul Breneman
to Michael Schnell: A nice and funny idea combining the benefits of scripting and compiling. A little bit…
Michael Schnell wrote 8 lines
On 04/29/2014 10:34 AM, Mark Morgan Lloyd wrote: > > Or possibly building the source for a dll/so on the fly. A nice and funny idea combining the benefits of scripting and compiling. A little bit similar to the "ahead of time" reatlime compiler in a Java or CIL framework. Did somebody ever indeed put something like this to work ?

That sounds like an interesting project. Maybe I should add such an example on one of these pages (that contain mini Free Pascal distributions): www.CtrlTerm.com
www.TurboControl.com/monitor.htm

Mark Morgan Lloyd
to Paul Breneman
Paul Breneman wrote 9 lines
On 04/29/2014 04:59 AM, Michael Schnell wrote: > On 04/29/2014 10:34 AM, Mark Morgan Lloyd wrote: >> >> Or possibly building the source for a dll/so on the fly. > A nice and funny idea combining the benefits of scripting and compiling. > A little bit similar to the "ahead of time" reatlime compiler in a Java > or CIL framework. > > Did somebody ever indeed put something like this to work ?

In my case it was a MIDI filter: display messages in a debugging window as they came in, edit the library to handle them specially, reload and repeat. The result was a program that could transpose multiple manuals and switch between transpositions depending on button presses, but where the message emitted by each button was very much dependant on the keyboard model and manufacturer.

I'm sure there's a better example.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Schnell
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
In my case it was a MIDI filter:...

Ahh, one keyboarder more on this list :-) .

Hi to you.

(off topic:
I once successfully tested doing MIDI/Audio VST plugins with Delphi, using an appropriate SDK that is available in the Internet. Right now I am done building a Windows embedded PC based blackbox for the sound software I use with a keyboard. Doing VS Plugins with fpc myself would be a nice option, but of course this box completely is on 64 Bit and I did not find an SDK to be used on this behalf. Did you ? )

-Michael

Mark Morgan Lloyd
to Michael Schnell
Michael Schnell wrote 6 lines
On 04/29/2014 06:59 PM, Mark Morgan Lloyd wrote: > > In my case it was a MIDI filter:... Ahh, one keyboarder more on this list :-) . Hi to you.

Only marginally. A small and elderly Yamaha hooked up to PianoBooster and a MIDI output box, with Brighton/Bristol available as an alternative output device.

Show quoted text 8 lines
(off topic: I once successfully tested doing MIDI/Audio VST plugins with Delphi, using an appropriate SDK that is available in the Internet. Right now I am done building a Windows embedded PC based blackbox for the sound software I use with a keyboard. Doing VS Plugins with fpc myself would be a nice option, but of course this box completely is on 64 Bit and I did not find an SDK to be used on this behalf. Did you ? )

I remember. In my case I'm interfacing to ALSA on 32-bit x86 Linux, I found a couple of sets of declarations etc. in Pascal... I can't remember whether one of them was adequate or if I merged them in some way. It seems to me that ALSA needs a lot of housekeeping code to do comparatively simple jobs.

In part, the project was an experiment in interfacing plugins etc. I was interested to explore the extent to which a plugin could be written as both a library and as a standalone program, in an attempt to conform to the spirit of GPL (answer: on unix it can't).

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]