I am trying to integrate Lua with C++ and I have some doubts when it comes to debugging the errors.
When any part of a lua script fails, be it a runtime error or a syntax error, Lua returns control to the host program quite silently. It basically luaL_dofile
ends up returning 1
.
From what I've been reading in the official reference , when an error occurs Lua fills the structure lua_Debug
with more detailed information if we call the function lua_getinfo
... or so I seem to understand. :-(
On the one hand I have an intuition about the procedure of obtaining more information to debug errors, but in practice... it doesn't work.
I paste here the code of main.cpp
#include <iostream>
#include "GetHttp.h"
#include "Conf.h"
#include "App.h"
#include "Poco/Logger.h"
extern "C"
{
#include "lua5.2/lua.h"
#include "lua5.2/lualib.h"
#include "lua5.2/lauxlib.h"
}
#include <luabind/luabind.hpp>
#include <luabind/out_value_policy.hpp>
#include <luabind/adopt_policy.hpp>
#include <luabind/return_reference_to_policy.hpp>
using namespace std;
int main(int argc, char **argv)
{
Conf conf;
GetHttp ini;
ini.setConf(&conf);
conf.logger.information("- Iniciando :-D");
const char file[]="mods/test.lua";
lua_State *L = luaL_newstate();
conf.logger.information("Leido lua_state");
luaL_openlibs(L);
conf.logger.information("Leido openlibs");
luabind::open(L);
conf.logger.information("Abierto luabind");
luabind::module(L)
[
luabind::class_<GetHttp>("GetHttp")
.def(luabind::constructor<>())
.def("Get",&GetHttp::miGet)
];
conf.logger.information("Cargado módulo");
int retorno = luaL_dofile(L,file);
lua_close(L);
if(retorno == 0)
{
conf.logger.information("Saliendo...");
}
else
{
cout << "Saliendo con errores" << endl;
conf.logger.information("Saliendo... con errores");
lua_Debug ar;
lua_getglobal(L, "extraerTelefonos");
lua_getinfo(L, ">Sn", &ar);
cout << "-- " << ar.what << endl;
cout << "-- " << ar.source << endl;
cout << "-- " << ar.linedefined << endl;
cout << "-- " << ar.name << endl;
cout << "-- " << ar.namewhat << endl;
}
return 0;
}
The second argument of lua_getglobal
I put the name of the function that I think is causing the error... but I don't know if the second parameter is valid for exactly that.
As you can see at the end of the program there is only garbage in lua_Debug
.
Program output:
Encontradas 30 coincidencias
Saliendo con errores
-- C
-- =[C]
-- -1
-- %
How do I get information about errors from a Lua script inside a C++ program?
What am I doing wrong?
Moves the indicated line to the end of the
if-else
.lua_close
releases script-related resources, including data retrievable by thelua_getinfo
.It should look like this: