Is there a Lua warning instead of a Lua Error?

Lua has the luaL_error and lua_error functions that will be used inside the C function, for example:

luaL_error( L, "something bad" ); 

This will result in an error message and Lua termination. The error message will contain a line and a file where this happens:

 Error: ../example/ex01.lua:6: something bad 

Is there a similar function that shows an error but does not break lua? but showing the line where it meets.

+4
source share
1 answer

Copy the source code of luaL_error and replace the call with lua_error at the end with the appropriate call to printf using the line lua_tostring(L,-1) . Something like that:

 LUALIB_API int luaL_warn (lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); luaL_where(L, 1); lua_pushvfstring(L, fmt, argp); va_end(argp); lua_concat(L, 2); printf("warning: %s\n",lua_tostring(L,-1)); return 0; } static int luaB_warn (lua_State *L) { return luaL_warn(L, "%s", luaL_checkstring(L, 1)); } 

Remember to export it to Lua by adding an entry to the base_funcs text in lbaselib.c or by calling lua_register(L,"warn",luaB_warn) .

+3
source

All Articles