]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
Split huge NasalSys header apart.
[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/misc/sg_dir.hxx>
7 #include <simgear/nasal/nasal.h>
8 #include <simgear/threads/SGQueue.hxx>
9 #include <simgear/props/props.hxx>
10
11 #include <map>
12
13
14 class FGNasalScript;
15 class FGNasalListener;
16 class SGCondition;
17 class FGNasalModelData;
18
19 namespace simgear { class BufferedLogCallback; }
20
21 SGPropertyNode* ghostToPropNode(naRef ref);
22 SGCondition* conditionGhost(naRef r);
23
24 class FGNasalSys : public SGSubsystem
25 {
26 public:
27     FGNasalSys();
28     virtual ~FGNasalSys();
29     virtual void init();
30     virtual void update(double dt);
31
32     // Loads a nasal script from an external file and inserts it as a
33     // global module of the specified name.
34     bool loadModule(SGPath file, const char* moduleName);
35
36     // Simple hook to run arbitrary source code.  Returns a bool to
37     // indicate successful execution.  Does *not* return any Nasal
38     // values, because handling garbage-collected objects from C space
39     // is deep voodoo and violates the "simple hook" idea.
40     bool parseAndRun(const char* sourceCode);
41
42     // Slightly more complicated hook to get a handle to a precompiled
43     // Nasal script that can be invoked via a call() method.  The
44     // caller is expected to delete the FGNasalScript returned from
45     // this function.  The "name" argument specifies the "file name"
46     // for the source code that will be printed in Nasal stack traces
47     // on error.
48  //   FGNasalScript* parseScript(const char* src, const char* name=0);
49
50     // Implementation of the settimer extension function
51     void setTimer(naContext c, int argc, naRef* args);
52
53     // Implementation of the setlistener extension function
54     naRef setListener(naContext c, int argc, naRef* args);
55     naRef removeListener(naContext c, int argc, naRef* args);
56
57     // Returns a ghost wrapper for the current _cmdArg
58     naRef cmdArgGhost();
59
60     void setCmdArg(SGPropertyNode* aNode);
61     
62     // Callbacks for command and timer bindings
63     virtual bool handleCommand( const char* moduleName,
64                                 const char* fileName,
65                                 const char* src,
66                                 const SGPropertyNode* arg = 0 );
67     virtual bool handleCommand(const SGPropertyNode* arg);
68
69     bool createModule(const char* moduleName, const char* fileName,
70                       const char* src, int len, const SGPropertyNode* cmdarg=0,
71                       int argc=0, naRef*args=0);
72
73     void deleteModule(const char* moduleName);
74
75     /**
76      * Set member of specified hash to given value
77      */
78     void hashset(naRef hash, const char* key, naRef val);
79
80     /**
81      * Set member of globals hash to given value
82      */
83     void globalsSet(const char* key, naRef val);
84
85     naRef call(naRef code, int argc, naRef* args, naRef locals);
86   
87     naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
88   
89     naRef propNodeGhost(SGPropertyNode* handle);
90   
91     void registerToLoad(FGNasalModelData* data);
92     void registerToUnload(FGNasalModelData* data);
93
94     // can't call this 'globals' due to naming clash
95     naRef nasalGlobals() const
96     { return _globals; }
97   
98     naContext context() const
99     { return _context; }
100   
101     // This mechanism is here to allow naRefs to be passed to
102     // locations "outside" the interpreter.  Normally, such a
103     // reference would be garbage collected unexpectedly.  By passing
104     // it to gcSave and getting a key/handle, it can be cached in a
105     // globals.__gcsave hash.  Be sure to release it with gcRelease
106     // when done.
107     int gcSave(naRef r);
108     void gcRelease(int key);
109     
110     /// retrive the associated log object, for displaying log
111     /// output somewhere (a UI, presumably)
112     simgear::BufferedLogCallback* log() const
113     { return _log; }
114 private:
115     //friend class FGNasalScript;
116     friend class FGNasalListener;
117     friend class FGNasalModuleListener;
118
119     SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
120     SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
121
122     //
123     // FGTimer subclass for handling Nasal timer callbacks.
124     // See the implementation of the settimer() extension function for
125     // more notes.
126     //
127     struct NasalTimer {
128         virtual void timerExpired();
129         virtual ~NasalTimer() {}
130         naRef handler;
131         int gcKey;
132         FGNasalSys* nasal;
133     };
134
135     // Listener
136     std::map<int, FGNasalListener *> _listener;
137     std::vector<FGNasalListener *> _dead_listener;
138     
139     static int _listenerId;
140
141     void loadPropertyScripts();
142     void loadPropertyScripts(SGPropertyNode* n);
143     void loadScriptDirectory(simgear::Dir nasalDir);
144     void addModule(std::string moduleName, simgear::PathList scripts);
145     void logError(naContext);
146     naRef parse(const char* filename, const char* buf, int len);
147     naRef genPropsModule();
148
149     naContext _context;
150     naRef _globals,
151           _string;
152
153     SGPropertyNode_ptr _cmdArg;
154
155     int _nextGCKey;
156     naRef _gcHash;
157     int _callCount;
158
159     simgear::BufferedLogCallback* _log;
160 public:
161     void handleTimer(NasalTimer* t);
162 };
163
164 #if 0
165 class FGNasalScript {
166 public:
167     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
168
169     bool call() {
170         naRef n = naNil();
171         naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
172         return naGetError(_nas->_context) == 0;
173     }
174     
175     FGNasalSys* sys() const { return _nas; }
176 private:
177     friend class FGNasalSys;
178     naRef _code;
179     int _gcKey;
180     FGNasalSys* _nas;
181 };
182 #endif
183
184 #endif // __NASALSYS_HXX