Lua的栈及基本栈操作

Lua的栈及基本栈操作

https://blog.csdn.net/mydriverc2/article/details/51134737

https://blog.csdn.net/mydriverc2/article/details/51134810

理解Lua栈

Lua通过一个“虚拟栈”与C/C++程序进行数据交互,所有的Lua C API都是通过操作这个栈来完成相应的数据通信。

Lua的这个“虚拟栈”解决了C/C++程序与Lua程序通信的两大问题:

  • Lua使用垃圾回收,而C/C++需要手动管理内存。

  • Lua使用动态类型,而C/C++使用的是静态类型。

因 为这个栈在Lua虚拟机内部,当一个Lua的变量放在栈里面的时候,虚拟机可以知道它有没有被宿主程序所使用,从而决定是否采用GC。另外Lua采用结构 体封装了类似“Lua_Value”的类型,让它可以存储任何C的类型。从而在数据交换的时候,任何类型都可以被放入栈的一个slot中。

由于栈是FILO的,所以,当我们在Lua里面操作这个栈的时候,每次操作的都是栈的顶部。而Lua的C API则有更多的控制权,它可以非常灵活地操纵这个栈的任意位置的元素。

基本Lua栈操作

  • 往栈里面压入一个值

1
2
3
4
5
6
7
void lua_pushnil      (lua_State *L);
void lua_pushboolean  (lua_State *L, int bool);
void lua_pushnumber   (lua_State *L, lua_Number n);
void lua_pushinteger  (lua_State *L, lua_Integer n);
void lua_pushunsigned (lua_State *L, lua_Unsigned n);
void lua_pushlstring  (lua_State *L, const char *s, size_t len);
void lua_pushstring   (lua_State *L, const char *s);
  • 查询栈里面的元素

1
int lua_is* (lua_State * L, int index);
  • 获取栈内给定位置的元素值

1
xxx lua_toXXX(lua_State * L, int index);

这里面的xxx可以是nil, boolean, string,integer等等。

  • 其它栈操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//取得栈中元素个数
 int  lua_gettop    (lua_State *L);
//设置栈的大小为一个指定的值,而lua_settop(L,0)会把当前栈清空
//如果指定的index大于之前栈的大小,那么空余的空间会被nil填充
//如果index小于之前的栈中元素个数,则多余的元素会被丢弃
 void lua_settop    (lua_State *L, int index);
//把栈中index所在位置的元素压入栈
 void lua_pushvalue (lua_State *L, int index);
//移除栈中index所在位置的元素
void lua_remove(lua_State *L, int index);
//在栈的顶部的元素移动至index处
void lua_insert(lua_State *L, int index);
//从栈顶弹出一个值,并把它设置到给定的index处
void lua_replace(lua_State *L, int index);
//把fromidx处的元素copy一份插入到toidx,这操作不会修改fromidx处的元素
void lua_copy(lua_State *L, int fromidx, int toidx);

另外,根据《Programming In Lua》一书中的所讲,我们可以定义一个函数stackDump来打印当前栈的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static void stackDump(lua_State* L){
    cout<<" begin dump lua stack"<<endl;
    int i = 0;
    int top = lua_gettop(L);
    for (i = 1; i <= top; ++i) {
        int t = lua_type(L, i);
        switch (t) {
            case LUA_TSTRING:
            {
                printf("'%s' ", lua_tostring(L, i));
            }
                break;
            case LUA_TBOOLEAN:
            {
                printf(lua_toboolean(L, i) ? "true " "false ");
            }break;
            case LUA_TNUMBER:
            {
                printf("%g ", lua_tonumber(L, i));
            }
                break;
            default:
            {
                printf("%s ", lua_typename(L, t));
            }
                break;
        }
    }
    cout<<" end dump lua stack"<<endl;
}

C/C++访问Lua的Table

假设我们的Lua文件中有一个Table为:

1
me = { name = "zilongshanren", age = 27}

我们可以通过以下C代码来访问它的元素:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//从Lua里面取得me这个table,并压入栈
lua_getglobal(L, "me");
if (!lua_istable(L, -1)) {
    CCLOG("error! me is not a table");
}
//往栈里面压入一个key:name
lua_pushstring(L, "name");
//取得-2位置的table,然后把栈顶元素弹出,取出table[name]的值并压入栈
lua_gettable(L, -2);   
//输出栈顶的name
CCLOG("name = %s", lua_tostring(L, -1));
stackDump(L);
//把栈顶元素弹出去
lua_pop(L, 1);
//压入另一个key:age
lua_pushstring(L, "age");
//取出-2位置的table,把table[age]的值压入栈
lua_gettable(L, -2);
stackDump(L);
CCLOG("age = %td", lua_tointeger(L, -1));

Lua5.1还引入了一个新方法:

1
lua_getfield(L, -1, "age");

它可以取代:

1
2
3
4
 //压入另一个key:age
 lua_pushstring(L, "age");
 //取出-2位置的table,把table[age]的值压入栈
 lua_gettable(L, -2);

下篇文章,我们将介绍Lua如何调用C/C++里面的函数。

本篇文章主要介绍C++和Lua相互传递数据。如果你还不知道怎么在C/C++里面调用Lua脚本的话,请参考这篇文章。本文主要介绍基本数据类型的传递,比如整型(int),字符串(string)、数字(number)及bool值。

加载并运行Lua脚本

由于在上一个教程里面已经介绍过如何在C/C++里面嵌入Lua,所以这一节就简单的介绍一下程序怎么用,配置就略过啦。

创建Lua虚拟机

1
lua_State *lua_state = luaL_newstate();

加载Lua库

