Method to treat LUA_USERDATA in C language
Hi,
I modify the record set that mysql returned and am going to go back up to a client.
I made "processing I did a hook in read_query_result in lua script, and to call C module".
But I do not understand a method to receive in the C side.
lua_type is LUA_USERDATA.
Is there the structure body which can handle these data?
Is there the method elsewhere?
Thanks,
Akihiro Seki
-- testlib.cpp -------------------------------
#include <lua.hpp>
#include <sstream>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
namespace
{
int testlib_decrypt_result(lua_State* L)
{
// ??????* recs = (???*)lua_touserdata(L,1);
const int num = lua_gettop(L);
if (num == 0) {
printf("No stack.\n");
return 1;
}
printf("Num:%d\n",num);
for (int i = num; i >= 1; i--) {
printf("%03d(%04d): ", i, -num + i - 1);
int type = lua_type(L, i);
switch(type) {
case LUA_TNIL:
printf("NIL\n");
break;
case LUA_TBOOLEAN:
printf("BOOLEAN %s\n", lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TLIGHTUSERDATA:
printf("LIGHTUSERDATA\n");
break;
case LUA_TNUMBER:
printf("NUMBER %f\n", lua_tonumber(L, i));
break;
case LUA_TSTRING:
printf("STRING %s\n", lua_tostring(L, i));
break;
case LUA_TTABLE:
printf("TABLE\n");
break;
case LUA_TFUNCTION:
printf("FUNCTION\n");
break;
case LUA_TUSERDATA:
printf("USERDATA\n");
break;
case LUA_TTHREAD:
printf("THREAD\n");
break;
}
}
printf("-----------------------------\n\n");
return 1;
}
const luaL_Reg testlib[] =
{
{ "decrypt_result", testlib_decrypt_result },
{ NULL , NULL }
};
} // namespace
extern "C" int luaopen_testlib(lua_State* L)
{
#if LUA_VERSION_NUM >= 502
luaL_newlib(L, testlib);
#else
luaL_register(L, "testlib", testlib);
#endif
return 1;
}
----------------------------------------------
-- sample.lua -------------------------------
function read_query_result( inj )
a = require("testlib")
ret = a.decrypt_result(inj.resultset)
print("decrypt_result:" .. ret)
proxy.response.type = proxy.MYSQLD_PACKET_OK
proxy.response.resultset = inj.resultset
return proxy.PROXY_SEND_RESULT
end
----------------------------------------------