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>
14 class FGNasalListener;
16 class FGNasalSys : public SGSubsystem
20 virtual ~FGNasalSys();
22 virtual void update(double dt) { /* noop */ }
24 // Loads a nasal script from an external file and inserts it as a
25 // global module of the specified name.
26 void loadModule(SGPath file, const char* moduleName);
28 // Simple hook to run arbitrary source code. Returns a bool to
29 // indicate successful execution. Does *not* return any Nasal
30 // values, because handling garbage-collected objects from C space
31 // is deep voodoo and violates the "simple hook" idea.
32 bool parseAndRun(const char* sourceCode);
34 // Slightly more complicated hook to get a handle to a precompiled
35 // Nasal script that can be invoked via a call() method. The
36 // caller is expected to delete the FGNasalScript returned from
37 // this function. The "name" argument specifies the "file name"
38 // for the source code that will be printed in Nasal stack traces
40 FGNasalScript* parseScript(const char* src, const char* name=0);
42 // Implementation of the settimer extension function
43 void setTimer(int argc, naRef* args);
45 // Implementation of the setlistener extension function
46 naRef setListener(int argc, naRef* args);
47 naRef removeListener(naContext c, int argc, naRef* args);
49 // Returns a ghost wrapper for the current _cmdArg
52 // Callbacks for command and timer bindings
53 virtual bool handleCommand(const SGPropertyNode* arg);
55 void createModule(const char* moduleName, const char* fileName,
56 const char* src, int len, const SGPropertyNode* arg=0);
58 void deleteModule(const char* moduleName);
60 naRef call(naRef code, naRef locals);
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();
73 virtual ~NasalTimer() {}
80 map<int, FGNasalListener *> _listener;
81 static int _listenerId;
83 void loadPropertyScripts();
84 void hashset(naRef hash, const char* key, naRef val);
85 void logError(naContext);
86 naRef parse(const char* filename, const char* buf, int len);
87 naRef genPropsModule();
88 naRef propNodeGhost(SGPropertyNode* handle);
90 // This mechanism is here to allow naRefs to be passed to
91 // locations "outside" the interpreter. Normally, such a
92 // reference would be garbage collected unexpectedly. By passing
93 // it to gcSave and getting a key/handle, it can be cached in a
94 // globals.__gcsave hash. Be sure to release it with gcRelease
97 void gcRelease(int key);
102 SGPropertyNode_ptr _cmdArg;
108 public: void handleTimer(NasalTimer* t);
112 class FGNasalScript {
114 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
118 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
119 return naGetError(_nas->_context) == 0;
123 friend class FGNasalSys;
130 class FGNasalListener : public SGPropertyChangeListener {
132 FGNasalListener(SGPropertyNode_ptr node, naRef handler,
133 FGNasalSys* nasal, int key);
136 void valueChanged(SGPropertyNode* node);
139 friend class FGNasalSys;
140 SGPropertyNode_ptr _node;
144 unsigned int _active;
148 class FGNasalModelData : public SGModelData {
150 FGNasalModelData() : _unload(0) {}
152 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
156 SGConstPropertyNode_ptr _unload;
159 #endif // __NASALSYS_HXX