Lua教程(4):Lua调用C/C++函数

时间:2022-09-22 09:20:23

  读后感悟:这篇文章讲解了静态的lua调用c/c++函数,所谓静态就是先运行c/c++代码来lua_register注册函数,而不是直接把函数写入库dll或so,然后在lua中require(lib),这也是本篇文章的不够好的地方,可以从这篇文章中看到两个详细的调用,地址链接:

http://blog.csdn.net/shun_fzll/article/details/39120965。而且这篇文章没有详细把代码串联起来。

   这里需要注意的是,最基本的原理是把typedef int (*lua_CFunction) (lua_State *L)的函数放入栈,然后再lua全局中赋值给一个表,lua_pushcfunction和lua_setglobal就是完成这个功能的函数,而已luaL_register就是帮你把多个函数放在一个表中,调用的时候就写成xxx.function。

    另外一定要注意:在lua中调用c/c++函数,函数的中的lua_State的栈是这次调用私有的,而不会受到其他栈影响,也不会影响其他栈,所以c/c++函数可以通过这个栈来取值和传值,这些在我示例代码中我都有展示。示例代码,自己可以运行看结果。

  

luacallcplus.cpp
#include"mylualib.h"

int main(void)
{
  lua_State *lua_state=luaL_newstate();  
  luaL_openlibs(lua_state);
  int stacksize=lua_gettop(lua_state);
  std::cout<<"stacksize 1:"<<stacksize<<std::endl<<std::endl;

  //lua_register(lua_state,"average",average);
  lua_pushcfunction(lua_state,average);
  stacksize=lua_gettop(lua_state);
  std::cout<<"lua_pushcfunction stacksize :"<<stacksize<<std::endl<<std::endl;
  lua_setglobal(lua_state,"average");
   
  lua_pushcfunction(lua_state,sub);
  lua_setglobal(lua_state,"sub");

  stacksize=lua_gettop(lua_state);
  std::cout<<"stacksize 2:"<<stacksize<<std::endl<<std::endl;
  
  luaL_Reg mylib[]=
  {
    {"myave",average},
    {"mysub",sub},
    {NULL,NULL}

  }; 
  luaL_register(lua_state,"ojr",mylib);
  lua_settop(lua_state,0); 

  stacksize=lua_gettop(lua_state);
  std::cout<<"stacksize 3:"<<stacksize<<std::endl<<std::endl;

  lua_pushnumber(lua_state,50);
  lua_pushnumber(lua_state,100);
  int status=luaL_loadfile(lua_state,"mylua.lua");
  std::cout<<"Show 1:"<<std::endl;
  luaShowStackFromTop2Bot(lua_state); 
  std::cout<<std::endl;

//  lua_pushnumber(lua_state,30);
//  lua_pushnumber(lua_state,40);
//  
//  std::cout<<"Show 2:"<<std::endl;
//  luaShowStackFromTop2Bot(lua_state);
//  std::cout<<std::endl;

  stacksize=lua_gettop(lua_state);
  std::cout<<"stacksize 4:"<<stacksize<<std::endl<<std::endl;

  if(status)
  {
    std::cout<<"loadfile error"<<std::endl;
  }
  int pstatus=0;
  pstatus=lua_pcall(lua_state,0,LUA_MULTRET,0);
  if(pstatus)
  {
   std::cout<<"error="<<pstatus<<std::endl;
   std::cout<<"lua error"<<std::endl;
  }
  std::cout<<"Show 3:"<<std::endl; 
  luaShowStackFromTop2Bot(lua_state);
  std::cout<<std::endl;
  lua_close(lua_state);


  return 0;
}
mylualib.h
#ifndef MYLUALIB
#define MYLUALIB

extern "C" {
#include<lua.h>
#include<lualib.h>
#include<lauxlib.h>

} 
#include<iostream>
#include<string>

void luaShowStackFromTop2Bot(lua_State *lua_state);

int average(lua_State *lua_state);


int sub(lua_State *lua_state);

#endif

