Nasal-Interpreter/stl/module.nas

43 lines
1.0 KiB
Plaintext
Raw Normal View History

# module.nas
2022-03-05 21:52:29 +08:00
# ValKmjolnir 2022/3/5
# this provides safe usage of dylib
# when dylib is closed,
# all the invalid functions cannot be called
var module_call_func=func(fptr,args){
2022-07-09 16:24:58 +08:00
return __dlcall;
2022-03-05 21:52:29 +08:00
}
var extern={
new: func(fptr){
var isopen=1;
2022-03-05 21:52:29 +08:00
return {
close:func(){isopen=0;},
call:func(args...){
return (!isopen)?
nil:
module_call_func(fptr,args);
}
2022-03-05 21:52:29 +08:00
};
}
};
var module={
new: func(name){
var lib=dylib.dlopen(name);
var f={};
2022-03-05 21:52:29 +08:00
return {
get:func(symbol){
if(contains(f,symbol))
return f[symbol];
var fp=extern.new(dylib.dlsym(lib,symbol));
f[symbol]=fp;
return fp;
},
close: func(){
foreach(var i;keys(f))
f[i].close();
dylib.dlclose(lib);
}
2022-03-05 21:52:29 +08:00
};
}
};