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>
17 class FGNasalListener;
20 /** Nasal model data container.
21 * load and unload methods must be run in main thread (not thread-safe). */
22 class FGNasalModelData : public SGReferenced
25 /** Constructor to be run in an arbitrary thread. */
26 FGNasalModelData(SGPropertyNode *root, const string& path, SGPropertyNode *prop,
27 SGPropertyNode* load, SGPropertyNode* unload) :
29 _root(root), _prop(prop),
30 _load(load), _unload(unload)
34 /** Load hook. Always call from inside the main loop. */
37 /** Unload hook. Always call from inside the main loop. */
41 static unsigned int _module_id;
43 string _module, _path;
44 SGPropertyNode_ptr _root, _prop;
45 SGConstPropertyNode_ptr _load, _unload;
48 /** Thread-safe proxy for FGNasalModelData.
49 * modelLoaded/destroy methods only register the requested
50 * operation. Actual (un)loading of Nasal module is deferred
51 * and done in the main loop. */
52 class FGNasalModelDataProxy : public simgear::SGModelData
55 FGNasalModelDataProxy(SGPropertyNode *root = 0) :
60 ~FGNasalModelDataProxy();
62 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
65 SGPropertyNode_ptr _root;
66 SGSharedPtr<FGNasalModelData> _data;
69 class FGNasalSys : public SGSubsystem
73 virtual ~FGNasalSys();
75 virtual void update(double dt);
77 // Loads a nasal script from an external file and inserts it as a
78 // global module of the specified name.
79 bool loadModule(SGPath file, const char* moduleName);
81 // Simple hook to run arbitrary source code. Returns a bool to
82 // indicate successful execution. Does *not* return any Nasal
83 // values, because handling garbage-collected objects from C space
84 // is deep voodoo and violates the "simple hook" idea.
85 bool parseAndRun(const char* sourceCode);
87 // Slightly more complicated hook to get a handle to a precompiled
88 // Nasal script that can be invoked via a call() method. The
89 // caller is expected to delete the FGNasalScript returned from
90 // this function. The "name" argument specifies the "file name"
91 // for the source code that will be printed in Nasal stack traces
93 FGNasalScript* parseScript(const char* src, const char* name=0);
95 // Implementation of the settimer extension function
96 void setTimer(naContext c, int argc, naRef* args);
98 // Implementation of the setlistener extension function
99 naRef setListener(naContext c, int argc, naRef* args);
100 naRef removeListener(naContext c, int argc, naRef* args);
102 // Returns a ghost wrapper for the current _cmdArg
105 // Callbacks for command and timer bindings
106 virtual bool handleCommand(const SGPropertyNode* arg);
108 bool createModule(const char* moduleName, const char* fileName,
109 const char* src, int len, const SGPropertyNode* cmdarg=0,
110 int argc=0, naRef*args=0);
112 void deleteModule(const char* moduleName);
114 naRef call(naRef code, int argc, naRef* args, naRef locals);
115 naRef propNodeGhost(SGPropertyNode* handle);
117 void registerToLoad(FGNasalModelData* data) { _loadList.push(data);}
118 void registerToUnload(FGNasalModelData* data) { _unloadList.push(data);}
121 friend class FGNasalScript;
122 friend class FGNasalListener;
123 friend class FGNasalModuleListener;
125 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
126 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
129 // FGTimer subclass for handling Nasal timer callbacks.
130 // See the implementation of the settimer() extension function for
134 virtual void timerExpired();
135 virtual ~NasalTimer() {}
142 map<int, FGNasalListener *> _listener;
143 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(string moduleName, simgear::PathList scripts);
150 void hashset(naRef hash, const char* key, naRef val);
151 void logError(naContext);
152 naRef parse(const char* filename, const char* buf, int len);
153 naRef genPropsModule();
155 // This mechanism is here to allow naRefs to be passed to
156 // locations "outside" the interpreter. Normally, such a
157 // reference would be garbage collected unexpectedly. By passing
158 // it to gcSave and getting a key/handle, it can be cached in a
159 // globals.__gcsave hash. Be sure to release it with gcRelease
162 void gcRelease(int key);
167 SGPropertyNode_ptr _cmdArg;
173 public: void handleTimer(NasalTimer* t);
177 class FGNasalScript {
179 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
183 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
184 return naGetError(_nas->_context) == 0;
188 friend class FGNasalSys;
195 class FGNasalListener : public SGPropertyChangeListener {
197 FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
198 int key, int id, int init, int type);
200 virtual ~FGNasalListener();
201 virtual void valueChanged(SGPropertyNode* node);
202 virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
203 virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
206 bool changed(SGPropertyNode* node);
207 void call(SGPropertyNode* which, naRef mode);
209 friend class FGNasalSys;
210 SGPropertyNode_ptr _node;
217 unsigned int _active;
225 class NasalXMLVisitor : public XMLVisitor {
227 NasalXMLVisitor(naContext c, int argc, naRef* args);
228 virtual ~NasalXMLVisitor() { naFreeContext(_c); }
230 virtual void startElement(const char* tag, const XMLAttributes& a);
231 virtual void endElement(const char* tag);
232 virtual void data(const char* str, int len);
233 virtual void pi(const char* target, const char* data);
236 void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
237 naRef make_string(const char* s, int n = -1);
240 naRef _start_element, _end_element, _data, _pi;
243 #endif // __NASALSYS_HXX