lua与c++的相互调用、C/C++修改Lua中Table的内容

 

一、lua与c++的相互调用 

1.1 lua调用C++

在lua中是以函数指针的形式调用函数, 并且所有的函数指针都必须满足如下此种类型:
typedef int (*lua_CFunction) (lua_State *L);  
也就是说, 偶们在C++中定义函数时必须以lua_State为参数, 以int为返回值才能被Lua所调用. 但是不要忘记了, 偶们的lua_State是支持栈的, 所以通过栈可以传递无穷个参数, 大小只受内存大小限制. 而返回的int值也只是指返回值的个数真正的返回值都存储在
lua_State的栈中. 偶们通常的做法是做一个wrapper, 把所有需要调用的函数都wrap一下, 这样就可以调用任意的函数了.

[cpp] view plain copy print ?
  1. #include
  2. usingnamespace std;
  3. #include
  4. extern"C" {
  5. #include
  6. #include
  7. #include
  8. }
  9. //#pragma comment(lib, "lua5.1.lib")
  10. lua_State* L;
  11. staticint average(lua_State *L)
  12. {
  13. //返回栈中元素的个数
  14. int n = lua_gettop(L);
  15. double sum = 0;
  16. int i;
  17. for (i = 1; i <= n; i++)
  18. {
  19. if (!lua_isnumber(L, i))
  20. {
  21. lua_pushstring(L, "Incorrect argument to 'average'");
  22. lua_error(L);
  23. }
  24. sum += lua_tonumber(L, i);
  25. }
  26. /* push the average */
  27. lua_pushnumber(L, sum / n);
  28. /* push the sum */
  29. lua_pushnumber(L, sum);
  30. /* return the number of results */
  31. return 2;
  32. }
  33. int main (int argc,char*argv[])
  34. {
  35. /* initialize Lua */
  36. L = lua_open();
  37. /* load Lua libraries */
  38. luaL_openlibs(L);
  39. /* register our function */
  40. lua_register(L, "average", average);
  41. /* run the script */
  42. luaL_dofile(L, "e15.lua");
  43. lua_getglobal(L,"avg");
  44. cout<<"avg is:"<
  45. lua_pop(L,1);
  46. lua_getglobal(L,"sum");
  47. cout<<"sum is:"<
  48. /* cleanup Lua */
  49. lua_close(L);
  50. return 0;
  51. }
  52. //程序
  53. //*lua_gettop()的作用是返回栈顶元素的序号. 由于Lua的栈是从1开始编号的,
  54. // 所以栈顶元素的序号也相当于栈中的元素个数. 在这里, 栈中元素的个数就
  55. // 是传入的参数个数.
  56. //* for循环计算所有传入参数的总和. 这里用到了数值转换lua_tonumber().
  57. //* 然后偶们用lua_pushnumber()把平均值和总和push到栈中.
  58. //* 最后, 偶们返回2, 表示有两个返回值.
  59. //* 虽然在C++中定义了average()函数, 但Lua程序并不知道, 所以需
  60. // 要在main函数中加入
  61. // // register our function
  62. // lua_register(L, "average", average);
  63. //  这两行的作用就是告诉e15.lua有average()这样一个函数.
  64. //* 这个程序可以存成cpp也可以存成c, 如果以.c为扩展名就不需要加extern "C"
  65. //   
  66. //编译的方法偶们上次说过了, 方法相同.
  67. //e15.lua执行的方法只能用上例中的C++中执行, 而不能用命令行方式执行.*/
#include using namespace std; #include extern "C" { #include #include #include } //#pragma comment(lib, "lua5.1.lib") lua_State* L; static int average(lua_State *L) { //返回栈中元素的个数 int n = lua_gettop(L); double sum = 0; int i; for (i = 1; i <= n; i++) { if (!lua_isnumber(L, i)) { lua_pushstring(L, "Incorrect argument to 'average'"); lua_error(L); } sum += lua_tonumber(L, i); } /* push the average */ lua_pushnumber(L, sum / n); /* push the sum */ lua_pushnumber(L, sum); /* return the number of results */ return 2; } int main (int argc,char*argv[]) { /* initialize Lua */ L = lua_open(); /* load Lua libraries */ luaL_openlibs(L); /* register our function */ lua_register(L, "average", average); /* run the script */ luaL_dofile(L, "e15.lua"); lua_getglobal(L,"avg"); cout<<"avg is:"<

脚本为

avg, sum = average(10, 20, 30, 40, 50)

print("The average is ", avg)

print("The sum is ", sum)

1.2C++调用lua

