Lua 和 C/C++ 互相調用實例分析

jopen 8年前發布 | 8K 次閱讀 C/C++開發

//創建lua運行上下文
lua_State* 
luaL_newstate(void) ;
//加載lua腳本文件
int luaL_loadfile(lua_State *L, const 
char *filename);

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);

下面進行實例說明:
func.lua

#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 ;
}
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!