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