Calling lua functions from .lua's using handles?

后端 未结 3 1455
渐次进展
渐次进展 2020-12-29 17:14

I\'m working on a small project trying to integrate lua with c++. My problem however is as follows:

I have multiple lua scripts, lets call them s1.lua s2.lua and s3.

3条回答
  •  一向
    一向 (楼主)
    2020-12-29 18:10

    This is effectively what gwell proposed using the C API:

    #include 
    
    #include "lua.h"
    
    static void
    executescript(lua_State *L, const char *filename, const char *function)
    {
        /* retrieve the environment from the resgistry */
        lua_getfield(L, LUA_REGISTRYINDEX, filename);
    
        /* get the desired function from the environment */
        lua_getfield(L, -1, function);
    
        return lua_call(L, 0, 0);
    }
    
    static void
    loadscript(lua_State *L, const char *filename)
    {
        /* load the lua script into memory */
        luaL_loadfile(L, filename);
    
        /* create a new function environment and store it in the registry */
        lua_createtable(L, 0, 1);
        lua_getglobal(L, "print");
        lua_setfield(L, -2, "print");
        lua_pushvalue(L, -1);
        lua_setfield(L, LUA_REGISTRYINDEX, filename);
    
        /* set the environment for the loaded script and execute it */
        lua_setfenv(L, -2);
        lua_call(L, 0, 0);
    
        /* run the script initialization function */
        executescript(L, filename, "init");
    }
    
    int
    main(int argc, char *argv[])
    {
        lua_State *L;
        int env1, env2;
    
        L = (lua_State *) luaL_newstate();
        luaL_openlibs(L);
    
        loadscript(L, "test1.lua");
        loadscript(L, "test2.lua");
    
        executescript(L, "test1.lua", "run");
        executescript(L, "test2.lua", "run");
        executescript(L, "test2.lua", "run");
        executescript(L, "test1.lua", "run");
    
        return 0;
    }
    

    Test scripts:

    -- test1.lua
    function init() output = 'test1' end
    function run() print(output) end
    
    -- test2.lua
    function init() output = 'test2' end
    function run() print(output) end
    

    Output:

    test1
    test2
    test2
    test1
    

    I omitted all error handling for brevity, but you'll want to check the return value of luaL_loadfile and use lua_pcall instead of lua_call.

提交回复
热议问题