]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalSys.hxx
bf6a0abf6d7f57fd6b32de4ace1dfa034c53172c
[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     // Simple hook to run arbitrary source code.  Returns a bool to
19     // indicate successful execution.  Does *not* return any Nasal
20     // values, because handling garbage-collected objects from C space
21     // is deep voodoo and violates the "simple hook" idea.
22     bool parseAndRun(const char* sourceCode);
23
24     // Slightly more complicated hook to get a handle to a precompiled
25     // Nasal script that can be invoked via a call() method.  The
26     // caller is expected to delete the FGNasalScript returned from
27     // this function.  The "name" argument specifies the "file name"
28     // for the source code that will be printed in Nasal stack traces
29     // on error.
30     FGNasalScript* parseScript(const char* src, const char* name=0);
31
32     // Implementation of the settimer extension function
33     void setTimer(int argc, naRef* args);
34
35     // Returns a ghost wrapper for the current _cmdArg
36     naRef cmdArgGhost();
37     
38     // Callbacks for command and timer bindings
39     virtual bool handleCommand(const SGPropertyNode* arg);
40
41 private:
42     friend class FGNasalScript;
43
44     //
45     // FGTimer subclass for handling Nasal timer callbacks.
46     // See the implementation of the settimer() extension function for
47     // more notes.
48     //
49     struct NasalTimer {
50         virtual void timerExpired();
51         naRef handler;
52         int gcKey;
53         FGNasalSys* nasal;
54     };
55
56     void loadPropertyScripts();
57     void initModule(const char* moduleName, const char* fileName,
58                     const char* src, int len);
59     void readScriptFile(SGPath file, const char* lib);
60     void hashset(naRef hash, const char* key, naRef val);
61     void logError();
62     naRef parse(const char* filename, const char* buf, int len);
63     naRef genPropsModule();
64     naRef propNodeGhost(SGPropertyNode* handle);
65
66     // This mechanism is here to allow naRefs to be passed to
67     // locations "outside" the interpreter.  Normally, such a
68     // reference would be garbage collected unexpectedly.  By passing
69     // it to gcSave and getting a key/handle, it can be cached in a
70     // globals.__gcsave hash.  Be sure to release it with gcRelease
71     // when done.
72     int gcSave(naRef r);
73     void gcRelease(int key);
74
75     naContext _context;
76     naRef _globals;
77
78     SGPropertyNode* _cmdArg;
79
80     int _nextGCKey;
81     naRef _gcHash;
82
83     public: void handleTimer(NasalTimer* t);
84 };
85
86 class FGNasalScript {
87 public:
88     ~FGNasalScript() { _nas->gcRelease(_gcKey); }
89
90     bool call() {
91         naCall(_nas->_context, _code, naNil(), naNil(), naNil());
92         return naGetError(_nas->_context) == 0;
93     }
94
95 private:
96     friend class FGNasalSys;
97     naRef _code;
98     int _gcKey;
99     FGNasalSys* _nas;
100 };
101
102 #endif // __NASALSYS_HXX