Lua5.2和5.1有哪些不同?相對與5.1有什麼進步?Lua5.2能用5.1的庫嗎?如果不能,有哪些可用的庫?

我一直在用Lua for windows,但它一直停留在5.1.4版本,而Lua已經到5.2.3了,找不到新版完整的中文版資料,我英語又不好,所以想了解一下,Lua5.2和5.1有哪些不同?相對與5.1有什麼進步?在Windows下,Lua5.2能用5.1的庫嗎?如果不能,有哪些可用的庫?非常感謝!


Lua 5.1 是個非常經典,歷史非常悠久,非常穩定的版本,已經持續了很多年,5.1 與 5.2 是完全不兼容的,相關的第三方庫必須重新為 5.2 適配。所以目前繼續使用 5.1 可能是更好的選擇。

一個重要的參考因素是:LuaJIT 是按照 5.1 的語法設計的,並且在可以預期的將來也永遠不會適配 5.2,LuaJIT 作者聲稱會增加 5.2 所增加的那些功能,但永遠不會適配 5.2 的語法,換句話說,他的發展思路是語法與兼容性不變,僅僅在 5.1 的語法基礎之上增加後續 Lua 版本的新特性,LuaJIT 在 API/ABI 方面都只兼容 Lua 5.1.4。

另外一個參考因素是:Lua 5.2 開發了很短的時間內,Lua 5.3 就已經開始開發了,這讓人感覺 Lua 5.2 並非是一個 API 長期穩定的版本,沒有經過時間的積澱,其周邊的庫難以象 5.1 那樣形成規模,而 5.3 的開發已經提上議事日程,這更使得對 5.2 的第三方庫適配進度會放緩。轉而適配 5.3。

就目前而言,如果我打算 LuaJIT / Lua 雙重適配的話,一定是選擇 5.1 語法的。


先搬運下5.2與5.1之間的差別,

1)在lua層面

  • Function module is deprecated. It is easy to set up a module with regular Lua code. Modules are not expected to set global variables.
  • Functions setfenv and getfenv were removed, because of the changes in environments.
  • Function math.log10 is deprecated. Use math.log with 10 as its second argument, instead.
  • Function loadstring is deprecated. Use load instead; it now accepts string arguments and are exactly equivalent to loadstring.
  • Function table.maxn is deprecated. Write it in Lua if you really need it.
  • Function os.execute now returns true when command terminates successfully and nil plus error information otherwise.
  • Function unpack was moved into the table library and therefore must be called as table.unpack.
  • Character class %z in patterns is deprecated, as now patterns may contain "" as a regular character.
  • The table package.loaders was renamed package.searchers.
  • 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.
  • The standard paths in the official distribution may change between versions.

在api層面

  • Pseudoindex LUA_GLOBALSINDEX was removed. You must get the global environment from the registry (see §4.5).
  • Pseudoindex LUA_ENVIRONINDEX and functions lua_getfenv/lua_setfenv were removed, as C functions no longer have environments.
  • 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.)
  • The osize argument to the allocation function may not be zero when creating a new block, that is, when ptr is NULL (see lua_Alloc). Use only the test ptr == NULL to check whether the block is new.
  • Finalizers (__gc metamethods) for userdata are called in the reverse order that they were marked for finalization, not that they were created (see §2.5.1). (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.
  • luaL_typerror was removed. Write your own version if you need it.
  • Function lua_cpcall is deprecated. You can simply push the function with lua_pushcfunction and call it with lua_pcall.
  • Functions lua_equal and lua_lessthan are deprecated. Use the new lua_compare with appropriate options instead.
  • Function lua_objlen was renamed lua_rawlen.
  • Function lua_load has an extra parameter, mode. Pass NULL to simulate the old behavior.
  • Function lua_resume has an extra parameter, from. Pass NULL or the thread doing the call.

可以看到如果不修改源代碼, 基本上5.2的庫不能直接用於5.1, 這裡主要的差別是

module, setfenv等函數, 這些函數都被刪除了, module可以通過編譯期增加LUA_COMPAT_MODULE宏來兼容(但語義還是有些許不同, 還需要對症修改對應代碼), 但setfenv只能手動實現了, 5.2的環境變成了函數的第一個up value,叫做_ENV, 使用debug.setupvalue函數可以修改這個upvalue達到setfenv的效果.

api層面去掉了global index, 請使用lua_getglobal/lua_setgloba函數訪問_G, pcall和call增加k參數,用於continuous等.

在自己的引擎中,建議開啟LUA_COMPAT_ALL宏,可以消除大部分兼容性問題, 然後在注意我上面提到的,基本上就沒有問題了.

5.1到5.2最大的好處(升級動力)對我來說,是table增加了_gc的原方法,luajit沒提供類似支持,用這個原方法可以顯著控制gc的頻率和代碼運行效率.


5.1.4經典版本存在時間過久,第三方庫很豐富。結果升級到5.2後,很多第三方都不支持了。

lua第三方庫不多,如果只是做非嵌入腳本,可以考慮使用python

另外,decoda這個怪獸級的調試器也只支持到5.1

5.3已經發了,支持原生整形和64位,一步到位吧


推薦閱讀:

C、Scheme、Lua 和 Go 究竟哪個最簡單?
如何評價七牛雲存儲的 qnlang?
actor模型除了erlang和skynet用得廣泛嗎?
如何用C語言實現異常/狀況處理機制?
Lua 語言有哪些不足?

TAG:MicrosoftWindows | Lua |