]> git.mxchange.org Git - flightgear.git/commitdiff
- rename initModule() to createModule() (as suggested by Andy)
authormfranz <mfranz>
Sun, 19 Jun 2005 17:09:03 +0000 (17:09 +0000)
committermfranz <mfranz>
Sun, 19 Jun 2005 17:09:03 +0000 (17:09 +0000)
- 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)

src/Scripting/NasalSys.cxx
src/Scripting/NasalSys.hxx

index 88cd7e379142fe4a9482ab7faa272d665a920a22..1c93b9cfcc8ff6cbdaaca3248070316b94f417e5 100644 (file)
@@ -385,7 +385,7 @@ void FGNasalSys::loadPropertyScripts()
         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: " <<
@@ -422,14 +422,14 @@ void FGNasalSys::loadModule(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)
 {
     naRef code = parse(fileName, src, len);
     if(naIsNil(code))
@@ -476,16 +476,24 @@ bool FGNasalSys::handleCommand(const SGPropertyNode* arg)
     // location in the property tree.  arg->getPath() returns an empty
     // string.
     const char* nasal = arg->getStringValue("script");
+    const char* moduleName = arg->getStringValue("module");
     naRef code = parse("<command>", 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, naNil(), naNil(), locals);
     if(!naGetError(_context)) return true;
     logError();
     return false;
index 8efb3159ff0d3a5054ad56240cd8db485b88477e..6f9df4953fda18500dbb3a9807c8c9ee715f6950 100644 (file)
@@ -42,6 +42,9 @@ public:
     // Callbacks for command and timer bindings
     virtual bool handleCommand(const SGPropertyNode* arg);
 
+    void createModule(const char* moduleName, const char* fileName,
+                    const char* src, int len);
+
 private:
     friend class FGNasalScript;
 
@@ -58,8 +61,6 @@ private:
     };
 
     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 logError();
     naRef parse(const char* filename, const char* buf, int len);