4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/nasal/nasal.h>
7 #include <simgear/scene/model/model.hxx>
9 #include <Main/globals.hxx>
16 class FGNasalListener;
18 class FGNasalSys : public SGSubsystem
22 virtual ~FGNasalSys();
24 virtual void update(double dt) { /* noop */ }
26 // Loads a nasal script from an external file and inserts it as a
27 // global module of the specified name.
28 void loadModule(SGPath file, const char* moduleName);
30 // Simple hook to run arbitrary source code. Returns a bool to
31 // indicate successful execution. Does *not* return any Nasal
32 // values, because handling garbage-collected objects from C space
33 // is deep voodoo and violates the "simple hook" idea.
34 bool parseAndRun(const char* sourceCode);
36 // Slightly more complicated hook to get a handle to a precompiled
37 // Nasal script that can be invoked via a call() method. The
38 // caller is expected to delete the FGNasalScript returned from
39 // this function. The "name" argument specifies the "file name"
40 // for the source code that will be printed in Nasal stack traces
42 FGNasalScript* parseScript(const char* src, const char* name=0);
44 // Implementation of the settimer extension function
45 void setTimer(int argc, naRef* args);
47 // Implementation of the setlistener extension function
48 naRef setListener(int argc, naRef* args);
49 naRef removeListener(int argc, naRef* args);
51 // Returns a ghost wrapper for the current _cmdArg
54 // Callbacks for command and timer bindings
55 virtual bool handleCommand(const SGPropertyNode* arg);
57 void createModule(const char* moduleName, const char* fileName,
58 const char* src, int len);
60 void deleteModule(const char* moduleName);
63 friend class FGNasalScript;
64 friend class FGNasalListener;
67 // FGTimer subclass for handling Nasal timer callbacks.
68 // See the implementation of the settimer() extension function for
72 virtual void timerExpired();
79 map<int, FGNasalListener *> _listener;
80 static int _listenerId;
82 void loadPropertyScripts();
83 void hashset(naRef hash, const char* key, naRef val);
84 void logError(naContext);
85 naRef parse(const char* filename, const char* buf, int len);
86 naRef genPropsModule();
87 naRef propNodeGhost(SGPropertyNode* handle);
89 // This mechanism is here to allow naRefs to be passed to
90 // locations "outside" the interpreter. Normally, such a
91 // reference would be garbage collected unexpectedly. By passing
92 // it to gcSave and getting a key/handle, it can be cached in a
93 // globals.__gcsave hash. Be sure to release it with gcRelease
96 void gcRelease(int key);
101 SGPropertyNode* _cmdArg;
106 public: void handleTimer(NasalTimer* t);
110 class FGNasalScript {
112 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
116 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
117 return naGetError(_nas->_context) == 0;
121 friend class FGNasalSys;
128 class FGNasalListener : public SGPropertyChangeListener {
130 FGNasalListener(SGPropertyNode_ptr node, naRef handler,
131 FGNasalSys* nasal, int key)
132 : _node(node), _handler(handler), _gcKey(key), _nas(nasal) {}
135 _nas->gcRelease(_gcKey);
138 void valueChanged(SGPropertyNode* node) {
139 _nas->_cmdArg = node;
140 naContext c = naNewContext();
142 naCall(c, _handler, 0, 0, naNil(), naNil());
150 friend class FGNasalSys;
151 SGPropertyNode_ptr _node;
158 class FGNasalModelData : public SGModelData {
160 FGNasalModelData() : _unload(0) {}
162 void modelLoaded(const string& path, SGPropertyNode *prop, ssgBranch *);
166 SGPropertyNode_ptr _unload;
169 #endif // __NASALSYS_HXX