详解关于Lua栈介绍以及实例

来源:互联网 发布:好吃小零食知乎 编辑:程序博客网 时间:2024/06/07 23:01

关于Lua栈介绍以及实例是本文要介绍的内容,主要是lua中如何使用,具体内容的实现来看本文详解。c++中载入lua的代码

  1. #include <> //所需要的头文件
  2. extern "C"
  3. {
  4. #include "include\lua.h"
  5. #include "include\lualib.h"
  6. #include "include\lauxlib.h"
  7. #pragma comment(lib, "lua.lib")
  8. };
  9. int main(void)
  10. {
  11. char buff[256]; //栈
  12. int error; //错误代码
  13. lua_State* L = lua_open(); //lua指针
  14. luaL_openlibs(L); //载入所有lua库
  15. //在此加入所需代码...
  16. lua_close(L); //关闭lua
  17. return 0;
  18. }

在Programming in lua中载入库的方法是分别载入5个库,分别是base、table、io、string、math,但是在使用中(lua5.1.3 + vs.net2003)发现io库在载入的时候会出现错误,程序无法继续执行,但不提示错误。

在网上查询了一下,有些人遇到的是非法引用内存(见这里),他的解决方法是改成上面代码中的方式:直接载入全部库。

在这里有一段解释:“关于luaopen_io调用失败的问题,在Lua的maillist里问了一下,有人说是因为io库里有些函数的运行是依赖于Lua建立的特定的环境,所以要用lua_call来调用,要么,就直接用luaL_openlibs来引入所有标准库。看了看帮助文档,还有Lua的源代码,似乎好像就是这么回事啊!”

再查官方文档(http://www.lua.org/manual/5.1/manual.html)中有一段:

  1. To have access to these libraries, the C host program should call the luaL_openlibs function,
  2. which opens all standard libraries. Alternatively, it can open them individually by calling luaopen_base (for the basic library),
  3. luaopen_package (for the package library), luaopen_string (for the string library),
  4. luaopen_table (for the table library), luaopen_math (for the mathematical library),
  5. luaopen_io (for the I/O library), luaopen_os (for the Operating System library),
  6. and luaopen_debug (for the debug library).
  7. These functions are declared in lualib.h and should not be called directly: you must call them like any other Lua C function, e.g.,
  8. by using lua_call. "

最后这句的意思是:“这些函数在lualib.h中定义并且不能直接调用:你必须以其他C函数调用方式来进行调用,例如使用lua_call。”

接着是lua_call的用法:

lua_call

原型:void lua_call (lua_State *L, int nargs, int nresults);

Calls a function.

功能:调用一个方法

调用一个函数必须按照以下的规则:首先,将要调用的函数入栈;之后,将函数参数按顺序入栈,就是说第一个参数最先入栈。最后调用lua_call;nargs是入栈的参数数。当函数被调用时弹出全部的参数和函数值。当函数返回后函数结果会压入栈。结果的数量取决于nresults(lua_call的最后一个参数)。除非nresults的值是LUA_MULTRET。以这种方式所有的函数结果都被入栈。由Lua管理栈空间中的这些返回值。函数的结果按顺序入栈(第一个元素最先入栈),所以在调用完成后最后一个参数在栈顶。

  1. Any error inside the called function is propagated upwards (with a longjmp).

在函数调用中产生的error会被向上传递(使用longjmp方式)。

  1. The following example shows how the host program may do the equivalent to this Lua code:

下面的例子展示了如何使宿主程序做如下lua代码的功能:

  1. a = f("how", t.x, 14)
  2. it is in C:

这是在C中:

  1. lua_getfield(L, LUA_GLOBALSINDEX, "f"); /* function to be called */
  2. lua_pushstring(L, "how"); /* 1st argument */
  3. lua_getfield(L, LUA_GLOBALSINDEX, "t"); /* table to be indexed */
  4. lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
  5. lua_remove(L, -2); /* remove 't' from the stack */
  6. lua_pushinteger(L, 14); /* 3rd argument */
  7. lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
  8. lua_setfield(L, LUA_GLOBALSINDEX, "a"); /* set global 'a' */

如果看不明白(其实我也不明白)那就直接按照最上面的代码中所写的方式载入lua库吧。

学习LUA也有一些时日了,个人认为对于LUA中的栈的理解很重要,嗯,写个小文章(真的很小)

如果你看了LUA的文档,那么就应该很清楚LUA与C交互数据时都是用到LUA中所谓的stack。那么当我调用lua_open函数之后栈是什么样的呢?空的(luaopen_base等会往栈上加进一些东西)。那么至于如何操作栈上的数据,我想官方文档上已经说得很清楚了,不过最初我对于栈的顺序有一些迷糊,所以就说说这个。现在假如我有如下的一段代码:

  1. lua_State* L = lua_open();
  2. lua_pushnumber( L, 211 );
  3. lua_pushnumber( L, 2222 );
  4. lua_newtable( L );
  5. lua_close( L );

那么当执行完lua_newtable之后栈上有三个元素,大致就是这样:

  1. 211
  2. 222
  3. table

现在211是第一个元素,index为1,不过LUA也可以用负数来表示,那么现在他是多少?

  1. index -index value
  2. 1 -3 211
  3. 2 -2 222
  4. 3 -1 table

嗯,很简单,再看看如果我们要设置一个TABLE的值怎么做?文档中说用lua_settable或是lua_rawset(这两者有什么区别应该和这里说的无关),它们参数意义、以及准备工作都一样,-1是值,-2是键值

  1. lua_settable( lua_state*, int )

第一个参数是要操作的脚本环境,第二个则是要操作的表在栈上的位置

一般的写法可能是这样

  1. // 代码A
  2. lua_getglobal( L, "myTable" ); // 获取要设置值的table
  3. lua_pushstring( L, "hp" ); // "hp"在栈上的位置为-1
  4. lua_pushnumber( L, 211 ); // "hp"在栈上的位置变为-2,而211则是-1
  5. lua_settable( L, -3 ); // 值被正确的设置到全局变量(表)的myTable中

如果我是想把hp这个值设置到全局表中呢?一般通过调用lua_setglobal宏

  1. lua_pushnumber( L, 211 );
  2. lua_setglobal( L, "hp" );

就这么简单,不过我们来看看lua_setglobal这个宏

  1. #define lua_setglobal(L,s) \
  2. (lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX))

