C函數返回多個參數給lua

lua通過一個運行時棧來維護參數傳遞及返回,使用lua_to*等函數獲取lua傳遞到C函數的參數,使用lua_push*從C函數返回值到lua腳本。此外也可以使用lua_getglobal從C函數獲取lua腳本定義的全局變量。

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

    #include <stdlib.h> /* For function exit() */
    #include <stdio.h> /* For input/output */

    void bail(lua_State *L, char *msg){
        fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
            msg, lua_tostring(L, -1));
        exit(1);
    }
    int lua_func_from_c(lua_State *L)
    {
        printf("This is C\n");
        int argc = lua_gettop(L);    /* number of arguments */
        const char * str = lua_tostring(L,1);    /* the first argument: string */
        int num = lua_tonumber(L,2); /* the second argument: number */
        
        printf("The first argument: %s\n", str);
        printf("The second argument: %d\n", num);

        lua_getglobal(L,"global_var");
        const char * global_str = lua_tostring(L,-1);
        printf("global_var is %s\n", global_str);

        int the_second_ret = 2*num;
        lua_pushstring(L, "the first return");
        lua_pushnumber(L, the_second_ret);
        return 2;            /* tell lua how many variables are returned */
    }
    int main(int argc, const char *argv[])
    {
        if(argc != 2)
        {
            return 1;
        }
        lua_State *L = luaL_newstate(); 	/* Create new lua state variable */

        /* Load Lua libraries, otherwise, the lua function in *.lua will be nil */
        luaL_openlibs(L);
        
        /* register new lua function in C */
        lua_register(L, "lua_func_from_c", lua_func_from_c);

        if( luaL_loadfile(L,argv[1]) ) 	/* Only load the lua script file */
            bail(L, "luaL_loadfile() failed");

        if( lua_pcall(L,0,0,0) ) 		/* Run the loaded lua file */
            bail(L, "lua_pcall() failed");
        lua_close(L);                 	/* Close the lua state variable */    

        return 0;
    }

lua腳本(my.lua)如下所示(在lua腳本中,如果變量不用local明確聲明爲局部變量,則默認爲全局變量):

    print("Hello world")
    global_var = "this is a global string"
    first, second = lua_func_from_c("the first one", 2)
    print("the first returned", first)
    print("the second returned", second)

執行結果:

Hello world
This is C
The first argument: the first one
The second argument: 2
global_var is this is a global string
the first returned the first return
the second returned 4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章