4

Lets say I have Lua embedded in C++ app. The question may sound strange but I am interested, is there a standard way to disable Lua features from C++ side?

For example I would like to disable new variable creation except for a few defined, like this:

local time = 10
local size = 20
function test()
  return time * size
end

I would like to make Lua VM fail if I define anything else than time, size and test function. Is this possible not hacking the VM itself?

Or for example I would like to disable loops(both for and while statements).

3 Answers 3

5

Restricting the names of global variables that can be created or accessed is easy: just load your script with an environment that has appropriate metamethods for __index and __newindex.

Restricting the names of local variables requires some deep hacking and is probably not worth it, since they cannot affect the host program.

Restricting keywords can't be done out of the box but can be done with some a simple hack:

To remove some keywords, simply add a space to the relevant strings in luaX_tokens in llex.c. These keywords will be available for variable names and the corresponding syntactical construct will not be accessible and will raise an error. For example, to remove loops, disable for, while, and repeat. You may leave in and until, but they won't do anything. If you want to do this dynamically, see this lua-l message.

2
  • @lukas.pukenis, sure. Please share whatever solution you choose.
    – lhf
    Feb 7, 2015 at 0:24
  • I haven't chosen any solution yet as I am not actually implementing it now. I need a pseudo language. I think Lua is perfect but I want to limit the language features(think for a game purposes) so wanted to know how possible it is :) Feb 7, 2015 at 8:20
4

No, there aren't any flags for disabling core features of the language. And there couldn't possible be anything like a compile-time flag for "only allow X variables" in any reasonable way.

Modifying lua to remove for and while might be fairly straightforward but modifying it to not allow creation of variables may not (and I think probably won't) be very easy.

Something like metalua would actually make that sort of thing easier I think.

1
  • I have the same opinion that blocking for example variables should be vital part in the code thus not easily removable. Didn't hear about metalua thanks for it. Feb 6, 2015 at 12:39
3

One option is getting the Lua source code and removing the parts that you don't want. It is not as crazy as it sounds: the code is very readable.

1
  • Thanks for the suggestion. I dont mind diving into Lua code but I would like to know if there's more easier way. Like maybe there are flags or something I have missed while searching for such a thing :). Feb 6, 2015 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.