[cpp] view plain copy print ?
  1. #include "stdafx.h"
  2. #include
  3. extern"C" {
  4. #include "lua.h"
  5. #include "lualib.h"
  6. #include "lauxlib.h"
  7. }
  8. /* Lua解释器指针 */
  9. lua_State* L;
  10. int main (int argc,char *argv[] )
  11. {
  12. /* 初始化Lua */
  13. L = lua_open();
  14. /* 载入Lua基本库 */
  15. luaL_openlibs(L);
  16. /* 运行脚本 */
  17. luaL_dofile(L, "Lua1.lua");
  18. /* 清除Lua */
  19. lua_close(L);
  20. /* 暂停 */
  21. printf( "Press enter to exit…" );
  22. getchar();
  23. return 0;
  24. }
#include "stdafx.h" #include extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } /* Lua解释器指针 */ lua_State* L; int main ( int argc, char *argv[] ) { /* 初始化Lua */ L = lua_open(); /* 载入Lua基本库 */ luaL_openlibs(L); /* 运行脚本 */ luaL_dofile(L, "Lua1.lua"); /* 清除Lua */ lua_close(L); /* 暂停 */ printf( "Press enter to exit…" ); getchar(); return 0; }

[cpp] view plain copy print ?
  1. /* A simple Lua interpreter. */
  2. #include
  3. extern"C" {
  4. #include
  5. #include
  6. #include
  7. }
  8. #include
  9. extern"C" {// 这是个C++程序, 所以要extern "C",
  10. // 因为lua的头文件都是C格式的
  11. #include "lua.h"
  12. #include "lualib.h"
  13. #include "lauxlib.h"
  14. }
  15. #pragma comment(lib, "lua5.1.lib")
  16. /* the Lua interpreter */
  17. lua_State* L;
  18. int luaadd (int x,int y )
  19. {
  20. int sum;
  21. /* the function name */
  22. lua_getglobal(L, "add");int nTop = lua_gettop(L);//得到栈的元素个数。栈顶的位置。
  23. /* the first argument */
  24. lua_pushnumber(L, x); nTop = lua_gettop(L);
  25. /* the second argument */
  26. lua_pushnumber(L, y); nTop = lua_gettop(L);
  27. /* call the function with 2
  28. arguments, return 1 result */
  29. lua_call(L, 2, 1); nTop = lua_gettop(L);
  30. /* get the result */
  31. sum = (int)lua_tonumber(L, -1); nTop = lua_gettop(L);
  32. /*清掉返回值*/
  33. lua_pop(L, 1); nTop = lua_gettop(L);
  34. /*取出脚本中的变量z的值*/
  35. lua_getglobal(L, "z"); nTop = lua_gettop(L);
  36. int z = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);
  37. lua_pop(L, 1); nTop = lua_gettop(L);
  38. //没调通
  39. /*lua_pushnumber(L, 4); nTop = lua_gettop(L);
  40. lua_setglobal(L, "r"); nTop = lua_gettop(L);
  41. int r = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);*/
  42. return sum;
  43. }
  44. int main (int argc,char *argv[] )
  45. {
  46. int sum;
  47. /* initialize Lua */
  48. L = lua_open();
  49. /* load Lua base libraries */
  50. //lua_baselibopen(L);
  51. /* load the script */
  52. luaL_dofile(L, "e12.lua");
  53. /* call the add function */
  54. sum = luaadd( 10, 15 );
  55. /* print the result */
  56. printf( "The sum is %d", sum );
  57. /* cleanup Lua */
  58. lua_close(L);
  59. return 0;
  60. }
  61. /*程序说明:
  62. main中过程偶们上次已经说过了, 所以这次只说说luaadd的过程
  63. * 首先用lua_getglobal()把add函数压栈
  64. * 然后用lua_pushnumber()依次把x,y压栈
  65. * 然后调用lua_call(), 并且告诉程序偶们有两个参数一个返回值
  66. * 接着偶们从栈顶取回返回值, 用lua_tonumber()
  67. * 最后偶们用lua_pop()把返回值清掉
  68. */
