8 – 与前一版本的兼容性

原文:https://www.lua.org/manual/5.4/manual.html

8 – Incompatibilities with the Previous Version – 与前一版本的兼容性

Here we list the incompatibilities that you may find when moving a program from Lua 5.3 to Lua 5.4.

​ 这里列出了将程序从 Lua 5.3 迁移到 Lua 5.4 时可能遇到的不兼容性。

You can avoid some incompatibilities by compiling Lua with appropriate options (see file luaconf.h). However, all these compatibility options will be removed in the future. More often than not, compatibility issues arise when these compatibility options are removed. So, whenever you have the chance, you should try to test your code with a version of Lua compiled with all compatibility options turned off. That will ease transitions to newer versions of Lua.

​ 通过使用适当的选项编译 Lua,可以避免一些不兼容性(参见文件 luaconf.h )。但是,所有这些兼容性选项都将在未来移除。通常,当这些兼容性选项被移除时,就会出现兼容性问题。因此,只要有机会,您都应该尝试使用已关闭所有兼容性选项的 Lua 版本来测试您的代码。这将简化向 Lua 新版本的过渡。

Lua versions can always change the C API in ways that do not imply source-code changes in a program, such as the numeric values for constants or the implementation of functions as macros. Therefore, you should never assume that binaries are compatible between different Lua versions. Always recompile clients of the Lua API when using a new version.

​ Lua 版本始终可以改变 C API,而不会导致程序中的源代码发生变化,例如常量的数值或函数作为宏的实现。因此,您绝不应假设二进制文件在不同的 Lua 版本之间是兼容的。在使用新版本时,务必重新编译 Lua API 的客户端。

Similarly, Lua versions can always change the internal representation of precompiled chunks; precompiled chunks are not compatible between different Lua versions.

​ 同样,Lua 版本始终可以改变预编译块的内部表示;预编译块在不同的 Lua 版本之间不兼容。

The standard paths in the official distribution may change between versions.

​ 官方发行版中的标准路径可能会在不同版本之间发生变化。

8.1 – Incompatibilities in the Language 语言中的不兼容性

  • The coercion of strings to numbers in arithmetic and bitwise operations has been removed from the core language. The string library does a similar job for arithmetic (but not for bitwise) operations using the string metamethods. However, unlike in previous versions, the new implementation preserves the implicit type of the numeral in the string. For instance, the result of "1" + "2" now is an integer, not a float. 算术和位运算中字符串到数字的强制转换已从核心语言中移除。字符串库对算术(但不是位运算)操作执行类似的工作,使用字符串元方法。但是,与以前版本不同,新实现保留了字符串中数字的隐式类型。例如, "1" + "2" 的结果现在是一个整数,而不是浮点数。
  • Literal decimal integer constants that overflow are read as floats, instead of wrapping around. You can use hexadecimal notation for such constants if you want the old behavior (reading them as integers with wrap around). 溢出的文字十进制整数常量被读作浮点数,而不是环绕。如果您想要旧的行为(将它们读作带有环绕的整数),则可以使用十六进制表示法表示此类常量。
  • The use of the __lt metamethod to emulate __le has been removed. When needed, this metamethod must be explicitly defined. 已删除使用 __lt 元方法来模拟 __le 。在需要时,必须显式定义此元方法。
  • The semantics of the numerical for loop over integers changed in some details. In particular, the control variable never wraps around. 数值 for 循环对整数的语义在某些细节上发生了变化。特别是,控制变量永远不会环绕。
  • A label for a goto cannot be declared where a label with the same name is visible, even if this other label is declared in an enclosing block. 不能在可见具有相同名称的标签的位置声明 goto 的标签,即使此其他标签是在封闭块中声明的也是如此。
  • When finalizing an object, Lua does not ignore __gc metamethods that are not functions. Any value will be called, if present. (Non-callable values will generate a warning, like any other error when calling a finalizer.) 在完成一个对象时,Lua 不会忽略不是函数的 __gc 元方法。如果存在,任何值都会被调用。(不可调用的值会生成一个警告,就像调用完成函数时发生的任何其他错误一样。)

