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