Created
May 16, 2025 00:56
-
-
Save crazymonkyyy/f3d40f5b7c0454bfaa118d5a73a3fe1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
lazy hotload: hot reload a single function file, depends on opend imports | |
kinda dumb, dont expect templates, changing headers or anything to work | |
pass relivtivepath filename without the `.d` | |
*/ | |
import core.stdc.stdlib; | |
import core.sys.posix.dlfcn; | |
//import std; | |
enum PATH=__FILE_FULL_PATH__[0..$-__FILE__.length]; | |
template hotload(string file){ | |
mixin("import "~file~";"); | |
enum code=import(file~".d"); | |
enum name=findname(code); | |
pragma(msg,"hotloading file "~file~" as function "~name); | |
alias F=mixin(file~"."~name); | |
pragma(msg,"this is parsing as "~typeof(F).stringof); | |
enum mangle=F.mangleof; | |
enum filepath=PATH~file~".so"; | |
pragma(msg,"mangle:"~mangle); | |
bool recompile=true; | |
void* handle; | |
typeof(&F) call; | |
auto hotload(T...)(T args){ | |
if(recompile){ | |
compile(file); | |
} | |
handle=dlopen(&filepath[0],RTLD_LAZY); | |
assert(handle!=null,"put here: dlerror.to!string"); | |
call=cast(typeof(&F)) dlsym(handle,mangle); | |
auto out_=call(args); | |
dlclose(handle); | |
return out_; | |
} | |
} | |
string findname(string code){ | |
int i/*,j*/,k; | |
while(code[++i]!='('){} | |
k=i; | |
while(code[--i]!=' '){} | |
i++; | |
return code[i..k]; | |
} | |
void compile(string file){ | |
enum compiler="opend -shared -of="; | |
system(&(compiler~file~".so "~file~".d\0")[0]); | |
} | |
//void main(){ | |
// import std; | |
// while(true){ | |
// hotload!"foo"(3).writeln; | |
// readln; | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment