rlm@1: /* rlm@1: ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ rlm@1: ** Auxiliary functions for building Lua libraries rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #ifndef lauxlib_h rlm@1: #define lauxlib_h rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: rlm@1: #if defined(LUA_COMPAT_GETN) rlm@1: LUALIB_API int (luaL_getn) (lua_State *L, int t); rlm@1: LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); rlm@1: #else rlm@1: #define luaL_getn(L,i) ((int)lua_objlen(L, i)) rlm@1: #define luaL_setn(L,i,j) ((void)0) /* no op! */ rlm@1: #endif rlm@1: rlm@1: #if defined(LUA_COMPAT_OPENLIB) rlm@1: #define luaI_openlib luaL_openlib rlm@1: #endif rlm@1: rlm@1: rlm@1: /* extra error code for `luaL_load' */ rlm@1: #define LUA_ERRFILE (LUA_ERRERR+1) rlm@1: rlm@1: rlm@1: typedef struct luaL_Reg { rlm@1: const char *name; rlm@1: lua_CFunction func; rlm@1: } luaL_Reg; rlm@1: rlm@1: rlm@1: rlm@1: LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, rlm@1: const luaL_Reg *l, int nup); rlm@1: LUALIB_API void (luaL_register) (lua_State *L, const char *libname, rlm@1: const luaL_Reg *l); rlm@1: LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); rlm@1: LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); rlm@1: LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); rlm@1: LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); rlm@1: LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, rlm@1: size_t *l); rlm@1: LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, rlm@1: const char *def, size_t *l); rlm@1: LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); rlm@1: LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); rlm@1: rlm@1: LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); rlm@1: LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, rlm@1: lua_Integer def); rlm@1: rlm@1: LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); rlm@1: LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); rlm@1: LUALIB_API void (luaL_checkany) (lua_State *L, int narg); rlm@1: rlm@1: LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); rlm@1: LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); rlm@1: rlm@1: LUALIB_API void (luaL_where) (lua_State *L, int lvl); rlm@1: LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); rlm@1: rlm@1: LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, rlm@1: const char *const lst[]); rlm@1: rlm@1: LUALIB_API int (luaL_ref) (lua_State *L, int t); rlm@1: LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); rlm@1: rlm@1: LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); rlm@1: LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, rlm@1: const char *name); rlm@1: LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); rlm@1: rlm@1: LUALIB_API lua_State *(luaL_newstate) (void); rlm@1: rlm@1: rlm@1: LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, rlm@1: const char *r); rlm@1: rlm@1: LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, rlm@1: const char *fname, int szhint); rlm@1: rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** =============================================================== rlm@1: ** some useful macros rlm@1: ** =============================================================== rlm@1: */ rlm@1: rlm@1: #define luaL_argcheck(L, cond,numarg,extramsg) \ rlm@1: ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) rlm@1: #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) rlm@1: #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) rlm@1: #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) rlm@1: #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) rlm@1: #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) rlm@1: #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) rlm@1: rlm@1: #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) rlm@1: rlm@1: #define luaL_dofile(L, fn) \ rlm@1: (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) rlm@1: rlm@1: #define luaL_dostring(L, s) \ rlm@1: (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) rlm@1: rlm@1: #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) rlm@1: rlm@1: #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) rlm@1: rlm@1: /* rlm@1: ** {====================================================== rlm@1: ** Generic Buffer manipulation rlm@1: ** ======================================================= rlm@1: */ rlm@1: rlm@1: rlm@1: rlm@1: typedef struct luaL_Buffer { rlm@1: char *p; /* current position in buffer */ rlm@1: int lvl; /* number of strings in the stack (level) */ rlm@1: lua_State *L; rlm@1: char buffer[LUAL_BUFFERSIZE]; rlm@1: } luaL_Buffer; rlm@1: rlm@1: #define luaL_addchar(B,c) \ rlm@1: ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ rlm@1: (*(B)->p++ = (char)(c))) rlm@1: rlm@1: /* compatibility only */ rlm@1: #define luaL_putchar(B,c) luaL_addchar(B,c) rlm@1: rlm@1: #define luaL_addsize(B,n) ((B)->p += (n)) rlm@1: rlm@1: LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); rlm@1: LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); rlm@1: LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); rlm@1: LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); rlm@1: LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); rlm@1: LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); rlm@1: rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: /* compatibility with ref system */ rlm@1: rlm@1: /* pre-defined references */ rlm@1: #define LUA_NOREF (-2) rlm@1: #define LUA_REFNIL (-1) rlm@1: rlm@1: #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ rlm@1: (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) rlm@1: rlm@1: #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) rlm@1: rlm@1: #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) rlm@1: rlm@1: rlm@1: #define luaL_reg luaL_Reg rlm@1: rlm@1: #endif rlm@1: rlm@1: