Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Lua Warning instead of Lua Error?

Lua has the luaL_error, and lua_error functions to be used inside a C function, like:

luaL_error( L, "something bad" );

This will cause that a error message was show and Lua execution halted. The error message will contain the line and file where it occurs:

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

Is there a similar function that shows the error but don't break the lua execution? but showing the line where it occurs.

like image 563
Zhen Avatar asked Mar 12 '13 11:03

Zhen


People also ask

What is a LUA warning WoW?

LUA errors are typically caused by corrupted interface files. Resetting your User Interface should resolve the issue.

What is assert LUA?

Lua assert is the function to use in the error handling of the Lua programming language. This is the function helping to handle the runtime error of the Lua source code to avoid complications between compile-time error and run time error.


1 Answers

Copy the source code of luaL_error and replace the call to lua_error at the end by a suitable call to printf using the string lua_tostring(L,-1). Something like this:

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));
}

Don't forget to export it to Lua by adding an entry in say base_funcs in lbaselib.c or by calling lua_register(L,"warn",luaB_warn).

like image 175
lhf Avatar answered Oct 01 '22 19:10

lhf