r/lua • u/st3f-ping • Apr 23 '25
Help Differences between Lua and LuaJIT?
Hi all. I've been a casual user of Lua for years and of LuaJIT for just a few months. I am not clear on all of the differences I need to know when writing code.
I know that integer division (//) is not implemented in LuaJIT and that LuaJIT has increased interoperability with C (which I haven't yet used). Yesterday I wrote a bit of code for LuaJIT that produces differently formatted output between Lua (5.4) and LuaJIT (5.1).
It worries me that there might be more gotchas lurking and a cheat sheet of everything a Lua programmer should know when switching to LuaJIT would be really useful (before I start diving into Lua version changes and seeing of this is a Lua version difference and not a Lua/LuaJIT difference).
Can anyone help?
15
u/hawhill Apr 23 '25
LuaJIT is compatible with Lua 5.1. It also has some features of newer versions, but it does not attempt to be fully compatible with them. https://luajit.org/extensions.html has those features from newer versions at the bottom of the document.
6
u/SkyyySi Apr 23 '25
I made a script to compare the standard libraries of different Lua versions. You can find a
.difffrom LuaJIT and Lua 5.4 (along with the script itself) here: https://gist.github.com/SkyyySi/c8673e30adfea24a1f4ce896b64a8600#file-lua-globals-luajit-lua54-diff (green additions are LuaJIT-only, red removals are Lua 5.4 only)8
u/QuirkyImage Apr 23 '25
People have told me in the past to almost treat 5.1 2 3 4 as separate languages . I pretty much stick with 5.1 because of Neovim, hammerspoon, luajit, fennel compiles to it and it’s a small language. I wonder how easy is it to reimplement?
1
u/st3f-ping Apr 23 '25 edited Apr 23 '25
I've tracked it down to that way the / operator is working. If I divide an integer by a factor of that integer e.g. 2/1, I know I get an integer back. So the statement:
generates this in LuaJIT (5.1)
but this in Lua (5.4)
(edit: am aware that I can use floor division in Lua to get the same behaviour as LuaJIT 5.1. My working assumption is that the behaviour of the / operator changed when // was introduced)
I'm guessing this is a version issue and not a Lua/LuaJIT difference. Can anyone that has Lua 5.1 installed check the statement for me?
3
u/[deleted] Apr 23 '25 edited Apr 23 '25
2
u/P-39_Airacobra Apr 23 '25
In my experience casually making games with LuaJIT, you should probably pick a specific ecosystem when you start out with a substantially sized project. You could write code compatible for both LuaJIT and Lua 5.4, but it would be a pain and you'd have to limit your feature sets dramatically. LuaJIT has a lot of helpful things that Lua 5.4 does not, and Lua 5.4 has a lot of helpful things that LuaJIT does not.
One sizable difference is the way environments are handled. It's not at all similar between the 2 versions, and libraries which use environments should take care to label which version of Lua they are meant for.
1
u/xoner2 Apr 24 '25
debug.getinfo/getlocal always works with PUC-lua. If jitted by luajit both will return nil.