Support for Lua 5.3

WoAH, CLiCKBAit! No, hold up, just keep reading.

So I’ve been around quite a bit and doing some research, and I’ve found plenty of reasons for and against Roblox ever changing the Lua version they use (although I personally have to say a bit library would be a great addition regardless, but anyways) I recently became interested in Lua 5.3.

With no in depth documentations of the bytecode format and such anywhere around, it became obvious to me that this wasn’t gonna be an easy copy-paste task.

So 2 days of writing code and 2 days of debugging it later, I am pleased to bring you the FiThree Lua VM in Lua. What is this you may ask, and how does it relate to running Lua 5.3 on Roblox at all? Well, it essentially replicates the functionality of lvm.c and lundump.c files in the 5.3 source, but using Lua 5.1+.

There’s not much to say about it, and although it’s primarily targetted towards Lua 5.2, simply shoving in a bit library makes it compatible with 5.1, and it doesn’t even need to be a native bit library.

HOW DO WE USE IT?!
Well, there are a couple things you’ll need. For one, the Lua 5.3 compiler (which is open source on the lua.org website), and an available 32 or 64 bit bitlib, whether implemented in C, Lua, or some other abomination of an add on. Your code needs to be compiled to bytecode on your machine before you can run it, as there currently isn’t(?) a Lua parser for 5.3 also made in Lua.

Once you have your bytecode string, make sure to convert it to the ASCII representation used in Lua strings (ex. \27\76\117\97\83...etc), and that’s what is actually fed into the VM. Using luaF_wrap for simplicity, a function can be created from the bytecode string.

Example:

local vm = require('Source')
local f = vm.luaF_dispatch( -- create the function
	'\27\76\117\97\83...etc', -- function (byte)code
	getfenv(0) -- function env
)

f() -- run the function

This will of course not be as fast as actual Lua, and it does not have the benefits of a native implementation, since it’s running through a layer of virtualization, but it does accomplish the goal of being able to execute Lua 5.3 in a Roblox environment, it’s just a small bit of extra preparation.

The source code for this is open source under my GitHub.
Link: GitHub - Rerumu/FiThree: Lua 5.3 bytecode interpreter, in Lua

More information on the language and things such as the compiler on the official site.
Link: Lua 5.3 readme

Some final notes…

This VM is still in its infancy, and I would appreciate it if any irregularities/differences from real 5.3 you encounter are reported to me.
I am aware of small inconsistencies with integers, but that’s simply because the range of a long long (5.3’s integer subtype) is not fully representable in Lua 5.1’s double floating point number type.
Bugs reported are bugs I know to patch, thank you!

(P.S since I know someone will mention it, “bUT Won’T THiS mAKe iT EAsiEr foR ExPLoiTerS to HAckErmAN mY GamE iF I uSe IT”, no, they’d still need their code to execute in a Roblox Lua thread and own a 5.3 compiler above that)

17 Likes

Very interesting. Thank you for the information

Hmm while I definitely find Lua 5.3 an interesting upgrade and use it myself for everything but Roblox, I do wonder what use case makes it viable to convert for instance project code to bytecode and have it executed by a virtualization layer. Something like camera logic or anything related to renderstep/small intervals seems like it’d be problematic. I have to admit I’d think for most usecases making a Lua 5.1 equivalent of the 5.3 functionality is quicker and easier to maintain.

Nonetheless it’s really cool to read through projects like this, and it strengthens understanding of Lua under the hood in the best way possible, so thank you for that!

Not gonna lie, I doubt this has much of an use case, except for maybe game logic in which you aren’t doing something every other frame or so. It’s still something neat to maybe use for fun, or just if you’re as lazy as me and don’t want to download Lua 5.3’s interpreter on your computer, but already have LuaJIT, wellllllll…

1 Like