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