views:

32

answers:

3

For languages what are some common ways of doing plugins or extending runtime code via user given modules/libraries/code...?? I was thinking of C/C++ but how other languages do this may be applicable as well.

A: 

OSGi is a component framework for Java that is widely used and is the basis of the Eclipse framework, which addresses pluggable UIs too.

djna
A: 

One way is to have the module compiled into a dynamic object, which the main program opens with dlopen(). It then uses dlsym() to look for a specific initialisation function which the module must define, and calls it. For example, you might say that the module stored in foo.so must define a function called module_foo_init().

The module's init routine then typically calls functions provided by main program to register handlers for certain events or hooks.

caf
A: 

The general way of doing this in most languages I've worked in:

  • You have two binary files, your main program, and a "plugin interface library".
  • The plugin interface library basically only contains either an interface or abstract base class, or something similar that plugins can code against and extend.
  • The main program uses reflection or some other standardized interface to extract an instance of a class that implements the interface from all the plugin libraries. Repeat this step for all plugins
  • Calls are then made against the interface whenever interaction with the plugin is required.

Designing the last part is where most of the work comes in. What calls or hooks, or whatever should be allowed? How are the registered, are they just functions, which can possibly be empty, or will there be some sort of "hook registration"? If the latter, how will that work (I usually pass an object into the constructor that can be used to register hooks with)?

Matthew Scharley