static int db_getinfo(lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg + 2, "flnSu"); if (lua_isnumber(L, arg + 1)) { if (!lua_getstack(L1, (int) lua_tointeger(L, arg + 1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } else if (lua_isfunction(L, arg + 1)) { lua_pushfstring(L, ">%s", options); options = lua_tostring(L, -1); lua_pushvalue(L, arg + 1); lua_xmove(L, L1, 1); } else return luaL_argerror(L, arg + 1, "function or level expected"); if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg + 2, "invalid option"); lua_createtable(L, 0, 2); if (strchr(options, 'S')) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "what", ar.what); } if (strchr(options, 'l')) settabsi(L, "currentline", ar.currentline); if (strchr(options, 'u')) settabsi(L, "nups", ar.nups); if (strchr(options, 'n')) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } if (strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (strchr(options, 'f')) treatstackoption(L, L1, "func"); return 1; /* return table */ }
static int getinfo (lua_State *L) { lua_Debug ar; const char *options = luaL_opt_string(L, 2, "flnSu"); char buff[20]; if (lua_isnumber(L, 1)) { if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } else if (lua_isfunction(L, 1)) { lua_pushvalue(L, 1); sprintf(buff, ">%.10s", options); options = buff; } else luaL_argerror(L, 1, "function or level expected"); if (!lua_getinfo(L, options, &ar)) luaL_argerror(L, 2, "invalid option"); lua_newtable(L); for (; *options; options++) { switch (*options) { case 'S': settabss(L, "source", ar.source); if (ar.source) settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabss(L, "what", ar.what); break; case 'l': settabsi(L, "currentline", ar.currentline); break; case 'u': settabsi(L, "nups", ar.nups); break; case 'n': settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); break; case 'f': lua_pushstring(L, "func"); lua_pushvalue(L, -3); lua_settable(L, -3); break; } } return 1; /* return table */ }
static lua_Object getfuncinfo (lua_Object func) { lua_Object result = lua_createtable(); char *str; int line; lua_funcinfo(func, &str, &line); if (line == -1) /* C function? */ settabss(result, "kind", "C"); else if (line == 0) { /* "main"? */ settabss(result, "kind", "chunk"); settabss(result, "source", str); } else { /* Lua function */ settabss(result, "kind", "Lua"); settabsi(result, "def_line", line); settabss(result, "source", str); } if (line != 0) { /* is it not a "main"? */ char *kind = lua_getobjname(func, &str); if (*kind) { settabss(result, "name", str); settabss(result, "where", kind); } } return result; }
/* ** Calls 'lua_getinfo' and collects all results in a new table. ** L1 needs stack space for an optional input (function) plus ** two optional outputs (function and line table) from function ** 'lua_getinfo'. */ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnStu"); checkstack(L, L1, 3); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ lua_xmove(L, L1, 1); } else { /* stack level */ if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg+2, "invalid option"); lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "what", ar.what); } if (strchr(options, 'l')) settabsi(L, "currentline", ar.currentline); if (strchr(options, 'u')) { settabsi(L, "nups", ar.nups); settabsi(L, "nparams", ar.nparams); settabsb(L, "isvararg", ar.isvararg); } if (strchr(options, 'n')) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } if (strchr(options, 't')) settabsb(L, "istailcall", ar.istailcall); if (strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (strchr(options, 'f')) treatstackoption(L, L1, "func"); return 1; /* return table */ }
static void getstack (void) { lua_Object func = lua_stackedfunction(luaL_check_int(1)); if (func == LUA_NOOBJECT) /* level out of range? */ return; else { lua_Object result = getfuncinfo(func); int currline = lua_currentline(func); if (currline > 0) settabsi(result, "current", currline); lua_pushobject(result); lua_pushstring("func"); lua_pushobject(func); lua_settable(); /* result.func = func */ lua_pushobject(result); } }
static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; //add by cuiwei 07.8.30 int i; char aparms[1024] = {0}; //add end lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnSu"); if (lua_isnumber(L, arg+1)) { if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { lua_pushnil(L); /* level out of range */ return 1; } } else if (lua_isfunction(L, arg+1)) { lua_pushfstring(L, ">%s", options); options = lua_tostring(L, -1); lua_pushvalue(L, arg+1); lua_xmove(L, L1, 1); } else return luaL_argerror(L, arg+1, "function or level expected"); if (!lua_getinfo(L1, options, &ar)) return luaL_argerror(L, arg+2, "invalid option"); lua_createtable(L, 0, 2); if (strchr(options, 'S')) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "what", ar.what); } if (strchr(options, 'l')) settabsi(L, "currentline", ar.currentline); if (strchr(options, 'u')) { settabsi(L, "nups", ar.nups); //add by cuiwei 07.8.30 if (ar.what != "C" ) { settabsi(L, "npars", ar.npars); settabsi(L, "has3dot", ar.has3dot?1:0); for ( i = 1; i <= ar.npars; i++ ) { strcat(aparms, ar.parms[i-1]); if ( i != ar.npars || ar.has3dot) { strcat(aparms, ","); } } if ( ar.has3dot ) { strcat(aparms, "..."); } settabss(L, "strparms", aparms); } //add end } if (strchr(options, 'n')) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } if (strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (strchr(options, 'f')) treatstackoption(L, L1, "func"); return 1; /* return table */ }