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