summaryrefslogtreecommitdiff
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
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
-rw-r--r--plugins/ExternalAPI/m_lua.h39
-rw-r--r--plugins/MirLua/Modules/m_toptoolbar/src/main.cpp8
-rw-r--r--plugins/MirLua/Modules/m_toptoolbar/src/stdafx.h3
-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
7 files changed, 136 insertions, 54 deletions
diff --git a/plugins/ExternalAPI/m_lua.h b/plugins/ExternalAPI/m_lua.h
index acc333a4ef..5d3fcda7bc 100644
--- a/plugins/ExternalAPI/m_lua.h
+++ b/plugins/ExternalAPI/m_lua.h
@@ -1,43 +1,4 @@
#ifndef _M_LUA_H_
#define _M_LUA_H_
-#include <lua.hpp>
-
-static __inline WPARAM luaM_towparam(lua_State *L, int idx)
-{
- 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;
- case LUA_TUSERDATA:
- case LUA_TLIGHTUSERDATA:
- return (WPARAM)lua_touserdata(L, idx);
- default:
- return NULL;
- }
-}
-
-static __inline LPARAM luaM_tolparam(lua_State *L, int idx)
-{
- 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;
- }
-}
-
#endif //_M_LUA_H_
diff --git a/plugins/MirLua/Modules/m_toptoolbar/src/main.cpp b/plugins/MirLua/Modules/m_toptoolbar/src/main.cpp
index 477cf7565b..af73c963ce 100644
--- a/plugins/MirLua/Modules/m_toptoolbar/src/main.cpp
+++ b/plugins/MirLua/Modules/m_toptoolbar/src/main.cpp
@@ -29,11 +29,11 @@ static TTBButton* MakeTBButton(lua_State *L)
lua_pop(L, 1);
lua_getfield(L, -1, "wParamUp");
- tbb->wParamUp = luaM_towparam(L, -1);
+ tbb->wParamUp = (WPARAM)lua_touserdata(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "lParamUp");
- tbb->lParamUp = luaM_tolparam(L, -1);
+ tbb->lParamUp = (LPARAM)lua_touserdata(L, -1);
lua_pop(L, 1);
// dn state
@@ -46,11 +46,11 @@ static TTBButton* MakeTBButton(lua_State *L)
lua_pop(L, 1);
lua_getfield(L, -1, "wParamDown");
- tbb->wParamDown = luaM_towparam(L, -1);
+ tbb->wParamDown = (WPARAM)lua_touserdata(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "lParamDown");
- tbb->lParamDown = luaM_tolparam(L, -1);
+ tbb->lParamDown = (LPARAM)lua_touserdata(L, -1);
lua_pop(L, 1);
return tbb;
diff --git a/plugins/MirLua/Modules/m_toptoolbar/src/stdafx.h b/plugins/MirLua/Modules/m_toptoolbar/src/stdafx.h
index 741eb49bd9..d69907bf2c 100644
--- a/plugins/MirLua/Modules/m_toptoolbar/src/stdafx.h
+++ b/plugins/MirLua/Modules/m_toptoolbar/src/stdafx.h
@@ -3,7 +3,8 @@
#include <windows.h>
-#include <m_lua.h>
+#include <lua.hpp>
+
#include <m_core.h>
#include <m_utils.h>
#include <m_toptoolbar.h>
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);