4 #include <simgear/math/SGMath.hxx> // keep before any cppbind include to enable
5 // SGVec2<T> conversion.
6 #include <simgear/misc/sg_dir.hxx>
7 #include <simgear/misc/sg_path.hxx>
8 #include <simgear/nasal/cppbind/NasalHash.hxx>
9 #include <simgear/nasal/nasal.h>
10 #include <simgear/props/props.hxx>
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/threads/SGQueue.hxx>
14 // Required only for MSVC
16 # include <Scripting/NasalModelData.hxx>
23 class FGNasalListener;
25 class FGNasalModelData;
27 class FGNasalModuleListener;
29 namespace simgear { class BufferedLogCallback; }
31 SGPropertyNode* ghostToPropNode(naRef ref);
33 class FGNasalSys : public SGSubsystem
37 virtual ~FGNasalSys();
39 virtual void shutdown();
41 virtual void update(double dt);
43 // Loads a nasal script from an external file and inserts it as a
44 // global module of the specified name.
45 bool loadModule(SGPath file, const char* moduleName);
47 // Simple hook to run arbitrary source code. Returns a bool to
48 // indicate successful execution. Does *not* return any Nasal
49 // values, because handling garbage-collected objects from C space
50 // is deep voodoo and violates the "simple hook" idea.
51 bool parseAndRun(const char* sourceCode);
53 // Slightly more complicated hook to get a handle to a precompiled
54 // Nasal script that can be invoked via a call() method. The
55 // caller is expected to delete the FGNasalScript returned from
56 // this function. The "name" argument specifies the "file name"
57 // for the source code that will be printed in Nasal stack traces
59 // FGNasalScript* parseScript(const char* src, const char* name=0);
61 // Implementation of the settimer extension function
62 void setTimer(naContext c, int argc, naRef* args);
64 // Implementation of the setlistener extension function
65 naRef setListener(naContext c, int argc, naRef* args);
66 naRef removeListener(naContext c, int argc, naRef* args);
68 // Returns a ghost wrapper for the current _cmdArg
71 void setCmdArg(SGPropertyNode* aNode);
74 * create Nasal props.Node for an SGPropertyNode*
75 * This is the actual ghost, wrapped in a Nasal sugar class.
77 naRef wrappedPropsNode(SGPropertyNode* aProps);
79 // Callbacks for command and timer bindings
80 virtual bool handleCommand( const char* moduleName,
83 const SGPropertyNode* arg = 0 );
84 virtual bool handleCommand(const SGPropertyNode* arg);
86 bool createModule(const char* moduleName, const char* fileName,
87 const char* src, int len, const SGPropertyNode* cmdarg=0,
88 int argc=0, naRef*args=0);
90 void deleteModule(const char* moduleName);
92 void addCommand(naRef func, const std::string& name);
93 void removeCommand(const std::string& name);
96 * Set member of specified hash to given value
98 void hashset(naRef hash, const char* key, naRef val);
101 * Set member of globals hash to given value
103 void globalsSet(const char* key, naRef val);
105 naRef call(naRef code, int argc, naRef* args, naRef locals);
106 naRef callWithContext(naContext ctx, naRef code, int argc, naRef* args, naRef locals);
108 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
109 naRef callMethodWithContext(naContext ctx, naRef code, naRef self, int argc, naRef* args, naRef locals);
111 naRef propNodeGhost(SGPropertyNode* handle);
113 void registerToLoad(FGNasalModelData* data);
114 void registerToUnload(FGNasalModelData* data);
116 // can't call this 'globals' due to naming clash
117 naRef nasalGlobals() const
120 nasal::Hash getGlobals() const
121 { return nasal::Hash(_globals, _context); }
123 // This mechanism is here to allow naRefs to be passed to
124 // locations "outside" the interpreter. Normally, such a
125 // reference would be garbage collected unexpectedly. By passing
126 // it to gcSave and getting a key/handle, it can be cached in a
127 // globals.__gcsave hash. Be sure to release it with gcRelease
130 void gcRelease(int key);
133 * Check if IOrules correctly work to limit access from Nasal scripts to the
136 * @note Just a simple test is performed to check if access to a path is
137 * possible which should never be possible (The actual path refers to
138 * a file/folder named 'do-not-access' in the file system root).
140 * @see http://wiki.flightgear.org/IOrules
142 * @return Whether the check was successful.
146 /// retrive the associated log object, for displaying log
147 /// output somewhere (a UI, presumably)
148 simgear::BufferedLogCallback* log() const
151 //friend class FGNasalScript;
152 friend class FGNasalListener;
153 friend class FGNasalModuleListener;
155 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
156 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
158 // Delay removing items of the _loadList to ensure the are already attached
159 // to the scene graph (eg. enables to retrieve world position in load
164 // FGTimer subclass for handling Nasal timer callbacks.
165 // See the implementation of the settimer() extension function for
169 virtual void timerExpired();
170 virtual ~NasalTimer() {}
177 std::map<int, FGNasalListener *> _listener;
178 std::vector<FGNasalListener *> _dead_listener;
180 std::vector<FGNasalModuleListener*> _moduleListeners;
182 static int _listenerId;
184 void loadPropertyScripts();
185 void loadPropertyScripts(SGPropertyNode* n);
186 void loadScriptDirectory(simgear::Dir nasalDir);
187 void addModule(std::string moduleName, simgear::PathList scripts);
188 static void logError(naContext);
189 naRef parse(naContext ctx, const char* filename, const char* buf, int len);
190 naRef genPropsModule();
197 SGPropertyNode_ptr _cmdArg;
199 simgear::BufferedLogCallback* _log;
201 typedef std::map<std::string, NasalCommand*> NasalCommandDict;
202 NasalCommandDict _commands;
204 naRef _wrappedNodeFunc;
206 void handleTimer(NasalTimer* t);
210 class FGNasalScript {
212 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
216 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
217 return naGetError(_nas->_context) == 0;
220 FGNasalSys* sys() const { return _nas; }
222 friend class FGNasalSys;
229 #endif // __NASALSYS_HXX