- rename initModule() to createModule() (as suggested by Andy)
- make it public - enable handleCommand() to execute a binding (or other nasal code defined in a property system subtree) in a particular namespace (-> "module" child)
This commit is contained in:
parent
5c31daf808
commit
ece07db538
2 changed files with 17 additions and 8 deletions
|
@ -385,7 +385,7 @@ void FGNasalSys::loadPropertyScripts()
|
||||||
const char* src = n->getStringValue("script");
|
const char* src = n->getStringValue("script");
|
||||||
if(!n->hasChild("script")) src = 0; // Hrm...
|
if(!n->hasChild("script")) src = 0; // Hrm...
|
||||||
if(src)
|
if(src)
|
||||||
initModule(module, n->getPath(), src, strlen(src));
|
createModule(module, n->getPath(), src, strlen(src));
|
||||||
|
|
||||||
if(!file_specified && !src)
|
if(!file_specified && !src)
|
||||||
SG_LOG(SG_NASAL, SG_ALERT, "Nasal error: " <<
|
SG_LOG(SG_NASAL, SG_ALERT, "Nasal error: " <<
|
||||||
|
@ -422,14 +422,14 @@ void FGNasalSys::loadModule(SGPath file, const char* module)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
initModule(module, file.c_str(), buf, len);
|
createModule(module, file.c_str(), buf, len);
|
||||||
delete[] buf;
|
delete[] buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and run. Save the local variables namespace, as it will
|
// Parse and run. Save the local variables namespace, as it will
|
||||||
// become a sub-object of globals.
|
// become a sub-object of globals.
|
||||||
void FGNasalSys::initModule(const char* moduleName, const char* fileName,
|
void FGNasalSys::createModule(const char* moduleName, const char* fileName,
|
||||||
const char* src, int len)
|
const char* src, int len)
|
||||||
{
|
{
|
||||||
naRef code = parse(fileName, src, len);
|
naRef code = parse(fileName, src, len);
|
||||||
if(naIsNil(code))
|
if(naIsNil(code))
|
||||||
|
@ -476,16 +476,24 @@ bool FGNasalSys::handleCommand(const SGPropertyNode* arg)
|
||||||
// location in the property tree. arg->getPath() returns an empty
|
// location in the property tree. arg->getPath() returns an empty
|
||||||
// string.
|
// string.
|
||||||
const char* nasal = arg->getStringValue("script");
|
const char* nasal = arg->getStringValue("script");
|
||||||
|
const char* moduleName = arg->getStringValue("module");
|
||||||
naRef code = parse("<command>", nasal, strlen(nasal));
|
naRef code = parse("<command>", nasal, strlen(nasal));
|
||||||
if(naIsNil(code)) return false;
|
if(naIsNil(code)) return false;
|
||||||
|
|
||||||
|
naRef locals = naNil();
|
||||||
|
if (moduleName[0]) {
|
||||||
|
naRef modname = naNewString(_context);
|
||||||
|
naStr_fromdata(modname, (char*)moduleName, strlen(moduleName));
|
||||||
|
if(!naHash_get(_globals, modname, &locals))
|
||||||
|
locals = naNewHash(_context);
|
||||||
|
}
|
||||||
// Cache the command argument for inspection via cmdarg(). For
|
// Cache the command argument for inspection via cmdarg(). For
|
||||||
// performance reasons, we won't bother with it if the invoked
|
// performance reasons, we won't bother with it if the invoked
|
||||||
// code doesn't need it.
|
// code doesn't need it.
|
||||||
_cmdArg = (SGPropertyNode*)arg;
|
_cmdArg = (SGPropertyNode*)arg;
|
||||||
|
|
||||||
// Call it!
|
// Call it!
|
||||||
naRef result = naCall(_context, code, naNil(), naNil(), naNil());
|
naRef result = naCall(_context, code, naNil(), naNil(), locals);
|
||||||
if(!naGetError(_context)) return true;
|
if(!naGetError(_context)) return true;
|
||||||
logError();
|
logError();
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -42,6 +42,9 @@ public:
|
||||||
// Callbacks for command and timer bindings
|
// Callbacks for command and timer bindings
|
||||||
virtual bool handleCommand(const SGPropertyNode* arg);
|
virtual bool handleCommand(const SGPropertyNode* arg);
|
||||||
|
|
||||||
|
void createModule(const char* moduleName, const char* fileName,
|
||||||
|
const char* src, int len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class FGNasalScript;
|
friend class FGNasalScript;
|
||||||
|
|
||||||
|
@ -58,8 +61,6 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
void loadPropertyScripts();
|
void loadPropertyScripts();
|
||||||
void initModule(const char* moduleName, const char* fileName,
|
|
||||||
const char* src, int len);
|
|
||||||
void hashset(naRef hash, const char* key, naRef val);
|
void hashset(naRef hash, const char* key, naRef val);
|
||||||
void logError();
|
void logError();
|
||||||
naRef parse(const char* filename, const char* buf, int len);
|
naRef parse(const char* filename, const char* buf, int len);
|
||||||
|
|
Loading…
Add table
Reference in a new issue