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 // Required only for MSVC
13 # include <Scripting/NasalModelData.hxx>
20 class FGNasalListener;
22 class FGNasalModelData;
24 namespace simgear { class BufferedLogCallback; }
26 SGPropertyNode* ghostToPropNode(naRef ref);
27 SGCondition* conditionGhost(naRef r);
29 class FGNasalSys : public SGSubsystem
33 virtual ~FGNasalSys();
35 virtual void update(double dt);
37 // Loads a nasal script from an external file and inserts it as a
38 // global module of the specified name.
39 bool loadModule(SGPath file, const char* moduleName);
41 // Simple hook to run arbitrary source code. Returns a bool to
42 // indicate successful execution. Does *not* return any Nasal
43 // values, because handling garbage-collected objects from C space
44 // is deep voodoo and violates the "simple hook" idea.
45 bool parseAndRun(const char* sourceCode);
47 // Slightly more complicated hook to get a handle to a precompiled
48 // Nasal script that can be invoked via a call() method. The
49 // caller is expected to delete the FGNasalScript returned from
50 // this function. The "name" argument specifies the "file name"
51 // for the source code that will be printed in Nasal stack traces
53 // FGNasalScript* parseScript(const char* src, const char* name=0);
55 // Implementation of the settimer extension function
56 void setTimer(naContext c, int argc, naRef* args);
58 // Implementation of the setlistener extension function
59 naRef setListener(naContext c, int argc, naRef* args);
60 naRef removeListener(naContext c, int argc, naRef* args);
62 // Returns a ghost wrapper for the current _cmdArg
65 void setCmdArg(SGPropertyNode* aNode);
68 * create Nasal props.Node for an SGPropertyNode*
69 * This is the actual ghost, wrapped in a Nasal sugar class.
71 naRef wrappedPropsNode(SGPropertyNode* aProps);
73 // Callbacks for command and timer bindings
74 virtual bool handleCommand( const char* moduleName,
77 const SGPropertyNode* arg = 0 );
78 virtual bool handleCommand(const SGPropertyNode* arg);
80 bool createModule(const char* moduleName, const char* fileName,
81 const char* src, int len, const SGPropertyNode* cmdarg=0,
82 int argc=0, naRef*args=0);
84 void deleteModule(const char* moduleName);
87 * Set member of specified hash to given value
89 void hashset(naRef hash, const char* key, naRef val);
92 * Set member of globals hash to given value
94 void globalsSet(const char* key, naRef val);
96 naRef call(naRef code, int argc, naRef* args, naRef locals);
98 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
100 naRef propNodeGhost(SGPropertyNode* handle);
102 void registerToLoad(FGNasalModelData* data);
103 void registerToUnload(FGNasalModelData* data);
105 // can't call this 'globals' due to naming clash
106 naRef nasalGlobals() const
109 naContext context() const
112 // This mechanism is here to allow naRefs to be passed to
113 // locations "outside" the interpreter. Normally, such a
114 // reference would be garbage collected unexpectedly. By passing
115 // it to gcSave and getting a key/handle, it can be cached in a
116 // globals.__gcsave hash. Be sure to release it with gcRelease
119 void gcRelease(int key);
121 /// retrive the associated log object, for displaying log
122 /// output somewhere (a UI, presumably)
123 simgear::BufferedLogCallback* log() const
126 //friend class FGNasalScript;
127 friend class FGNasalListener;
128 friend class FGNasalModuleListener;
130 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
131 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
134 // FGTimer subclass for handling Nasal timer callbacks.
135 // See the implementation of the settimer() extension function for
139 virtual void timerExpired();
140 virtual ~NasalTimer() {}
147 std::map<int, FGNasalListener *> _listener;
148 std::vector<FGNasalListener *> _dead_listener;
150 static int _listenerId;
152 void loadPropertyScripts();
153 void loadPropertyScripts(SGPropertyNode* n);
154 void loadScriptDirectory(simgear::Dir nasalDir);
155 void addModule(std::string moduleName, simgear::PathList scripts);
156 void logError(naContext);
157 naRef parse(const char* filename, const char* buf, int len);
158 naRef genPropsModule();
164 SGPropertyNode_ptr _cmdArg;
170 simgear::BufferedLogCallback* _log;
172 void handleTimer(NasalTimer* t);
176 class FGNasalScript {
178 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
182 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
183 return naGetError(_nas->_context) == 0;
186 FGNasalSys* sys() const { return _nas; }
188 friend class FGNasalSys;
195 #endif // __NASALSYS_HXX