Lua is an [[MIT License]] licensed programming language written in [[3. Reference/Software/Programming Languages/C|C]].
- [Website](https://www.lua.org)
- [Source](https://www.lua.org/source/) (official site, browseable and with a download, no repo)
- [Source](https://github.com/lua/lua) (GitHub mirror, updated irregularly)
- [Documentation](https://www.lua.org/docs.html)
- [Wikipedia](https://en.wikipedia.org/wiki/Lua_(programming_language))
> QUOTE
# Notability
# Philosophy
## Closed Development
Despite its permissive [[Open Source]] license, Lua has a closed development cycle. The core team only reluctantly interacts with the outside world and the only way to submit patches is via the mailing list (as of [[2023-12-14]])!
The official source code repo is private. Their private repo is irregularly mirrored to GitHub and the Lua official website.
# OS Support
# Features
- Tables
- 1-indexing
- Embeddable
# Examples
```lua
a = { 1, 2, 3, 4 }
#a == 4 -- length operator
a[#a] == 4
```
# Tips
## Checking If a Library is Available
If you use `require`, you get an error at runtime and can't do anything about it. This is bad. However, Lua supports an alternative function calling strategy which returns multiple values instead of an error.
```lua
status, library = pcall(require, "library_name")
```
# Resources
```cardlink
url: https://devhints.io/lua
title: "Lua cheatsheet"
description: "The one-page guide to Lua: usage, examples, links, snippets, and more."
host: devhints.io
image: https://assets.devhints.io/previews/lua.jpg
```
## Implementations
### LuaJIT
There are a few edge cases[^1] where it falls over, but in 99% of the cases you care about, LuaJIT is one of the fasted interpreted languages available.
#### GC Thrashing
If you create and destroy a lot of small arrays very quickly, your program may spend more time [[Garbage Collection|collecting garbage]] than running your code. In particularly pathological cases, this can slow code down by 100x.[^1]
The solution is to ensure that LuaJIT is using Lua's own tables instead of calling out to [[C]] using [[FFI]]. It does this because [[C]] arrays are faster to use than tables, but unfortunately slower to FFI, instantiate, and GC.
## Libraries
### File System
```cardlink
url: https://github.com/lunarmodules/luafilesystem
title: "GitHub - lunarmodules/luafilesystem: LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution."
description: "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution. - lunarmodules/luafilesystem"
host: github.com
favicon: https://github.githubassets.com/favicons/favicon.svg
image: https://opengraph.githubassets.com/71f2dc86fb5432bd03c3a1ca3a19f307407dfa73393f912f5c24eecf765df307/lunarmodules/luafilesystem
```
### Shell
```cardlink
url: https://git.sr.ht/~mna/luashell
title: "~mna/luashell - sourcehut git"
description: "A small Lua module to help write what would be shell scripts in Lua."
host: git.sr.ht
favicon: https://git.sr.ht/static/logo.svg
```
```cardlink
url: https://github.com/zserge/luash
title: "GitHub - zserge/luash: Tiny lua module to write shell scripts with lua (inspired by Python's sh module)"
description: "Tiny lua module to write shell scripts with lua (inspired by Python's sh module) - zserge/luash"
host: github.com
favicon: https://github.githubassets.com/favicons/favicon.svg
image: https://opengraph.githubassets.com/139d24cf5a066f57754043b43cf8e0288ce6809c6de63ba5fd486f9966dbbdc4/zserge/luash
```
```cardlink
url: https://github.com/parke/lush
title: "GitHub - parke/lush: Experimental Lua module for writing POSIX shell script style programs in Lua."
description: "Experimental Lua module for writing POSIX shell script style programs in Lua. - parke/lush"
host: github.com
favicon: https://github.githubassets.com/favicons/favicon.svg
image: https://opengraph.githubassets.com/34c1a8de7fc3d01432726effcf0ecdc7aa8cfb0a055022accf3d93846faf18ca/parke/lush
```
## Generic
Libraries with a lot of general helpful functions.
```cardlink
url: https://github.com/lunarmodules/Penlight
title: "GitHub - lunarmodules/Penlight: A set of pure Lua libraries focusing on input data handling (such as reading configuration files), functional programming (such as map, reduce, placeholder expressions,etc), and OS path management. Much of the functionality is inspired by the Python standard libraries."
description: "A set of pure Lua libraries focusing on input data handling (such as reading configuration files), functional programming (such as map, reduce, placeholder expressions,etc), and OS path management...."
host: github.com
favicon: https://github.githubassets.com/favicons/favicon.svg
image: https://opengraph.githubassets.com/60a0aab2d3cf5975cfe6238eb3da5351a2a7f9128d7e05563e1a748edf9b80dc/lunarmodules/Penlight
```
## Debugging and Analysis
Inspect supports metatables and outputs structured, human readable text.
```cardlink
url: https://github.com/kikito/inspect.lua
title: "GitHub - kikito/inspect.lua: Human-readable representation of Lua tables"
description: "Human-readable representation of Lua tables. Contribute to kikito/inspect.lua development by creating an account on GitHub."
host: github.com
favicon: https://github.githubassets.com/favicons/favicon.svg
image: https://opengraph.githubassets.com/0f840b5a925d2302e1aa87bff4c4a54e03e97f26bd1cbfbf989a6ec90427c12c/kikito/inspect.lua
```
- https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
- https://github.com/pkulchenko/serpent
# References
[^1]: https://vult-dsp.github.io/vult/blog/performance-arrays/