static void lstop(lua_State *L, lua_Debug *ar) { (void)ar; /* unused arg. */ lua_sethook(L, NULL, 0, 0); /* Avoid luaL_error -- a C hook doesn't add an extra frame. */ luaL_where(L, 0); lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1)); lua_error(L); }
LUALIB_API int luaL_error (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); return lua_error(L); }
static int luaB_error (lua_State *L) { int level = (int)luaL_optinteger(L, 2, 1); lua_settop(L, 1); if (lua_type(L, 1) == LUA_TSTRING && level > 0) { luaL_where(L, level); /* add extra information */ lua_pushvalue(L, 1); lua_concat(L, 2); } return lua_error(L); }
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); }
int gt_lua_error(lua_State *L, GtError *err) { gt_assert(L && err); gt_assert(gt_error_is_set(err)); luaL_where(L, 1); lua_pushstring(L, gt_error_get(err)); gt_error_delete(err); lua_concat(L, 2); return lua_error(L); }
int luaL_error (LuaThread *L, const char *fmt, ...) { THREAD_CHECK(L); va_list argp; va_start(argp, fmt); luaL_where(L, 1); lua_pushvfstring(L, fmt, argp); va_end(argp); lua_concat(L, 2); return lua_error(L); }
/***************************************************************************** Internal api error function. Invoking this will cause Lua to stop executing the current context and throw an exception, so to speak. *****************************************************************************/ int luascript_error_vargs(lua_State *L, const char *format, va_list vargs) { fc_assert_ret_val(L != NULL, -1); luaL_where(L, 1); lua_pushvfstring(L, format, vargs); lua_concat(L, 2); return lua_error(L); }
static int luaB_error (lua_State *L) { int level = luaL_optint(L, 2, 1); luaL_checkany(L, 1); if (!lua_isstring(L, 1) || level == 0) lua_pushvalue(L, 1); /* propagate error message without changes */ else { /* add extra information */ luaL_where(L, level); lua_pushvalue(L, 1); lua_concat(L, 2); } return lua_error(L); }
// Gets the result of luaL_where as a string. static std::string luax_getwhere(lua_State *L, int level) { luaL_where(L, level); const char *str = lua_tostring(L, -1); std::string where; if (str) where = str; lua_pop(L, 1); return where; }
/************************************************************************** Internal api error function. Invoking this will cause Lua to stop executing the current context and throw an exception, so to speak. **************************************************************************/ int script_error(const char *fmt, ...) { va_list argp; va_start(argp, fmt); luaL_where(state, 1); lua_pushvfstring(state, fmt, argp); va_end(argp); lua_concat(state, 2); return lua_error(state); }
void luax_markdeprecated(lua_State *L, const char *name, APIType api, DeprecationType type, const char *replacement) { MarkDeprecated deprecated(name, api, type, replacement); if (deprecated.info != nullptr && deprecated.info->uses == 1) { luaL_where(L, 1); const char *where = lua_tostring(L, -1); if (where != nullptr) deprecated.info->where = where; lua_pop(L, 1); } }
static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { if (lua_isstring(L, -1)) { /* error object is a string? */ luaL_where(L, 1); /* add extra info */ lua_insert(L, -2); lua_concat(L, 2); } lua_error(L); /* propagate error */ } return r; }
static const char* msgstr (const char *fn, const char *fmt, va_list va) { assert( globalL ); if (mad_trace_location) { luaL_where(globalL, 1); if (fn) lua_pushfstring(globalL, "%s: ", fn); lua_pushvfstring(globalL, fmt, va); lua_concat(globalL, fn ? 3 : 2); } else lua_pushvfstring(globalL, fmt, va); return lua_tostring(globalL, -1); }
/* ==================== __lua_error ==================== */ I32 __lua_error(lua_State* ls) { LUA_STACK; LUA_WATCH(ls); luaL_where(ls, 1); LUA_WATCH(ls); if(lua_isstring(ls, -2)) { lua_pushvalue(ls, -2); LUA_WATCH(ls); lua_concat(ls, 2); LUA_WATCH(ls); } GLog(LT_SCRIPT, lua_tostring(ls, -1)); return lua_error(ls); }
int mylua_panic(lua_State *L) { luaL_where(L, 0); std::string errorMsg = lua_tostring(L, -1); lua_pop(L, 1); errorMsg += lua_tostring(L, -1); lua_pop(L, 1); lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); lua_call(L, 0, 1); errorMsg += "\n"; errorMsg += lua_tostring(L, -1); errorMsg += "\n"; Error("%s", errorMsg.c_str()); return 0; }
static int warning(lua_State *L) { #ifdef DEBUG int __startStackDepth = lua_gettop(L); #endif luaL_checkstring(L, 1); luaL_where(L, 1); lua_pushstring(L, "WARNING - "); lua_pushvalue(L, 1); lua_concat(L, 3); lua_pop(L, 1); #ifdef DEBUG assert(__startStackDepth == lua_gettop(L)); #endif return 0; }
static int warning(lua_State *L) { #ifdef DEBUG int __startStackDepth = lua_gettop(L); #endif luaL_checkstring(L, 1); luaL_where(L, 1); lua_pushstring(L, "WARNING - "); lua_pushvalue(L, 1); lua_concat(L, 3); BS_Log::log("%s\n", luaL_checkstring(L, -1)); lua_pop(L, 1); #ifdef DEBUG BS_ASSERT(__startStackDepth == lua_gettop(L)); #endif return 0; }
static int bindtable(lua_State *T, sqlite3_stmt *stmt) { int parameters = sqlite3_bind_parameter_count(stmt); const char *name; const char *err; int i; for (i = 1; i <= parameters; i++) { int ret; name = sqlite3_bind_parameter_name(stmt, i); lua_settop(T, 2); if (name == NULL || name[0] == '?') lua_rawgeti(T, 2, i); else if (name[0] == '@') lua_getfield(T, 2, name + 1); else lua_getfield(T, 2, name); ret = _bind_arg(T, 3, stmt, i); if (ret != SQLITE_OK) { err = sqlite3_errmsg(sqlite3_db_handle(stmt)); goto error; } } return 0; error: (void)sqlite3_clear_bindings(stmt); luaL_where(T, 1); if (name == NULL || name[0] == '?') lua_pushfstring(T, "error binding %d: %s", i, err); else lua_pushfstring(T, "error binding '%s': %s", name, err); lua_concat(T, 2); return -1; }
int pi_lua_panic(lua_State *L) { luaL_where(L, 0); std::string errorMsg = lua_tostring(L, -1); lua_pop(L, 1); errorMsg += lua_tostring(L, -1); lua_pop(L, 1); lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); lua_call(L, 0, 1); errorMsg += "\n"; errorMsg += lua_tostring(L, -1); errorMsg += "\n"; Error("%s", errorMsg.c_str()); // Error() is noreturn // XXX when Lua management is good enough, we can probably remove panic // entirely in favour of pcall and a nicer error handling system RETURN_ZERO_NONGNU_ONLY; }
void trap_args(lua_State *s, const char *funcname, const char *fmt, ...) { int i; va_list v; const char *where; va_start(v, fmt); for (i = 1; *fmt; ++fmt, ++i) { if (lua_isnil(s, i)) break; switch (*fmt) { case 'i': *va_arg(v, int*) = lua_tointeger(s, i); break; case 'u': *va_arg(v, unsigned int*) = lua_tounsigned(s, i); break; case 'n': *va_arg(v, double*) = lua_tonumber(s, i); break; case 's': *va_arg(v, const char**) = lua_tostring(s, i); break; case 'p': *va_arg(v, void**) = lua_touserdata(s, i); break; case 'b': *va_arg(v, int*) = lua_toboolean(s, i); break; } } if (*fmt) { luaL_where(s, 1); where = lua_tostring(s, -1); FATAL("%s Invalid call to %s", where, funcname); } va_end(v); }
int LuaProgressSink::LuaDebugOut(lua_State *L) { ProgressSink *ps = GetObjPointer(L, lua_upvalueindex(1)); // Check trace level if (lua_isnumber(L, 1)) { if (lua_tointeger(L, 1) > ps->GetTraceLevel()) return 0; // remove trace level lua_remove(L, 1); } // Only do format-string handling if there's more than one argument left // (If there's more than one argument left, assume first is a format string and rest are format arguments) if (lua_gettop(L) > 1) { // Format the string lua_getglobal(L, "string"); lua_getfield(L, -1, "format"); // Here stack contains format string, format arguments, 'string' table, format function // remove 'string' table lua_remove(L, -2); // put the format function into place lua_insert(L, 1); // call format function if (lua_pcall(L, lua_gettop(L) - 1, 1, 0)) { // format failed so top of the stack now has an error message // which we want to add position information to luaL_where(L, 1); lua_insert(L, 1); lua_concat(L, 2); lua_error(L); } } // Top of stack is now a string to output ps->Log(luaL_checkstring(L, 1)); return 0; }
/** Generate and throw an error. @function [parent=#global] error @param level stack level where the error must be reported @param narg indice of the erroneous argument @param expected name of the expected type @param got name of the type actually found @return never returns (throws a Lua error instead) */ static int error( lua_State *L, int level, int narg, const char *expected, const char *got) { lua_Debug ar; lua_getstack( L, level, & ar); lua_getinfo( L, "n", & ar); luaL_where( L, level+1); lua_pushfstring( L, " bad argument #%d to %s (%s expected, got %s)", narg, ar.name, expected, got); lua_concat( L, 2); #if 0 /* Debugging cruft */ int i; for(i=0;i<10;i++){ if( ! lua_getstack( L, i, & ar)) break; lua_getinfo( L, "n", & ar); printf( "\tat level %d: '%s' / '%s'\n", i, ar.name, ar.namewhat); } printf( "\tend of error, level was %d\n\n", level); #endif return lua_error( L); }
static int bindtable(lua_State *T, sqlite3_stmt *stmt) { int parameters = sqlite3_bind_parameter_count(stmt); const char *name; const char *err; int i; for (i = 1; i <= parameters; i++) { int ret; name = sqlite3_bind_parameter_name(stmt, i); lua_settop(T, 2); if (name == NULL || name[0] == '?') lua_rawgeti(T, 2, i); else if (name[0] == '@') lua_getfield(T, 2, name + 1); else lua_getfield(T, 2, name); switch (lua_type(T, 3)) { case LUA_TNIL: /* Should nil mean NULL.. ret = sqlite3_bind_null(stmt, i); break; ..or don't bind.. */ continue; case LUA_TNUMBER: ret = sqlite3_bind_double(stmt, i, lua_tonumber(T, 3)); break; case LUA_TSTRING: { size_t len; const char *str = lua_tolstring(T, 3, &len); ret = sqlite3_bind_text(stmt, i, str, len, SQLITE_STATIC); } break; default: err = "expected nil, number or string"; goto error; } if (ret != SQLITE_OK) { err = sqlite3_errmsg(sqlite3_db_handle(stmt)); goto error; } } return 0; error: (void)sqlite3_clear_bindings(stmt); luaL_where(T, 1); if (name == NULL || name[0] == '?') lua_pushfstring(T, "error binding %d: %s", i, err); else lua_pushfstring(T, "error binding '%s': %s", name, err); lua_concat(T, 2); return -1; }
JNIEXPORT void JNICALL Java_m_lua_Lua_Lwhere (JNIEnv* env, jobject thiz, jlong nativeObj, jint lvl) { pushJNIEnv(env, nativeObj); luaL_where((lua_State*) nativeObj, lvl); }
int LuaState::Where(int lvl) { luaL_where(m_state, lvl); return LuaStackObject(this, GetTop()); }
std::string errorLocation(lua_State* L) { luaL_where(L, 1); return lua_tostring(L, -1); }