4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/misc/sg_dir.hxx>
7 #include <simgear/nasal/nasal.h>
8 #include <simgear/threads/SGQueue.hxx>
9 #include <simgear/props/props.hxx>
11 // Required only for MSVC
13 # include <Scripting/NasalModelData.hxx>
20 class FGNasalListener;
22 class FGNasalModelData;
24 namespace simgear { class BufferedLogCallback; }
26 SGPropertyNode* ghostToPropNode(naRef ref);
27 SGCondition* conditionGhost(naRef r);
29 class FGNasalSys : public SGSubsystem
33 virtual ~FGNasalSys();
35 virtual void update(double dt);
37 // Loads a nasal script from an external file and inserts it as a
38 // global module of the specified name.
39 bool loadModule(SGPath file, const char* moduleName);
41 // Simple hook to run arbitrary source code. Returns a bool to
42 // indicate successful execution. Does *not* return any Nasal
43 // values, because handling garbage-collected objects from C space
44 // is deep voodoo and violates the "simple hook" idea.
45 bool parseAndRun(const char* sourceCode);
47 // Slightly more complicated hook to get a handle to a precompiled
48 // Nasal script that can be invoked via a call() method. The
49 // caller is expected to delete the FGNasalScript returned from
50 // this function. The "name" argument specifies the "file name"
51 // for the source code that will be printed in Nasal stack traces
53 // FGNasalScript* parseScript(const char* src, const char* name=0);
55 // Implementation of the settimer extension function
56 void setTimer(naContext c, int argc, naRef* args);
58 // Implementation of the setlistener extension function
59 naRef setListener(naContext c, int argc, naRef* args);
60 naRef removeListener(naContext c, int argc, naRef* args);
62 // Returns a ghost wrapper for the current _cmdArg
65 void setCmdArg(SGPropertyNode* aNode);
67 // Callbacks for command and timer bindings
68 virtual bool handleCommand( const char* moduleName,
71 const SGPropertyNode* arg = 0 );
72 virtual bool handleCommand(const SGPropertyNode* arg);
74 bool createModule(const char* moduleName, const char* fileName,
75 const char* src, int len, const SGPropertyNode* cmdarg=0,
76 int argc=0, naRef*args=0);
78 void deleteModule(const char* moduleName);
81 * Set member of specified hash to given value
83 void hashset(naRef hash, const char* key, naRef val);
86 * Set member of globals hash to given value
88 void globalsSet(const char* key, naRef val);
90 naRef call(naRef code, int argc, naRef* args, naRef locals);
92 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
94 naRef propNodeGhost(SGPropertyNode* handle);
96 void registerToLoad(FGNasalModelData* data);
97 void registerToUnload(FGNasalModelData* data);
99 // can't call this 'globals' due to naming clash
100 naRef nasalGlobals() const
103 naContext context() const
106 // This mechanism is here to allow naRefs to be passed to
107 // locations "outside" the interpreter. Normally, such a
108 // reference would be garbage collected unexpectedly. By passing
109 // it to gcSave and getting a key/handle, it can be cached in a
110 // globals.__gcsave hash. Be sure to release it with gcRelease
113 void gcRelease(int key);
115 /// retrive the associated log object, for displaying log
116 /// output somewhere (a UI, presumably)
117 simgear::BufferedLogCallback* log() const
120 //friend class FGNasalScript;
121 friend class FGNasalListener;
122 friend class FGNasalModuleListener;
124 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
125 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
128 // FGTimer subclass for handling Nasal timer callbacks.
129 // See the implementation of the settimer() extension function for
133 virtual void timerExpired();
134 virtual ~NasalTimer() {}
141 std::map<int, FGNasalListener *> _listener;
142 std::vector<FGNasalListener *> _dead_listener;
144 static int _listenerId;
146 void loadPropertyScripts();
147 void loadPropertyScripts(SGPropertyNode* n);
148 void loadScriptDirectory(simgear::Dir nasalDir);
149 void addModule(std::string moduleName, simgear::PathList scripts);
150 void logError(naContext);
151 naRef parse(const char* filename, const char* buf, int len);
152 naRef genPropsModule();
158 SGPropertyNode_ptr _cmdArg;
164 simgear::BufferedLogCallback* _log;
166 void handleTimer(NasalTimer* t);
170 class FGNasalScript {
172 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
176 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
177 return naGetError(_nas->_context) == 0;
180 FGNasalSys* sys() const { return _nas; }
182 friend class FGNasalSys;
189 #endif // __NASALSYS_HXX