pathologically simple hotloading

H. S. Teoh hsteoh at qfbox.info
Fri Jun 5 23:46:12 UTC 2026


On Fri, Jun 05, 2026 at 08:18:58PM +0000, monkyyy via Digitalmars-d-learn wrote:
> can anyone imagine any simplifications?

What's the point of iterating the `store` array?    Just use an AA and
lookup the function name as string key, if null, fill it with dlsym. If
still null, it doesn't exist, else call it right away.  Instant
on-demand function binding!


--T

> ```d app.d
> import std;
> import core.stdc.stdlib;
> import core.sys.posix.dlfcn;
> 
> struct enity{
> 	int i;
> 	ubyte[100] j;
> 	//int x;
> 	//int y;
> }
> string exe(string input){
> 	//auto config=0;//Config.stderrPassThrough|Config.retainStdout;
> 	//input.writeln;
> 	return input.executeShell(/*null,config*/).output[0..min($,$-1)];
> }
> void runcompiler(){
> 	"opend -shared -of=hotload.so hotload.d".exe.writeln;
> }
> void* handle;
> void*[] store;
> string[] api;
> enum PATH=__FILE_FULL_PATH__[0..$-__FILE__.length];
> void loadso(){
> 	handle=dlopen((PATH~"/hotload.so").toStringz,RTLD_LAZY);
> 	assert(handle!=null,dlerror.to!string);
> 	store=[];
> 	foreach(s;api){
> 		store~=dlsym(handle,s.toStringz);
> 	}
> }
> void hotload(string s,T...)(ref enity e,T args){
> 	alias F=extern(C) void function(enity*,T);
> 	auto I=api.countUntil(s);
> 	if(I==-1){return;}
> 	if(store[I]==null){return;}
> 	F call=cast(F)(store[I]);
> 	call(&e,args);
> }
> void main(){
> 	api~="foo";
> 	api~="bar";
> 	runcompiler;
> 	loadso;
> 	//---
> 	enity[10] enitys;
> 	foreach(ref e;enitys){
> 		hotload!"foo"(e);
> 	}
> 	foreach(e;enitys){
> 		e.i.writeln;
> 	}
> 	foreach(ref e;enitys){
> 		hotload!"bar"(e,true);
> 	}
> }
> ```
> 
> ```d hotload.d
> import std;
> extern(C):
> struct thingy{
> 	int x;
> 	int y;
> }
> void foo(thingy* t){
> 	t.x=uniform(0,10);
> 	t.y=uniform(20,50);
> 	(*t).writeln;
> }
> void bar(thingy* t,bool b){
> 	(*t).writeln;
> }
> ```
> 
> As written I believe ill need to improve my compiler bugs ulities to make a
> ct appendable string[]
> i'll need to detect enity-like data types and ensure that
> 
> trade off between a uda based like my old version: https://codeberg.org/monkyyy/raylib2026/src/commit/e5a7e93cc379981ca44bab70f92f3dfd68653944/hotloadlib.d#L35
> 
> I think a void* version is more flexible and should have less compile time
> (as hotload.d isnt even processed) its changing the "direction" of the type
> inference so by not reading `hotload` maybe I avoid spooky order of
> complation issues in exchange for less safety?
> 
> idk, I feel like this is all under explored, how to best use dlsym with
> template based apis?



More information about the Digitalmars-d-learn mailing list