main.cpp
#include <stdio.h>
#include "lua.hpp"
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student():m_age(0), m_name(""){}
~Student(){}
int m_age;
string m_name;
void setAge(int age){m_age = age;}
int getAge(){return m_age;}
void setName(const char* name){m_name = name;}
const char* getName(){return m_name.c_str();}
};
int Lua_CreateStudent(lua_State *L)
{
Student **s = (Student**)lua_newuserdata(L, sizeof(Student*));
*s = new Student;
// 元表
luaL_getmetatable(L, "Meta_Student");
lua_setmetatable(L, -2);
return 1;
}
int Lua_SetAge(lua_State *L)
{
Student **s = (Student**)luaL_checkudata(L, -2, "Meta_Student");
luaL_argcheck(L, s != NULL, -2, "invalid user data");
luaL_checktype(L, -1, LUA_TNUMBER);
int age = lua_tonumber(L, -1);
(*s)->setAge(age);
return 0;
}
int Lua_GetAge(lua_State *L)
{
Student **s = (Student**)luaL_checkudata(L, -1, "Meta_Student");
luaL_argcheck(L, s != NULL, -1, "invalid user data");
lua_settop(L, 0);
lua_pushnumber(L, (*s)->getAge());
return 1;
}
int Lua_SetName(lua_State *L)
{
Student **s = (Student**)luaL_checkudata(L, -2, "Meta_Student");
luaL_argcheck(L, s != NULL, -2, "invalid user data");
luaL_checktype(L, -1, LUA_TSTRING);
const char* name = lua_tostring(L, -1);
(*s)->setName(name);
return 0;
}
int Lua_GetName(lua_State *L)
{
Student **s = (Student**)luaL_checkudata(L, -1, "Meta_Student");
luaL_argcheck(L, s != NULL, -1, "invalid user data");
lua_settop(L, 0);
lua_pushstring(L, (*s)->getName());
return 1;
}
int Lua_Destory(lua_State *L)
{
Student **s = (Student**)luaL_checkudata(L, -1, "Meta_Student");
luaL_argcheck(L, s != NULL, -1, "invalid user data");
delete (*s);
lua_settop(L, 0);
return 0;
}
int LuaOpenLibStudent(lua_State *pLua)
{
static const struct luaL_Reg studentlib_meta[] = {
{"setAge", Lua_SetAge},
{"getAge", Lua_GetAge},
{"setName",Lua_SetName},
{"getName",Lua_GetName},
{NULL, NULL}
};
// 元表
luaL_newmetatable(pLua, "Meta_Student");
lua_pushvalue(pLua, -1);
lua_setfield(pLua, -2, "__index");
lua_pushcfunction(pLua, Lua_Destory);
lua_setfield(pLua, -2, "__gc");
// // getAge方法
// lua_pushcfunction(pLua, Lua_GetAge);
// lua_setfield(pLua, -2, "getAge");
// // setAge方法
// lua_pushcfunction(pLua, Lua_SetAge);
// lua_setfield(pLua, -2, "setAge");
// // getName方法
// lua_pushcfunction(pLua, Lua_GetName);
// lua_setfield(pLua, -2, "getName");
// // setName方法
// lua_pushcfunction(pLua, Lua_SetName);
// lua_setfield(pLua, -2, "setName");
/*
luaL_register,参数二,NULL值表示以栈顶table作为存储函数的table
等于一组或者多组
lua_pushcfunction
lua_setfield
*/
luaL_register(pLua, NULL, studentlib_meta);
static const struct luaL_Reg studentlib[] = {
{"create", Lua_CreateStudent},
{NULL, NULL}
};
luaL_register(pLua, "Stu", studentlib);
// 清除栈
//int n = lua_gettop(pLua);
//lua_pop(pLua, n);
// 清除栈
lua_settop(pLua, 0);
return 0;
}
int main()
{
lua_State* pLua = luaL_newstate();
luaL_openlibs(pLua);
LuaOpenLibStudent(pLua);
char szTmp[512];
snprintf(szTmp, sizeof(szTmp), "package.path=package.path..';./?.lua;./?.luac'\0");
if (luaL_dostring(pLua, szTmp) != 0){}
if (luaL_dostring(pLua, "require 'init'") != 0){}
lua_settop(pLua, 0);
lua_getglobal(pLua, "test");
lua_pushnumber(pLua, 20);
lua_pushstring(pLua, "Hello World~!");
int nRet = lua_pcall(pLua, 2, 0, 0);
lua_close(pLua);
return 0;
}
init.lua
require "Stu"
function test(intValue, strValue)
print(intValue, strValue)
local s = Stu.create()
s:setName("Hello")
print(s:getName())
s:setAge(200)
print(s:getAge())
print ("Hello World!")
end