lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2014/1/17 Elias Barrionovo <elias.tandel@gmail.com>:

> I always thought assert() called error() internally.

Well, now, this sort of question is not that hard to answer.
Take a look at lbaselib.c.

assert() is very simple indeed.

static int luaB_assert (lua_State *L) {
  if (!lua_toboolean(L, 1))
    return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  return lua_gettop(L);
}

error() is more sophisticated, because, as the manual states,
| Usually, error adds some information about the error position at
| the beginning of the message, if the message is a string.

static int luaB_error (lua_State *L) {
  int level = luaL_optint(L, 2, 1);
  lua_settop(L, 1);
  if (lua_isstring(L, 1) && level > 0) {  /* add extra information? */
    luaL_where(L, level);
    lua_pushvalue(L, 1);
    lua_concat(L, 2);
  }
  return lua_error(L);
}

So assert() does not call error(). Internally, assert calls luaL_error()
with format string "%s", so the message you supply must have
type string. error calls lua_error() which does not care what type
your message is.