Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/lauxlib.h # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `lua / lauxlib.h` 271 lines ยท 8644 bytes Raw Blame History 1 /* 2 ** $Id: lauxlib.h $ 3 ** Auxiliary functions for building Lua libraries 4 ** See Copyright Notice in lua.h 5 */ 6 7 8 #ifndef lauxlib_h 9 #define lauxlib_h 10 11 12 #include 13 #include 14 15 #include "luaconf.h" 16 #include "lua.h" 17 18 19 /* global table */ 20 #define LUA_GNAME "_G" 21 22 23 typedef struct luaL_Buffer luaL_Buffer; 24 25 26 /* extra error code for 'luaL_loadfilex' */ 27 #define LUA_ERRFILE (LUA_ERRERR+1) 28 29 30 /* key, in the registry, for table of loaded modules */ 31 #define LUA_LOADED_TABLE "_LOADED" 32 33 34 /* key, in the registry, for table of preloaded loaders */ 35 #define LUA_PRELOAD_TABLE "_PRELOAD" 36 37 38 typedef struct luaL_Reg { 39 const char *name; 40 lua_CFunction func; 41 } luaL_Reg; 42 43 44 #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 45 46 LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 47 #define luaL_checkversion(L) \ 48 luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 49 50 LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 51 LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 52 LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 53 LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 54 LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); 55 LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 56 size_t *l); 57 LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 58 const char *def, size_t *l); 59 LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 60 LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 61 62 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 63 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 64 lua_Integer def); 65 66 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 67 LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 68 LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 69 70 LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 71 LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 72 LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 73 LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 74 75 LUALIB_API void (luaL_where) (lua_State *L, int lvl); 76 LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 77 78 LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 79 const char *const lst[]); 80 81 LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 82 LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 83 84 LUALIB_API void *(luaL_alloc) (void *ud, void *ptr, size_t osize, 85 size_t nsize); 86 87 88 /* predefined references */ 89 #define LUA_NOREF (-2) 90 #define LUA_REFNIL (-1) 91 92 LUALIB_API int (luaL_ref) (lua_State *L, int t); 93 LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 94 95 LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 96 const char *mode); 97 98 #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 99 100 LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 101 const char *name, const char *mode); 102 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 103 104 LUALIB_API lua_State *(luaL_newstate) (void); 105 106 LUALIB_API unsigned (luaL_makeseed) (lua_State *L); 107 108 LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 109 110 LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, 111 const char *p, const char *r); 112 LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, 113 const char *p, const char *r); 114 115 LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 116 117 LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 118 119 LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 120 const char *msg, int level); 121 122 LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 123 lua_CFunction openf, int glb); 124 125 /* 126 ** =============================================================== 127 ** some useful macros 128 ** =============================================================== 129 */ 130 131 132 #define luaL_newlibtable(L,l) \ 133 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 134 135 #define luaL_newlib(L,l) \ 136 (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 137 138 #define luaL_argcheck(L, cond,arg,extramsg) \ 139 ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) 140 141 #define luaL_argexpected(L,cond,arg,tname) \ 142 ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) 143 144 #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 145 #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 146 147 #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 148 149 #define luaL_dofile(L, fn) \ 150 (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 151 152 #define luaL_dostring(L, s) \ 153 (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 154 155 #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 156 157 #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 158 159 #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 160 161 162 /* 163 ** Perform arithmetic operations on lua_Integer values with wrap-around 164 ** semantics, as the Lua core does. 165 */ 166 #define luaL_intop(op,v1,v2) \ 167 ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) 168 169 170 /* push the value used to represent failure/error */ 171 #if defined(LUA_FAILISFALSE) 172 #define luaL_pushfail(L) lua_pushboolean(L, 0) 173 #else 174 #define luaL_pushfail(L) lua_pushnil(L) 175 #endif 176 177 178 179 /* 180 ** {====================================================== 181 ** Generic Buffer manipulation 182 ** ======================================================= 183 */ 184 185 struct luaL_Buffer { 186 char *b; /* buffer address */ 187 size_t size; /* buffer size */ 188 size_t n; /* number of characters in buffer */ 189 lua_State *L; 190 union { 191 LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ 192 char b[LUAL_BUFFERSIZE]; /* initial buffer */ 193 } init; 194 }; 195 196 197 #define luaL_bufflen(bf) ((bf)->n) 198 #define luaL_buffaddr(bf) ((bf)->b) 199 200 201 #define luaL_addchar(B,c) \ 202 ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 203 ((B)->b[(B)->n++] = (c))) 204 205 #define luaL_addsize(B,s) ((B)->n += (s)) 206 207 #define luaL_buffsub(B,s) ((B)->n -= (s)) 208 209 LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 210 LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 211 LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 212 LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 213 LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 214 LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 215 LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 216 LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 217 218 #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 219 220 /* }====================================================== */ 221 222 223 224 /* 225 ** {====================================================== 226 ** File handles for IO library 227 ** ======================================================= 228 */ 229 230 /* 231 ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 232 ** initial structure 'luaL_Stream' (it may contain other fields 233 ** after that initial structure). 234 */ 235 236 #define LUA_FILEHANDLE "FILE*" 237 238 239 typedef struct luaL_Stream { 240 FILE *f; /* stream (NULL for incompletely created streams) */ 241 lua_CFunction closef; /* to close stream (NULL for closed streams) */ 242 } luaL_Stream; 243 244 /* }====================================================== */ 245 246 247 /* 248 ** {============================================================ 249 ** Compatibility with deprecated conversions 250 ** ============================================================= 251 */ 252 #if defined(LUA_COMPAT_APIINTCASTS) 253 254 #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 255 #define luaL_optunsigned(L,a,d) \ 256 ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 257 258 #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 259 #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 260 261 #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 262 #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 263 264 #endif 265 /* }============================================================ */ 266 267 268 269 #endif 270 271