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>
16 class FGNasalListener;
18 class FGNasalSys : public SGSubsystem
22 virtual ~FGNasalSys();
24 virtual void update(double dt);
26 // Loads a nasal script from an external file and inserts it as a
27 // global module of the specified name.
28 bool loadModule(SGPath file, const char* moduleName);
30 // Simple hook to run arbitrary source code. Returns a bool to
31 // indicate successful execution. Does *not* return any Nasal
32 // values, because handling garbage-collected objects from C space
33 // is deep voodoo and violates the "simple hook" idea.
34 bool parseAndRun(const char* sourceCode);
36 // Slightly more complicated hook to get a handle to a precompiled
37 // Nasal script that can be invoked via a call() method. The
38 // caller is expected to delete the FGNasalScript returned from
39 // this function. The "name" argument specifies the "file name"
40 // for the source code that will be printed in Nasal stack traces
42 FGNasalScript* parseScript(const char* src, const char* name=0);
44 // Implementation of the settimer extension function
45 void setTimer(naContext c, int argc, naRef* args);
47 // Implementation of the setlistener extension function
48 naRef setListener(naContext c, int argc, naRef* args);
49 naRef removeListener(naContext c, int argc, naRef* args);
51 // Returns a ghost wrapper for the current _cmdArg
54 // Callbacks for command and timer bindings
55 virtual bool handleCommand(const SGPropertyNode* arg);
57 bool createModule(const char* moduleName, const char* fileName,
58 const char* src, int len, const SGPropertyNode* cmdarg=0,
59 int argc=0, naRef*args=0);
61 void deleteModule(const char* moduleName);
63 naRef call(naRef code, int argc, naRef* args, naRef locals);
64 naRef propNodeGhost(SGPropertyNode* handle);
67 friend class FGNasalScript;
68 friend class FGNasalListener;
69 friend class FGNasalModuleListener;
72 // FGTimer subclass for handling Nasal timer callbacks.
73 // See the implementation of the settimer() extension function for
77 virtual void timerExpired();
78 virtual ~NasalTimer() {}
85 map<int, FGNasalListener *> _listener;
86 vector<FGNasalListener *> _dead_listener;
87 static int _listenerId;
89 void loadPropertyScripts();
90 void loadPropertyScripts(SGPropertyNode* n);
91 void loadScriptDirectory(simgear::Dir nasalDir);
92 void addModule(string moduleName, simgear::PathList scripts);
93 void hashset(naRef hash, const char* key, naRef val);
94 void logError(naContext);
95 naRef parse(const char* filename, const char* buf, int len);
96 naRef genPropsModule();
98 // This mechanism is here to allow naRefs to be passed to
99 // locations "outside" the interpreter. Normally, such a
100 // reference would be garbage collected unexpectedly. By passing
101 // it to gcSave and getting a key/handle, it can be cached in a
102 // globals.__gcsave hash. Be sure to release it with gcRelease
105 void gcRelease(int key);
110 SGPropertyNode_ptr _cmdArg;
116 public: void handleTimer(NasalTimer* t);
120 class FGNasalScript {
122 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
126 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
127 return naGetError(_nas->_context) == 0;
131 friend class FGNasalSys;
138 class FGNasalListener : public SGPropertyChangeListener {
140 FGNasalListener(SGPropertyNode* node, naRef code, FGNasalSys* nasal,
141 int key, int id, int init, int type);
143 virtual ~FGNasalListener();
144 virtual void valueChanged(SGPropertyNode* node);
145 virtual void childAdded(SGPropertyNode* parent, SGPropertyNode* child);
146 virtual void childRemoved(SGPropertyNode* parent, SGPropertyNode* child);
149 bool changed(SGPropertyNode* node);
150 void call(SGPropertyNode* which, naRef mode);
152 friend class FGNasalSys;
153 SGPropertyNode_ptr _node;
160 unsigned int _active;
168 class FGNasalModelData : public simgear::SGModelData {
170 FGNasalModelData(SGPropertyNode *root = 0) : _root(root), _unload(0) {}
172 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
175 static unsigned int _module_id;
177 SGPropertyNode_ptr _root;
178 SGConstPropertyNode_ptr _unload;
182 class NasalXMLVisitor : public XMLVisitor {
184 NasalXMLVisitor(naContext c, int argc, naRef* args);
185 virtual ~NasalXMLVisitor() { naFreeContext(_c); }
187 virtual void startElement(const char* tag, const XMLAttributes& a);
188 virtual void endElement(const char* tag);
189 virtual void data(const char* str, int len);
190 virtual void pi(const char* target, const char* data);
193 void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
194 naRef make_string(const char* s, int n = -1);
197 naRef _start_element, _end_element, _data, _pi;
200 #endif // __NASALSYS_HXX