summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lua/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lbaselib.c')
-rw-r--r--apps/plugins/lua/lbaselib.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/apps/plugins/lua/lbaselib.c b/apps/plugins/lua/lbaselib.c
index ad94984191..cb7b29a92a 100644
--- a/apps/plugins/lua/lbaselib.c
+++ b/apps/plugins/lua/lbaselib.c
@@ -19,6 +19,13 @@
#include "lauxlib.h"
#include "lualib.h"
+#ifdef LUA_OPTIMIZE_DEBUG_USER
+#include "lobject.h"
+#include "lstate.h"
+#include "ldebug.h"
+#include "lfunc.h"
+#endif
+
@@ -302,6 +309,70 @@ static int luaB_loadfile (lua_State *L) {
}
+#ifdef LUA_OPTIMIZE_DEBUG_USER
+/* stripdebug([level[, function]]).
+ * level: 1 don't discard debug
+ * 2 discard Local and Upvalue debug info
+ * 3 discard Local, Upvalue and lineno debug info.
+ * function: Function to be stripped as per setfenv except 0 not permitted.
+ * If no arguments then the current default setting is returned.
+ * If function is omitted, this is the default setting for future compiles
+ * The function returns an estimated integer count of the bytes stripped.
+ */
+static int luaB_stripdebug (lua_State *L) {
+ int level;
+
+ if (L->top == L->base) {
+ lua_pushlightuserdata(L, &luaG_stripdebug);
+ lua_gettable(L, LUA_REGISTRYINDEX);
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 1);
+ lua_pushinteger(L, LUA_OPTIMIZE_DEBUG);
+ }
+ return 1;
+ }
+
+ level = luaL_checkint(L, 1);
+ if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3");
+
+ if (L->top == L->base + 1) {
+ /* Store the default level in the registry if no function parameter */
+ lua_pushlightuserdata(L, &luaG_stripdebug);
+ lua_pushinteger(L, level);
+ lua_settable(L, LUA_REGISTRYINDEX);
+ lua_settop(L,0);
+ return 0;
+ }
+
+ if (level == 1) {
+ lua_settop(L,0);
+ lua_pushinteger(L, 0);
+ return 1;
+ }
+
+ if (!lua_isfunction(L, 2)) {
+ int scope = luaL_checkint(L, 2);
+ if (scope > 0) {
+ /* if the function parameter is a +ve integer then climb to find function */
+ lua_Debug ar;
+ lua_pop(L, 1); /* pop level as getinfo will replace it by the function */
+ if (lua_getstack(L, scope, &ar)) {
+ lua_getinfo(L, "f", &ar);
+ }
+ }
+ }
+
+ if(!lua_isfunction(L, 2) || lua_iscfunction(L, -1)) luaL_argerror(L, 2, "must be a Lua Function");
+ // lua_lock(L);
+ Proto *f = clvalue(L->base + 1)->l.p;
+ // lua_unlock(L);
+ lua_settop(L,0);
+ lua_pushinteger(L, luaG_stripdebug(L, f, level, 16));
+ return 1;
+}
+#endif
+
+
/*
** Reader for generic `load' function: `lua_load' uses the
** stack for internal stuff, so the reader cannot change the
@@ -482,6 +553,9 @@ static const luaL_Reg base_funcs[] = {
{"select", luaB_select},
{"setfenv", luaB_setfenv},
{"setmetatable", luaB_setmetatable},
+#ifdef LUA_OPTIMIZE_DEBUG_USER
+ {"stripdebug", luaB_stripdebug},
+#endif
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},