1

I'm having a really odd issue with a Lua implementation into my C++ project. For some reason, it doesn't allow scripts containing operators like #, :, % (and some more, less important) to be executed.

In the results...

I need to use for i = 1, table.getn(tbl) do instead of for i = 1, #tbl do.

I need to use string.gsub(str, [..]) instead of str:gsub([..]).

and so on... This is getting really annoying looking for a work-arounds.

My first thought was the encoding. I have tried with multiple common encodings though and none worked.

Errors I am having:

When using str:gsub([..]) instead of string.gsub(str, [..]):

attempt to index global `str' (a string value)

When using #tbl instead of table.getn(tbl):

unexpected symbol near `#'

What might be the problem? I will appreciate every solution because I am out of ideas.


Specifications:

Lua version:

#define LUA_VERSION "Lua 5.0.3"

C++ 11, FreeBSD 10

Lucas
  • 3,517
  • 13
  • 46
  • 75
  • What about `%` isn't working? – Etan Reisner Feb 23 '15 at 20:46
  • @EtanReisner It simply doesn't work at all. Eg. when doing `a % 2 == 0` I need to work around this (this guy here had similar issue http://stackoverflow.com/questions/9695697/lua-replacement-for-the-operator). Btw. updated my question and added errors I'm getting back. – Lucas Feb 23 '15 at 20:49
  • The `%` *operator* is a relatively recent addition to Lua, added in 5.1. – Keith Thompson Feb 23 '15 at 20:50
  • So there I have my issue. It seems I need to simply update the Lua to have everything working. – Lucas Feb 23 '15 at 20:51
  • Or you can write code that doesn't depend on the newer features. BTW, it's not the *characters* that are the issue it's the operators. (All those characters are supported in comments and string literals, for example.) You might want to update your question to make that clearer. – Keith Thompson Feb 23 '15 at 21:09

1 Answers1

7

The length operator is a lua 5.1 addition. It did not exist in lua 5.0.

Similarly the default string metatable appears to be a lua 5.1 addition.

Compare the lua 5.0 implementation of luaopen_string to the lua 5.1 implementation of luaopen_string.

Similarly (again) the modulo operation is also a 5.1 addition. Compare the Arithmetic Operators section of the 5.0 manual and the `5.1 manual1.

The 5.1 manual section includes what the operator is defined as so you can implement it yourself (or use whatever other definition you need instead).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148