mylualib.cpp
#include"mylualib.h"

void luaShowStackFromTop2Bot(lua_State *lua_state)
{
  int top=lua_gettop(lua_state);
  for(;top>0;--top)
  {
    int type=lua_type(lua_state,top);
    switch(type) {
     case LUA_TSTRING:
         std::cout<<"string="<<lua_tostring(lua_state,top)<<std::endl;  
	 break;
     case LUA_TNUMBER:
	 std::cout<<"number="<<lua_tonumber(lua_state,top)<<std::endl;
         break;
     case LUA_TBOOLEAN:
	 std::cout<<"boolean="<<(lua_toboolean(lua_state,top)?"true":"false")<<std::endl;
         break;
     default:
	 std::cout<<"other="<<lua_typename(lua_state,type)<<std::endl;



    }



  }

}

int average(lua_State *lua_state)
{
  //  while(1)
  //  {
//
   // }
  int n=lua_gettop(lua_state);
  //检查lua传回来的lua_state是否是新的,每个都是新的?
  std::cout<<"average begin size:"<<n<<std::endl;
  std::cout<<"average begin stack:"<<std::endl;
  luaShowStackFromTop2Bot(lua_state);
  std::cout<<std::endl;
  double sum=0;
  int i;
  for(i=1;i<=n;++i)
  {
    sum+=lua_tonumber(lua_state,i);
  }
  lua_pushnumber(lua_state,sum/n);
  lua_pushnumber(lua_state,sum);

  n=lua_gettop(lua_state);
  std::cout<<"average end size:"<<n<<std::endl;
  std::cout<<"average end stack:"<<std::endl;
  luaShowStackFromTop2Bot(lua_state);
  std::cout<<std::endl;

  //返回返回值得个数
  return 2;
}

int sub(lua_State *lua_state)
{
  int n=lua_gettop(lua_state);
  std::cout<<"sub begin size:"<<n<<std::endl;
  std::cout<<"sub begin stack:"<<std::endl;
  luaShowStackFromTop2Bot(lua_state);
  std::cout<<std::endl;
  
  double num1=0;
  double sum=0;
  num1=lua_tonumber(lua_state,1);
  for(int i=2;i<=n;++i)
  {
   sum+=lua_tonumber(lua_state,i);
  }
  lua_pushnumber(lua_state,num1-sum);

   n=lua_gettop(lua_state);
  std::cout<<"sub end  size:"<<n<<std::endl;
  std::cout<<"sub end  stack:"<<std::endl;
  luaShowStackFromTop2Bot(lua_state);
  std::cout<<std::endl;

  return 1;
}


mylua.lua
avg,sum=average(10,20,30,40,50)
sub=sub(50,20,10)
print("avg=",avg)
print("sum=",sum)
print("sub=",sub)


avg2,sum2=ojr.myave(1,2,3)
sub2=ojr.mysub(3,2)
print("avg2=",avg2)
print("sum2=",sum2)
print("sub2=",sub2)

结果如下:

stacksize 1:0

lua_pushcfunction stacksize :1

stacksize 2:0

stacksize 3:0

Show 1:
other=function
number=100
number=50

stacksize 4:3

average begin size:5
average begin stack:
number=50
number=40
number=30
number=20
number=10

average end size:7
average end stack:
number=150
number=30
number=50
number=40
number=30
number=20
number=10

sub begin size:3
sub begin stack:
number=10
number=20
number=50

sub end  size:4
sub end  stack:
number=20
number=10
number=20
number=50

avg=	30
sum=	150
sub=	20
average begin size:3
average begin stack:
number=3
number=2
number=1

average end size:5
average end stack:
number=6
number=2
number=3
number=2
number=1

sub begin size:2
sub begin stack:
number=2
number=3

sub end  size:3
sub end  stack:
number=1
number=2
number=3

