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>
15 class FGNasalListener;
17 class FGNasalModelData;
19 namespace simgear { class BufferedLogCallback; }
21 SGPropertyNode* ghostToPropNode(naRef ref);
22 SGCondition* conditionGhost(naRef r);
24 class FGNasalSys : public SGSubsystem
28 virtual ~FGNasalSys();
30 virtual void update(double dt);
32 // Loads a nasal script from an external file and inserts it as a
33 // global module of the specified name.
34 bool loadModule(SGPath file, const char* moduleName);
36 // Simple hook to run arbitrary source code. Returns a bool to
37 // indicate successful execution. Does *not* return any Nasal
38 // values, because handling garbage-collected objects from C space
39 // is deep voodoo and violates the "simple hook" idea.
40 bool parseAndRun(const char* sourceCode);
42 // Slightly more complicated hook to get a handle to a precompiled
43 // Nasal script that can be invoked via a call() method. The
44 // caller is expected to delete the FGNasalScript returned from
45 // this function. The "name" argument specifies the "file name"
46 // for the source code that will be printed in Nasal stack traces
48 // FGNasalScript* parseScript(const char* src, const char* name=0);
50 // Implementation of the settimer extension function
51 void setTimer(naContext c, int argc, naRef* args);
53 // Implementation of the setlistener extension function
54 naRef setListener(naContext c, int argc, naRef* args);
55 naRef removeListener(naContext c, int argc, naRef* args);
57 // Returns a ghost wrapper for the current _cmdArg
60 void setCmdArg(SGPropertyNode* aNode);
62 // Callbacks for command and timer bindings
63 virtual bool handleCommand( const char* moduleName,
66 const SGPropertyNode* arg = 0 );
67 virtual bool handleCommand(const SGPropertyNode* arg);
69 bool createModule(const char* moduleName, const char* fileName,
70 const char* src, int len, const SGPropertyNode* cmdarg=0,
71 int argc=0, naRef*args=0);
73 void deleteModule(const char* moduleName);
76 * Set member of specified hash to given value
78 void hashset(naRef hash, const char* key, naRef val);
81 * Set member of globals hash to given value
83 void globalsSet(const char* key, naRef val);
85 naRef call(naRef code, int argc, naRef* args, naRef locals);
87 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
89 naRef propNodeGhost(SGPropertyNode* handle);
91 void registerToLoad(FGNasalModelData* data);
92 void registerToUnload(FGNasalModelData* data);
94 // can't call this 'globals' due to naming clash
95 naRef nasalGlobals() const
98 naContext context() const
101 // This mechanism is here to allow naRefs to be passed to
102 // locations "outside" the interpreter. Normally, such a
103 // reference would be garbage collected unexpectedly. By passing
104 // it to gcSave and getting a key/handle, it can be cached in a
105 // globals.__gcsave hash. Be sure to release it with gcRelease
108 void gcRelease(int key);
110 /// retrive the associated log object, for displaying log
111 /// output somewhere (a UI, presumably)
112 simgear::BufferedLogCallback* log() const
115 //friend class FGNasalScript;
116 friend class FGNasalListener;
117 friend class FGNasalModuleListener;
119 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
120 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
123 // FGTimer subclass for handling Nasal timer callbacks.
124 // See the implementation of the settimer() extension function for
128 virtual void timerExpired();
129 virtual ~NasalTimer() {}
136 std::map<int, FGNasalListener *> _listener;
137 std::vector<FGNasalListener *> _dead_listener;
139 static int _listenerId;
141 void loadPropertyScripts();
142 void loadPropertyScripts(SGPropertyNode* n);
143 void loadScriptDirectory(simgear::Dir nasalDir);
144 void addModule(std::string moduleName, simgear::PathList scripts);
145 void logError(naContext);
146 naRef parse(const char* filename, const char* buf, int len);
147 naRef genPropsModule();
153 SGPropertyNode_ptr _cmdArg;
159 simgear::BufferedLogCallback* _log;
161 void handleTimer(NasalTimer* t);
165 class FGNasalScript {
167 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
171 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
172 return naGetError(_nas->_context) == 0;
175 FGNasalSys* sys() const { return _nas; }
177 friend class FGNasalSys;
184 #endif // __NASALSYS_HXX