C API — 스택 조작 함수
C API — 스택 조작 함수 (The Application Program Interface — Stack-Manipulation Functions)
이 절은 루아 C API의 스택 조작 함수들을 정리한 레퍼런스예요. 스택의 기본 개념은 "[스택 (The Stack)]" 절에 있어요. 모든 함수는 첫 인자로 lua_State *L을 받아요.
스택 크기와 위치
int lua_gettop (lua_State *L)— 스택 맨 위의 인덱스(요소 수)를 반환해요.void lua_settop (lua_State *L, int idx)— 스택 크기를idx로 설정해요. 늘어나면nil로 채우고, 줄어들면 제거해요.idx가 0이면 스택을 완전히 비워요.int lua_absindex (lua_State *L, int idx)— 상대 인덱스(-1등)를 절대 인덱스로 변환해요.
값 밀어 올리기 (Push)
void lua_pushnil(L)—nilpush.void lua_pushboolean(L, int b)— 정수b를 boolean으로 push.void lua_pushnumber(L, lua_Number n)— 실수 push.void lua_pushinteger(L, lua_Integer n)— 정수 push.const char *lua_pushlstring(L, const char *s, size_t len)— 길이가len인 문자열 push, 문자열 포인터 반환.const char *lua_pushstring(L, const char *s)— 널 종료 문자열 push.const char *lua_pushfstring(L, const char *fmt, ...)— 형식화된 문자열 push (루아 자체 printf).const char *lua_pushvfstring(L, const char *fmt, va_list argp)— 가변 인자 버전.void lua_pushvalue(L, int idx)—idx의 값을 복사해 push.void lua_pushcclosure(L, lua_CFunction fn, int n)— 업밸류n개를 가진 C 함수 push.void lua_pushcfunction(L, lua_CFunction f)— C 함수 push (업밸류 0개).void lua_pushlightuserdata(L, void *p)— light userdata push.void lua_pushglobaltable(L)— 전역 테이블 push.void lua_pushthread(L)— 현재 스레드를 push하고, 메인 스레드면 1을 반환.
lua_pushliteral(L, s)는 lua_pushlstring과 strlen이 결합된 매크로예요 (컴파일 타임에 길이를 알 수 있을 때).
스택 요소 이동/교체
void lua_remove(L, int idx)—idx의 요소를 제거하고 위 요소들을 내려요.void lua_insert(L, int idx)— 맨 위 요소를idx위치로 이동.void lua_replace(L, int idx)— 맨 위 요소로idx의 값을 교체 (맨 위는 팝).void lua_copy(L, int fromidx, int toidx)—fromidx값을toidx로 복사.void lua_rotate(L, int idx, int n)—idx부터 맨 위까지를n만큼 회전.int lua_checkstack(L, int extra)— 스택에extra만큼 추가 공간 확보. 성공 시 1.void lua_xmove(lua_State *from, lua_State *to, int n)—from스택에서to스택으로n개 이동.
스택 값 접근/타입 검사 (Access)
int lua_type(L, int idx)—idx값의 타입 상수 반환 (LUA_TNIL등), 유효하지 않으면LUA_TNONE.const char *lua_typename(L, int tp)— 타입 상수의 이름 문자열.int lua_isnumber(L, idx),lua_isstring,lua_iscfunction,lua_isuserdata,lua_istable,lua_isfunction,lua_isthread,lua_isnil,lua_isboolean,lua_isinteger,lua_isyieldable— 타입 검사 (1 또는 0 반환).int lua_rawequal(L, idx1, idx2)— 두 인덱스의 값이 원시적으로 같은지.int lua_compare(L, idx1, idx2, op)— 두 값을 연산자(LUA_OPEQ등)로 비교.
값 읽기/변환 (Convert)
lua_Number lua_tonumber(L, idx),lua_Integer lua_tointeger(L, idx)— 숫자로 변환.lua_toboolean(L, idx)— boolean으로 (0/1).const char *lua_tolstring(L, idx, size_t *len)— 문자열로 (길이 함께), 숫자도 변환.const char *lua_tostring(L, idx)— 문자열로 (길이 무시).lua_CFunction lua_tocfunction(L, idx)— C 함수 포인터.void *lua_touserdata(L, idx)— userdata 포인터.lua_State *lua_tothread(L, idx)— 스레드.const void *lua_topointer(L, idx)— 값의 포인터 표현.lua_Unsigned lua_tointegerx(L, idx, int *isnum)— 변환 가능 여부를isnum으로.
산술/비교/연결/길이 연산
void lua_arith(L, int op)— 스택 맨 위 두 값을 연산해서 push.void lua_concat(L, int n)— 맨 위n개를 연결해 push.void lua_len(L, int idx)—idx값의 길이를 push.int lua_stringtonumber(L, const char *s)— 문자열을 숫자로 변환해 push.
출처: C API — 스택 조작 함수 (The Application Program Interface — Stack-Manipulation Functions)
본문
스택 규율과 자동 정리
C 함수가 반환하면 루아는 스택을 호출 전 상태로 자동으로 되돌려요 (반환값은 맨 위에 남김). 따라서 C 함수는 스택을 어지럽혀도 안전하지만, 반환값의 수는 C 함수의 반환 정수로 명시해야 해요.
인덱스 규칙 요약
- 1 = 맨 아래,
lua_gettop(L)= 맨 위. -1= 맨 위,-gettop= 맨 아래.LUA_REGISTRYINDEX= 특별한 가상 인덱스(레지스트리).- 유효하지 않은 인덱스에 쓰기 함수를 호출하면 동작이 정의되지 않거나 오류가 날 수 있어요.