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);
52 friend class FGNasalScript;
53 friend class FGNasalListener;
56 // FGTimer subclass for handling Nasal timer callbacks.
57 // See the implementation of the settimer() extension function for
61 virtual void timerExpired();
67 void loadPropertyScripts();
68 void hashset(naRef hash, const char* key, naRef val);
70 naRef parse(const char* filename, const char* buf, int len);
71 naRef genPropsModule();
72 naRef propNodeGhost(SGPropertyNode* handle);
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
81 void gcRelease(int key);
86 SGPropertyNode* _cmdArg;
91 public: void handleTimer(NasalTimer* t);
96 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
99 naCall(_nas->_context, _code, 0, 0, naNil(), naNil());
100 return naGetError(_nas->_context) == 0;
104 friend class FGNasalSys;
110 class FGNasalListener : public SGPropertyChangeListener {
112 FGNasalListener(naRef handler, FGNasalSys* nasal, int gcKey)
113 : _handler(handler), _gcKey(gcKey), _nas(nasal) {}
115 void valueChanged(SGPropertyNode* node) {
116 _nas->_cmdArg = node;
117 naCall(_nas->_context, _handler, 0, 0, naNil(), naNil());
118 if(naGetError(_nas->_context))
123 friend class FGNasalSys;
129 #endif // __NASALSYS_HXX