这么看来实际上我们上面的代码被替换成了

  1. lua_pushnumber( L, 211 );
  2. lua_pushstring( L, "hp" );
  3. lua_insert( L, -2 ); // 这一步看上去或许比较诡异,实际上是把-1的值放到lua_insert的第二个参数所指的位置,然后这个位置后面的参数往上移
  4. //这里实际上最终结果就是-1和-2对调,但从逻辑上并不是对调
  5. lua_settable( L, LUA_GLOBALSINDEX ); // 这里为什么不用lua_rawset?我认为是有原因的^@^

将上面的代码与代码A结合起来看,在lua_settable时index值不同,而它做的工作是如果发现index是LUA_GLOBALSINDEX 那么就取出全局表(还有一个LUA_REGISTERINDEX,类似),否则从stack上取元素,当然,这个栈位置取出的不是一个table就会失败。所以代码A中指定的是-3是刚从全局表中取出的myTable表(这里假设他是一个table),上面的代码片段则是取出的全局表。所以lua_settable的index是什么值都可以,只要它指向的是一个table

实际上lua中与c的接口也就主要在栈的操作上,基本上你在写一个lua与C结合的程序时你最最需要做的工作就是明白你当前栈上有什么元素以及它们的位置。我一般会在纸上画出他们的位置,如果你熟了,对于几句在一起有关联的lua调用则可以很快的看出栈的变化。比如

  1. lua_gettable/lua_rawget
  2. lua_pushstring( L, "hp" );
  3. lua_gettable( L, LUA_GLOBALSINDEX );

只看第一句,栈顶是一个字符串,但两句放在一起,最终栈顶是一个全局表上一个名为hp的实际值

  1. lua_pushstring( L, "hp" );
  2. lua_pushnumber( L, 211 );
  3. lua_settable( L, LUA_GLOBALSINDEX );

无论第二句pushnumber还是pushvalue,pushstring什么的,最终这三句执行之后对于栈来说是没有任何变化的,因为lua_settable/lua_rawset会移走-1和-2

总之,对于栈的变化,在看一个函数的文档时先看它参数又需要栈上那些位置的元素并正确设置栈上的值,看清楚他会取栈上那些位置的元素作为这个lua api调用的使用并为之把正确的值放到栈上,最后注意函数完成之后会清除/移走那些位置的元素,我想应该就没什么问题了

  1. lua_gettable
  2. lua_getglobal(L, "mytable") <== push mytable
  3. lua_pushnumber(L, 1) <== push key 1
  4. lua_gettable(L, -2) <== pop key 1, push mytable[1]
  5. lua_settable
  6. lua_getglobal(L, "mytable") <== push mytable
  7. lua_pushnumber(L, 1) <== push key 1
  8. lua_pushstring(L, "abc") <== push value "abc"
  9. lua_settable(L, -3) <== mytable[1] = "abc", pop key & value

lua_rawget:

用法同lua_gettable,但更快(因为当key不存在时不用访问元方法__index)

lua_rawset:

用法同lua_settable,但更快(因为当key不存在时不用访问元方法__newindex)

lua_rawgeti必须为数值键

  1. lua_getglobal(L, "mytable") <== push mytable
  2. lua_rawgeti(L, -1, 1) <== push mytable[1],作用同下面两行调用
  3. --lua_pushnumber(L, 1) <== push key 1
  4. --lua_rawget(L,-2) <== pop key 1, push mytable[1]

lua_rawseti必须为数值键

  1. lua_getglobal(L, "mytable") <== push mytable
  2. lua_pushstring(L, "abc") <== push value "abc"
  3. lua_rawseti(L, -2, 1) <== mytable[1] = "abc", pop value "abc"

lua_getfield必须为字符串

  1. lua_getglobal(L, "mytable") <== push mytable
  2. lua_getfield(L, -1, "x") <== push mytable["x"],作用同下面两行调用
  3. --lua_pushstring(L, "x") <== push key "x"
  4. --lua_gettable(L,-2) <== pop key "x", push mytable["x"]

lua_setfield必须为字符串

  1. lua_getglobal(L, "mytable") <== push mytable
  2. lua_pushstring(L, "abc") <== push value "abc"
  3. lua_setfield(L, -2, "x") <== mytable["x"] = "abc", pop value "abc"

详解:详解关于Lua栈介绍以及实例的内容介绍完了,希望通过本文的学习能对你有所帮助