What does # mean in Lua?

That is the length operator: The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte). The length of a table t is defined to be any integer index n such that … Read more

Load Lua-files by relative path

There is a way of deducing the “local path” of a file (more concretely, the string that was used to load the file). If you are requiring a file inside lib.foo.bar, you might be doing something like this: require ‘lib.foo.bar’ Then you can get the path to the file as the first element (and only) … Read more

Lua os.execute return value

You can use io.popen() instead. This returns a file handle you can use to read the output of the command. Something like the following may work: local handle = io.popen(command) local result = handle:read(“*a”) handle:close() Note that this will include the trailing newline (if any) that the command emits.

Why is LuaJIT so good?

Mike Pall has talked about this in a few places: http://article.gmane.org/gmane.comp.lang.lua.general/58908 http://lambda-the-ultimate.org/node/3851 http://www.reddit.com/user/mikemike As with every performant system, the answer in the end comes down to two things: algorithms and engineering. LuaJIT uses advanced compilation techniques, and it also has a very finely engineered implementation. For example, when the fancy compilation techniques can’t handle a … Read more

How do you copy a Lua table by value?

Table copy has many potential definitions. It depends on whether you want simple or deep copy, whether you want to copy, share or ignore metatables, etc. There is no single implementation that could satisfy everybody. One approach is to simply create a new table and duplicate all key/value pairs: function table.shallow_copy(t) local t2 = {} … Read more

How can I create a secure Lua sandbox?

You can set the function environment that you run the untrusted code in via setfenv(). Here’s an outline: local env = {ipairs} setfenv(user_script, env) pcall(user_script) The user_script function can only access what is in its environment. So you can then explicitly add in the functions that you want the untrusted code to have access to … Read more