1
2
3
4
5
6
7
8
9
10
11
12
static const luaL_Reg lualibs[] =
    {
        {"base", luaopen_base},
        {"io", luaopen_io},
        {NULL, NULL}
    };
    const luaL_Reg *lib = lualibs;
    for(; lib->func != NULL; lib++)
    {
        luaL_requiref(lua_state, lib->name, lib->func, 1);
        lua_settop(lua_state, 0);
    }

运行Lua脚本

1
2
3
4
5
6
7
8
9
10
11
12
std::string scriptPath = FileUtils::getInstance()->fullPathForFilename("hello.lua");
    int status = luaL_loadfile(lua_state, scriptPath.c_str());
    std::cout << " return: " << status << std::endl;
    int result = 0;
    if(status == LUA_OK)
    {
        result = lua_pcall(lua_state, 0, LUA_MULTRET, 0);
    }
    else
    {
        std::cout << " Could not load the script." << std::endl;
    }

这里我们使用的是luaL_loadfile而不是之前的luaL_dofile,其实luaL_dofile只是一个宏定义:

1
2
#define luaL_dofile(L, fn) 
    (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

我们先调用luaL_loadfile可以判断Lua脚本是否加载成功,然后再调用lua_pcall来执行Lua脚本。

C/C++调用Lua函数

首先,我们在hello.lua里面定义一个Lua函数:

1
2
3
4
-- add two numbers
function add ( x, y )
    return x + y
end

Lua的函数定义是以function为keyword,然后以end结尾,同时它的参数是没有形参类型的,另外,Lua的函数可以返回多个值。不过我们这里只返回了一个值。

接下来,让我们看看如果在C++程序里面调用这个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int luaAdd(lua_State *lua_state , int x, int y)
{
    int sum;
    //获取lua里面的add函数并把它放到lua的栈顶
    lua_getglobal(lua_state, "add");
    //往lua栈里面压入两个参数
    lua_pushnumber(lua_state, x);
    lua_pushnumber(lua_state, y);
    //调用lua函数,这里的2是参数的个数,1是返回值的个数
    lua_call(lua_state, 2, 1);
    //从栈顶读取返回值,注意这里的参数是-1
    sum = lua_tointeger(lua_state, -1);
    //最后我们把返回值从栈顶拿掉
    lua_pop(lua_state, 1);
    return sum;
}

然后,我们就可以在程序里面调用它了:

1
std::cout<< "2 + 1= " << luaAdd(lua_state,4,1)<<std::endl;

注意,这个方法调用要在lua_pcall调用之后。

操作Lua全局变量

C++里面获取Lua全局变量的值

首先,我们在hello.lua里面定义一个全局变量

1
myname = "子龙山人"

然后我们在C++里面访问它:

1
2
3
4
lua_getglobal(lua_state, "myname");
    std::string myname = lua_tostring(lua_state, -1);
    lua_pop(lua_state, 1);
    std::cout<<"Hello: "<<myname<<std::endl;

这一次我们又是通过lua_getglobal来把myname这个全局变量压到lua栈,然后用lua_tostring来取这个值。

C++里面修改Lua全局变量的值

这次我们使用的是lua_setglobal来传递数据给Lua:

1
2
 lua_pushstring(lua_state, "World");
 lua_setglobal(lua_state, "myname");

这时,我们只要在hello.lua的最开始部分,调用print(myname)就可以打印传递进来的值了。

C++传递Table给Lua

1
2
3
4
5
6
7
8
9
10
   lua_createtable(lua_state, 2, 0);
    lua_pushnumber(lua_state, 1);
    lua_pushnumber(lua_state, 49);
//    lua_settable(lua_state, -3);
    lua_rawset(lua_state, -3);
    lua_pushnumber(lua_state, 2);
    lua_pushstring(lua_state, "Life is a beach");
//    lua_settable(lua_state, -3);
    lua_rawset(lua_state, -3);
    lua_setglobal(lua_state, "arg");

这里我们传递了一个table给lua,这个table为{49,"Life is a beach"}。Lua table的索引是从1开始的,然后我们在lua脚本里面可以这样子来访问这个table:

1
2
3
for i=1,#arg do
    print("      ", i, arg[i])
end

这里的#arg是获得table的长度,然后使用arg[i]来获取table的索引i处的value。

Lua返回多个值给C++

首先是Lua代码:

1
2
3
4
local temp = {9, "hehehej"}
-- temp[1]=9
-- temp[2]="See you space cowboy!"
return temp,9,1

然后是C++代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 std::stringstream str_buf;
    while(lua_gettop(lua_state))
    {
        str_buf.str(std::string());
        str_buf << " ";
        switch(lua_type(lua_state, lua_gettop(lua_state)))
        {
            case LUA_TNUMBER:
                str_buf << "script returned the number: "
                << lua_tonumber(lua_state, lua_gettop(lua_state));
                break;
            case LUA_TTABLE:
                str_buf << "script returned a table";
                break;
            case LUA_TSTRING:
                str_buf << "script returned the string: "
                << lua_tostring(lua_state, lua_gettop(lua_state));
                break;
            case LUA_TBOOLEAN:
                str_buf << "script returned the boolean: "
                << lua_toboolean(lua_state, lua_gettop(lua_state));
                break;
            default:
                str_buf << "script returned an unknown-type value";
        }
        lua_pop(lua_state, 1);
        std::cout << str_buf.str() << std::endl;
    }

最后输出结果为:

1
2
3
4
5
[C++] Values returned from the script:
 script returned the number: 1
 script returned the number: 9
 script returned a table
[C++] Closing the Lua state

在Lua里面return值的顺序是table,9,1,而在C++里面是倒过来的。因为我们是使用栈作为数据结构来传递数据,而栈是先进后出的。

=========== End

原文地址:https://www.cnblogs.com/lsgxeva/p/11158228.html