简体   繁体   English

从文件获取变量

[英]Getting variables from a file

Supposed I have a "test.lua" file like this: 假设我有一个“ test.lua”文件,如下所示:

myVar = 5

Food = function()

end

If I load the file via loadfile or via Lua API (in C++ or whatever) and run it, the variables will be saved under the global namespace _G; 如果我通过loadfile或Lua API(使用C ++或其他语言)加载文件并运行它,则变量将保存在全局命名空间_G下; however, I'd like to have them separately, like _test.myVar and _G.myVar. 但是,我想单独使用它们,例如_test.myVar和_G.myVar。 (The reason for this is that I want to have a list of the variables from that file only). (这样做的原因是我只想拥有该文件中的变量列表)。 Thanks. 谢谢。

运行脚本之前,请使用lua_setfenv

module('_test')

myVar = 5

Food = function()

end

Then, from some other file: 然后,从其他文件中:

require 'test.lua' --> or loadfile('test.lua')()

print(myVar) --> nil
print(Food) --> nil
print(_test.myVar) --> 5
print(_test.Food) --> function

What lhf said. lhf说了什么。

For additional robustness in the face of files that may not be from a completely trusted source, you should read about sandboxing at the wiki. 对于可能来自完全不受信任的来源的文件,要获得更多的健壮性,您应该在Wiki上阅读有关沙箱的信息。

The key idea is to be careful about what global functions and variables are available to code executing in the context of your data file. 关键思想是要小心哪些全局函数和变量可用于在数据文件的上下文中执行代码。 An easy way to get a lot of control over that is to construct the environment table that supplies the globals to the script so that it only contains a white list of safe functions. 获得大量控制权的一种简单方法是构造为脚本提供全局变量的环境表,使其仅包含安全功能白名单。 You do this by constructing a suitable table, and then setting it as the environment of the freshly compiled script before calling it. 为此,可以构造一个合适的表,然后在调用它之前将其设置为新编译脚本的环境。 lua_setfenv() from the C API or setfenv from the Lua side can both be used on the object returned by a successful call to luaL_loadfile() , loadfile or one of their relatives in either the C API or from Lua, respectively. 来自C API的lua_setfenv()或来自Lua端的setfenv都可以在成功调用luaL_loadfile()loadfile或其loadfile或Lua中的亲戚之一返回的对象上使用。 Once the script is loaded and the environment assigned, the you run it with lua_pcall() or pcall . 加载脚本并分配环境后,您可以使用lua_pcall()pcall运行它。

Don't forget to check everything for errors. 不要忘记检查所有内容是否有错误。

When the script completes, variables it created have been written or updated in its environment table, rather than _G . 脚本完成后,已在其环境表(而不是_G写入或更新了它创建的变量。

Naturally, that environment table could use a metatable to make some of the globals you supply the script effectively read-only as well. 自然,该环境表可以使用一个元表来使您提供脚本的某些全局变量也有效地只读。

For additional control, some have taken the idea further and arranged to limit the number of virtual instruction cycles or real clock time allowed for the script to run. 为了进行额外的控制,有些人将其构想进一步扩展并安排为限制脚本运行所允许的虚拟指令周期数或实际时钟时间。 It is even possible to inspect the bytecode after loading for certain opcodes. 甚至可以在加载某些操作码后检查字节码。 That can be used to prevent even attempting to execute a script that contains a loop in many cases. 在许多情况下,这可以用来防止甚至尝试执行包含循环的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM