lua - deptno/deptno.github.io GitHub Wiki
lua
lua 5.1 -> luajit -> 5.x
neovim μ lua 5.1 μ μ€ν¬λ¦½νΈ μμ§μΌλ‘ λ΄μ₯νκ³ μλ€. μΆνμ neovim μμ λ΄μ₯ μ€ν¬λ¦½νΈ λ²μ μ΄ μ λ°μ΄νΈλλ κ²½μ° 5.1 μμ νΈνμ±μ 보μ₯νλ€.
cli
lua # μΈν°νλ¦¬ν° μμ
lua [FILENAME] # νμΌμ μ€ν
lua -i [FILENAME] # μΈν°νλ¦¬ν° μμκ³Ό ν¨κ² ν΄λΉ νμΌμ λΆλ¬μμ μ€ν `dofile` κ³Ό κ°μ
lua -e "LUA_CODE" # μ½λ μ€ν
lua -llib -e "LUA_CODE" # libμ μν¬νΈν ν `LUA_CODE` μ€ν
lua -llib -e -i "LUA_CODE" # libμ μν¬νΈν ν `LUA_CODE` μ€ν νμ μΈν°νλ¦¬ν° λͺ¨λλ‘ μ§μ
arg
arg
λ μ μ λ°°μ΄λ‘ cli
λ₯Ό ν΅ν΄μ μ€νν λ μ€ν¬λ¦½νΈ νμΌλͺ
μ κΈ°μ€μΌλ‘ 0
μΈλ±μ€κ° μ ν΄μ§κ³ κ·Έ μ΄μ μ -
μΈλ±μ€κ° λλ€
lua -e "print('hello')" script a b
arg[-3]
"lua"arg[-2]
"-e"arg[-1]
"print('hello')"arg[ 0]
"script"arg[ 1]
"a"arg[ 2]
"b"
νκ²½λ³μ
INIT_LUA
νκ²½ λ³μμ λ΄μ©μ λ°λ₯Έ μ€ν λΆλ₯
@
λ‘ μμνλ κ²½μ°μλ νμΌλͺ μΌλ‘ μΈμ§νκ³ ν΄λΉ νμΌμ μν¬νΈ@
κ° μλ λ€λ₯Έ κ²μΌλ‘ μμνλ κ²½μ°μλ 루μ μ½λλ₯Ό μ€ν,dofile
κ³Ό κ°μ μν μ΄λΌκ³ μκ°λ¨INIT_LUA_5_2
- λ²μ λͺ μλ°λΌμ suffix κ° λ¬λΌμ§λλ° 5.1 μ΄λ luajit μμλ μ¬μ©μ΄ κ°λ₯νμ§λ νμΈμ΄ νμ
INIT_LUA
INIT_LUA_5_2
κ° μ½ν νμλ μ½μ΄μ§λ νμΈ νμ
λ¬Έλ²
import
dofile("FILENAME") -- eval
require("FILENAME") -- import
μ μ
_CAPITAL
_
λ°λ‘ μμλλ λλ¬Έμ μ μλ³μλ μμ½μ΄λ‘ μ¬μ©
μ κ·ννμ
lua λ μ κ·ννμμ fully ꡬννμ§ μλλ€
escape
- ν¨ν΄ λ§€μΉ λ΄μμλ μ΄μ λ§€μΉ κ²°κ³Όλ₯Ό μ¬μ©ν μ μμ
(['"])hello%1
'
νΉμ"
μ΄ λ§€μΉλμμλ%1
μ μ΄μ μ λ§€μΉλ κ²°κ³Όμ κ°μ λ¬Έμμ΄λ‘ ν½μ€λ¨- μ±κ³΅,
"hello"
,'hello'
- μ€ν¨,
"hello'
,'hello"
- μ±κ³΅,
- string pattern λ§€μΉμλ μ κ·ννμμ
{7, 40}
μ ν΄λΉ νλ κ΅¬λ¬Έμ΄ μλ€ diary:2023-10-12- git sha1 μ λ§€μΉνκΈ° μν΄ μ¬μ©νλ€.
local is_git_hash = function(hash)
local char = '^[0-9a-fA-F]$'
local matched = string.match(hash, string.format('%s*', char:rep(8)))
return matched ~= nil and #matched <= 40 and matched
end
ννλ‘ κ΅¬ννλ€. 7κΈμμ΄μ 40κΈμ μ΄νμΌλ λ§€μΉλλ€, λ§ν¬λ μ½λλ line matching μ κ°μ ν΄μ ^$
κ° μ κ±°λ λ²μ μ΄λ€
vs javascript
νμ νμΈ
- lua: type(λ³μ)
- js: typeof [λ³μ]
true/false
- lua:
false
,nil
λ§μfalse
λ‘ κ°μ£Όνλ€ - js:
undefined
,null
,""
,0
,NaN
λ± λ λ§μ μν©μfalse
λ‘ κ°μ£Όνλ€
regexp|μ κ·ννμ
- lua:
%
λ₯Ό escape λ¬Έμλ‘ μ¬μ© - js:
\
λ₯Ό escape λ¬Έμλ‘ μ¬μ©
lexical scoping
function a() {
v = 2
}
let v = 1
console.log(v) // 1
a()
console.log(v) // 2
function a()
v = 2
end
local v = 1
print(v) // 1
a()
print(v) // 1
μλ°μ€ν¬λ¦½νΈμλ λ€λ₯΄κ² ν΄λ‘μ μ€μ½νμ΄ μ‘°κΈ λ€λ₯΄λ€
function a
μ μ μΈ μμ μ΄local v
λ³΄λ€ μ΄μ μ΄λΌlocal v
μ μ κ·Όν μ μμ- ν¨μλ΄μμ μ κ·Όνλ €λ λ³μλ μ μλ³μμ΄κ±°λ νμ¬ ν¨μμ μ μ μ΄μ μ μ μΈλμ΄μΌνλ€.
local a
λ§ μ μΈνκ³a = function {body} end
λ±μ μ μλ λ¦κ² μλλλ€. λμΆ© μ€μ½νμ΄ ν¨μ νΈμΆ μμ μ΄ μλλΌ μ μ μμ μ λ§λ€μ΄μ§λ€κ³ μκ°νλ©΄ λ λ―νλ€
garbage collection
local
μ λ©λͺ¨λ¦¬ ν λΉμ μμ±νμ§ μμ§λ§ string μ μμ±νλ€. λμΌν string μ μ°μμΌλ‘μ¬μ©νλ κ²μ λ©λͺ¨λ¦¬λ₯Ό λλΉνμ§μλλ€.- κ°λ³μΈμ ν¨μλ ν¨μκ° νΈμΆλ λ μΈμ ν μ΄λΈμ μμ±νλ€
collectgarbage("count") * 1024
λ‘ νμΈ
coroutines
- coroutine.create λ‘ coroutine.yeild λ°νμμ μ€λ¨μ μ μ μ₯νκ³ μΆν coroutine.resume μ ν΅ν΄ κ³μ μ€νν μ μλ ν¨μλ₯Ό λ°ννλ€.
- coroutine.yeild λ₯Ό ν΅ν΄ λ°ν νλ μμ μ΄ λ€μ coroutine.resume μ ν΅ν΄ μΈμλ₯Ό μ λ¬νλ μμ μΌλ‘ μ΄μ΄μ§λ€
μ±
doc
- https://github.com/defold/doc/blob/master/docs/ko/manuals/lua.md
- https://wariua.github.io/lua-manual/