8.2 – Incompatibilities in the Libraries 库中的不兼容性

  • The function print does not call tostring to format its arguments; instead, it has this functionality hardwired. You should use __tostring to modify how values are printed. 函数 print 不调用 tostring 来格式化其参数;相反,它具有这种硬连线的功能。您应该使用 __tostring 来修改值的打印方式。
  • The pseudo-random number generator used by the function math.random now starts with a somewhat random seed. Moreover, it uses a different algorithm. 函数 math.random 使用的伪随机数生成器现在以一个有点随机的种子开始。此外,它使用不同的算法。
  • By default, the decoding functions in the utf8 library do not accept surrogates as valid code points. An extra parameter in these functions makes them more permissive. 默认情况下, utf8 库中的解码函数不接受代理项作为有效的代码点。这些函数中的一个额外参数使它们更宽松。
  • The options “setpause” and “setstepmul” of the function collectgarbage are deprecated. You should use the new option “incremental” to set them. 函数 collectgarbage 的选项“ setpause ”和“ setstepmul ”已弃用。您应该使用新选项“ incremental ”来设置它们。
  • The function io.lines now returns four values, instead of just one. That can be a problem when it is used as the sole argument to another function that has optional parameters, such as in load(io.lines(filename, "L")). To fix that issue, you can wrap the call into parentheses, to adjust its number of results to one. 函数 io.lines 现在返回四个值,而不仅仅是一个值。当它用作另一个具有可选参数的函数的唯一参数时,这可能会成为一个问题,例如在 load(io.lines(filename, "L")) 中。要解决此问题,您可以将调用包装在括号中,以将其结果数调整为一个。

8.3 – Incompatibilities in the API API 中的不兼容性

  • Full userdata now has an arbitrary number of associated user values. Therefore, the functions

    lua_newuserdata
    

    ,

    lua_setuservalue
    

    , and

    lua_getuservalue
    

    were replaced by

    lua_newuserdatauv

    ,

    lua_setiuservalue

    , and

    lua_getiuservalue

    , which have an extra argument.

    完整用户数据现在具有任意数量的关联用户值。因此,函数 lua_newuserdatalua_setuservaluelua_getuservalue 已被 lua_newuserdatauvlua_setiuservaluelua_getiuservalue 替换,这些函数具有一个额外的参数。

    For compatibility, the old names still work as macros assuming one single user value. Note, however, that userdata with zero user values are more efficient memory-wise.

    ​ 为了兼容性,旧名称仍然可以作为宏使用,假设只有一个用户值。但是,请注意,具有零个用户值的用户数据在内存方面更有效。

  • The function lua_resume has an extra parameter. This out parameter returns the number of values on the top of the stack that were yielded or returned by the coroutine. (In previous versions, those values were the entire stack.) 函数 lua_resume 有一个额外的参数。此 out 参数返回协程让步或返回的栈顶部的值的数量。(在以前的版本中,这些值是整个栈。)

  • The function lua_version returns the version number, instead of an address of the version number. The Lua core should work correctly with libraries using their own static copies of the same core, so there is no need to check whether they are using the same address space. 函数 lua_version 返回版本号,而不是版本号的地址。Lua 核心应该与使用其自身静态副本的库正确协作,因此无需检查它们是否使用相同的地址空间。

  • The constant LUA_ERRGCMM was removed. Errors in finalizers are never propagated; instead, they generate a warning. 常量 LUA_ERRGCMM 已移除。终结器中的错误绝不会传播;相反,它们会生成警告。

  • The options LUA_GCSETPAUSE and LUA_GCSETSTEPMUL of the function lua_gc are deprecated. You should use the new option LUA_GCINC to set them. 函数 lua_gc 的选项 LUA_GCSETPAUSELUA_GCSETSTEPMUL 已弃用。您应该使用新选项 LUA_GCINC 来设置它们。

最后修改 January 31, 2024: 更新 (e0896df)