4 #include <simgear/misc/sg_path.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6 #include <simgear/misc/sg_dir.hxx>
7 #include <simgear/nasal/nasal.h>
8 #include <simgear/threads/SGQueue.hxx>
9 #include <simgear/props/props.hxx>
11 #include <Scripting/NasalModelData.hxx>
17 class FGNasalListener;
19 class FGNasalModelData;
21 namespace simgear { class BufferedLogCallback; }
23 SGPropertyNode* ghostToPropNode(naRef ref);
24 SGCondition* conditionGhost(naRef r);
26 class FGNasalSys : public SGSubsystem
30 virtual ~FGNasalSys();
32 virtual void update(double dt);
34 // Loads a nasal script from an external file and inserts it as a
35 // global module of the specified name.
36 bool loadModule(SGPath file, const char* moduleName);
38 // Simple hook to run arbitrary source code. Returns a bool to
39 // indicate successful execution. Does *not* return any Nasal
40 // values, because handling garbage-collected objects from C space
41 // is deep voodoo and violates the "simple hook" idea.
42 bool parseAndRun(const char* sourceCode);
44 // Slightly more complicated hook to get a handle to a precompiled
45 // Nasal script that can be invoked via a call() method. The
46 // caller is expected to delete the FGNasalScript returned from
47 // this function. The "name" argument specifies the "file name"
48 // for the source code that will be printed in Nasal stack traces
50 // FGNasalScript* parseScript(const char* src, const char* name=0);
52 // Implementation of the settimer extension function
53 void setTimer(naContext c, int argc, naRef* args);
55 // Implementation of the setlistener extension function
56 naRef setListener(naContext c, int argc, naRef* args);
57 naRef removeListener(naContext c, int argc, naRef* args);
59 // Returns a ghost wrapper for the current _cmdArg
62 void setCmdArg(SGPropertyNode* aNode);
64 // Callbacks for command and timer bindings
65 virtual bool handleCommand( const char* moduleName,
68 const SGPropertyNode* arg = 0 );
69 virtual bool handleCommand(const SGPropertyNode* arg);
71 bool createModule(const char* moduleName, const char* fileName,
72 const char* src, int len, const SGPropertyNode* cmdarg=0,
73 int argc=0, naRef*args=0);
75 void deleteModule(const char* moduleName);
78 * Set member of specified hash to given value
80 void hashset(naRef hash, const char* key, naRef val);
83 * Set member of globals hash to given value
85 void globalsSet(const char* key, naRef val);
87 naRef call(naRef code, int argc, naRef* args, naRef locals);
89 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
91 naRef propNodeGhost(SGPropertyNode* handle);
93 void registerToLoad(FGNasalModelData* data);
94 void registerToUnload(FGNasalModelData* data);
96 // can't call this 'globals' due to naming clash
97 naRef nasalGlobals() const
100 naContext context() const
103 // This mechanism is here to allow naRefs to be passed to
104 // locations "outside" the interpreter. Normally, such a
105 // reference would be garbage collected unexpectedly. By passing
106 // it to gcSave and getting a key/handle, it can be cached in a
107 // globals.__gcsave hash. Be sure to release it with gcRelease
110 void gcRelease(int key);
112 /// retrive the associated log object, for displaying log
113 /// output somewhere (a UI, presumably)
114 simgear::BufferedLogCallback* log() const
117 //friend class FGNasalScript;
118 friend class FGNasalListener;
119 friend class FGNasalModuleListener;
121 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
122 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
125 // FGTimer subclass for handling Nasal timer callbacks.
126 // See the implementation of the settimer() extension function for
130 virtual void timerExpired();
131 virtual ~NasalTimer() {}
138 std::map<int, FGNasalListener *> _listener;
139 std::vector<FGNasalListener *> _dead_listener;
141 static int _listenerId;
143 void loadPropertyScripts();
144 void loadPropertyScripts(SGPropertyNode* n);
145 void loadScriptDirectory(simgear::Dir nasalDir);
146 void addModule(std::string moduleName, simgear::PathList scripts);
147 void logError(naContext);
148 naRef parse(const char* filename, const char* buf, int len);
149 naRef genPropsModule();
155 SGPropertyNode_ptr _cmdArg;
161 simgear::BufferedLogCallback* _log;
163 void handleTimer(NasalTimer* t);
167 class FGNasalScript {
169 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
173 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
174 return naGetError(_nas->_context) == 0;
177 FGNasalSys* sys() const { return _nas; }
179 friend class FGNasalSys;
186 #endif // __NASALSYS_HXX