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/cppbind/NasalHash.hxx>
8 #include <simgear/nasal/nasal.h>
9 #include <simgear/threads/SGQueue.hxx>
10 #include <simgear/props/props.hxx>
12 // Required only for MSVC
14 # include <Scripting/NasalModelData.hxx>
21 class FGNasalListener;
23 class FGNasalModelData;
25 namespace simgear { class BufferedLogCallback; }
27 SGPropertyNode* ghostToPropNode(naRef ref);
28 SGCondition* conditionGhost(naRef r);
30 class FGNasalSys : public SGSubsystem
34 virtual ~FGNasalSys();
36 virtual void update(double dt);
38 // Loads a nasal script from an external file and inserts it as a
39 // global module of the specified name.
40 bool loadModule(SGPath file, const char* moduleName);
42 // Simple hook to run arbitrary source code. Returns a bool to
43 // indicate successful execution. Does *not* return any Nasal
44 // values, because handling garbage-collected objects from C space
45 // is deep voodoo and violates the "simple hook" idea.
46 bool parseAndRun(const char* sourceCode);
48 // Slightly more complicated hook to get a handle to a precompiled
49 // Nasal script that can be invoked via a call() method. The
50 // caller is expected to delete the FGNasalScript returned from
51 // this function. The "name" argument specifies the "file name"
52 // for the source code that will be printed in Nasal stack traces
54 // FGNasalScript* parseScript(const char* src, const char* name=0);
56 // Implementation of the settimer extension function
57 void setTimer(naContext c, int argc, naRef* args);
59 // Implementation of the setlistener extension function
60 naRef setListener(naContext c, int argc, naRef* args);
61 naRef removeListener(naContext c, int argc, naRef* args);
63 // Returns a ghost wrapper for the current _cmdArg
66 void setCmdArg(SGPropertyNode* aNode);
69 * create Nasal props.Node for an SGPropertyNode*
70 * This is the actual ghost, wrapped in a Nasal sugar class.
72 naRef wrappedPropsNode(SGPropertyNode* aProps);
74 // Callbacks for command and timer bindings
75 virtual bool handleCommand( const char* moduleName,
78 const SGPropertyNode* arg = 0 );
79 virtual bool handleCommand(const SGPropertyNode* arg);
81 bool createModule(const char* moduleName, const char* fileName,
82 const char* src, int len, const SGPropertyNode* cmdarg=0,
83 int argc=0, naRef*args=0);
85 void deleteModule(const char* moduleName);
88 * Set member of specified hash to given value
90 void hashset(naRef hash, const char* key, naRef val);
93 * Set member of globals hash to given value
95 void globalsSet(const char* key, naRef val);
97 naRef call(naRef code, int argc, naRef* args, naRef locals);
99 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
101 naRef propNodeGhost(SGPropertyNode* handle);
103 void registerToLoad(FGNasalModelData* data);
104 void registerToUnload(FGNasalModelData* data);
106 // can't call this 'globals' due to naming clash
107 naRef nasalGlobals() const
110 naContext context() const
113 nasal::Hash getGlobals() const
114 { return nasal::Hash(_globals, _context); }
116 // This mechanism is here to allow naRefs to be passed to
117 // locations "outside" the interpreter. Normally, such a
118 // reference would be garbage collected unexpectedly. By passing
119 // it to gcSave and getting a key/handle, it can be cached in a
120 // globals.__gcsave hash. Be sure to release it with gcRelease
123 void gcRelease(int key);
125 /// retrive the associated log object, for displaying log
126 /// output somewhere (a UI, presumably)
127 simgear::BufferedLogCallback* log() const
130 //friend class FGNasalScript;
131 friend class FGNasalListener;
132 friend class FGNasalModuleListener;
134 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
135 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
137 // Delay removing items of the _loadList to ensure the are already attached
138 // to the scene graph (eg. enables to retrieve world position in load
143 // FGTimer subclass for handling Nasal timer callbacks.
144 // See the implementation of the settimer() extension function for
148 virtual void timerExpired();
149 virtual ~NasalTimer() {}
156 std::map<int, FGNasalListener *> _listener;
157 std::vector<FGNasalListener *> _dead_listener;
159 static int _listenerId;
161 void loadPropertyScripts();
162 void loadPropertyScripts(SGPropertyNode* n);
163 void loadScriptDirectory(simgear::Dir nasalDir);
164 void addModule(std::string moduleName, simgear::PathList scripts);
165 void logError(naContext);
166 naRef parse(const char* filename, const char* buf, int len);
167 naRef genPropsModule();
173 SGPropertyNode_ptr _cmdArg;
179 simgear::BufferedLogCallback* _log;
181 void handleTimer(NasalTimer* t);
185 class FGNasalScript {
187 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
191 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
192 return naGetError(_nas->_context) == 0;
195 FGNasalSys* sys() const { return _nas; }
197 friend class FGNasalSys;
204 #endif // __NASALSYS_HXX