Calling lua functions from .lua's using handles?

后端 未结 3 1463
渐次进展
渐次进展 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 17:56

    The setfenv() function can be used to create a sandbox or environment for each file loaded.

    This example shows that all three files could be loaded with conflicting functions and that the functions can be called in any order. Similar code could be written in C++. This example only exports the print function to each environment, more might be needed in your scenario.

    function newEnv()
      -- creates a simple environment
      return {["print"]=print}
    end
    
    local e={} -- environment table
    local c    -- chunk variable
    
    -- first instance
    c = loadstring([[function f() print("1") end]])
    e[#e+1] = newEnv()
    setfenv(c, e[#e]) -- set the loaded chunk's environment
    pcall(c) -- process the chunk (places the function into the enviroment)
    
    -- second instance
    c = loadstring([[function f() print("2") end]])
    e[#e+1] = newEnv()
    setfenv(c, e[#e])
    pcall(c)
    
    -- third instance
    c = loadstring([[function f() print("3") end]])
    e[#e+1] = newEnv()
    setfenv(c, e[#e])
    pcall(c)
    
    pcall(e[3].f) --> 3
    pcall(e[2].f) --> 2
    pcall(e[1].f) --> 1
    pcall(e[1].f) --> 1
    pcall(e[2].f) --> 2
    pcall(e[3].f) --> 3
    

提交回复
热议问题