微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

lua与c++的相互调用

一.   lua调用C++

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

  1. #include<iostream>  
  2. using namespace std;  
  3. #include<stdio.h>  
  4. extern "C" {  
  5. #include <lua.h>  
  6. #include <lualib.h>  
  7. #include <lauxlib.h>  
  8. }  
  9. //#pragma comment(lib, "lua5.1.lib")  
  10. lua_State* L;  
  11. static int 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.       
  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:"<<lua_tointeger(L,-1)<<endl;  
  45.     lua_pop(L,1);  
  46.     lua_getglobal(L,"sum");  
  47.     cout<<"sum is:"<<lua_tointeger(L,-1)<<endl;  
  48. /* cleanup Lua */  
  49.     lua_close(L);  
  50. return 0;  
  51. //程序  
  52. //*lua_gettop()的作用是返回栈顶元素的序号. 由于Lua的栈是从1开始编号的,  
  53. // 所以栈顶元素的序号也相当于栈中的元素个数. 在这里, 栈中元素的个数就  
  54. // 是传入的参数个数.  
  55. //* for循环计算所有传入参数的总和. 这里用到了数值转换lua_tonumber().  
  56. //* 然后偶们用lua_pushnumber()把平均值和总和push到栈中.  
  57. //* 最后, 偶们返回2, 表示有两个返回值.  
  58. //* 虽然在C++中定义了average()函数, 但Lua程序并不知道, 所以需  
  59. //  要在main函数中加入  
  60. //     // register our function   
  61. //  lua_register(L, "average", average);  
  62. //  这两行的作用就是告诉e15.lua有average()这样一个函数.  
  63. //* 这个程序可以存成cpp也可以存成c, 如果以.c为扩展名就不需要加extern "C"  
  64. //       
  65. //编译的方法偶们上次说过了, 方法相同.  
  66. //e15.lua执行的方法只能用上例中的C++中执行, 而不能用命令行方式执行.*/  

脚本为

 

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

print("The average is ",avg)

print("The sum is ",sum)

二.  C++调用lua

[cpp]  view plain copy
    #include "stdafx.h"  
  1. #include <stdio.h>  
  2. extern "C" {  
  3. #include "lua.h"  
  4. #include "lualib.h"  
  5. #include "lauxlib.h"  
  6. /* Lua解释器指针 */  
  7. lua_State* L;  
  8. int main ( char *argv[] )  
  9. /* 初始化Lua */  
  10. /* 载入Lua基本库 */  
  11. /* 运行脚本 */  
  12. "Lua1.lua");  
  13. /* 清除Lua */  
  14. /* 暂停 */  
  15.     printf( "Press enter to exit…" );  
  16.     getchar();  
  17. }  

copy@H_502_629@

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

脚本为:

-- add two numbers

function add ( x,y )

return x + y + 2

end

z = 6

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