]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
Add a lower-bound type navaid lookup, and the ability to specify navaid type in the...
[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     // Returns a ghost wrapper for the current _cmdArg
40     naRef cmdArgGhost();
41     
42     // Callbacks for command and timer bindings
43     virtual bool handleCommand(const SGPropertyNode* arg);
44
45     void createModule(const char* moduleName, const char* fileName,
46                     const char* src, int len);
47
48 private:
49     friend class FGNasalScript;
50
51     //
52     // FGTimer subclass for handling Nasal timer callbacks.
53     // See the implementation of the settimer() extension function for
54     // more notes.
55     //
56     struct NasalTimer {
57         virtual void timerExpired();
58         naRef handler;
59         int gcKey;
60         FGNasalSys* nasal;
61     };
62
63     void loadPropertyScripts();
64     void hashset(naRef hash, const char* key, naRef val);
65     void logError();
66     naRef parse(const char* filename, const char* buf, int len);
67     naRef genPropsModule();
68     naRef propNodeGhost(SGPropertyNode* handle);
69
70     // This mechanism is here to allow naRefs to be passed to
71     // locations "outside" the interpreter.  Normally, such a
72     // reference would be garbage collected unexpectedly.  By passing
73     // it to gcSave and getting a key/handle, it can be cached in a
74     // globals.__gcsave hash.  Be sure to release it with gcRelease
75     // when done.
76     int gcSave(naRef r);
77     void gcRelease(int key);
78
79     naContext _context;
80     naRef _globals;
81
82     SGPropertyNode* _cmdArg;
83
84     int _nextGCKey;
85     naRef _gcHash;
86
87     public: void handleTimer(NasalTimer* t);
88 };
89
90 class FGNasalScript {
91 public:
92     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
93
94     bool call() {
95         naCall(_nas->_context, _code, 0, 0, naNil(), naNil());
96         return naGetError(_nas->_context) == 0;
97     }
98
99 private:
100     friend class FGNasalSys;
101     naRef _code;
102     int _gcKey;
103     FGNasalSys* _nas;
104 };
105
106 #endif // __NASALSYS_HXX