66#include < chrono>
77#include < string>
88#include < sstream>
9-
10- #include < jLog/jLog.h>
9+ #include < iostream>
1110
1211#include < lua.h>
1312#include < LuaNumber.h>
@@ -178,18 +177,16 @@ class Console
178177public:
179178
180179 /* *
181- * @brief Construct a new Console with a jLog::Log .
180+ * @brief Construct a new Console.
182181 *
183- * @param l jLog::Log outputting Lua's messages.
184182 */
185183 Console
186184 (
187- jLog::Log & l,
188185 VisualisationState * visualisationState,
189186 CommandLine * options,
190187 Camera * camera
191188 )
192- : lastCommandOrProgram(" " ), lastStatus(false ), log(l)
189+ : lastCommandOrProgram(" " ), lastStatus(false )
193190 {
194191 lua = luaL_newstate ();
195192 luaL_openlibs (lua);
@@ -199,6 +196,17 @@ class Console
199196 extraSpace.camera = camera;
200197 extraSpace.exit = &exit;
201198 *static_cast <LuaExtraSpace**>(lua_getextraspace (lua)) = &extraSpace;
199+
200+ const char * init = R"(
201+ function use(module)
202+ for k,v in pairs(module) do
203+ if not _G[k] then
204+ _G[k] = module[k]
205+ end
206+ end
207+ end
208+ use(sfoav))" ;
209+ runString (init);
202210 }
203211
204212 ~Console (){ lua_close (lua); }
@@ -216,11 +224,7 @@ class Console
216224 {
217225 lastCommandOrProgram = file;
218226 lastStatus = luaL_loadfile (lua, file.c_str ());
219- int epos = lua_gettop (lua);
220- lua_pushcfunction (lua, traceback);
221- lua_insert (lua, epos);
222- lastStatus = lastStatus || lua_pcall (lua, 0 , LUA_MULTRET, epos);
223- lua_remove (lua, epos);
227+ lastStatus = lastStatus || lua_pcall (lua, 0 , LUA_MULTRET, 0 );
224228 return handleErrors ();
225229 }
226230 return false ;
@@ -236,13 +240,31 @@ class Console
236240 bool runString (std::string program)
237241 {
238242 if (luaIsOk ())
239- { lastCommandOrProgram = program;
243+ {
244+ lastCommandOrProgram = program;
240245 lastStatus = luaL_dostring (lua,program.c_str ());
241246 return handleErrors ();
242247 }
243248 return false ;
244249 }
245250
251+ /* *
252+ * @brief Attempt to run a Lua script from std::string.
253+ *
254+ * @param file Lua script.
255+ * @return true Error occured.
256+ * @return false OK.
257+ */
258+ bool runString (const char * program)
259+ {
260+ if (luaIsOk ())
261+ { lastCommandOrProgram = std::string (program);
262+ lastStatus = luaL_dostring (lua,program);
263+ return handleErrors ();
264+ }
265+ return false ;
266+ }
267+
246268 bool luaIsOk (){ return lua_status (lua) == LUA_OK ? true : false ; }
247269
248270 /* *
@@ -310,27 +332,12 @@ class Console
310332 bool lastStatus;
311333 bool exit = false ;
312334
313- jLog::Log & log;
314-
315- static int traceback (lua_State * lua) {
316- if (lua_isstring (lua, -1 ))
317- {
318- stackTrace = lua_tostring (lua, -1 );
319- lua_pop (lua, 1 );
320- }
321- luaL_traceback (lua, lua, NULL , 1 );
322- stackTrace += std::string (" \n " ) + lua_tostring (lua, -1 );
323- lua_pop (lua, 1 );
324- return 0 ;
325- }
326-
327335 bool handleErrors ()
328336 {
329337 if (lastStatus)
330338 {
331- std::string msg = " Exited with error running " +lastCommandOrProgram+" \n " ;
332- msg += stackTrace;
333- jLog::ERR (jLog::ERRORCODE::LUA_ERROR, msg) >> log;
339+ if (lua_isstring (lua, -1 )) { std::cerr << lua_tostring (lua, -1 ) << " \n " ; }
340+ else { std::cerr << " Exited with error running " +lastCommandOrProgram+" \n " ; }
334341 return true ;
335342 }
336343 else
0 commit comments