lua与C++相互调用

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

lua和c/c++的数据交互通过"栈"进行 ,操作数据时,首先将数据拷贝到"栈"上,然后获取数据,栈中的每个数据通过索引值进行定位,索引值为正时表示相对于栈底的偏移索引,索引值为负时表示相对于栈顶的偏移索引,索引值以1或-1为起始值,因此栈顶索引值永远为-1 ,栈底索引值永远为1 。 "栈"相当于数据在lua和c/c++之间的中转地。每种数据都有相应的存取接口 。
数据入"栈"接口:
void  (lua_pushnil) (lua_State *L);
void  (lua_pushnumber) (lua_State *L, lua_Number n);
void  (lua_pushinteger) (lua_State *L, lua_Integer n);
void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);
void  (lua_pushstring) (lua_State *L, const char *s);
void  (lua_pushboolean) (lua_State *L, int b);
void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);

数据获取接口:
lua_Number      (lua_tonumber) (lua_State *L, int idx);
lua_Integer     (lua_tointeger) (lua_State *L, int idx);
int             (lua_toboolean) (lua_State *L, int idx);
const char *    (lua_tolstring) (lua_State *L, int idx, size_t *len);
lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);


"栈"操作接口:
int   (lua_gettop) (lua_State *L);
void  (lua_settop) (lua_State *L, int idx);
void  (lua_pushvalue) (lua_State *L, int idx);
void  (lua_remove) (lua_State *L, int idx);
void  (lua_insert) (lua_State *L, int idx);
void  (lua_replace) (lua_State *L, int idx);
int   (lua_checkstack) (lua_State *L, int sz);

lua中定义的变量和函数存放在一个全局table中,索引值为LUA_GLOBALSINDEX ,table相关操作接口:
void  (lua_gettable) (lua_State *L, int idx);
void  (lua_getfield) (lua_State *L, int idx, const char *k);
void  (lua_settable) (lua_State *L, int idx);
void  (lua_setfield) (lua_State *L, int idx, const char *k);

当"栈"中包含执行脚本需要的所有要素(函数名和参数)后,调用lua_pcall执行脚本:
int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);

以下是一些参考用例:
[PS]*************************************************************************
#include "stdafx.h"
#include <stdio.h>

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;
}
[PS]*************************************************************************
#include<iostream>
using namespace std;
#include<stdio.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
//#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:"<<lua_tointeger(L,-1)<<endl;
 lua_pop(L,1);
 lua_getglobal(L,"sum");
 cout<<"sum is:"<<lua_tointeger(L,-1)<<endl;
 /* cleanup Lua */
 lua_close(L);

  return 0;
}

//程序中:
//*lua_gettop()的作用是返回栈顶元素的序号. 由于Lua的栈是从1开始编号的, 所以栈顶元素的序号也相当于栈中的元素个数. 在这里, 栈中元素的个数就
是传入的参数个数。
//* for循环计算所有传入参数的总和. 这里用到了数值转换lua_tonumber()。
//* 然后偶们用lua_pushnumber()把平均值和总和push到栈中。
//* 最后, 偶们返回2, 表示有两个返回值(这里不能错,push了几个就是返回几,否则外部按照你返回的值取值会出错)。
//* 虽然在C++中定义了average()函数, 但Lua程序并不知道, 所以需要在main函数中加入
//  lua_register(L, "average", average);
// 这行的作用就是注册函数后告诉e15.lua有average()这样一个函数.
//* 这个程序可以存成cpp也可以存成c, 如果以.c为扩展名就不需要加extern "C"
//编译的方法偶们上次说过了, 方法相同.
//e15.lua执行的方法只能用上例中的C++中执行, 而不能用命令行方式执行.*/

lua脚本为:
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)
[PS]*************************************************************************
/* A simple Lua interpreter. */
#include <stdio.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <stdio.h>
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()把返回值清掉


lua脚本为:
-- add two numbers
function add ( x, y )
return x + y + 2
end
 
z = 6
[PS]*************************************************************************
(func.lua)

--变量定义
width=1 ;
height=2 ;
--lua函数定义,实现加法
function sum(a,b)
    return a+b ;
end
--lua函数定义,实现字符串相加
function mystrcat(a,b)
    return a..b ;
end
--lua函数定义,通过调用c代码中的csum函数实现加法
function mysum(a,b)
    return csum(a,b) ;
end

(test_lua.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//lua头文件
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#define err_exit(num,fmt,args)  \
    do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)
#define err_return(num,fmt,args)  \
    do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)

