Safekipedia

Lua

Adapted from Wikipedia · Adventurer experience

Logo of the programming language Lua

Lua is a small and powerful programming language. It was made in 1993 to help add special features to software easily. Because it is simple, Lua can work on many kinds of computers and operating systems.

People who write programs like Lua because it is easy to connect to other parts of a program. It is good for adding special features without making the whole program too hard to use or slow. Lua is very useful for making games, tools, and many other kinds of software.

History

Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes at the Pontifical Catholic University of Rio de Janeiro in Brazil.

At that time, Brazil had rules that made it difficult to buy software from other countries. Because of this, the team at Tecgraf, where they worked, decided to build their own tools. They needed a flexible language for their projects, and the existing languages didn’t fit what they wanted. So they created Lua, a simple and easy-to-use language that could be added to other programs. They made sure Lua would work on many different types of computers and would be easy for everyone to use, even those who weren’t expert programmers.

Features

Lua is a flexible language that can be used in many ways. It has simple rules and can be changed for different needs. Lua is small and easy to add to other programs.

Lua works well with other programming ideas. It can handle many types of data, like numbers, words, and true/false values. One of its special features is the "table," which acts like a list or a group of pairs. Lua also has tools to help manage memory and create new functions.

Implementation

Lua programs are not read directly by computers. Instead, they are changed into a special form called bytecode. This bytecode is used by a part of the computer called a virtual machine. This process usually happens while the program is running, but it can also be done earlier to make things faster.

The Lua virtual machine works in a way that is similar to many computers, using a register-based system. This helps it work more efficiently.

C API

Lua is made to be used inside other programs. It has a special way for C programs to work with Lua, called the C API. The C API has two parts: the main Lua part and a helper library. It is simple and easy to use.

The C API works like a stack. You can add and remove different types of data, like numbers, to the stack. You can also work with tables using this stack. The stack in Lua is special because you can look at places in it directly. For example, the top item is called -1, and items from the bottom are called by positive numbers. When you want to use a Lua function from C, you put the needed information on the stack and then call the function. When writing a C function that Lua can use, you get the information from the stack.

Here’s a simple example of calling a Lua function from C:

#include 
#include         // Lua main library (lua_*)
#include     // Lua auxiliary library (luaL_*)

int main(void)
{
    // create a Lua state
    lua_State *L = luaL_newstate();

    // load and execute a string
    if (luaL_dostring(L, "function foo (x,y) return x+y end")) {
        lua_close(L);
        return -1;
    }

    // push value of global "foo" (the function defined above)
    // to the stack, followed by integers 5 and 3
    lua_getglobal(L, "foo");
    lua_pushinteger(L, 5);
    lua_pushinteger(L, 3);
    lua_call(L, 2, 1); // call a function with two arguments and one return value
    printf("Result: %d\n", lua_tointeger(L, -1)); // print integer value of item at stack top
    lua_pop(L, 1); // return stack to original state
    lua_close(L); // close Lua state
    return 0;
}

Running this example gives:

$ cc -o example example.c -llua
$ ./example
Result: 8

The C API also includes special tables in the Lua stack. One is the globals table, which holds important values. There is also a registry where C programs can save Lua values to use later.

Modules

Besides the basic Lua features, you can create extra features using the Lua API. These extra features are small pieces of code that add new abilities to Lua. Lua scripts can load these extra features using a command like require. There is a system to manage these extra features called LuaRocks, which helps organize and share them. There are also connections between Lua and many other popular programming languages. For C++, there are several ways to connect using templates or automatic tools.

Applications

Main article: List of applications using Lua

Lua is often used to make video games because it is easy to add to games, works quickly, and is simple to learn. Popular games that use Lua include Roblox, Garry's Mod, World of Warcraft, Payday 2, Project Zomboid, Phantasy Star Online 2, Dota 2, Crysis, and many more. Besides games, Lua is also used in other software like Adobe Lightroom, Moho, iClone, Aerospike, and system tools in FreeBSD and NetBSD. It is also used on websites that use MediaWiki with the Scribunto extension.

Lua is popular for making games easier to change and expand. It is used in many tools and programs such as LuaTeX, Redis, ScyllaDB, Neovim, a text editor, Nginx, a web server, Wireshark, a network tool, and Pure Data, a music and art tool.

Derived languages

Languages that compile to Lua

MoonScript is a flexible scripting language that changes into Lua code. Instead of using special words to start and end sections, it uses line breaks and spacing. You can find it used on the game website Itch.io.

Haxe can change into Lua code for different versions of Lua and LuaJIT.

Fennel is a type of Lisp language that creates Lua code. Urn is another Lisp language built using Lua. Amulet is similar to ML and creates Lua files. LunarML is a Standard ML compiler that makes Lua or JavaScript files.

Dialects

LuaJIT is a fast version of Lua 5.1 that compiles code when it runs.

Luau was made by Roblox Corporation. It is based on Lua 5.1 but adds new features and focuses on speed. It also has better safety checks for running code from unknown sources.

Ravi is a Lua 5.3 language with fast running and optional type checking. Shine is a version of LuaJIT with extra features like a module system. Glua is a changed version of Lua used in the game Garry’s Mod.

Teal is a Lua language written in Lua that checks types while it runs. PICO-8, a pretend video game system, uses a small part of Lua called PICO-8 Lua. Pluto is a version of Lua 5.4 with extra features but still works like regular Lua.

The Lua community also adds extra features to the main Lua system.

Images

A screenshot showing a section of the Coordinates module in the Lua programming language.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on Lua, available under CC BY-SA 4.0.

Images from Wikimedia Commons. Tap any image to view credits and license.