原文连接:https://blog.csdn.net/u012787710/article/details/52495011。
userdata (Lua5.3)
Q:什么是 userdata ?
A:分为两类,full userdata 和 light userdata。Lua使用他们来表示C中的类型。
Q:两种”userdata”的区别?
A:
例子一 light userdata
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
typedef struct
{
int x;
int y;
int z;
}TData;
static int getAttribute(lua_State* L)
{
TData *data = (TData*)lua_touserdata(L, 1);
std::string attribute = luaL_checkstring(L, 2);
int result = 0;
if (attribute == "x")
{
result = data->x;
}
else if (attribute == "y")
{
result = data->y;
}
else
{
result = data->z;
}
lua_pushnumber(L, result);
return 1;
}
static luaL_Reg dataLib[] = {
{ "__index", getAttribute },
{ NULL, NULL }
};
void getMetaTable(lua_State* L, luaL_Reg* methods)
{
lua_pushlightuserdata(L, methods);
lua_gettable(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1)) {
/* not found */
lua_pop(L, 1);
lua_newtable(L);
luaL_setfuncs(L, methods, 0);
lua_pushlightuserdata(L, methods);
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
}
}
int main()
{
const char* filename = "test.lua";
lua_State *lua = luaL_newstate();
if (lua == NULL)
{
fprintf(stderr, "open lua failed");
return -1;
}
luaL_openlibs(lua);
TData input = { 123, 231, 321 };
lua_pushlightuserdata(lua, &input);
getMetaTable(lua, dataLib);
lua_setmetatable(lua, -2);
lua_setglobal(lua, "input");
if (luaL_dofile(lua, filename))
{
//luaL_error(lua, "load file %s failed", filename);
}
lua_getglobal(lua, "data");
int output = lua_tointeger(lua, -1);
std::cout << output << std::endl;
return 0;
}
lua文件
data = input.x;
print(data)
例子二 full userdata
//#include <string.h>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <iostream>
using namespace std;
static struct StudentTag
{
char *strName; // 学生姓名
char *strNum; // 学号
int iSex; // 学生性别
int iAge; // 学生年龄
}T;
static int Student(lua_State *L)
{
size_t iBytes = sizeof(struct StudentTag);
struct StudentTag *pStudent;
pStudent = (struct StudentTag *)lua_newuserdata(L, iBytes);
//设置元表
luaL_getmetatable(L, "Student");
lua_setmetatable(L, -2);
//lua_pushnumber(L, 123);
return 1; // 新的userdata已经在栈上了
}
static int GetName(lua_State *L)
{
struct StudentTag *pStudent = (struct StudentTag *)luaL_checkudata(L, 1, "Student");
lua_pushstring(L, pStudent->strName);
return 1;
}
static int SetName(lua_State *L)
{
// 第一个参数是userdata
struct StudentTag *pStudent = (struct StudentTag *)luaL_checkudata(L, 1, "Student");
// 第二个参数是一个字符串
const char *pName = luaL_checkstring(L, 2);
luaL_argcheck(L, pName != NULL && pName != "", 2, "Wrong Parameter");
pStudent->strName =(char*) pName;
return 0;
}
static int GetAge(lua_State *L)
{
struct StudentTag *pStudent = (struct StudentTag *)luaL_checkudata(L, 1, "Student");
lua_pushinteger(L, pStudent->iAge);
return 1;
}
static int SetAge(lua_State *L)
{
struct StudentTag *pStudent = (struct StudentTag *)luaL_checkudata(L, 1, "Student");
int iAge = luaL_checkinteger(L, 2);
luaL_argcheck(L, iAge >= 6 && iAge <= 100, 2, "Wrong Parameter");
pStudent->iAge = iAge;
return 0;
}
static int GetSex(lua_State *L)
{
// 这里由你来补充
return 1;
}
static int SetSex(lua_State *L)
{
// 这里由你来补充
return 0;
}
static int GetNum(lua_State *L)
{
// 这里由你来补充
return 1;
}
static int SetNum(lua_State *L)
{
// 这里由你来补充
return 0;
}
static luaL_Reg arrayFunc_meta[] =
{
{ "getName", GetName },
{ "setName", SetName },
{ "getAge", GetAge },
{ "setAge", SetAge },
{ "getSex", GetSex },
{ "setSex", SetSex },
{ "getNum", GetNum },
{ "setNum", SetNum },
{ NULL, NULL }
};
static luaL_Reg arrayFunc[] =
{
{ "new", Student},
{ NULL, NULL }
};
extern "C" _declspec(dllexport) int luaopen_mytestlib(lua_State *L)
{
// 创建一个新的元表
luaL_newmetatable(L, "Student");
// 元表.__index = 元表
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, arrayFunc_meta, 0);
luaL_newlib(L, arrayFunc);
lua_pushvalue(L, -1);
lua_setglobal(L, "Sdudent"); /* the module name */
return 1;
}
lua文件
require "mytestlib"
local objStudent = Sdudent.new()
objStudent:setName("果冻")
local strName = objStudent:getName()
print(strName )
for k,v in pairs(getmetatable(objStudent)) do
print(tostring(k),tostring(v))
end
参考
http://blog.csdn.net/mydriverc2/article/details/51007803
http://blog.csdn.net/rain_qingtian/article/details/44781183
原文链接:https://blog.csdn.net/fwb330198372/article/details/82217022
userdata和lightuserdata
可以让C返回一个句柄给Lua,而Lua可以将句柄再通过在C中注册的方法传回C
userdata:
userdata通过Lua的API(lua_newuserdata())分配内存,就像C里的malloc()函数分配内存,
但不需要调用free()去释放内存,该内存是由LUA的GC机制进行回收。
lightuserdata:
lightuserdata通过LUA的API(lua_pushlightuserdata())创建,返回一个指针。
当你需要把这个一个C的对象传到LUA里,并且需要自己管理这个指针时,lightuserdata就很适用了。
但是,这个C对象是需要自己管理,LUA并不会帮忙回收。
例如Cocos2d-x拥有自己的GC机制,使用lightuserdata把数据对象的指针传到LUA里。
区别:
1.userdata是由LUA的GC机制进行回收
2.lightuserdata需要自己管理分配和回收。
Lua中的userdata连接(Lua5.3):https://blog.csdn.net/qq826364410/article/details/88672091
userdata 是一种用户自定义数据,用于表示一种由应用程序或 C/C++ 语言库所创建的类型,可以将任意 C/C++ 的任意数据类型的数据(通常是 struct 、指针和类)存储到 Lua 变量中调用。
lua的userdata解析 (Lua 5.1)
在我发表《Lua中的类型与值》这篇文章时,就有读者给我留言了,说:你应该好好总结一下Lua中的function和userdata类型。现在是时候总结了。对于function,我在《Lua中的函数》这篇文章中进行了总结,而这篇文章将会对Lua中的userdata进行仔细的总结。