可以说我至少有两个lua脚本文件.
test1.lua test2.lua
两者都定义了一个init函数和其他具有相似名称的函数.
如何使用c ++/c将每个脚本文件加载到使用Lua 5.2的单独环境中,以便相同的函数名称不会发生冲突 - 我发现5.1的示例代码对我不起作用(因为setenv已经消失而lua_setuservalue没有似乎工作)
这里的示例从.lua使用句柄调用lua函数?
基本上如果我用setuservalue替换setenv - 我得到访问冲突.
在非官方的Lua常见问题有大约在Lua沙箱中的条目.我的猜测是你可以很容易地将这个逻辑转换成你的C/C++代码.
另请参阅lua-users wiki上的LuaFiveTo.
更正
它确实不像看起来那么微不足道.但最后要点很简单:加载你的块,按_ENV表,使用lua_setupvalue(L,-2,1).重要的是表格应该位于堆栈的顶部.
作为一个小例子,使用2个环境默认为_G通过metatables读取内容:
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int main(void){
lua_State *L = luaL_newstate();
char *file1 = "file1.lua";
char *file2 = "file2.lua";
luaL_openlibs(L);
luaL_loadfile(L,file2); // S: 1
luaL_loadfile(L,file1); // S: 2
lua_newtable(L); // ENV for file 1: S: 321
lua_newtable(L); // ENV for file 2: S: 4321
//lets have each function have its metatable, where missed lookups are
//instead looked up in the global table _G
lua_newtable(L); // metatable S: 54321
lua_getglobal(L,"_G"); // pushes _G, which will be the __index metatable entry S: 654321
lua_setfield(L,-2,"__index"); // metatable on top S: 54321
lua_pushvalue(L,-1); // copy the metatable S: 554321
lua_setmetatable(L,-3); // set the last copy for env2 S: 54321
lua_setmetatable(L,-3); // set the original for env1 S: 4321
// here we end up having 2 tables on the stack for 2 environments
lua_setupvalue(L,1,1); // first upvalue == _ENV so set it. S: 321
lua_setupvalue(L,2,1); // set _ENV for file S: 21
// Remaining on the stack: 2 chunks with env set.
lua_pcall(L,0,LUA_MULTRET,0);
lua_pcall(L,0,LUA_MULTRET,0);
lua_close(L);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于2个Lua文件:
-- file1.lua
function init()
A="foo"
print("Hello from file1")
print(A)
end
init()
-- file2.lua
-- this shows that stuff defined in file1 will not polute the environment for file2
print("init function is",tostring(init))
function init()
A="bar"
print("Hello from file2")
print(A)
end
init()
Run Code Online (Sandbox Code Playgroud)