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);
32 SGCondition* conditionGhost(naRef r);
34 class FGNasalSys : public SGSubsystem
38 virtual ~FGNasalSys();
40 virtual void shutdown();
42 virtual void update(double dt);
44 // Loads a nasal script from an external file and inserts it as a
45 // global module of the specified name.
46 bool loadModule(SGPath file, const char* moduleName);
48 // Simple hook to run arbitrary source code. Returns a bool to
49 // indicate successful execution. Does *not* return any Nasal
50 // values, because handling garbage-collected objects from C space
51 // is deep voodoo and violates the "simple hook" idea.
52 bool parseAndRun(const char* sourceCode);
54 // Slightly more complicated hook to get a handle to a precompiled
55 // Nasal script that can be invoked via a call() method. The
56 // caller is expected to delete the FGNasalScript returned from
57 // this function. The "name" argument specifies the "file name"
58 // for the source code that will be printed in Nasal stack traces
60 // FGNasalScript* parseScript(const char* src, const char* name=0);
62 // Implementation of the settimer extension function
63 void setTimer(naContext c, int argc, naRef* args);
65 // Implementation of the setlistener extension function
66 naRef setListener(naContext c, int argc, naRef* args);
67 naRef removeListener(naContext c, int argc, naRef* args);
69 // Returns a ghost wrapper for the current _cmdArg
72 void setCmdArg(SGPropertyNode* aNode);
75 * create Nasal props.Node for an SGPropertyNode*
76 * This is the actual ghost, wrapped in a Nasal sugar class.
78 naRef wrappedPropsNode(SGPropertyNode* aProps);
80 // Callbacks for command and timer bindings
81 virtual bool handleCommand( const char* moduleName,
84 const SGPropertyNode* arg = 0 );
85 virtual bool handleCommand(const SGPropertyNode* arg);
87 bool createModule(const char* moduleName, const char* fileName,
88 const char* src, int len, const SGPropertyNode* cmdarg=0,
89 int argc=0, naRef*args=0);
91 void deleteModule(const char* moduleName);
93 void addCommand(naRef func, const std::string& name);
94 void removeCommand(const std::string& name);
97 * Set member of specified hash to given value
99 void hashset(naRef hash, const char* key, naRef val);
102 * Set member of globals hash to given value
104 void globalsSet(const char* key, naRef val);
106 naRef call(naRef code, int argc, naRef* args, naRef locals);
107 naRef callWithContext(naContext ctx, naRef code, int argc, naRef* args, naRef locals);
109 naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
110 naRef callMethodWithContext(naContext ctx, naRef code, naRef self, int argc, naRef* args, naRef locals);
112 naRef propNodeGhost(SGPropertyNode* handle);
114 void registerToLoad(FGNasalModelData* data);
115 void registerToUnload(FGNasalModelData* data);
117 // can't call this 'globals' due to naming clash
118 naRef nasalGlobals() const
121 nasal::Hash getGlobals() const
122 { return nasal::Hash(_globals, _context); }
124 // This mechanism is here to allow naRefs to be passed to
125 // locations "outside" the interpreter. Normally, such a
126 // reference would be garbage collected unexpectedly. By passing
127 // it to gcSave and getting a key/handle, it can be cached in a
128 // globals.__gcsave hash. Be sure to release it with gcRelease
131 void gcRelease(int key);
133 /// retrive the associated log object, for displaying log
134 /// output somewhere (a UI, presumably)
135 simgear::BufferedLogCallback* log() const
138 //friend class FGNasalScript;
139 friend class FGNasalListener;
140 friend class FGNasalModuleListener;
142 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _loadList;
143 SGLockedQueue<SGSharedPtr<FGNasalModelData> > _unloadList;
145 // Delay removing items of the _loadList to ensure the are already attached
146 // to the scene graph (eg. enables to retrieve world position in load
151 // FGTimer subclass for handling Nasal timer callbacks.
152 // See the implementation of the settimer() extension function for
156 virtual void timerExpired();
157 virtual ~NasalTimer() {}
164 std::map<int, FGNasalListener *> _listener;
165 std::vector<FGNasalListener *> _dead_listener;
167 std::vector<FGNasalModuleListener*> _moduleListeners;
169 static int _listenerId;
171 void loadPropertyScripts();
172 void loadPropertyScripts(SGPropertyNode* n);
173 void loadScriptDirectory(simgear::Dir nasalDir);
174 void addModule(std::string moduleName, simgear::PathList scripts);
175 static void logError(naContext);
176 naRef parse(naContext ctx, const char* filename, const char* buf, int len);
177 naRef genPropsModule();
184 SGPropertyNode_ptr _cmdArg;
186 simgear::BufferedLogCallback* _log;
188 typedef std::map<std::string, NasalCommand*> NasalCommandDict;
189 NasalCommandDict _commands;
191 naRef _wrappedNodeFunc;
193 void handleTimer(NasalTimer* t);
197 class FGNasalScript {
199 ~FGNasalScript() { _nas->gcRelease(_gcKey); }
203 naCall(_nas->_context, _code, 0, &n, naNil(), naNil());
204 return naGetError(_nas->_context) == 0;
207 FGNasalSys* sys() const { return _nas; }
209 friend class FGNasalSys;
216 #endif // __NASALSYS_HXX