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/scene/model/modellib.hxx>
9 #include <simgear/xml/easyxml.hxx>
10 #include <simgear/threads/SGQueue.hxx>
16 class FGNasalListener;
19 /** Nasal model data container.
20 * load and unload methods must be run in main thread (not thread-safe). */
21 class FGNasalModelData : public SGReferenced
24 /** Constructor to be run in an arbitrary thread. */
25 FGNasalModelData(SGPropertyNode *root, const string& path, SGPropertyNode *prop,
26 SGPropertyNode* load, SGPropertyNode* unload) :
28 _root(root), _prop(prop),
29 _load(load), _unload(unload)
33 /** Load hook. Always call from inside the main loop. */
36 /** Unload hook. Always call from inside the main loop. */
40 static unsigned int _module_id;
42 string _module, _path;
43 SGPropertyNode_ptr _root, _prop;
44 SGConstPropertyNode_ptr _load, _unload;
47 /** Thread-safe proxy for FGNasalModelData.
48 * modelLoaded/destroy methods only register the requested
49 * operation. Actual (un)loading of Nasal module is deferred
50 * and done in the main loop. */
51 class FGNasalModelDataProxy : public simgear::SGModelData
54 FGNasalModelDataProxy(SGPropertyNode *root = 0) :
59 ~FGNasalModelDataProxy();
61 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
64 SGPropertyNode_ptr _root;
65 SGSharedPtr<FGNasalModelData> _data;
68 SGPropertyNode* ghostToPropNode(naRef ref);
69 SGCondition* conditionGhost(naRef r);
71 class FGNasalSys : public SGSubsystem
75 virtual ~FGNasalSys();
77 virtual void update(double dt);
79 // Loads a nasal script from an external file and inserts it as a
80 // global module of the specified name.
81 bool loadModule(SGPath file, const char* moduleName);
83 // Simple hook to run arbitrary source code. Returns a bool to
84 // indicate successful execution. Does *not* return any Nasal
85 // values, because handling garbage-collected objects from C space
86 // is deep voodoo and violates the "simple hook" idea.
87 bool parseAndRun(const char* sourceCode);
89 // Slightly more complicated hook to get a handle to a precompiled
90 // Nasal script that can be invoked via a call() method. The
91 // caller is expected to delete the FGNasalScript returned from
92 // this function. The "name" argument specifies the "file name"
93 // for the source code that will be printed in Nasal stack traces
95 FGNasalScript* parseScript(const char* src, const char* name=0);
97 // Implementation of the settimer extension function
98 void setTimer(naContext c, int argc, naRef* args);
100 // Implementation of the setlistener extension function
101 naRef setListener(naContext c, int argc, naRef* args);
102 naRef removeListener(naContext c, int argc, naRef* args);
104 // Returns a ghost wrapper for the current _cmdArg
107 // Callbacks for command and timer bindings
108 virtual bool handleCommand( const char* moduleName,
109 const char* fileName,
111 const SGPropertyNode* arg = 0 );
112 virtual bool handleCommand(const SGPropertyNode* arg);
114 bool createModule(const char* moduleName, const char* fileName,
115 const char* src, int len, const SGPropertyNode* cmdarg=0,
116 int argc=0, naRef*args=0);
118 void deleteModule(const char* moduleName);
121 * Set member of specified hash to given value
123 void hashset(naRef hash, const char* key, naRef val);
126 * Set member of globals hash to given value
128 void globalsSet(const char* key, naRef val);
130 naRef call(naRef code, int argc, naRef* args, naRef locals);
132 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
134 naRef propNodeGhost(SGPropertyNode* handle);
136 void registerToLoad(FGNasalModelData* data) { _loadList.push(data);}
137 void registerToUnload(FGNasalModelData* data) { _unloadList.push(data);}
139 // can't call this 'globals' due to naming clash
140 naRef nasalGlobals() const
143 naContext context() const
146 // This mechanism is here to allow naRefs to be passed to
147 // locations "outside" the interpreter. Normally, such a
148 // reference would be garbage collected unexpectedly. By passing
149 // it to gcSave and getting a key/handle, it can be cached in a
150 // globals.__gcsave hash. Be sure to release it with gcRelease
153 void gcRelease(int key);
155 friend class FGNasalScript;
156 friend class FGNasalListener;
157 friend class FGNasalModuleListener;
159 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
160 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
163 // FGTimer subclass for handling Nasal timer callbacks.
164 // See the implementation of the settimer() extension function for
168 virtual void timerExpired();
169 virtual ~NasalTimer() {}
176 std::map<int, FGNasalListener *> _listener;
177 vector<FGNasalListener *> _dead_listener;
178 static int _listenerId;
180 void loadPropertyScripts();
181 void loadPropertyScripts(SGPropertyNode* n);
182 void loadScriptDirectory(simgear::Dir nasalDir);
183 void addModule(string moduleName, simgear::PathList scripts);
184 void logError(naContext);
185 naRef parse(const char* filename, const char* buf, int len);
186 naRef genPropsModule();
191 SGPropertyNode_ptr _cmdArg;
197 public: void handleTimer(NasalTimer* t);
201 class FGNasalScript {
203 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
207 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
208 return naGetError(_nas->_context) == 0;
212 friend class FGNasalSys;
219 class FGNasalListener : public SGPropertyChangeListener {
221 FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
222 int key, int id, int init, int type);
224 virtual ~FGNasalListener();
225 virtual void valueChanged(SGPropertyNode* node);
226 virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
227 virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
230 bool changed(SGPropertyNode* node);
231 void call(SGPropertyNode* which, naRef mode);
233 friend class FGNasalSys;
234 SGPropertyNode_ptr _node;
241 unsigned int _active;
249 class NasalXMLVisitor : public XMLVisitor {
251 NasalXMLVisitor(naContext c, int argc, naRef* args);
252 virtual ~NasalXMLVisitor() { naFreeContext(_c); }
254 virtual void startElement(const char* tag, const XMLAttributes& a);
255 virtual void endElement(const char* tag);
256 virtual void data(const char* str, int len);
257 virtual void pi(const char* target, const char* data);
260 void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
261 naRef make_string(const char* s, int n = -1);
264 naRef _start_element, _end_element, _data, _pi;
267 #endif // __NASALSYS_HXX