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