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 #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. // Command setup.
@ -1054,6 +1065,7 @@ static struct {
} built_ins[] = { } built_ins[] = {
{"null", do_null}, {"null", do_null},
{"nasal", do_nasal}, {"nasal", do_nasal},
{"nasal-module-reload", do_reload_nasal_module},
{"pause", do_pause}, {"pause", do_pause},
{"load", do_load}, {"load", do_load},
{"save", do_save}, {"save", do_save},

View file

@ -800,7 +800,7 @@ public:
_sys->gcRelease(_gcRoot); _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)); _sys->setCmdArg(const_cast<SGPropertyNode*>(aNode));
naRef args[1]; naRef args[1];
@ -1541,8 +1541,12 @@ bool FGNasalSys::createModule(const char* moduleName, const char* fileName,
if (!naHash_get(_globals, modname, &locals)) if (!naHash_get(_globals, modname, &locals))
locals = naNewHash(ctx); 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); callWithContext(ctx, code, argc, args, locals);
hashset(_globals, moduleName, locals); hashset(_globals, moduleName, locals);
@ -1565,6 +1569,28 @@ void FGNasalSys::deleteModule(const char* moduleName)
naFreeContext(ctx); 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 FGNasalSys::getModule(const std::string& moduleName) const
{ {
naRef mod = naHash_cget(_globals, (char*)moduleName.c_str()); naRef mod = naHash_cget(_globals, (char*)moduleName.c_str());

View file

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