//lua中调用的c函数定义,实现加法
int csum(lua_State* l)
{
    int a = lua_tointeger(l,1) ;
    int b = lua_tointeger(l,2) ;
    lua_pushinteger(l,a+b) ;
    return 1 ;
}

int main(int argc,char** argv)
{
    lua_State * l = luaL_newstate() ;        //创建lua运行环境
    if ( l == NULL ) err_return(-1,"luaL_newstat() failed");
    int ret = 0 ;
    ret = luaL_loadfile(l,"func.lua") ;      //加载lua脚本文件
    if ( ret != 0 ) err_return(-1,"luaL_loadfile failed");
    ret = lua_pcall(l,0,0,0) ;
    if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;

    lua_getglobal(l,"width");              //获取lua中定义的变量
    lua_getglobal(l,"height");
    printf("height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
    lua_pop(l,1) ;                        //恢复lua的栈

    int a = 11 ;
    int b = 12 ;
    lua_getglobal(l,"sum");               //调用lua中的函数sum
    lua_pushinteger(l,a) ;
    lua_pushinteger(l,b) ;
    ret = lua_pcall(l,2,1,0) ;
    if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
    lua_pop(l,1) ;

    const char str1[] = "hello" ;
    const char str2[] = "world" ;
    lua_getglobal(l,"mystrcat");          //调用lua中的函数mystrcat
    lua_pushstring(l,str1) ;
    lua_pushstring(l,str2) ;
    ret = lua_pcall(l,2,1,0) ;
    if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf("mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
    lua_pop(l,1) ;

    lua_pushcfunction(l,csum) ;         //注册在lua中使用的c函数
    lua_setglobal(l,"csum") ;           //绑定到lua中的名字csum

    lua_getglobal(l,"mysum");           //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法
    lua_pushinteger(l,a) ;
    lua_pushinteger(l,b) ;
    ret = lua_pcall(l,2,1,0) ;
    if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf("mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
    lua_pop(l,1) ;

    lua_close(l) ;                     //释放lua运行环境
    return 0 ;
}
[PS]*************************************************************************
下面的代码演示了在C++和lua脚本之间传递数据。
首先在C++中创建一个table,添加元素,然后放置到lua的全局表中。在lua脚本中可以使用C++创建的这个表。
然后在脚本中创建一个表,以脚本返回值的方式返回给C++,在C++中可以读取表中的值。

例子代码需要一个args.lua的lua文件,要手工创建,我把它放到了C盘根目录下。

// cpplua.cpp : Defines the entry point for the console application.  
 
#include "stdafx.h"  
 
extern "C" 
{  
#include <lua.h>  
#include <lauxlib.h>  
#include <lualib.h>  
}  
#include <iostream>  
 
/* args.lua文件的内容
io.write( "[lua] These args were passed into the script from C\n" );

for i=1,table.getn(arg) do
print(i,arg[i])
end

io.write("[lua] Script returning data back to C\n")

local temp = {}
temp[1]=9
temp[2]=8
temp[3]=7
temp[4]=6
temp[5]=5
temp["test1 key"]="test1 value"
temp[6]="test 6"
temp["test 99"]=99

for i,n in pairs(temp)
do
print (i,n)
end

return temp,9,1

*/ 
int _tmain(int argc, _TCHAR* argv[])  
{  
    int status;  
 
    // lua_open: 创建一个新的lua环境  
     lua_State* state = lua_open();  
 
    // 在state环境上打开标准库,  
    // 标准库包括:  
    // luaopen_base  
    // luaopen_package  
    // luaopen_table  
    // luaopen_io  
    // luaopen_os  
    // luaopen_string  
    // luaopen_math  
    // luaopen_debug  
     luaL_openlibs(state);  /* open libraries */ 
 
     status = luaL_loadfile( state, "c:\\args.lua" );  
 
     std::cout << "[C++] Passing 'arg' array to script" << std::endl;  
 
    // 创建一个新的表  
     lua_newtable( state );  
 
    //  
    // set first element "1" to value 45  
    //  
    // 调用lua的函数,都是通过压栈出栈来完成的  
    // 为表执行一个t[k]=v的操作,则需要先将k压栈,再将v压栈,再调用操作函数  
    // 这个操作函数会使用栈上的元素,并“可能”将弹出元素和压入元素  
    // lua_rawset直接赋值(不触发metamethods方法)。  
      
    // lua_rawset/lua_settable使用:  
    // 它从栈中获取参数。以table在栈中的索引作为参数,  
    // 并将栈中的key和value出栈。  
    // lua_pushnumber函数调用之前,  
    // table是在栈顶位置(索引为-1)。index和value入栈之后,  
    // table索引变为-3。  
     lua_pushnumber( state, 1 );  
     lua_pushnumber( state, 45 );  
     lua_rawset( state, -3 );  
 
    // set second element "2" to value 99  
     lua_pushnumber( state, 2 );  
     lua_pushnumber( state, 99 );  
     lua_rawset( state, -3 );  
 
    // set the number of elements (index to the last array element)  
    // lua_pushliteral压入一个字符串,不需要指定长度  
    // 如果lua_pushlstring,则需要指定长度  
     lua_pushliteral( state, "n" );  
     lua_pushnumber( state, 2 );  
     lua_rawset( state, -3 );  
 
    // set the name of the array that the script will access  
    // Pops a value from the stack and sets it as the new value of global name.  
    // 从栈顶弹出一个值,并将其设置全局变量"arg"的新值。  
     lua_setglobal( state, "arg" );  
 
 
     std::cout << "[C++] Running script" << std::endl;  
 
    int result = 0;  
    if (status == 0)  
     {  
         result = lua_pcall( state, 0, LUA_MULTRET, 0 );  
     }  
    else 
     {  
         std::cout << "bad" << std::endl;  
     }  
 
    if (result != 0)  
     {  
         std::cerr << "[C++] script failed" << std::endl;  
     }  
 
     std::cout << "[C++] These values were returned from the script" << std::endl;  
 
    // lua_gettop返回栈顶的索引  
    // 如果索引为0,则表示栈为空  
    while (lua_gettop( state ))  
     {  
        switch (lua_type( state, lua_gettop( state ) ))  
         {  
        case LUA_TNUMBER:  
             {  
                 std::cout << "script returned " << lua_tonumber( state, lua_gettop( state ) ) << std::endl;  
                break;  
             }  
        case LUA_TTABLE:    
             {  
                 std::cout << "script returned a table" << std::endl;  
                  
                // 简单的遍历表的功能  
                // ***好像lua不保存表的元素的添加顺序***  
 
                // 压入第一个键  
                 lua_pushnil(state);  /* 第一个 key */ 
                int t = -2;  
                while (lua_next(state, t) != 0)  
                 {  
                    /* 'key' (索引-2) 和 'value' (索引-1) */ 
                    const char* key = "unknown";  
                    const char* value;  
                    if(lua_type(state, -2) == LUA_TSTRING)  
                     {  
                         key = lua_tostring(state, -2);  
                         value = lua_tostring(state, -1);  
                     }  
                    else if(lua_type(state, -2) == LUA_TNUMBER)  
                     {  
                        // 因为lua_tostring会更改栈上的元素,  
                        // 所以不能直接在key上进行lua_tostring  
                        // 因此,复制一个key,压入栈顶,进行lua_tostring  
                         lua_pushvalue(state, -2);  
                         key = lua_tostring(state, -1);  
                         lua_pop(state, 1);  
                         value = lua_tostring(state, -1);  
                     }  
                    else 
                     {  
                         value = lua_tostring(state, -1);  
                     }  
 
                     std::cout    <<"key="<< key  
                                 << ", value=" << value << std::endl;  
 
                    /* 移除 'value' ;保留 'key' 做下一次迭代 */ 
                     lua_pop(state, 1);  
                 }  
 
                break;  
             }  
        case LUA_TSTRING:  
             {  
                 std::cout << "script returned " << lua_tostring( state, lua_gettop( state ) ) << std::endl;  
                break;  
             }  
        case LUA_TBOOLEAN:  
             {  
                 std::cout << "script returned " << lua_toboolean( state, lua_gettop( state ) ) << std::endl;  
                break;  
             }  
        default:  
             std::cout << "script returned unknown param" << std::endl;  
            break;  
         }  
         lua_pop( state, 1 );  
     }  
     lua_close( state );  
    return 0;  

本例用了一个控制台工程,输出如下:

[C++] Passing 'arg' array to script
[C++] Running script
[lua] These args were passed into the script from C
1       45
2       99
[lua] Script returning data back to C
1       9
2       8
3       7
4       6
5       5
6       test 6
test 99 99
test1 key       test1 value
[C++] These values were returned from the script
script returned 1
script returned 9
script returned a table
key=1, value=9
key=2, value=8
key=3, value=7
key=4, value=6
key=5, value=5
key=6, value=test 6
key=test 99, value=99
key=test1 key, value=test1 value

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值