api.md 5.4 KB


title: "Api" date: 2023-11-04T14:29:51+07:00

draft: true

类型

lua_Number
lua_Integer

luaL_Buffer

函数

lua_State* luaL_newstate();

void luaL_openlibs(lua_State*L);
int luaL_loadbuffer(lua_State*L, const char* buf, int len, const char* name);
int luaL_newmetatable (lua_State *L, const char *tname);
int luaL_optint (lua_State *L, int narg, int d);
lua_Integer luaL_optinteger (lua_State *L,
                             int narg,
                             lua_Integer d);
long luaL_optlong (lua_State *L, int narg, long d);
const char *luaL_optlstring (lua_State *L,
                             int narg,
                             const char *d,
                             size_t *l);
const char *luaL_optstring (lua_State *L,
                            int narg,
                            const char *d);

lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number d);

// 
void luaL_checkstack (lua_State *L, int sz, const char *msg);
const char *luaL_checkstring (lua_State *L, int narg);
const char *luaL_checklstring (lua_State *L, int narg, size_t *l);
lua_Number luaL_checknumber (lua_State *L, int narg);
int luaL_checkoption (lua_State *L,
                      int narg,
                      const char *def,
                      const char *const lst[]);
void *luaL_checkudata (lua_State *L, int narg, const char *tname);
// 检查函数参数 narg 是否是类型 t
void luaL_checktype(lua_State*L, int narg, int t);

int lua_load(lua_State*L, lua_Reader reader, void*data, const char* chunkname);
void lua_call(lua_State*L, int nargs, int nresults);
int lua_pcall(lua_State*L, int nargs, int nresults, int errfunc);

int lua_status (lua_State *L);
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
void lua_close(lua_State*L);

/* push 系列
	栈中索引是以 1 开始。第一个压入栈中元素为 1. -1 表示栈顶
*/
void lua_pushnil(lua_State*L);
void lua_pushboolean(lua_State*L, int bool);
void lua_pushnumber(lua_State*L, lua_Number n);
void lua_pushinteger(lua_State*L, lua_Integer n);
void lua_pushlstring(lua_State*L, const char*s, size_t len);
void lua_pushstring(lua_State*L, const char*s);
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
int lua_checkstack(lua_State*L, int sz);

/*返回值
LUA_TNONE
LUA_TNIL
LUA_TNUMBER
LUA_TBOOLEAN
LUA_TSTRING
LUA_TTABLE
LUA_TFUNCTION
LUA_USERDATA
LUA_TTHREAD
LUA_TLIGHTUSERDATA
*/
int lua_type(lua_State*L, int index);
// 转换 type 为字符串
const char* lua_typename(lua_State*L, int type)

/* lua_is***
	兼容类型返回 1, 否则返回 0。(lua_isboolean例外)
*/
int lua_isnumber(lua_State*L, int idx);
int lua_isboolean(lua_State*L, int idx);
int lua_isfunction(lua_State*L, int idx);
int lua_istable(lua_State*L, int idx);
int lua_isstring(lua_State*L, int idx);
int lua_isnil(lua_State*L, int idx);
int lua_iscfunction(lua_State*L, int idx);

/** lua_to***
*/
int lua_toboolean(lua_State*L, int idx);
lua_Number lua_tonumber(lua_State*L, int idx);
lua_Integer lua_tointeger(lua_State*L, int idx);
const char* lua_tolstring(lua_State*L, int idx, size_t* len);
const char* lua_tostring(lua_State*L, int idx);
lua_CFunction lua_tocfunction (lua_State *L, int index);
const void *lua_topointer (lua_State *L, int index);
lua_State *lua_tothread (lua_State *L, int index);
void *lua_touserdata (lua_State *L, int index);
size_t lua_objlen(lua_State*L, int idx);

/** stack
 */
int lua_gettop(lua_State*L);
void lua_settop(lua_State*L, int idx);
// 指定索引入栈
void lua_pushvalue(lua_State*L, int idx);
void lua_remove(lua_State*L, int idx);
// 栈顶元素插入
void lua_insert(lua_State*L, int idx);
// 弹出栈顶,并设置到索引
void lua_replace(lua_State*L, int idx);
void lua_pop(lua_State*L, int num);

lua_CFunction lua_atpanic(lua_State*L, lua_CFunction panicf);

int lua_error(lua_State*L);
/**
 what:
 LUA_GCSTOP
 LUA_GCCRESTART
 LUA_GCCOLLECT
 LUA_GCCOUNT
 LUA_GCCOUNTB
 LUA_GCSTOP
 LUA_GCSETPAAUSE
 LUA_GCSETSTEPMUL
 */
int lua_gc(lua_State*L, int what, int data);

void lua_getfenv (lua_State *L, int index);
int lua_setfenv (lua_State *L, int index);
void lua_getglobal (lua_State *L, const char *name);
void lua_setglobal (lua_State *L, const char *name);

// table
void lua_getfield (lua_State *L, int index, const char *k);
void lua_setfield (lua_State *L, int index, const char *k);
int lua_getmetatable (lua_State *L, int index);
int lua_setmetatable (lua_State *L, int index);
void lua_gettable (lua_State *L, int index);
void lua_settable (lua_State *L, int index);


int lua_rawequal (lua_State *L, int index1, int index2);
// 
void lua_rawget (lua_State *L, int index);

void lua_rawgeti (lua_State *L, int index, int n);

void lua_rawset (lua_State *L, int index);

void lua_rawseti (lua_State *L, int index, int n);



void luaL_buffinit (lua_State *L, luaL_Buffer *B);
void luaL_addchar (luaL_Buffer *B, char c);
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
void luaL_addsize (luaL_Buffer *B, size_t n);
void luaL_addstring (luaL_Buffer *B, const char *s);
void luaL_addvalue (luaL_Buffer *B);
void luaL_pushresult (luaL_Buffer *B);

C 中保存状态

  • 全局变量(注册表)
  • 函数环境(环境)
  • upvalue(closure)

元表和元方法

算术类

  • __add
  • __sub
  • __mul
  • __div
  • __unm (相反数)
  • __mod
  • __pow
  • __concat

关系类

  • __eq
  • __lt
  • __le

库定义

  • __tostring
  • __metatable

table 访问

  • __index 访问不存在的 key
  • __newindex 对不存在的 key 赋值