在c中調用lua函數

src

include

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

API

lua_State* ls ;    //lua狀態機
ls = lua_open() ;    //建立狀態機
luaopen_base( ls ) ;    //載入一些io、string、math等lua自身的函式庫
luaL_openlibs( ls ) ;    //是載入lua的輔助庫
luaL_dofile( ls, "hanoi.lua" ) ;    //把我們寫好的lua文件檔引進,供c來調用
lua_getglobal( ls, "Hanoi" )    //在lua的全域變數列表中尋找Hanoi名稱,
lua_pushnumber 或 lua_pushstring    //都是將c的參數傳給lua中的函式,
lua_call( ls, 4, 0 )    //呼叫lua的函式,第二、第三個參數是說明lua的函式需要多少個參數、多少個回傳值,
lua_pop( ls, 0 )    //最後pop掉回傳值,這裡的函式沒有傳回值,所以第二個參數為0

C code

#include <cstdio>
#include <cstdlib>
#include "Lua/lua.hpp"
#pragma comment( lib, "Lua/lua51.lib" )

lua_State* ls ;    //lua狀態機

//調用lua函式
void Hanoi( int n, char* src, char* temp, char* dest )
{
 lua_getglobal( ls, "Hanoi" ) ;    //取得lua中的變量
 lua_pushnumber( ls, (double)n ) ;    //將第1個參數壓入lua
 lua_pushstring( ls, src ) ;    //將第2個參數壓入lua
 lua_pushstring( ls, temp ) ;    //將第3個參數壓入lua
 lua_pushstring( ls, dest ) ;    //將第4個參數壓入lua

 lua_call( ls, 4, 0 ) ;    //call lua function, 4個參數, 0個回傳數
 lua_pop( ls, 0 ) ;    //pop
}

int main( int argc, char** argv )
{
 ls = lua_open() ;
 luaopen_base( ls ) ;
 luaL_openlibs( ls ) ;
 luaL_dofile( ls, "hanoi.lua" ) ;

 int n = 3 ;
 printf( "Towers of Hanoi (with %d disks) \n", n ) ;
 Hanoi( n, "A", "B", "C" ) ;

 lua_close( ls ) ;
 system( "PAUSE" ) ;
 return 0 ;
}

Lua code

function Hanoi( n, src, temp, dest )
    if n == 1 then
        print( string.format( "%s -> %s", src, dest ) )
    else
        Hanoi( n-1, src, dest, temp )
        print( string.format( "%s -> %s", src, dest ) )
        Hanoi( n-1, temp, src, dest )
    end
end

在lua調用C函數

src

API

int argc = lua_gettop( L ) ;    //總共的參數個數
int n = (int)lua_tonumber( L , 1 ) ;    //取得第一個參數,lua_tonumber、lua_tostring等api,把參數從lua抓到c的函式
lua_pushnumber( L, (double)answer ) ;    //return answer傳給lua
lua_register( ls, "Fibonacci", Fibonacci ) ;    //向lua註冊一個c函式
luaL_dofile( ls, "fibonacci.lua" ) ;    //執行
lua_close( ls ) ;    //關閉lua狀態機

C code

#include <cstdio>
#include <cstdlib>
#include "Lua/lua.hpp"
#pragma comment( lib, "Lua/lua51.lib" )

lua_State* ls ;

//提供給lua調用的函式
static int Fibonacci( lua_State* L )
{
 int argc = lua_gettop( L ) ; //總共的參數個數
 int n = (int)lua_tonumber( L , 1 ) ; //取得第一個參數
 //這個Fibonacci函式剛好只有一個參數

 //fibonacci演算的主體
 int last = 1, next_last = 0 ;
 int answer = 1 ;

 if( n <= 1 ) {
 lua_pushnumber( L, (double)last ) ;
 return 1 ; //這個lua函式回傳值只有一個
 }

 for( int i = 2 ; i <= n ; ++i ) {
 answer = last + next_last ;
 next_last = last ;
 last = answer ;
 }
 //演算結束

 //return answer傳給lua
 lua_pushnumber( L, (double)answer ) ;
 return 1 ; //這個lua函式回傳值只有一個
}

int main( int argc, char** argv )
{
 ls = lua_open() ;
 luaopen_base( ls ) ;
 luaL_openlibs( ls ) ;

 //向lua註冊一個c函式
 lua_register( ls, "Fibonacci", Fibonacci ) ;
 luaL_dofile( ls, "fibonacci.lua" ) ; //執行

 lua_close( ls ) ;
 system( "PAUSE" ) ;
 return 0 ;
}

Lua code

n = 10 
print( "This is lua to call C function!" )
print( "Fibonacci("..n..") = "..Fibonacci(n) )

C lib for Lua

luaL_openlib函数接受一个C函数的列表和他们对应的函数名,并且作为一个库在一个table中注册所有这些函数

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static int hello_c (lua_State *L) {
    const char * from_lua = lua_tostring(L,1); //從Lua取得第一個參數
    printf("Lua: %s\n",from_lua);    
    lua_pushstring(L,"Hi Lua, nice to meet you"); //送回傳值給lua
    return 1;
}
static const struct luaL_Reg mylualib [] = {
    {"hello_c", hello_c}, //一个字符串和一个函数指针
    {NULL, NULL} /* sentinel */ //注意数组中最后一对必须是{NULL, NULL},用来表示结束
};
int luaopen_bettylib (lua_State *L) {
    1. luaL_openlib(L, "bettylib", mylualib, 0);//luaL_openlib声明主函数
    //luaL_openlib的第二个参数是库的名称
    or 使用
    2. luaL_newlib(L, mylualib);     /* register a array of c functions exported to lua */
    lua_pushvalue(L, -1);
    lua_setglobal(L, "bettylib"); /* the module name */

    return 1;
}

results matching ""

    No results matching ""