4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/nasal/nasal.h>
10 class FGNasalSys : public SGSubsystem
14 virtual ~FGNasalSys();
16 virtual void update(double dt) { /* noop */ }
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);
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);
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
34 FGNasalScript* parseScript(const char* src, const char* name=0);
36 // Implementation of the settimer extension function
37 void setTimer(int argc, naRef* args);
39 // Implementation of the setlistener extension function
40 void setListener(int argc, naRef* args);
42 // Returns a ghost wrapper for the current _cmdArg
45 // Callbacks for command and timer bindings
46 virtual bool handleCommand(const SGPropertyNode* arg);
48 void createModule(const char* moduleName, const char* fileName,
49 const char* src, int len);
51 void screenPrint(const char* src);
54 friend class FGNasalScript;
55 friend class FGNasalListener;
58 // FGTimer subclass for handling Nasal timer callbacks.
59 // See the implementation of the settimer() extension function for
63 virtual void timerExpired();
69 void loadPropertyScripts();
70 void hashset(naRef hash, const char* key, naRef val);
71 void logError(naContext);
72 naRef parse(const char* filename, const char* buf, int len);
73 naRef genPropsModule();
74 naRef propNodeGhost(SGPropertyNode* handle);
76 // This mechanism is here to allow naRefs to be passed to
77 // locations "outside" the interpreter. Normally, such a
78 // reference would be garbage collected unexpectedly. By passing
79 // it to gcSave and getting a key/handle, it can be cached in a
80 // globals.__gcsave hash. Be sure to release it with gcRelease
83 void gcRelease(int key);
88 SGPropertyNode* _cmdArg;
93 public: void handleTimer(NasalTimer* t);
98 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
102 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
103 return naGetError(_nas->_context) == 0;
107 friend class FGNasalSys;
113 class FGNasalListener : public SGPropertyChangeListener {
115 FGNasalListener(naRef handler, FGNasalSys* nasal)
116 : _handler(handler), _nas(nasal) {}
118 void valueChanged(SGPropertyNode* node) {
119 _nas->_cmdArg = node;
120 naContext c = naNewContext();
121 naCall(c, _handler, 0, 0, naNil(), naNil());
128 friend class FGNasalSys;
133 #endif // __NASALSYS_HXX