/* A simple Lua interpreter. */ #include extern "C" { #include #include #include } #include extern "C" { // 这是个C++程序, 所以要extern "C", // 因为lua的头文件都是C格式的 #include "lua.h" #include "lualib.h" #include "lauxlib.h" } #pragma comment(lib, "lua5.1.lib") /* the Lua interpreter */ lua_State* L; int luaadd ( int x, int y ) { int sum; /* the function name */ lua_getglobal(L, "add"); int nTop = lua_gettop(L); //得到栈的元素个数。栈顶的位置。 /* the first argument */ lua_pushnumber(L, x); nTop = lua_gettop(L); /* the second argument */ lua_pushnumber(L, y); nTop = lua_gettop(L); /* call the function with 2 arguments, return 1 result */ lua_call(L, 2, 1); nTop = lua_gettop(L); /* get the result */ sum = (int)lua_tonumber(L, -1); nTop = lua_gettop(L); /*清掉返回值*/ lua_pop(L, 1); nTop = lua_gettop(L); /*取出脚本中的变量z的值*/ lua_getglobal(L, "z"); nTop = lua_gettop(L); int z = (int)lua_tonumber(L, 1);nTop = lua_gettop(L); lua_pop(L, 1); nTop = lua_gettop(L); //没调通 /*lua_pushnumber(L, 4); nTop = lua_gettop(L); lua_setglobal(L, "r"); nTop = lua_gettop(L); int r = (int)lua_tonumber(L, 1);nTop = lua_gettop(L);*/ return sum; } int main ( int argc, char *argv[] ) { int sum; /* initialize Lua */ L = lua_open(); /* load Lua base libraries */ //lua_baselibopen(L); /* load the script */ luaL_dofile(L, "e12.lua"); /* call the add function */ sum = luaadd( 10, 15 ); /* print the result */ printf( "The sum is %d", sum ); /* cleanup Lua */ lua_close(L); return 0; } /*程序说明: main中过程偶们上次已经说过了, 所以这次只说说luaadd的过程 * 首先用lua_getglobal()把add函数压栈 * 然后用lua_pushnumber()依次把x,y压栈 * 然后调用lua_call(), 并且告诉程序偶们有两个参数一个返回值 * 接着偶们从栈顶取回返回值, 用lua_tonumber() * 最后偶们用lua_pop()把返回值清掉 */

脚本为:

-- add two numbers

function add ( x, y )

return x + y + 2

end

z = 6

 

二、C/C++修改Lua中的Table的内容

WriteLuaTable.lua

luat_Test1={a=123, b=456, c=789}
luat_Test2={123, 456, 789}

WriteLuaTable.cpp

#include

static void WriteTableFromKey(lua_State *L, const char* lpszTableName, const char* lpszTableItem, int nVal)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, nVal);
lua_setfield(L, -2, lpszTableItem);
lua_pop(L, 1);
}

static void WriteTableFromIndex(lua_State *L, const char* lpszTableName, int index, int nVal)
{
lua_getglobal(L, lpszTableName);
lua_pushnumber(L, nVal);
lua_rawseti(L, -2, index);
lua_pop(L, 1);
}

static void ReadTableFromKey(lua_State *L, const char* lpszTableName, const char* lpszTableItem)
{
lua_getglobal(L, lpszTableName);

lua_pushstring(L, lpszTableItem);
lua_gettable(L, -2);
printf("%s.%s=%d\n", lpszTableName, lpszTableItem, (int)lua_tonumber(L, -1));
lua_pop(L, 2);
}

static void ReadTableFromIndex(lua_State *L, const char* lpszTableName, int index)
{
lua_getglobal(L, lpszTableName);
lua_rawgeti(L, -1, index);
printf("%s[%d]=%d\n", lpszTableName, index, (int)lua_tonumber(L, -1));
lua_pop(L, 2);
}

int main (int argc, char* argv[])
{
lua_State *L = lua_open();
luaopen_base(L);

luaL_dofile(L, "WriteLuaTable.lua");

ReadTableFromKey(L, "luat_Test1", "a");
ReadTableFromKey(L, "luat_Test1", "b");
ReadTableFromKey(L, "luat_Test1", "c");
puts("\n");
WriteTableFromKey(L, "luat_Test1", "a", 147); // luat_Test1['a'] = 147
WriteTableFromKey(L, "luat_Test1", "b", 258); // luat_Test1['b'] = 258
WriteTableFromKey(L, "luat_Test1", "c", 369); // luat_Test1['c'] = 369
WriteTableFromKey(L, "luat_Test1", "d", 159); // luat_Test1['d'] = 159
ReadTableFromKey(L, "luat_Test1", "a");
ReadTableFromKey(L, "luat_Test1", "b");
ReadTableFromKey(L, "luat_Test1", "c");
ReadTableFromKey(L, "luat_Test1", "d");
puts("\n--------------------------");
ReadTableFromIndex(L, "luat_Test2", 1);
ReadTableFromIndex(L, "luat_Test2", 2);
ReadTableFromIndex(L, "luat_Test2", 3);
puts("\n");
WriteTableFromIndex(L, "luat_Test2", 1, 147); // luat_Test2[1] = 147
WriteTableFromIndex(L, "luat_Test2", 2, 258); // luat_Test2[2] = 258
WriteTableFromIndex(L, "luat_Test2", 3, 369); // luat_Test2[3] = 369
WriteTableFromIndex(L, "luat_Test2", 4, 159); // luat_Test2[4] = 159
ReadTableFromIndex(L, "luat_Test2", 1);
ReadTableFromIndex(L, "luat_Test2", 2);
ReadTableFromIndex(L, "luat_Test2", 3);
ReadTableFromIndex(L, "luat_Test2", 4);
lua_close(L);
return 0;
}

 

 


你可能感兴趣的:(Lua)