]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
700b23011c902fde83270ae8be09112f64511f22
[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
9 #include <map>
10 SG_USING_STD(map);
11
12
13 class FGNasalScript;
14 class FGNasalListener;
15
16 class FGNasalSys : public SGSubsystem
17 {
18 public:
19     FGNasalSys();
20     virtual ~FGNasalSys();
21     virtual void init();
22     virtual void update(double dt);
23
24     // Loads a nasal script from an external file and inserts it as a
25     // global module of the specified name.
26     void loadModule(SGPath file, const char* moduleName);
27
28     // Simple hook to run arbitrary source code.  Returns a bool to
29     // indicate successful execution.  Does *not* return any Nasal
30     // values, because handling garbage-collected objects from C space
31     // is deep voodoo and violates the "simple hook" idea.
32     bool parseAndRun(const char* sourceCode);
33
34     // Slightly more complicated hook to get a handle to a precompiled
35     // Nasal script that can be invoked via a call() method.  The
36     // caller is expected to delete the FGNasalScript returned from
37     // this function.  The "name" argument specifies the "file name"
38     // for the source code that will be printed in Nasal stack traces
39     // on error.
40     FGNasalScript* parseScript(const char* src, const char* name=0);
41
42     // Implementation of the settimer extension function
43     void setTimer(int argc, naRef* args);
44
45     // Implementation of the setlistener extension function
46     naRef setListener(naContext c, int argc, naRef* args);
47     naRef removeListener(naContext c, int argc, naRef* args);
48
49     // Returns a ghost wrapper for the current _cmdArg
50     naRef cmdArgGhost();
51
52     // Callbacks for command and timer bindings
53     virtual bool handleCommand(const SGPropertyNode* arg);
54
55     void createModule(const char* moduleName, const char* fileName,
56                     const char* src, int len, const SGPropertyNode* arg=0);
57
58     void deleteModule(const char* moduleName);
59
60     naRef call(naRef code, naRef locals);
61
62 private:
63     friend class FGNasalScript;
64     friend class FGNasalListener;
65
66     //
67     // FGTimer subclass for handling Nasal timer callbacks.
68     // See the implementation of the settimer() extension function for
69     // more notes.
70     //
71     struct NasalTimer {
72         virtual void timerExpired();
73         virtual ~NasalTimer() {}
74         naRef handler;
75         int gcKey;
76         FGNasalSys* nasal;
77     };
78
79     // Listener
80     map<int, FGNasalListener *> _listener;
81     static int _listenerId;
82     bool _purgeListeners;
83
84     void loadPropertyScripts();
85     void hashset(naRef hash, const char* key, naRef val);
86     void logError(naContext);
87     naRef parse(const char* filename, const char* buf, int len);
88     naRef genPropsModule();
89     naRef propNodeGhost(SGPropertyNode* handle);
90
91     // This mechanism is here to allow naRefs to be passed to
92     // locations "outside" the interpreter.  Normally, such a
93     // reference would be garbage collected unexpectedly.  By passing
94     // it to gcSave and getting a key/handle, it can be cached in a
95     // globals.__gcsave hash.  Be sure to release it with gcRelease
96     // when done.
97     int gcSave(naRef r);
98     void gcRelease(int key);
99
100     naContext _context;
101     naRef _globals;
102
103     SGPropertyNode_ptr _cmdArg;
104
105     int _nextGCKey;
106     naRef _gcHash;
107     int _callCount;
108
109     public: void handleTimer(NasalTimer* t);
110 };
111
112
113 class FGNasalScript {
114 public:
115     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
116
117     bool call() {
118         naRef n = naNil();
119         naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
120         return naGetError(_nas->_context) == 0;
121     }
122
123 private:
124     friend class FGNasalSys;
125     naRef _code;
126     int _gcKey;
127     FGNasalSys* _nas;
128 };
129
130
131 class FGNasalListener : public SGPropertyChangeListener {
132 public:
133     FGNasalListener(SGPropertyNode_ptr node, naRef handler,
134                     FGNasalSys* nasal, int key, int id);
135
136     ~FGNasalListener();
137     void valueChanged(SGPropertyNode* node);
138
139 private:
140     friend class FGNasalSys;
141     SGPropertyNode_ptr _node;
142     naRef _handler;
143     int _gcKey;
144     int _id;
145     FGNasalSys* _nas;
146     unsigned int _active;
147     bool _dead;
148 };
149
150
151 class FGNasalModelData : public SGModelData {
152 public:
153     FGNasalModelData(SGPropertyNode *props = 0) : _props(props), _unload(0) {}
154     ~FGNasalModelData();
155     void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
156
157 private:
158     string _module;
159     SGPropertyNode_ptr _props;
160     SGConstPropertyNode_ptr _unload;
161 };
162
163 #endif // __NASALSYS_HXX