Skip to content

Instantly share code, notes, and snippets.

@crazymonkyyy
Last active June 21, 2025 03:01
Show Gist options
  • Save crazymonkyyy/e6edc498376da0501c851c8c339eba4b to your computer and use it in GitHub Desktop.
Save crazymonkyyy/e6edc498376da0501c851c8c339eba4b to your computer and use it in GitHub Desktop.
import std;
import core.stdc.stdlib;
import core.sys.posix.dlfcn;
version(D_OpenD){
enum compiler="opend -shared -of=";
} else {
version(DigitalMars) enum compiler="dmd -shared -of=";
version(LDC) enum compiler="ldc -shared -of=";
}
//unittest{
// compiler.writeln;
//}
bool maxwatch(T,S)(ref T a,T b,S c){//todo revist the concept, I think theres more here
if(a+c>=b){return false;}
a=b;
return true;
}
//void maxwatchb(T,S)(ref T a,T b,S c){
// {return false;}
// a=b;
// return true;
//}
//unittest{
// int lastmax=int.min;
// foreach(i;[1,2,3,102,205,260,500]){
// "---".writeln;
// lastmax.writeln;
// lastmax.maxwatcha(i,100).writeln(i);
// }
//}
template Stringof(alias A){enum Stringof=__traits(identifier,A);}//https://github.com/dlang/dmd/issues/19266
enum PATH=__FILE_FULL_PATH__[0..$-__FILE__.length];//todo move to header and fix import to be relitive
mixin template hotloadImport(string file,alias mark="hotload",Duration iolimit=dur!"msecs"(500)){
template impl(string _:file){
mixin("import "~file~";");
alias functions=Filter!(isCallable,getSymbolsByUDA!(mixin(file),mark));
static assert(functions.length!=0,"no functions detected, add a `@\"hotload\":`");
bool considercompiling(){
static SysTime lastio, lastmodified;
return maxwatch(lastio,Clock.currTime(),iolimit) && maxwatch(lastmodified,(file~".d").timeLastModified,dur!"msecs"(0));
}
enum filepath=PATH~file~".so";
template hotload(alias F){
auto hotload(T...)(T args){
void* handle;//copyed from outside scope, what was I thinking before?
if(considercompiling){compile(file);}
handle=dlopen(&filepath[0],RTLD_LAZY);
assert(handle!=null,dlerror.to!string);
typeof(&F) call=cast(typeof(&F)) dlsym(handle,F.mangleof);
scope(exit) dlclose(handle);//does this do anything?
return call(args);
}}
}
static foreach(A;impl!file.functions){
pragma(msg,Stringof!A);
mixin("alias "~Stringof!A~"=impl!file.hotload!A;");
}
}
void system_(immutable(char)* c){
c.to!string.writeln;
system(c);
}
void compile(string file){
//enum compiler="opend -shared -of=";
system_(&(compiler~file~".so "~file~".d\0")[0]);
}
@"hotload"{
int bar(){return 100;}
void foo(){"bye".writeln;}
}
unittest{
int i;
mixin hotloadImport!"hotloading";
while(i++<bar){
foo;
import core.thread;
Thread.sleep(100.msecs);
}
}
import std;
import core.stdc.stdlib;
import core.sys.posix.dlfcn;
version(D_OpenD){
enum compiler="opend -shared -of=";
} else {
version(DigitalMars) enum compiler="dmd -shared -of=";
version(LDC) enum compiler="ldc -shared -of=";
}
bool maxwatch(T,S)(ref T a,T b,S c){//todo revist the concept, I think theres more here
if(a+c>=b){return false;}
a=b;
return true;
}
template Stringof(alias A){enum Stringof=__traits(identifier,A);}//https://github.com/dlang/dmd/issues/19266
enum PATH=__FILE_FULL_PATH__[0..$-__FILE__.length];//todo move to header and fix import to be relitive
mixin template hotloadImport(string file,alias mark="hotload",Duration iolimit=dur!"msecs"(500)){
template impl(string _:file){
mixin("import "~file~";");
alias functions=Filter!(isCallable,getSymbolsByUDA!(mixin(file),mark));
static assert(functions.length!=0,"no functions detected, add a `@\"hotload\":`");
bool considercompiling(){
static SysTime lastio, lastmodified;
return maxwatch(lastio,Clock.currTime(),iolimit) && maxwatch(lastmodified,(file~".d").timeLastModified,dur!"msecs"(0));
}
enum filepath=PATH~file~".so";
template hotload(alias F){
auto hotload(T...)(T args){
void* handle;//copyed from outside scope, what was I thinking before?
if(considercompiling){compile(file);}
handle=dlopen(&filepath[0],RTLD_LAZY);
assert(handle!=null,dlerror.to!string);
typeof(&F) call=cast(typeof(&F)) dlsym(handle,F.mangleof);
scope(exit) dlclose(handle);//does this do anything?
return call(args);
}}
}
static foreach(A;impl!file.functions){
pragma(msg,Stringof!A);
mixin("alias "~Stringof!A~"=impl!file.hotload!A;");
}
}
void compile(string file)=>system(&(compiler~file~".so "~file~".d\0")[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment