Note: Before you read the post I want you to please excuse my knowledge on zig, compilers and shared libraries. I know I’m not great and im happy to learn a lot from you all
Ever since I learnt go in 2020 i’ve had the itch to make a cross platform plugin system in golang that compiles to respective platform as shared libraries and allows plug-and-play like capabilities.
Think of a RPC HTTP Server that allows you to add more features by just dragging a .dll file (or a .so for Unix systems) into a “plugins” folder.
I’ve tried to replicate it in golang using shared library compilation. Go also has a plugin build mode, but it’s still not compatible with Windows.
Real Question:
Just my thoughts and 2 cents, would it be possible to build a plugin system that allows us to have portable plug-and-play features in zig? If so how should we/one build it? I would like you guys opinions.
Hi welcome to the forum :). I’m no expert, but my guess would be that loading/unloading plugins, and monitoring a folder or events for loading/unloading plugins should be quite straightforward to achieve. As for the “interface” for the plugins that’s another story, If you need something quite static, defining your own VTable (for example you can look at something like the Allocator VTable) of optional function pointer should do the trick, for something more dynamic tho I don’t have any idea how you could do it, It’s probably doable but It would require more knowledge than I have. Have you considered embedding a dynamic language, like Lua ? It could potentially solve your problem if performance is not absolutely critical.
Native code plugins (versus interpreters/bytecode vms) by their nature are separate compilation units, and Zig is a single-compilation-unit language, and all communication between host and plugin must be via a C interface.
The other half is a how you discover and load the plugins which as previously noted, is easily achievable.
In some cases you’d have a strategy where your plugin convention is to use an array of function pointers communicated by a single external symbol or function. Otherwise, the caller must handle the binding by lookups specific to the OS binary format in question (ELF, COFF…).
Wasm could be a good fit here. Many modern languages, including zig, support compiling to wasm and there are a range of VMs you could embed in your app to handle running plugin code.
@pierrelgol you’re right, when it comes to performance having a VTable / Exported Function Table should be enough.
@ajoino I’m assuming this to be a lib, like a library that allows us to have modular codebase and split the functionalities to different pluggable module each. Something that comes to my mind is yt-dlp (youtube-dl) rewrite in zig. It would support youtube and other urls, but you can support more websites by dragging and dropping a .DLL or a .so file