summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src
diff options
context:
space:
mode:
authorAlexander Lantsev <aunsane@gmail.com>2016-04-17 15:43:34 +0000
committerAlexander Lantsev <aunsane@gmail.com>2016-04-17 15:43:34 +0000
commit55b1f7fafef7085638c4c8604b248bbe9fe08124 (patch)
treecf8fc89ba253985bab9410116d091be1249e81dd /plugins/MirLua/src
parent1b3541ce712bc0abd6da9c544afd0d62e32fe930 (diff)
MirLua:
- added function topointer into _G - overrided tonumber to support pointers - api methods is only support light userdata as wparam/lparam git-svn-id: http://svn.miranda-ng.org/main/trunk@16697 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/MirLua/src')
-rw-r--r--plugins/MirLua/src/m_core.cpp11
-rw-r--r--plugins/MirLua/src/mlua.cpp5
-rw-r--r--plugins/MirLua/src/mlua_utils.cpp116
-rw-r--r--plugins/MirLua/src/stdafx.h8
4 files changed, 130 insertions, 10 deletions
diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/m_core.cpp
index 6ec526101e..3b7e841c1e 100644
--- a/plugins/MirLua/src/m_core.cpp
+++ b/plugins/MirLua/src/m_core.cpp
@@ -360,6 +360,15 @@ static int core_TerminateThread(lua_State *L)
return 1;
}
+int core_ptr2number(lua_State *L)
+{
+ ObsoleteMethod(L, "Use tonumber(x) instead");
+
+ luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+ lua_pushnumber(L, (intptr_t)lua_touserdata(L, 1));
+ return 1;
+}
+
luaL_Reg coreApi[] =
{
{ "CreateHookableEvent", core_CreateHookableEvent },
@@ -389,7 +398,7 @@ luaL_Reg coreApi[] =
{ "ForkThread", core_ForkThread },
{ "TerminateThread", core_TerminateThread },
- { "PointerToNumber", luaM_ptr2number },
+ { "PointerToNumber", core_ptr2number },
{ "GetFullPath", core_GetFullPath },
diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp
index fea62b2d93..f2cdafb41b 100644
--- a/plugins/MirLua/src/mlua.cpp
+++ b/plugins/MirLua/src/mlua.cpp
@@ -50,6 +50,11 @@ void CMLua::Load()
lua_register(L, "print", luaM_print);
lua_register(L, "a", luaM_toansi);
lua_register(L, "u", luaM_toucs2);
+ lua_register(L, "topointer", luaM_topointer);
+
+ lua_getglobal(L, "tonumber");
+ lua_setglobal(L, "_tonumber");
+ lua_register(L, "tonumber", luaM_tonumber);
lua_atpanic(L, luaM_atpanic);
diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp
index 7d4515bede..ea032b052e 100644
--- a/plugins/MirLua/src/mlua_utils.cpp
+++ b/plugins/MirLua/src/mlua_utils.cpp
@@ -114,24 +114,124 @@ int luaM_toucs2(lua_State *L)
return 1;
}
-bool luaM_toboolean(lua_State *L, int idx)
+int luaM_topointer(lua_State *L)
{
- if (lua_isnumber(L, idx))
- return lua_tonumber(L, idx) > 0;
- return lua_toboolean(L, idx) > 0;
+ 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 > INT_MAX)
+ {
+ const char *msg = lua_pushfstring(L, "%f is larger than %d", value, INT_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_ptr2number(lua_State *L)
+int luaM_tonumber(lua_State *L)
{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- lua_pushnumber(L, (intptr_t)lua_touserdata(L, 1));
+ if (lua_islightuserdata(L, 1))
+ {
+ lua_Integer value = (lua_Integer)lua_touserdata(L, 1);
+ lua_pushinteger(L, value);
+ }
+ else
+ {
+ lua_getglobal(L, "_tonumber");
+ lua_pushvalue(L, 1);
+ lua_pushnumber(L, 2);
+ luaM_pcall(L, 2, 1);
+ }
+
return 1;
}
+WPARAM luaM_towparam(lua_State *L, int idx)
+{
+ if (lua_islightuserdata(L, idx))
+ {
+ return (WPARAM)lua_touserdata(L, idx);
+ }
+
+ char text[512];
+ mir_snprintf(text, "Type %s is not supported. Use topointer(x) instead", luaL_typename(L, idx));
+ Log(text);
+ if (db_get_b(NULL, MODULE, "PopupOnObsolete", 0))
+ ShowNotification(MODULE, text, MB_OK | MB_ICONWARNING, NULL);
+
+ switch (lua_type(L, idx))
+ {
+ case LUA_TBOOLEAN:
+ return lua_toboolean(L, idx);
+ case LUA_TNUMBER:
+ return (WPARAM)lua_tonumber(L, idx);
+ case LUA_TSTRING:
+ return (WPARAM)lua_tostring(L, idx);
+ break;
+ default:
+ return NULL;
+ }
+}
+
+LPARAM luaM_tolparam(lua_State *L, int idx)
+{
+ if (lua_islightuserdata(L, idx))
+ {
+ return (LPARAM)lua_touserdata(L, idx);
+ }
+
+ char text[512];
+ mir_snprintf(text, "Type %s is not supported. Use topointer(x) instead", luaL_typename(L, idx));
+ Log(text);
+ if (db_get_b(NULL, MODULE, "PopupOnObsolete", 0))
+ ShowNotification(MODULE, text, MB_OK | MB_ICONWARNING, NULL);
+
+ switch (lua_type(L, idx))
+ {
+ case LUA_TBOOLEAN:
+ return lua_toboolean(L, idx);
+ case LUA_TNUMBER:
+ return (LPARAM)lua_tonumber(L, idx);
+ case LUA_TSTRING:
+ return (LPARAM)lua_tostring(L, idx);
+ case LUA_TUSERDATA:
+ case LUA_TLIGHTUSERDATA:
+ return (LPARAM)lua_touserdata(L, idx);
+ default:
+ return NULL;
+ }
+}
+
+bool luaM_toboolean(lua_State *L, int idx)
+{
+ if (lua_isnumber(L, idx))
+ return lua_tonumber(L, idx) > 0;
+ return lua_toboolean(L, idx) > 0;
+}
+
void ObsoleteMethod(lua_State *L, const char *message)
{
lua_Debug ar;
- if (lua_getstack(L, 0, &ar) == 0 || lua_getinfo(L, "n", &ar) == 0)
+ if (lua_getstack(L, 0, &ar) == 0 || lua_getinfo(L, "n", &ar) == 0)
return;
char text[512];
diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h
index a8a1ba4ff3..48218e8651 100644
--- a/plugins/MirLua/src/stdafx.h
+++ b/plugins/MirLua/src/stdafx.h
@@ -34,6 +34,7 @@
#include <m_toptoolbar.h>
#include <m_lua.h>
+#include <lua.hpp>
#include "version.h"
#include "resource.h"
@@ -110,7 +111,12 @@ int luaM_print(lua_State *L);
int luaM_toansi(lua_State *L);
int luaM_toucs2(lua_State *L);
-int luaM_ptr2number(lua_State *L);
+
+int luaM_topointer(lua_State *L);
+int luaM_tonumber(lua_State *L);
+
+WPARAM luaM_towparam(lua_State *L, int idx);
+LPARAM luaM_tolparam(lua_State *L, int idx);
bool luaM_toboolean(lua_State *L, int idx);