Lua
lastmod 2025-09-25created 2014-08-1572 edits
|
|
The Lua programming language is a lightweight imperative/procedural language, designed primarily as a scripting language that can be used as an extension language. Lua is the Portuguese word for "moon".
Category
- Lua:Binding
- LuaJIT
- LuaRocks: Lua package manager
- LuaDist: Designed to create self-contained, standalone directories using Lua and modules (dist). Everything in LuaDist is CMake-based, which means it can be easily compiled with any compiler / IDE supported by CMake. LuaDist also has an extensive repository containing Lua modules and many C dependencies / libraries, so you can create a truly self-contained Lua "distribution".
Libraries
- luabind: A library that makes C++ functions and classes usable from Lua.
- LuaTinker: LUA to C++ Binding Library
- luaj: Lightweight, fast, Java-centric Lua interpreter.
- tolua: A solution that generates binding code by creating a separate template.
- CPB: A solution for accessing C++ functions and variables from Lua.
- LuaBridge: A lightweight, dependency-free library for binding Lua to C++
- MoonScript: A language that compiles to Lua.
- LuaRocks: package manager for Lua modules.
- Torch: A scientific computing framework for LuaJIT
- sol2
- luacheck: A tool for linting and static analysis of Lua code.
Micro frameworks
- Lapis: A web framework for Lua/MoonScript.
MVC frameworks
- Sailor: https://github.com/Etiene/sailor
- Orbit (GPL): https://github.com/keplerproject/orbit
Event-driven frameworks
Web server
- lua-http: HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
CMS, Wikis
- Ophal: https://github.com/ophal
- LuaPress: https://github.com/Fizzadar/Luapress
- Sputnik: https://github.com/yuri/sputnik/
GUI
IDE
Language
- Lunatic Python (Lua, Python)
- Terra
- Nelua
- Fennel - A programming language that runs on the Lua runtime
Syntax
The reserved words of LUA are as follows.
and break do else elseif end false for function goto if in
local nil not or repeat return then true until while
The list of tokens is as follows.
Example
Ansi Terminal Colors
Include another lua file
Assume the following file exists.
-- scriptTest.lua (in your scripts directory)
local M = {}
local function testFunction()
print("Test function called")
end
M.testFunction = testFunction
return M
Use it as follows.
Print all global variables
Memory module
int my_loader(lua_State* state) {
// get the module name
const char* name = lua_tostring(state);
// find if you have such module loaded
if (mymodules.find(name) != mymodules.end())
{
luaL_loadbuffer(state, buffer, size, name);
// the chunk is now at the top of the stack
return 1;
}
// didn't find anything
return 0;
}
// When you load the lua state, insert this into package.loaders
Lua path
int setLuaPath( lua_State* L, const char* path )
{
lua_getglobal( L, "package" );
lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
std::string cur_path = lua_tostring( L, -1 ); // grab path string from top of stack
cur_path.append( ";" ); // do your path magic here
cur_path.append( path );
lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
lua_pushstring( L, cur_path.c_str() ); // push the new one
lua_setfield( L, -2, "path" ); // set the field "path" in table at -2 with value at top of stack
lua_pop( L, 1 ); // get rid of package table from top of stack
return 0; // all done!
}
Default module and calss
-- 'point.lua' file
local Sqrt = math.sqrt
local M={}
function M.New(x,y)
local pt = {x=x or 0, y = y or 0} -- 먼저 멤버변수를 테이블로 새로 생성
function pt:GetLength() -- 첫 번째 멤버함수를 pt안에서 생성
return Sqrt(self.x*self.x + self.y*self.y)
end
function pt:DistTo(pt2) -- 두 번째 멤버함수를 pt 안에서 생성
local dx = self.x - pt2.x
local dy = self.y - pt2.y
return Sqrt(dx*dx + dy*dy)
end
return pt -- 생성된 테이블(인스턴스)를 반환한다.
end
return M
If you want to use the file above:
local CPoint = require "point" -- 외부모듈을 읽어들인다.
local pt1 = CPoint.New(10,20) -- 첫 번째 인스턴스 생성
local pt2 = CPoint.New(30,40) -- 두 번째 인스턴스 생성
print("length of pt1:".. pt1:GetLength() ) -- 길이 22.36이 찍힘
print("length of pt2:".. pt2:GetLength() ) -- 길이 50이 찍힘
print("distance:".. pt1:DistTo(pt2) ) -- 두 점의 거리 28.28이 찍힌다
File IO : Implicit File Descriptors
Implicit file descriptors use the standard input/ output modes or using a single input and single output file. A sample of using implicit file descriptors is shown below.
-- Opens a file in read
file = io.open("test.lua", "r")
-- sets the default input file as test.lua
io.input(file)
-- prints the first line of the file
print(io.read())
-- closes the open file
io.close(file)
-- Opens a file in append mode
file = io.open("test.lua", "a")
-- sets the default output file as test.lua
io.output(file)
-- appends a word test to the last line of the file
io.write("-- End of the test.lua file")
-- closes the open file
io.close(file)
Troubleshooting
Undefined LUA_GLOBALSINDEX
LUA_GLOBALSINDEX cannot be used.
According to the Lua 5.2 manual, the API change is as follows.
Pseudoindex LUA_GLOBALSINDEX was removed. You must get the global environment from the registry (see §4.5).
-
LUA_REGISTRYINDEX -
LUA_RIDX_MAINTHREAD: At this index the registry has the main thread of the state. (The main thread is the one created together with the state.) -
LUA_RIDX_GLOBALS: At this index the registry has the global environment.
Local Download
- LUA 5.3.2 (2015-11-25)
- Lua-5.3.2.tar.gz (md5: 33278c2ab5ee3c1a875be8d55c1ca2a1)
- Lua 5.3 Reference Manual (Korean)
- https://wariua.github.io/lua-manual/5.3/manual.html
-
Lua_5.3_Reference_Manual_-_ko.pdf
Troubleshooting
readline.h: No search file or directory
Sometimes the readline library cannot be found.
lua.c:80:31: fatal error: readline/readline.h: No such file or directory
#include <readline/readline.h>
On Ubuntu, install it as follows.
See also
- Python
- Codea
- Codea Reference: Also has a well-organized summary of Lua-related API usage.
Favorite site
- LUA web site
- Wikipedia (en) LUA description
- Lua 5.3 Reference Manual
- [Recommended] Programming in Lua (first edition)
Tutorials
- Lua basics
- ARM Lua
- [Recommended] Learn Lua in 15 Minutes
- Kor ver: Learn Lua in 15 Minutes (Korean) 2
Guide
- Building the Lua library for Android
- Lua basic programming 01 5
- Lua basic programming 02 6
- Using Lua with C++ - A short tutorial 7
- Preventing name collisions in Lua modules (3/n)
- Lua stack-related functions.
- luaL_newthread() - How to avoid gc~
- (Lua) Tables - CodeShared
Example
- World of Warcraft LUA APIs
- Gist - kizzx2/fun.cpp - Illustrative C++ Lua binding example/tutorial
- Gist - Youka/lua_myobject.cpp - Example of Lua in C++ and userdata objects
- lua-users wiki: User Data With Pointer Example
- lua-users wiki: Binding With Metatable And Closures
- lua-users wiki: Library With Userdata Example
- lua-users wiki: Peter Shook
- lua-users wiki: User Data Example
- lua-users wiki: Sample Code
Lua GUI
References
- C++
- COCO API
- Computer Language
- Conky
- Dwm
- Fennel
- Game Engine
- Game Programming
- GameKit
- GDScript
- Glue code
- IntelliJ IDEA
- Lite XL
- LOVE
- Luabind
- LuaJIT
- LuaL register
- LuaTinker
- Luau
- Neovim:Lua
- Pragtical
- Programming
- Prototype-based programming
- Python
- Python:Libraries
- Scripting Language
- Tbag
- Telegram:Bot
- Text editor
- Text-based user interface
- Torch
- Turbo.lua
- Web Application Server
- Web Server