avg2=	2
sum2=	6
sub2=	1
Show 3:
number=100
number=50

  

    原文地址:http://4gamers.cn/blog/2014/07/30/lua-call-cpp-functions/,原文内容:

    本教程将介绍如何在Lua里面调用C/C++函数。在Lua里面调用C/C++函数其实是比较简单,本文将通过两个示例演示具体的做法:一个是求平均数,另一个是打印Lua函数的一些参数信息。最后,本文会介绍如何把这两个函数定义成一个模块,这样Lua代码里面就可以不再使用全局的名字空间了。


前言

当我们需要在Lua里面调用C/C++函数时,所有的函数都必须满足以下函数签名:

typedef int (*lua_CFunction) (lua_State *L);

换句话说,所有的函数必须接收一个lua_State作为参数,同时返回一个整数值。因为这个函数使用Lua栈作为参数,所以它可以从栈里面读取任意数量和任意类型的参数。而这个函数的返回值则表示函数返回时有多少返回值被压入Lua栈。(因为Lua的函数是可以返回多个值的)


示例一

定义C++函数指针

int average(lua_State *L)
{
    // get number of arguments 
    int n = lua_gettop(L);
    double sum = 0;
    int i;
    // loop through each argument 
    for (i = 1; i <= n; i++)
    {
        // total the arguments 
        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;
}


注册此函数给Lua

lua_register(L, "average", average);


Lua里面调用此函数

avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)

示例二

定义C++函数

int displayLuaFunction(lua_State *l)
{
    // number of input arguments
    int argc = lua_gettop(l);
    // print input arguments
    std::cout << "[C++] Function called from Lua with " << argc 
              << " input arguments" << std::endl;
    for(int i=0; i<argc; i++)
    {
        std::cout << " input argument #" << argc-i << ": "
                  << lua_tostring(l, lua_gettop(l)) << std::endl;
        lua_pop(l, 1);
    }
    // push to the stack the multiple return values
    std::cout << "[C++] Returning some values" << std::endl;
    lua_pushnumber(l, 12);
    lua_pushstring(l, "See you space cowboy");
    // number of return values
    return 2;
}


注册此Lua函数

// push the C++ function to be called from Lua
std::cout << "[C++] Pushing the C++ function" << std::endl;
lua_pushcfunction(L, displayLuaFunction);
lua_setglobal(L, "displayLuaFunction");


注意,上一个示例,我们使用的是函数是

lua_register(L, "average", average);

它其实只是一个宏定义,其实现也是上面两个函数组成的。


在Lua里调用此函数

io.write('[Lua] Calling the C functionn')
a,b = displayLuaFunction(12, 3.141592, 'hola')
-- print the return values
io.write('[Lua] The C function returned <' .. a .. '> and <' .. b .. '>\n')


实现一个Lua模块

首先,我们把这两个C函数封装到一个数组里面:

static const luaL_Reg mylibs[]=
{
    {"average", average},
    {"displayLuaFunction", displayLuaFunction},
    {NULL, NULL}
};


接下来,我们定义另一个C函数,让它注册我们的Lua模块:

int lua_openmylib(lua_State *L)
{
    luaL_newlib(L, mylibs);
    return 1;
};


这里的luaL_newlib会生成一个table,并把所有的mylibs里面的函数填充进去。最后,lua_openmylib返回值为1,表示会把刚刚生成的table压入栈。


最后,我们像之前注册Lua的标准库一样,注册我们新的库,并给它起名字为mylib:

static const luaL_Reg lualibs[] =
{
       {"base", luaopen_base},
       {"io", luaopen_io},
       {"mylib", lua_openmylib},
       {NULL, NULL}
};

此时,我们在Lua里面调用之前的两个函数就需要带上模块名字前缀了:

avg, sum = mylib.average(10, 20, 30, 40, 50)
a,b = mylib.displayLuaFunction(12, 3.141592, 'hola')


结语

注意:这里C函数参数里的Lua栈是私有的,每一个函数都有自己的栈。当一个c/c++函数把返回值压入Lua栈以后,该栈会自动被清空。


推荐阅读

Calling C++ Functions From Lua

CALLING C++ FUNCTIONS FROM LUA 5.2

http://csl.name/lua/