Lua
Adapted from Wikipedia · Discoverer experience
Lua is a small and powerful programming language designed to be used inside other applications. It was created in 1993 to help developers add custom features to software easily. Because it is written in a simple way, Lua can run on many different types of computers and operating systems.
Programmers like Lua because it is easy to connect to other parts of a program. It works well for adding special features without making the whole program too complicated or slow. This makes Lua very useful for creating games, tools, and many other types of software where flexibility and speed are important.
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 the time, Brazil had rules that made it hard 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 quite 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 to fit 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 on the fly.
Implementation
Lua programs are not read directly by the computer. Instead, they are changed into a special form called bytecode. This bytecode is then 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 before to make things faster.
The Lua virtual machine works in a way that is similar to many computers, using something called a register-based system. This helps it work more efficiently by reducing the number of steps needed.
C API
Lua is designed to be used inside other programs, and it offers a special way for C programs to work with Lua. This special way is called the C API. The API has two parts: the main Lua part and a helper library. It is made to be simple and does not need extra steps to keep track of things in C code, unlike another language called Python.
The C API works like a stack, where you can add and remove different types of data such as numbers. You can also work with tables using this stack. The stack in Lua is a bit different 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, and another is a registry where C programs can save Lua values to use later.
Modules
Besides the basic Lua features, you can also 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, just like they load regular Lua features. There is a system to manage these extra features called LuaRocks, which helps organize and share them. There are also ready-made 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 in making 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 turns into Lua code. Instead of using special words to start and end sections, it uses line breaks and spacing. One place it is used is on the game website Itch.io.
Haxe can turn 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 just 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.
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.
Safekipedia