栈的操作:https://www.cnblogs.com/lsgxeva/p/11158228.html
lua的api参考:https://www.lua.org/manual/5.3/manual.html
print(package.path)
package.path = package.path .. ";E:\\OGL5\\c++calllua\\luascript\\?.lua;"
print("hello")
print(package.path)
require 'person'
c = add(1,4)
print(c)
myName = "beauty girl"
helloTable = {name="xiaoming", age=12}
C++访问lua的table
int main (int argc, char **argv) {
/* 初始化Lua */
lua_State* L = luaL_newstate();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 运行脚本 */
luaL_dofile(L, "hello.lua"); //加载脚本
lua_getglobal(L, "helloTable"); //获取全局变量的background的值,并将其放入栈顶
lua_pushstring(L, "name"); //name变量入栈,成栈顶
lua_gettable(L, -2); //取出helloTable中name的值,替换掉栈顶name
const char* sName = lua_tostring(L, -1); //取出栈顶的值
lua_pop(L, 1); //pop掉栈顶的值
lua_pushstring(L, "age"); //age变量入栈,成栈顶
lua_gettable(L, -2); //取出age的值,替换栈顶的age变量
int age = lua_tointeger(L, -1); //取出栈顶的值
/* 清除Lua */
lua_close(L);
system("pause");
}
lua_getfield函数——得到表里的某个key的值,此值放在了栈顶上。
int lua_getfield (lua_State *L, int index, const char *k);
Pushes onto the stack the value t[k], where t is the value at the given index. As in Lua, this function may trigger a metamethod for the “index” event (see §2.4).
Returns the type of the pushed value.
居然会触发index元方法。
举例:
lua_State* L = luaL_newstate();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 运行脚本 */
luaL_dofile(L, "hello.lua");
lua_getglobal(L, "helloTable");
lua_getfield(L, -1, "age"); //helloTable此时在栈顶,所以-1,取出age的值,放入栈顶
int nParam_a = lua_tointeger(L, -1); //取出栈顶元素值
lua_getfield(L, -2, "name"); //helloTable此时在-2位置,因为age的值压入栈了,然后name被取出,入栈,此时栈里有3个元素
char* nParam_b = lua_tostring(L, -1); //取出栈顶元素值
int count= lua_gettop(L); //栈里有3个元素
lua_setfield函数——对table的某个字段赋值
void lua_setfield (lua_State *L, int index, const char *k);
Does the equivalent to t[k] = v, where t is the value at the given index and v is the value at the top of the stack.
This function pops the value from the stack. As in Lua, this function may trigger a metamethod for the “newindex” event.
举例:
lua_State* L = luaL_newstate();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 运行脚本 */
luaL_dofile(L, "hello.lua"); //加载lua文件
lua_getglobal(L, "helloTable"); //表入栈,在栈顶
lua_pushstring(L, "nihao"); //nihao值入栈,为栈顶
lua_setfield(L, -2, "name"); //-2是表在栈中的位置,lua_setfield将栈顶nihao的值赋给helloTable的name属性,并且将nihao栈顶pop掉,此时栈顶为表helloTable
lua_getfield(L, -1, "name"); //-1位helloTable所在的位置,name为属性
char* name = lua_tostring(L, -1); //取出栈顶的元素
int count= lua_gettop(L);
C++调用lua的函数:
lua_State* L = luaL_newstate();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 运行脚本 */
luaL_dofile(L, "hello.lua"); //加载lua文件
lua_getglobal(L, "add");
if (!lua_isfunction(L, -1))
{
int a = 1;
}
lua_pushinteger(L, 1); //参数1
lua_pushinteger(L, 2); //参数2
lua_pcall(L, 2, 1, 0); // 2表示有两个参数;1表示有1个返回值;0位错误的函数回调,暂未使用;调用之后,返回值在栈顶
int result = lua_tointeger(L, -1);
lua_rawlen函数——计算字符串、table的长度
size_t lua_rawlen (lua_State *L, int index);
Returns the raw “length” of the value at the given index: for strings, this is the string length; for tables, this is the result of the length operator (‘#’) with no metamethods; for userdata, this is the size of the block of memory allocated for the userdata; for other values, it is 0.
lua_State* L = luaL_newstate();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 运行脚本 */
luaL_dofile(L, "hello.lua"); //加载lua文件
lua_getglobal(L, "helloTable2");
int count = lua_rawlen(L, -1); //这里返回的helloTable2的长度为3,而如果是helloTable,则返回的是长度为0
lua_getglobal函数——获取指定名称的变量,并将其值放在栈顶
int lua_getglobal (lua_State *L, const char *name);
Pushes onto the stack the value of the global name. Returns the type of that value.
把name的值放在栈顶,函数返回的类型为,name值的类型。
lua_gettop函数——获取栈里的元素的个数
int lua_gettop (lua_State *L);
Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the number of elements in the stack; in particular, 0 means an empty stack.
返回栈顶元素的索引,0表示栈为空;索引是从1开始,同时此数字表示栈里的元素的个数;
举例:
int main (int argc, char **argv) {
int status, result;
lua_State *L = luaL_newstate(); /* create state */
luaL_openlibs(L);
luaL_dofile(L, "hello.lua"); //执行lua脚本
int count = lua_gettop(L); //此时count=0,栈里无元素
int type1 = lua_getglobal(L, "flag"); //将flag放在栈顶
count = lua_gettop(L); //此时count=1,有1个元素
int type2 = lua_getglobal(L, "helloTable"); //将helloTable放在栈顶
count = lua_gettop(L); //此时count=2,有2个元素
}
lua_pushstring函数——把一个字符串放在栈顶
const char *lua_pushstring (lua_State *L, const char *s);
Pushes the zero-terminated string pointed to by s onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns.
Returns a pointer to the internal copy of the string.
If s is NULL, pushes nil and returns NULL.
把字符串s,放入栈中,返回的是const char*指针。
举例:
int main (int argc, char **argv) {
int status, result;
lua_State *L = luaL_newstate(); /* create state */
luaL_openlibs(L);
luaL_dofile(L, "hello.lua"); //执行lua脚本
int count = lua_gettop(L); //此时count=0,栈里无元素
int type1 = lua_getglobal(L, "flag"); //将flag放在栈顶
count = lua_gettop(L); //此时count=1,有1个元素
int type2 = lua_getglobal(L, "helloTable"); //将helloTable放在栈顶
count = lua_gettop(L); //此时count=2,有2个元素
const char* str = lua_pushstring(L, "name"); //name变量入栈,成栈顶
count = lua_gettop(L); //此时count=3,有3个元素
lua_rawgeti函数——将table中指定index的值,放在栈顶
int lua_rawgeti (lua_State *L, int index, lua_Integer n);
Pushes onto the stack the value t[n], where t is the table at the given index. The access is raw, that is, it does not invoke the __index metamethod.
Returns the type of the pushed value.
int main (int argc, char **argv) {
int status, result;
lua_State *L = luaL_newstate(); /* create state */
luaL_openlibs(L);
luaL_dofile(L, "hello.lua"); //执行lua脚本
int type2 = lua_getglobal(L, "helloTable"); //将helloTable放在栈顶
int result1 = lua_rawgeti(L, -1, 1); //helloTable在栈顶,第二个参数为-1,xxx是在table的第1个元素,所以是1
const char* str3 = lua_tostring(L, -1); //读取栈顶的字符串,并不pop操作
}
lua栈的索引:
lua的栈索引是可以是负数,栈顶是 -1,向着栈底,依次递减,-2,-3,……
lua的栈索引也可以是正数,栈顶为 n,向着栈底,依次递减,n-1,n-2,……,栈底为1
测试:
int main (int argc, char **argv) {
int status, result;
lua_State *L = luaL_newstate(); /* create state */
luaL_openlibs(L);
luaL_dofile(L, "hello.lua"); //执行lua脚本
int type2 = lua_getglobal(L, "helloTable"); //将helloTable放在栈顶,此时栈里有1个元素
int result1 = lua_rawgeti(L, -1, 1); //helloTable在栈顶,所以第2个参数为-1,第3个参数为1,表示取出第1个元素,放在栈顶
const char* str3 = lua_tostring(L, -1); //取出栈顶的元素,不做pop操作
lua_pushinteger(L, 123); //压入一个123数字,此时栈里有3个元素
int a = lua_tonumber(L, 3); //栈里有3个元素了,所以123,所在的索引是3
int a2 = lua_tonumber(L, -1); //栈里有3个元素,但是可以用-1,索引到栈顶元素