1 /* 2 ** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $ 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 <stddef.h> 13 #include <stdio.h> 14 15 #include "lua.h" 16 17 18 19 /* extra error code for 'luaL_load' */ 20 #define LUA_ERRFILE (LUA_ERRERR+1) 21 22 23 typedef struct luaL_Reg { 24 const char *name; 25 lua_CFunction func; 26 } luaL_Reg; 27 28 29 #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 30 31 LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 32 #define luaL_checkversion(L) \ 33 luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 34 35 LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 36 LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 37 LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 38 LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 39 LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 40 size_t *l); 41 LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 42 const char *def, size_t *l); 43 LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 44 LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 45 46 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 47 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 48 lua_Integer def); 49 50 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 51 LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 52 LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 53 54 LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 55 LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 56 LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 57 LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58 59 LUALIB_API void (luaL_where) (lua_State *L, int lvl); 60 LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 61 62 LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 63 const char *const lst[]); 64 65 LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 66 LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 67 68 /* pre-defined references */ 69 #define LUA_NOREF (-2) 70 #define LUA_REFNIL (-1) 71 72 LUALIB_API int (luaL_ref) (lua_State *L, int t); 73 LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 74 75 LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 76 const char *mode); 77 78 #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 79 80 LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 81 const char *name, const char *mode); 82 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 83 84 LUALIB_API lua_State *(luaL_newstate) (void); 85 86 LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 87 88 LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 89 const char *r); 90 91 LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 92 93 LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 94 95 LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 96 const char *msg, int level); 97 98 LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 99 lua_CFunction openf, int glb); 100 101 /* 102 ** =============================================================== 103 ** some useful macros 104 ** =============================================================== 105 */ 106 107 108 #define luaL_newlibtable(L,l) \ 109 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 110 111 #define luaL_newlib(L,l) \ 112 (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 113 114 #define luaL_argcheck(L, cond,arg,extramsg) \ 115 ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) 116 #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 117 #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 118 119 #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 120 121 #define luaL_dofile(L, fn) \ 122 (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 123 124 #define luaL_dostring(L, s) \ 125 (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 126 127 #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 128 129 #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 130 131 #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 132 133 134 /* 135 ** {====================================================== 136 ** Generic Buffer manipulation 137 ** ======================================================= 138 */ 139 140 typedef struct luaL_Buffer { 141 char *b; /* buffer address */ 142 size_t size; /* buffer size */ 143 size_t n; /* number of characters in buffer */ 144 lua_State *L; 145 char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 146 } luaL_Buffer; 147 148 149 #define luaL_addchar(B,c) \ 150 ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 151 ((B)->b[(B)->n++] = (c))) 152 153 #define luaL_addsize(B,s) ((B)->n += (s)) 154 155 LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 156 LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 157 LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 158 LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 159 LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 160 LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 161 LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 162 LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 163 164 #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 165 166 /* }====================================================== */ 167 168 169 170 /* 171 ** {====================================================== 172 ** File handles for IO library 173 ** ======================================================= 174 */ 175 176 /* 177 ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 178 ** initial structure 'luaL_Stream' (it may contain other fields 179 ** after that initial structure). 180 */ 181 182 #define LUA_FILEHANDLE "FILE*" 183 184 185 typedef struct luaL_Stream { 186 FILE *f; /* stream (NULL for incompletely created streams) */ 187 lua_CFunction closef; /* to close stream (NULL for closed streams) */ 188 } luaL_Stream; 189 190 /* }====================================================== */ 191 192 193 194 /* compatibility with old module system */ 195 #if defined(LUA_COMPAT_MODULE) 196 197 LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 198 int sizehint); 199 LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 200 const luaL_Reg *l, int nup); 201 202 #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 203 204 #endif 205 206 207 /* 208 ** {================================================================== 209 ** "Abstraction Layer" for basic report of messages and errors 210 ** =================================================================== 211 */ 212 213 /* print a string */ 214 #if !defined(lua_writestring) 215 #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 216 #endif 217 218 /* print a newline and flush the output */ 219 #if !defined(lua_writeline) 220 #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 221 #endif 222 223 /* print an error message */ 224 #if !defined(lua_writestringerror) 225 #define lua_writestringerror(s,p) \ 226 (fprintf(stderr, (s), (p)), fflush(stderr)) 227 #endif 228 229 /* }================================================================== */ 230 231 232 /* 233 ** {============================================================ 234 ** Compatibility with deprecated conversions 235 ** ============================================================= 236 */ 237 #if defined(LUA_COMPAT_APIINTCASTS) 238 239 #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 240 #define luaL_optunsigned(L,a,d) \ 241 ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 242 243 #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 244 #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 245 246 #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 247 #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 248 249 #endif 250 /* }============================================================ */ 251 252 253 254 #endif 255 256 257