1
0
Fork 0

Nasal: support for module reload via command

This won’t work for all modules, depending on what inter-dependencies
exist.
This commit is contained in:
James Turner 2022-11-06 11:44:09 +00:00
parent 6bb22caea2
commit c509744b0f
3 changed files with 78 additions and 37 deletions

View file

@ -1036,6 +1036,17 @@ do_profiler_stop(const SGPropertyNode *arg, SGPropertyNode *root)
#endif
}
static bool do_reload_nasal_module(const SGPropertyNode* arg, SGPropertyNode*)
{
auto nasalSys = globals->get_subsystem<FGNasalSys>();
if (!nasalSys) {
SG_LOG(SG_GUI, SG_ALERT, "reloadModuleFromFile command: Nasal subsystem not found");
return false;
}
return nasalSys->reloadModuleFromFile(arg->getStringValue("module"));
}
////////////////////////////////////////////////////////////////////////
// Command setup.
@ -1054,6 +1065,7 @@ static struct {
} built_ins[] = {
{"null", do_null},
{"nasal", do_nasal},
{"nasal-module-reload", do_reload_nasal_module},
{"pause", do_pause},
{"load", do_load},
{"save", do_save},

View file

@ -800,7 +800,7 @@ public:
_sys->gcRelease(_gcRoot);
}
virtual bool operator()(const SGPropertyNode* aNode, SGPropertyNode * root)
bool operator()(const SGPropertyNode* aNode, SGPropertyNode* root) override
{
_sys->setCmdArg(const_cast<SGPropertyNode*>(aNode));
naRef args[1];
@ -1541,8 +1541,12 @@ bool FGNasalSys::createModule(const char* moduleName, const char* fileName,
if (!naHash_get(_globals, modname, &locals))
locals = naNewHash(ctx);
_cmdArg = (SGPropertyNode*)cmdarg;
// store the filename in the module hash, so we could reload it
naRef modFilePath = naNewString(ctx);
naStr_fromdata(modFilePath, (char*)fileName, strlen(fileName));
hashset(locals, "__moduleFilePath", modFilePath);
_cmdArg = (SGPropertyNode*)cmdarg;
callWithContext(ctx, code, argc, args, locals);
hashset(_globals, moduleName, locals);
@ -1565,6 +1569,28 @@ void FGNasalSys::deleteModule(const char* moduleName)
naFreeContext(ctx);
}
bool FGNasalSys::reloadModuleFromFile(const std::string& moduleName)
{
if (!_inited || naIsNil(_globals)) {
return false;
}
naRef locals = naHash_cget(_globals, (char*)moduleName.c_str());
if (naIsNil(locals)) {
// no such module
return false;
}
naRef filePath = naHash_cget(locals, (char*)"__moduleFilePath");
if (naIsNil(filePath)) {
return false;
}
SGPath path = SGPath::fromUtf8(naStr_data(filePath));
deleteModule(moduleName.c_str());
return loadModule(path, moduleName.c_str());
}
naRef FGNasalSys::getModule(const std::string& moduleName) const
{
naRef mod = naHash_cget(_globals, (char*)moduleName.c_str());

View file

@ -163,6 +163,8 @@ public:
*/
static naRef getPropertyValue(naContext c, SGPropertyNode* node);
bool reloadModuleFromFile(const std::string& moduleName);
private:
void initLogLevelConstants();
@ -176,6 +178,7 @@ private:
std::string& errors);
naRef genPropsModule();
private:
//friend class FGNasalScript;
friend class FGNasalListener;