summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_utils.cpp
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2017-09-12 22:02:30 +0300
committeraunsane <aunsane@gmail.com>2017-09-12 22:03:37 +0300
commit6f99f13cf590c323fd5bd5b4d6855e59f582fb0e (patch)
tree4b9a66758433be8273c7c20e06ab305901b4fdfb /plugins/MirLua/src/mlua_utils.cpp
parent6611ef6791a897d23cb79ab7b56eb48d49452b80 (diff)
MirLua: massive refactoring
- m_msg_buttonbar moved to m_srrm - version bump
Diffstat (limited to 'plugins/MirLua/src/mlua_utils.cpp')
-rw-r--r--plugins/MirLua/src/mlua_utils.cpp199
1 files changed, 15 insertions, 184 deletions
diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp
index f21bd8fcbf..9abbc6e492 100644
--- a/plugins/MirLua/src/mlua_utils.cpp
+++ b/plugins/MirLua/src/mlua_utils.cpp
@@ -48,8 +48,9 @@ void ObsoleteMethod(lua_State *L, const char *message)
ShowNotification(MODULE, text, MB_OK | MB_ICONWARNING, NULL);
}
-void ReportError(const char *message)
+void ReportError(lua_State *L)
{
+ const char *message = lua_tostring(L, -1);
Log(message);
if (db_get_b(NULL, MODULE, "PopupOnError", 0))
ShowNotification(MODULE, message, MB_OK | MB_ICONERROR);
@@ -57,8 +58,7 @@ void ReportError(const char *message)
int luaM_atpanic(lua_State *L)
{
- ReportError(lua_tostring(L, -1));
-
+ ReportError(L);
return 0;
}
@@ -66,196 +66,27 @@ int luaM_pcall(lua_State *L, int n, int r)
{
int res = lua_pcall(L, n, r, 0);
if (res != LUA_OK)
- ReportError(lua_tostring(L, -1));
+ ReportError(L);
return res;
}
-int luaM_print(lua_State *L)
-{
- CMStringA data;
- int nargs = lua_gettop(L);
- for (int i = 1; i <= nargs; i++)
- {
- switch (lua_type(L, i))
- {
- case LUA_TNIL:
- data.Append("nil");
- break;
- case LUA_TBOOLEAN:
- data.AppendFormat("%s", lua_toboolean(L, i) ? "true" : "false");
- break;
- case LUA_TNUMBER:
- case LUA_TSTRING:
- data.AppendFormat("%s", lua_tostring(L, i));
- break;
- default:
- data.AppendFormat("%s(0x%p)", luaL_typename(L, i), lua_topointer(L, i));
- break;
- }
- data.Append(", ");
- }
- if (data.GetLength() >= 1)
- data.Delete(data.GetLength() - 2, 2);
-
- Log(data.GetBuffer());
-
- return 0;
-}
-
-int luaM_toansi(lua_State *L)
-{
- const char *value = luaL_checkstring(L, 1);
- int codepage = luaL_optinteger(L, 2, Langpack_GetDefaultCodePage());
-
- ptrA string(mir_strdup(value));
- lua_pushstring(L, mir_utf8decodecp(string, codepage, NULL));
-
- return 1;
-}
-
-int luaM_toucs2(lua_State *L)
+int luaM_getenv(lua_State *L)
{
- const char *value = luaL_checkstring(L, 1);
-
- ptrW unicode(mir_utf8decodeW(value));
- size_t length = mir_wstrlen(unicode) * sizeof(wchar_t);
-
- ptrA string((char*)mir_calloc(length + 1));
- memcpy(string, unicode, length);
-
- lua_pushlstring(L, string, length + 1);
-
- return 1;
-}
-
-int luaM_topointer(lua_State *L)
-{
- switch (lua_type(L, 1))
- {
- case LUA_TBOOLEAN:
- lua_pushlightuserdata(L, (void*)lua_toboolean(L, 1));
- break;
- case LUA_TNUMBER:
- {
- if (lua_isinteger(L, 1))
- {
- lua_Integer value = lua_tointeger(L, 1);
- if (value > INTPTR_MAX)
- {
- const char *msg = lua_pushfstring(L, "%f is larger than %d", value, INTPTR_MAX);
- return luaL_argerror(L, 1, msg);
- }
- lua_pushlightuserdata(L, (void*)value);
- }
- }
- break;
- case LUA_TSTRING:
- lua_pushlightuserdata(L, (void*)lua_tostring(L, 1));
- break;
- case LUA_TLIGHTUSERDATA:
- lua_pushvalue(L, 1);
- default:
- return luaL_argerror(L, 1, luaL_typename(L, 1));
- }
-
- return 1;
-}
-
-int luaM_tonumber(lua_State *L)
-{
- if (lua_islightuserdata(L, 1))
- {
- lua_Integer value = (lua_Integer)lua_touserdata(L, 1);
- lua_pushinteger(L, value);
- return 1;
- }
-
- int n = lua_gettop(L);
- lua_getglobal(L, "_tonumber");
- lua_pushvalue(L, 1);
- if (n == 2)
- lua_pushvalue(L, 2);
- luaM_pcall(L, n, 1);
-
- return 1;
-}
-
-UINT_PTR luaM_tomparam(lua_State *L, int idx)
-{
- switch (lua_type(L, idx))
- {
- case LUA_TBOOLEAN:
- return lua_toboolean(L, idx);
- case LUA_TSTRING:
- return (UINT_PTR)lua_tostring(L, idx);
- case LUA_TLIGHTUSERDATA:
- return (UINT_PTR)lua_touserdata(L, idx);
- case LUA_TNUMBER:
- {
- if (lua_isinteger(L, idx))
- {
- lua_Integer value = lua_tointeger(L, idx);
- return value <= INTPTR_MAX
- ? (UINT_PTR)value
- : NULL;
- }
- }
- default:
- return NULL;
- }
-}
-
-int luaM_interpolate(lua_State *L)
-{
- const char *string = luaL_checkstring(L, 1);
-
- char pattern[128];
-
- if (lua_istable(L, 2))
- {
- for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2))
- {
- lua_pushvalue(L, -2);
- const char *key = lua_tostring(L, -1);
- const char *val = lua_tostring(L, -2);
-
- mir_snprintf(pattern, "{%s}", key);
- string = luaL_gsub(L, string, pattern, val);
- lua_pop(L, 1);
- }
- }
- else
+ lua_Debug ar;
+ if (lua_getstack(L, 1, &ar) == 0 ||
+ lua_getinfo(L, "f", &ar) == 0 ||
+ lua_iscfunction(L, -1))
{
- int nargs = lua_gettop(L);
- for (int i = 2; i <= nargs; i++)
- {
- const char *val = lua_tostring(L, i);
-
- mir_snprintf(pattern, "{%d}", i - 1);
- string = luaL_gsub(L, string, pattern, val);
- lua_pop(L, 1);
- }
+ lua_pop(L, 1);
+ return 0;
}
- lua_Debug ar;
-
- size_t level = 1;
-
- while (lua_getstack(L, level++, &ar))
+ const char *env = lua_getupvalue(L, -1, 1);
+ if (!env || strcmp(env, "_ENV") != 0)
{
- size_t i = 1;
- while (const char *name = lua_getlocal(L, &ar, i++))
- {
- const char *val = lua_tostring(L, -1);
- if (val)
- {
- mir_snprintf(pattern, "${%s}", name);
- string = luaL_gsub(L, string, pattern, val);
- lua_pop(L, 1);
- }
- }
+ lua_pop(L, 1);
+ return 0;
}
- lua_pushstring(L, string);
return 1;
}