在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
与储存在lua解释器中特殊数据结构里的本地变量不同,全局变量储存在一个表table中。而Lua的一个非常有用的特性就是每个function都可以去改变这个table,这些table中的每一个都称之为一个环境environment。所以改变了这个表的function可以看到一组不同的全局变量。 Lua的默认全局环境即默认的全局表使用"_G"索引储存在它自己内部,Lua5.1和5.2之后版本中环境的工作方式非常不同 Environments in Lua 5.2
当Lua编译或者load一个代码块等同于代码:
_ENV:
load (ld [, source [, mode [, env]]])Loads a chunk. If ld is a string, the chunk is this string. If ld is a function, load calls it repeatedly to get the chunk pieces. Each call to ld must return a string that concatenates with previous results. A return of an empty string, nil, or no value signals the end of the chunk.If there are no syntactic errors, returns the compiled chunk as a function; otherwise, returns nil plus the error message. If the resulting function has upvalues, the first upvalue is set to the value of env, if that parameter is given, or to the value of the global environment. (When you load a main chunk, the resulting function will always have exactly one upvalue, the _ENV variable . When you load a binary chunk created from a function , the resulting function can have arbitrary upvalues.) source is used as the source of the chunk for error messages and debug information . When absent, it defaults to ld, if ld is a string, or to "=(load)" otherwise. The string mode controls whether the chunk can be text or binary (that is, a precompiled chunk). It may be the string "b" (only binary chunks), "t" (only text chunks), or "bt" (both binary and text). The default is "bt". In most cases, you don't need to use environments, unless you want to sandbox a loaded chunk, to give it convenient access to certain functions by making them look global, or to prevent it from seeing unsafe functions for security reasons. This is why 5.2's load function takes a parameter that lets you set the chunk's _ENV to a custom table, instead of_G. loadfile ([filename [, mode [, env]]])Similar to load, but gets the chunk from file filename or from the standard input, if no file name is given. 案例代码:
运行结果: Environments in Lua 5.1
load (func [, chunkname])Loads a chunk using function func to get its pieces. Each call to func must return a string that concatenates with previous results. A return of an empty string, nil, or no value signals the end of the chunk. If there are no errors, returns the compiled chunk as a function; otherwise, returns nil plus the error message. The environment of the returned function is the global environment. chunkname is used as the chunk name for error messages and debug information. When absent, it defaults to "=(load)". loadfile ([filename])Similar to load, but gets the chunk from file filename or from the standard input, if no file name is given. loadstring (string [, chunkname])Similar to load, but gets the chunk from the given string. To load and run a given string, use the idiom
When absent, chunkname defaults to the given string. 沙盒环境代码:
Usage兼容代码:
总结 The 5.1 way is sometimes considered simpler and more versatile, but it also requires special treatment of environments (instead of using the existing local variable system). Also, the 5.2 way is designed with the idea that a function's environment is supposed to be private, instead of being accessible from everywhere without the debug library, so it can be considered safer. Lua5.1的方法有时候会被认为更加的简单和多功能,但是它也需要对环境做出特殊的处理(替代现有的局部变量系统)。而Lua5.2方法是认为函数的环境应该为私有的理念来设计的,而不是在不需要调试库的情况下从任何地方都可以访问,因此可以认为Lua5.2的方法更加安全。 REF http://lua-users.org/wiki/EnvironmentsTutorial http://www.ogeek.net/article/55816.htm http://blog.csdn.net/xenyinzen/article/details/3485633 |
请发表评论