3
votes

I am in a pure Lua 5.2 environment and I need to execute Lua 5.1 code. This code is arbitrary code from the user, so I can't port to Lua 5.2 in advance. As far as I can see, this would entail reimplementing getfenv/setfenv, changing the _VERSION string, hiding the global _ENV and reimplementing the removed deprecated features. Have I missed anything, or is there a better way to go about doing this? (I can only write code in pure Lua, and I need to be able to execute 5.1 code as this is meant to be a backwards-compatibility thing.)

1
This cannot be done directly if you insist in complete compatibility. For instance, the Lua 5.1 code might include a call to load with Lua 5.1 byte code. If you can do with less, see lua-users.org/wiki/LuaVersionCompatibility ; in particular, github.com/davidm/lua-compat-env. - lhf
@lhf I just want as much compatibility as I can possibly get. Thankfully, I'm in an environment wheee bytecode has been disables anyways for security reasons. EDIT: Thanks for the links. Extremely helpful. - programmedpixel
@lhf You should post those as an answer, as there simply isn't anything better that someone could come up with. - Llamageddon

1 Answers

0
votes

This may be a hard to do in full generality, but there are partial solutions that may work for you.

For a compatibility layer, see lua-compat-env.

For further discussion and other solutions, see Lua Version Compatibility.