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