]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
minor cleanup: don't need the args array in the class anymore
[flightgear.git] / src / Scripting / NasalSys.hxx
1 #ifndef __NASALSYS_HXX
2 #define __NASALSYS_HXX
3
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/model.hxx>
8 #include <simgear/xml/easyxml.hxx>
9
10 #include <map>
11 SG_USING_STD(map);
12
13
14 class FGNasalScript;
15 class FGNasalListener;
16
17 class FGNasalSys : public SGSubsystem
18 {
19 public:
20     FGNasalSys();
21     virtual ~FGNasalSys();
22     virtual void init();
23     virtual void update(double dt);
24
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);
28
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);
34
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
40     // on error.
41     FGNasalScript* parseScript(const char* src, const char* name=0);
42
43     // Implementation of the settimer extension function
44     void setTimer(naContext c, int argc, naRef* args);
45
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);
49
50     // Returns a ghost wrapper for the current _cmdArg
51     naRef cmdArgGhost();
52
53     // Callbacks for command and timer bindings
54     virtual bool handleCommand(const SGPropertyNode* arg);
55
56     void createModule(const char* moduleName, const char* fileName,
57                     const char* src, int len, const SGPropertyNode* arg=0);
58
59     void deleteModule(const char* moduleName);
60
61     naRef call(naRef code, naRef locals);
62
63 private:
64     friend class FGNasalScript;
65     friend class FGNasalListener;
66
67     //
68     // FGTimer subclass for handling Nasal timer callbacks.
69     // See the implementation of the settimer() extension function for
70     // more notes.
71     //
72     struct NasalTimer {
73         virtual void timerExpired();
74         virtual ~NasalTimer() {}
75         naRef handler;
76         int gcKey;
77         FGNasalSys* nasal;
78     };
79
80     // Listener
81     map<int, FGNasalListener *> _listener;
82     static int _listenerId;
83     bool _purgeListeners;
84
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);
91
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
97     // when done.
98     int gcSave(naRef r);
99     void gcRelease(int key);
100
101     naContext _context;
102     naRef _globals;
103
104     SGPropertyNode_ptr _cmdArg;
105
106     int _nextGCKey;
107     naRef _gcHash;
108     int _callCount;
109
110     public: void handleTimer(NasalTimer* t);
111 };
112
113
114 class FGNasalScript {
115 public:
116     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
117
118     bool call() {
119         naRef n = naNil();
120         naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
121         return naGetError(_nas->_context) == 0;
122     }
123
124 private:
125     friend class FGNasalSys;
126     naRef _code;
127     int _gcKey;
128     FGNasalSys* _nas;
129 };
130
131
132 class FGNasalListener : public SGPropertyChangeListener {
133 public:
134     FGNasalListener(SGPropertyNode_ptr node, naRef handler,
135                     FGNasalSys* nasal, int key, int id);
136
137     ~FGNasalListener();
138     void valueChanged(SGPropertyNode* node);
139
140 private:
141     friend class FGNasalSys;
142     SGPropertyNode_ptr _node;
143     naRef _handler;
144     int _gcKey;
145     int _id;
146     FGNasalSys* _nas;
147     unsigned int _active;
148     bool _dead;
149 };
150
151
152 class FGNasalModelData : public SGModelData {
153 public:
154     FGNasalModelData(SGPropertyNode *props = 0) : _props(props), _unload(0) {}
155     ~FGNasalModelData();
156     void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
157
158 private:
159     string _module;
160     SGPropertyNode_ptr _props;
161     SGConstPropertyNode_ptr _unload;
162 };
163
164
165 class NasalXMLVisitor : public XMLVisitor {
166 public:
167     NasalXMLVisitor(naContext c, int argc, naRef* args);
168     virtual ~NasalXMLVisitor() { naFreeContext(_c); }
169
170     virtual void startElement(const char* tag, const XMLAttributes& a);
171     virtual void endElement(const char* tag);
172     virtual void data(const char* str, int len);
173     virtual void pi(const char* target, const char* data);
174
175 private:
176     void call(naRef func, int num, naRef a = naNil(), naRef b = naNil());
177     naRef make_string(const char* s, int n = -1);
178
179     naContext _c;
180     naRef _start_element, _end_element, _data, _pi;
181 };
182
183 #endif // __NASALSYS_HXX