4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/nasal/nasal.h>
7 #include <simgear/scene/model/modellib.hxx>
8 #include <simgear/xml/easyxml.hxx>
15 class FGNasalListener;
17 class FGNasalSys : public SGSubsystem
21 virtual ~FGNasalSys();
23 virtual void update(double dt);
25 // Loads a nasal script from an external file and inserts it as a
26 // global module of the specified name.
27 void loadModule(SGPath file, const char* moduleName);
29 // Simple hook to run arbitrary source code. Returns a bool to
30 // indicate successful execution. Does *not* return any Nasal
31 // values, because handling garbage-collected objects from C space
32 // is deep voodoo and violates the "simple hook" idea.
33 bool parseAndRun(const char* sourceCode);
35 // Slightly more complicated hook to get a handle to a precompiled
36 // Nasal script that can be invoked via a call() method. The
37 // caller is expected to delete the FGNasalScript returned from
38 // this function. The "name" argument specifies the "file name"
39 // for the source code that will be printed in Nasal stack traces
41 FGNasalScript* parseScript(const char* src, const char* name=0);
43 // Implementation of the settimer extension function
44 void setTimer(naContext c, int argc, naRef* args);
46 // Implementation of the setlistener extension function
47 naRef setListener(naContext c, int argc, naRef* args);
48 naRef removeListener(naContext c, int argc, naRef* args);
50 // Returns a ghost wrapper for the current _cmdArg
53 // Callbacks for command and timer bindings
54 virtual bool handleCommand(const SGPropertyNode* arg);
56 void createModule(const char* moduleName, const char* fileName,
57 const char* src, int len, const SGPropertyNode* arg=0);
59 void deleteModule(const char* moduleName);
61 naRef call(naRef code, int argc, naRef* args, naRef locals);
64 friend class FGNasalScript;
65 friend class FGNasalListener;
68 // FGTimer subclass for handling Nasal timer callbacks.
69 // See the implementation of the settimer() extension function for
73 virtual void timerExpired();
74 virtual ~NasalTimer() {}
81 map<int, FGNasalListener *> _listener;
82 vector<FGNasalListener *> _dead_listener;
83 static int _listenerId;
85 void loadPropertyScripts();
86 void hashset(naRef hash, const char* key, naRef val);
87 void logError(naContext);
88 naRef parse(const char* filename, const char* buf, int len);
89 naRef genPropsModule();
90 naRef propNodeGhost(SGPropertyNode* handle);
92 // This mechanism is here to allow naRefs to be passed to
93 // locations "outside" the interpreter. Normally, such a
94 // reference would be garbage collected unexpectedly. By passing
95 // it to gcSave and getting a key/handle, it can be cached in a
96 // globals.__gcsave hash. Be sure to release it with gcRelease
99 void gcRelease(int key);
104 SGPropertyNode_ptr _cmdArg;
110 public: void handleTimer(NasalTimer* t);
114 class FGNasalScript {
116 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
120 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
121 return naGetError(_nas->_context) == 0;
125 friend class FGNasalSys;
132 class FGNasalListener : public SGPropertyChangeListener {
134 FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
135 int key, int id, int type);
137 virtual ~FGNasalListener();
138 virtual void valueChanged(SGPropertyNode* node);
139 virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
140 virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
143 bool changed(SGPropertyNode* node);
144 void call(SGPropertyNode* which, naRef mode);
146 friend class FGNasalSys;
147 SGPropertyNode_ptr _node;
153 unsigned int _active;
162 class FGNasalModelData : public simgear::SGModelData {
164 FGNasalModelData(SGPropertyNode *props = 0) : _props(props), _unload(0) {}
166 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
170 SGPropertyNode_ptr _props;
171 SGConstPropertyNode_ptr _unload;
175 class NasalXMLVisitor : public XMLVisitor {
177 NasalXMLVisitor(naContext c, int argc, naRef* args);
178 virtual ~NasalXMLVisitor() { naFreeContext(_c); }
180 virtual void startElement(const char* tag, const XMLAttributes& a);
181 virtual void endElement(const char* tag);
182 virtual void data(const char* str, int len);
183 virtual void pi(const char* target, const char* data);
186 void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
187 naRef make_string(const char* s, int n = -1);
190 naRef _start_element, _end_element, _data, _pi;
193 #endif // __NASALSYS_HXX