Incompatibilities with Lua 5.2

来源:互联网 发布:域名注册 不需要实名 编辑:程序博客网 时间:2024/06/03 22:52



You can avoid some incompatibilities by compiling Lua with appropriate options luaconf.h. However, all these compatibility options will be removed in the next version of Lua.

Similarly, all features marked as deprecated in Lua 5.1 have been removed in Lua 5.2.


从Lua5.1到Lua5.2版本,可以通过在luaconf.h配置文件中使用适合的选项来避免不兼容情况。但是,所有这些兼容选项都将在下一个版本中移除。

同样,在lua5.1中所有被标记为不建议使用的特性在lua5.2版本中已经被移除。




1 – Changes in the Language

1.1 The concept of environment changed. Only Lua functions have environments. To set the environment of a Lua function, use the variable _ENV or the function load.
    C functions no longer have environments. Use an upvalue with a shared table if you need to keep shared state among several C functions.
    You may use luaL_setfuncs to open a C library with all functions sharing a common upvalue.
    To manipulate the "environment" of a userdata (which is now called user value), use the new functions lua_getuservalue and lua_setuservalue.

1.2 Lua identifiers cannot use locale-dependent letters.
1.3 Doing a step or a full collection in the garbage collector does not restart the collector if it has been stopped.
1.4 Weak tables with weak keys now perform like ephemeron tables.
1.5 The event tail return in debug hooks was removed. Instead, tail calls generate a special new event, tail call, so that the debugger can know that there will not be a corresponding return event.
1.6 Equality between function values has changed.
Now, a function definition may not create a new value; it may reuse some previous value if there is no observable difference to the new function.

1.1    在Lua中环境的概念已经改变,仅仅在Lua Code函数中才拥有环境, 在Lua Code中设置环境变量可以使用_ENV 或者load函数
C Code函数已经不存在环境概念.如果需要在多个C code函数中保存共享状态,可以使用upvalue来共享一个table.
为了操作一个userdata的"环境",可以使用lua_getuservalue 和lua_setuservalue 两个新函数。

1.2     lua标识符不再使用命令相关的字母

1.3    当GC已经停止的时候,通过step或者 full collection不能重启GC.

1.4    Weak tables with weak keys now perform like ephemeron tables.

2、Changes in the Libraries

2.1 Function module is deprecated. It is easy to set up a module with regular Lua code. Modules are not expected to set global variables.
2.2 Functions setfenv and getfenv were removed, because of the changes in environments.
2.3 Function math.log10 is deprecated. Use math.log with 10 as its second argument, instead.
2.4 Function table.maxn is deprecated. Write it in Lua if you really need it.
2.5 Function os.execute now returns true when command terminates successfully and nil plus error information otherwise.
2.6 Function unpack was moved into the table library and therefore must be called as table.unpack.
2.7 Character class %z in patterns is deprecated, as now patterns may contain '\0' as a regular character.
2.8 The table package.loaders was renamed package.searchers.
2.9 Lua does not have bytecode verification anymore. So, all functions that load code (load and loadfile) are potentially insecure when loading untrusted binary data.
(Actually, those functions were already insecure because of flaws in the verification algorithm.)
When in doubt, use the mode argument of those functions to restrict them to loading textual chunks.
2.10 The standard paths in the official distribution may change between versions.

3 、Changes in the API
3.1 Pseudoindex LUA_GLOBALSINDEX was removed. You must get the global environment from the registry
3.2 Pseudoindex LUA_ENVIRONINDEX and functions lua_getfenv/lua_setfenv were removed, as C functions no longer have environments.
3.3 Function luaL_register is deprecated. Use luaL_setfuncs so that your module does not create globals.
 (Modules are not expected to set global variables anymore.)
3.4 The osize argument to the allocation function may not be zero when creating a new block, that is, when ptr is NULL .
 Use only the test ptr == NULL to check whether the block is new.
3.5 Finalizers (__gc metamethods) for userdata are called in the reverse order that they were marked for finalization, not that they were created
(Most userdata are marked immediately after they are created.)
Moreover, if the metatable does not have a __gc field when set, the finalizer will not be called, even if it is set later.
3.6 luaL_typerror was removed. Write your own version if you need it.
3.7 Function lua_cpcall is deprecated. You can simply push the function with lua_pushcfunction and call it with lua_pcall
3.8 Functions lua_equal and lua_lessthan are deprecated. Use the new lua_compare with appropriate options instead.
3.9 Function lua_objlen was renamed lua_rawlen.
3.10 Function lua_load has an extra parameter, mode. Pass NULL to simulate the old behavior.
3.11 Function lua_resume has an extra parameter, from. Pass NULL or the thread doing the call.

3.1 伪索引 LUA_GLOBALSINDEX 被删除。必须通过注册表才能获取全局环境
3.2 伪索引 LUA_ENVIRONINDEX 和 lua_getfenv/lua_setfenv 全被删除,C code函数不再存在环境。
3.3 luaL_register 不被建议使用。当不创建全局函数时,使用luaL_setfuncs 函数。
3.5 userdata的终止方法__gc 元方法在最后才被标记终止.而不是在userdata被创建时被标记终止。
3.9  lua_objlen 被重新命名成 lua_rawlen


可以看出在从lua5.1到lua5.2,最大改变就是关于 environment概念的改变。同时优化GC垃圾回收器和部分方法。

0 0
原创粉丝点击