C 함수 lua_gettop은 C 함수에 전달 된 매개 변수의 수를 반환합니다. 이 모든 것을 스택에서 읽어서 C 데이터 구조에 저장하거나 Lua 레지스트리 (Registry 및 luaL_ref 참조)에 저장하고 나중에 사용할 수 있도록 참조를 저장해야합니다. 아래 예제 프로그램은 레지스트리 접근 방식을 사용합니다.
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdio.h>
#include <stdlib.h>
/* this function prints the name and extra variables as a demonstration */
static void
TheConstructor(lua_State *L, const char *name, int *registry, int n)
{
int i;
puts(name);
for (i = 0; i < n; ++i) {
lua_rawgeti(L, LUA_REGISTRYINDEX, registry[i]);
puts(lua_tostring(L, -1));
}
free(registry);
}
static int
CreateEntity(lua_State *L)
{
const char *NAME = luaL_checkstring(L, 1);
int *registry;
int i, n;
/* remove the name parameter from the stack */
lua_remove(L, 1);
/* check how many arguments are left */
n = lua_gettop(L);
/* create an array of registry entries */
registry = calloc(n, sizeof (int));
for (i = n; i > 0; --i)
registry[i-1] = luaL_ref(L, LUA_REGISTRYINDEX);
TheContructor(L, NAME, registry, n);
return 0;
}
int
main(int argc, char **argv[])
{
const char TEST_CHUNK[] =
"CreateEntity('foo', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)";
lua_State *L;
L = luaL_newstate();
lua_register(L, "CreateEntity", CreateEntity);
luaL_dostring(L, TEST_CHUNK);
lua_close(L);
return EXIT_SUCCESS;
}
실제로 추종자가 있기 때문에 lua 태그가 추가되었습니다. – BMitch
#lua 추종자를위한 만세! ;) – sbk
C 또는 C++에 있습니까? – Puppy