X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FScripting%2FNasalSys.cxx;h=870d6def1455f0831e095243af62223d333bb799;hb=9c28ed02577e6d32e1365567107adbd048f3d743;hp=6163a30b92d66c930a42fef8b0506ed33b0b6114;hpb=142854d3d191817784fd15d9d51ba3742b9ce26c;p=flightgear.git diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 6163a30b9..870d6def1 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -71,7 +71,7 @@ bool FGNasalSys::parseAndRun(const char* sourceCode) if(naIsNil(code)) return false; - naCall(_context, code, naNil(), naNil(), naNil()); + naCall(_context, code, 0, 0, naNil(), naNil()); if(!naGetError(_context)) return true; logError(); @@ -86,11 +86,11 @@ FGNasalScript* FGNasalSys::parseScript(const char* src, const char* name) char buf[256]; if(!name) { - sprintf(buf, "FGNasalScript@%8.8x", (int)script); + sprintf(buf, "FGNasalScript@%p", script); name = buf; } - script->_code = parse(name, src); + script->_code = parse(name, src, strlen(src)); if(naIsNil(script->_code)) { delete script; return 0; @@ -337,7 +337,7 @@ void FGNasalSys::init() fullpath.append(dent->d_name); SGPath file(dent->d_name); if(file.extension() != "nas") continue; - readScriptFile(fullpath, file.base().c_str()); + loadModule(fullpath, file.base().c_str()); } // Pull scripts out of the property tree, too @@ -367,7 +367,7 @@ void FGNasalSys::loadPropertyScripts() const char* file = fn->getStringValue(); SGPath p(globals->get_fg_root()); p.append(file); - readScriptFile(p, module); + loadModule(p, module); j++; } @@ -378,14 +378,14 @@ void FGNasalSys::loadPropertyScripts() if(file) { SGPath p(globals->get_fg_root()); p.append(file); - readScriptFile(p, module); + loadModule(p, module); } */ const char* src = n->getStringValue("script"); if(!n->hasChild("script")) src = 0; // Hrm... if(src) - initModule(module, n->getPath(), src, strlen(src)); + createModule(module, n->getPath(), src, strlen(src)); if(!file_specified && !src) SG_LOG(SG_NASAL, SG_ALERT, "Nasal error: " << @@ -411,7 +411,7 @@ void FGNasalSys::logError() // Reads a script file, executes it, and places the resulting // namespace into the global namespace under the specified module // name. -void FGNasalSys::readScriptFile(SGPath file, const char* module) +void FGNasalSys::loadModule(SGPath file, const char* module) { int len = 0; char* buf = readfile(file.c_str(), &len); @@ -422,17 +422,15 @@ void FGNasalSys::readScriptFile(SGPath file, const char* module) return; } - initModule(module, file.c_str(), buf, len); + createModule(module, file.c_str(), buf, len); delete[] buf; } // Parse and run. Save the local variables namespace, as it will // become a sub-object of globals. -void FGNasalSys::initModule(const char* moduleName, const char* fileName, - const char* src, int len) +void FGNasalSys::createModule(const char* moduleName, const char* fileName, + const char* src, int len) { - if(len == 0) len = strlen(src); - naRef code = parse(fileName, src, len); if(naIsNil(code)) return; @@ -446,7 +444,7 @@ void FGNasalSys::initModule(const char* moduleName, const char* fileName, if(!naHash_get(_globals, modname, &locals)) locals = naNewHash(_context); - naCall(_context, code, naNil(), naNil(), locals); + naCall(_context, code, 0, 0, naNil(), locals); if(naGetError(_context)) { logError(); return; @@ -456,7 +454,6 @@ void FGNasalSys::initModule(const char* moduleName, const char* fileName, naRef FGNasalSys::parse(const char* filename, const char* buf, int len) { - if(len == 0) len = strlen(buf); int errLine = -1; naRef srcfile = naNewString(_context); naStr_fromdata(srcfile, (char*)filename, strlen(filename)); @@ -474,21 +471,25 @@ naRef FGNasalSys::parse(const char* filename, const char* buf, int len) bool FGNasalSys::handleCommand(const SGPropertyNode* arg) { - // Parse the Nasal source. I'd love to use the property name of - // the argument, but it's actually a *clone* of the original - // location in the property tree. arg->getPath() returns an empty - // string. const char* nasal = arg->getStringValue("script"); - naRef code = parse("", nasal); + const char* moduleName = arg->getStringValue("module"); + naRef code = parse(arg->getPath(true), nasal, strlen(nasal)); 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 // performance reasons, we won't bother with it if the invoked // code doesn't need it. _cmdArg = (SGPropertyNode*)arg; // Call it! - naRef result = naCall(_context, code, naNil(), naNil(), naNil()); + naRef result = naCall(_context, code, 0, 0, naNil(), locals); if(!naGetError(_context)) return true; logError(); return false; @@ -497,7 +498,7 @@ bool FGNasalSys::handleCommand(const SGPropertyNode* arg) // settimer(func, dt, simtime) extension function. The first argument // is a Nasal function to call, the second is a delta time (from now), // in seconds. The third, if present, is a boolean value indicating -// that "simulator" time (rather than real time) is to be used. +// that "real world" time (rather than simulator time) is to be used. // // Implementation note: the FGTimer objects don't live inside the // garbage collector, so the Nasal handler functions have to be @@ -514,7 +515,7 @@ void FGNasalSys::setTimer(int argc, naRef* args) naRef delta = argc > 1 ? args[1] : naNil(); if(naIsNil(delta)) return; - bool simtime = (argc > 2 && naTrue(args[2])) ? true : false; + bool simtime = (argc > 2 && naTrue(args[2])) ? false : true; // Generate and register a C++ timer handler NasalTimer* t = new NasalTimer; @@ -529,7 +530,7 @@ void FGNasalSys::setTimer(int argc, naRef* args) void FGNasalSys::handleTimer(NasalTimer* t) { - naCall(_context, t->handler, naNil(), naNil(), naNil()); + naCall(_context, t->handler, 0, 0, naNil(), naNil()); if(naGetError(_context)) logError(); gcRelease(t->gcKey);