18e3e3a7aSWarner Losh /* 20495ed39SKyle Evans ** $Id: lauxlib.h $ 38e3e3a7aSWarner Losh ** Auxiliary functions for building Lua libraries 48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h 58e3e3a7aSWarner Losh */ 68e3e3a7aSWarner Losh 78e3e3a7aSWarner Losh 88e3e3a7aSWarner Losh #ifndef lauxlib_h 98e3e3a7aSWarner Losh #define lauxlib_h 108e3e3a7aSWarner Losh 118e3e3a7aSWarner Losh 128e3e3a7aSWarner Losh #include <stddef.h> 138e3e3a7aSWarner Losh #include <stdio.h> 148e3e3a7aSWarner Losh 15*8c784bb8SWarner Losh #include "luaconf.h" 168e3e3a7aSWarner Losh #include "lua.h" 178e3e3a7aSWarner Losh 188e3e3a7aSWarner Losh 190495ed39SKyle Evans /* global table */ 200495ed39SKyle Evans #define LUA_GNAME "_G" 210495ed39SKyle Evans 220495ed39SKyle Evans 230495ed39SKyle Evans typedef struct luaL_Buffer luaL_Buffer; 240495ed39SKyle Evans 258e3e3a7aSWarner Losh 268e3e3a7aSWarner Losh /* extra error code for 'luaL_loadfilex' */ 278e3e3a7aSWarner Losh #define LUA_ERRFILE (LUA_ERRERR+1) 288e3e3a7aSWarner Losh 298e3e3a7aSWarner Losh 308e3e3a7aSWarner Losh /* key, in the registry, for table of loaded modules */ 318e3e3a7aSWarner Losh #define LUA_LOADED_TABLE "_LOADED" 328e3e3a7aSWarner Losh 338e3e3a7aSWarner Losh 348e3e3a7aSWarner Losh /* key, in the registry, for table of preloaded loaders */ 358e3e3a7aSWarner Losh #define LUA_PRELOAD_TABLE "_PRELOAD" 368e3e3a7aSWarner Losh 378e3e3a7aSWarner Losh 388e3e3a7aSWarner Losh typedef struct luaL_Reg { 398e3e3a7aSWarner Losh const char *name; 408e3e3a7aSWarner Losh lua_CFunction func; 418e3e3a7aSWarner Losh } luaL_Reg; 428e3e3a7aSWarner Losh 438e3e3a7aSWarner Losh 448e3e3a7aSWarner Losh #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 458e3e3a7aSWarner Losh 468e3e3a7aSWarner Losh LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 478e3e3a7aSWarner Losh #define luaL_checkversion(L) \ 488e3e3a7aSWarner Losh luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 498e3e3a7aSWarner Losh 508e3e3a7aSWarner Losh LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 518e3e3a7aSWarner Losh LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 528e3e3a7aSWarner Losh LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 538e3e3a7aSWarner Losh LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 540495ed39SKyle Evans LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); 558e3e3a7aSWarner Losh LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 568e3e3a7aSWarner Losh size_t *l); 578e3e3a7aSWarner Losh LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 588e3e3a7aSWarner Losh const char *def, size_t *l); 598e3e3a7aSWarner Losh LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 608e3e3a7aSWarner Losh LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 618e3e3a7aSWarner Losh 628e3e3a7aSWarner Losh LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 638e3e3a7aSWarner Losh LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 648e3e3a7aSWarner Losh lua_Integer def); 658e3e3a7aSWarner Losh 668e3e3a7aSWarner Losh LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 678e3e3a7aSWarner Losh LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 688e3e3a7aSWarner Losh LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 698e3e3a7aSWarner Losh 708e3e3a7aSWarner Losh LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 718e3e3a7aSWarner Losh LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 728e3e3a7aSWarner Losh LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 738e3e3a7aSWarner Losh LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 748e3e3a7aSWarner Losh 758e3e3a7aSWarner Losh LUALIB_API void (luaL_where) (lua_State *L, int lvl); 768e3e3a7aSWarner Losh LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 778e3e3a7aSWarner Losh 788e3e3a7aSWarner Losh LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 798e3e3a7aSWarner Losh const char *const lst[]); 808e3e3a7aSWarner Losh 818e3e3a7aSWarner Losh LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 828e3e3a7aSWarner Losh LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 838e3e3a7aSWarner Losh 840495ed39SKyle Evans 858e3e3a7aSWarner Losh /* predefined references */ 868e3e3a7aSWarner Losh #define LUA_NOREF (-2) 878e3e3a7aSWarner Losh #define LUA_REFNIL (-1) 888e3e3a7aSWarner Losh 898e3e3a7aSWarner Losh LUALIB_API int (luaL_ref) (lua_State *L, int t); 908e3e3a7aSWarner Losh LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 918e3e3a7aSWarner Losh 928e3e3a7aSWarner Losh LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 938e3e3a7aSWarner Losh const char *mode); 948e3e3a7aSWarner Losh 958e3e3a7aSWarner Losh #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 968e3e3a7aSWarner Losh 978e3e3a7aSWarner Losh LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 988e3e3a7aSWarner Losh const char *name, const char *mode); 998e3e3a7aSWarner Losh LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 1008e3e3a7aSWarner Losh 1018e3e3a7aSWarner Losh LUALIB_API lua_State *(luaL_newstate) (void); 1028e3e3a7aSWarner Losh 1038e3e3a7aSWarner Losh LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 1048e3e3a7aSWarner Losh 105*8c784bb8SWarner Losh LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, 1060495ed39SKyle Evans const char *p, const char *r); 1070495ed39SKyle Evans LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, 1080495ed39SKyle Evans const char *p, const char *r); 1098e3e3a7aSWarner Losh 1108e3e3a7aSWarner Losh LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 1118e3e3a7aSWarner Losh 1128e3e3a7aSWarner Losh LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 1138e3e3a7aSWarner Losh 1148e3e3a7aSWarner Losh LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 1158e3e3a7aSWarner Losh const char *msg, int level); 1168e3e3a7aSWarner Losh 1178e3e3a7aSWarner Losh LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 1188e3e3a7aSWarner Losh lua_CFunction openf, int glb); 1198e3e3a7aSWarner Losh 1208e3e3a7aSWarner Losh /* 1218e3e3a7aSWarner Losh ** =============================================================== 1228e3e3a7aSWarner Losh ** some useful macros 1238e3e3a7aSWarner Losh ** =============================================================== 1248e3e3a7aSWarner Losh */ 1258e3e3a7aSWarner Losh 1268e3e3a7aSWarner Losh 1278e3e3a7aSWarner Losh #define luaL_newlibtable(L,l) \ 1288e3e3a7aSWarner Losh lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 1298e3e3a7aSWarner Losh 1308e3e3a7aSWarner Losh #define luaL_newlib(L,l) \ 1318e3e3a7aSWarner Losh (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 1328e3e3a7aSWarner Losh 1338e3e3a7aSWarner Losh #define luaL_argcheck(L, cond,arg,extramsg) \ 134*8c784bb8SWarner Losh ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) 1350495ed39SKyle Evans 1360495ed39SKyle Evans #define luaL_argexpected(L,cond,arg,tname) \ 137*8c784bb8SWarner Losh ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) 1380495ed39SKyle Evans 1398e3e3a7aSWarner Losh #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 1408e3e3a7aSWarner Losh #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 1418e3e3a7aSWarner Losh 1428e3e3a7aSWarner Losh #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 1438e3e3a7aSWarner Losh 1448e3e3a7aSWarner Losh #define luaL_dofile(L, fn) \ 1458e3e3a7aSWarner Losh (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 1468e3e3a7aSWarner Losh 1478e3e3a7aSWarner Losh #define luaL_dostring(L, s) \ 1488e3e3a7aSWarner Losh (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 1498e3e3a7aSWarner Losh 1508e3e3a7aSWarner Losh #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 1518e3e3a7aSWarner Losh 1528e3e3a7aSWarner Losh #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 1538e3e3a7aSWarner Losh 1548e3e3a7aSWarner Losh #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 1558e3e3a7aSWarner Losh 1568e3e3a7aSWarner Losh 157*8c784bb8SWarner Losh /* 158*8c784bb8SWarner Losh ** Perform arithmetic operations on lua_Integer values with wrap-around 159*8c784bb8SWarner Losh ** semantics, as the Lua core does. 160*8c784bb8SWarner Losh */ 161*8c784bb8SWarner Losh #define luaL_intop(op,v1,v2) \ 162*8c784bb8SWarner Losh ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) 163*8c784bb8SWarner Losh 164*8c784bb8SWarner Losh 1650495ed39SKyle Evans /* push the value used to represent failure/error */ 1660495ed39SKyle Evans #define luaL_pushfail(L) lua_pushnil(L) 1670495ed39SKyle Evans 1680495ed39SKyle Evans 1698e3e3a7aSWarner Losh /* 170*8c784bb8SWarner Losh ** Internal assertions for in-house debugging 171*8c784bb8SWarner Losh */ 172*8c784bb8SWarner Losh #if !defined(lua_assert) 173*8c784bb8SWarner Losh 174*8c784bb8SWarner Losh #if defined LUAI_ASSERT 175*8c784bb8SWarner Losh #include <assert.h> 176*8c784bb8SWarner Losh #define lua_assert(c) assert(c) 177*8c784bb8SWarner Losh #else 178*8c784bb8SWarner Losh #define lua_assert(c) ((void)0) 179*8c784bb8SWarner Losh #endif 180*8c784bb8SWarner Losh 181*8c784bb8SWarner Losh #endif 182*8c784bb8SWarner Losh 183*8c784bb8SWarner Losh 184*8c784bb8SWarner Losh 185*8c784bb8SWarner Losh /* 1868e3e3a7aSWarner Losh ** {====================================================== 1878e3e3a7aSWarner Losh ** Generic Buffer manipulation 1888e3e3a7aSWarner Losh ** ======================================================= 1898e3e3a7aSWarner Losh */ 1908e3e3a7aSWarner Losh 1910495ed39SKyle Evans struct luaL_Buffer { 1928e3e3a7aSWarner Losh char *b; /* buffer address */ 1938e3e3a7aSWarner Losh size_t size; /* buffer size */ 1948e3e3a7aSWarner Losh size_t n; /* number of characters in buffer */ 1958e3e3a7aSWarner Losh lua_State *L; 1960495ed39SKyle Evans union { 1970495ed39SKyle Evans LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ 1980495ed39SKyle Evans char b[LUAL_BUFFERSIZE]; /* initial buffer */ 1990495ed39SKyle Evans } init; 2000495ed39SKyle Evans }; 2010495ed39SKyle Evans 2020495ed39SKyle Evans 2030495ed39SKyle Evans #define luaL_bufflen(bf) ((bf)->n) 2040495ed39SKyle Evans #define luaL_buffaddr(bf) ((bf)->b) 2058e3e3a7aSWarner Losh 2068e3e3a7aSWarner Losh 2078e3e3a7aSWarner Losh #define luaL_addchar(B,c) \ 2088e3e3a7aSWarner Losh ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 2098e3e3a7aSWarner Losh ((B)->b[(B)->n++] = (c))) 2108e3e3a7aSWarner Losh 2118e3e3a7aSWarner Losh #define luaL_addsize(B,s) ((B)->n += (s)) 2128e3e3a7aSWarner Losh 2130495ed39SKyle Evans #define luaL_buffsub(B,s) ((B)->n -= (s)) 2140495ed39SKyle Evans 2158e3e3a7aSWarner Losh LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 2168e3e3a7aSWarner Losh LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 2178e3e3a7aSWarner Losh LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 2188e3e3a7aSWarner Losh LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 2198e3e3a7aSWarner Losh LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 2208e3e3a7aSWarner Losh LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 2218e3e3a7aSWarner Losh LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 2228e3e3a7aSWarner Losh LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 2238e3e3a7aSWarner Losh 2248e3e3a7aSWarner Losh #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 2258e3e3a7aSWarner Losh 2268e3e3a7aSWarner Losh /* }====================================================== */ 2278e3e3a7aSWarner Losh 2288e3e3a7aSWarner Losh 2298e3e3a7aSWarner Losh 2308e3e3a7aSWarner Losh /* 2318e3e3a7aSWarner Losh ** {====================================================== 2328e3e3a7aSWarner Losh ** File handles for IO library 2338e3e3a7aSWarner Losh ** ======================================================= 2348e3e3a7aSWarner Losh */ 2358e3e3a7aSWarner Losh 2368e3e3a7aSWarner Losh /* 2378e3e3a7aSWarner Losh ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 2388e3e3a7aSWarner Losh ** initial structure 'luaL_Stream' (it may contain other fields 2398e3e3a7aSWarner Losh ** after that initial structure). 2408e3e3a7aSWarner Losh */ 2418e3e3a7aSWarner Losh 2428e3e3a7aSWarner Losh #define LUA_FILEHANDLE "FILE*" 2438e3e3a7aSWarner Losh 2448e3e3a7aSWarner Losh 2458e3e3a7aSWarner Losh typedef struct luaL_Stream { 2468e3e3a7aSWarner Losh FILE *f; /* stream (NULL for incompletely created streams) */ 2478e3e3a7aSWarner Losh lua_CFunction closef; /* to close stream (NULL for closed streams) */ 2488e3e3a7aSWarner Losh } luaL_Stream; 2498e3e3a7aSWarner Losh 2508e3e3a7aSWarner Losh /* }====================================================== */ 2518e3e3a7aSWarner Losh 2528e3e3a7aSWarner Losh /* 2538e3e3a7aSWarner Losh ** {================================================================== 2548e3e3a7aSWarner Losh ** "Abstraction Layer" for basic report of messages and errors 2558e3e3a7aSWarner Losh ** =================================================================== 2568e3e3a7aSWarner Losh */ 2578e3e3a7aSWarner Losh 2588e3e3a7aSWarner Losh /* print a string */ 2598e3e3a7aSWarner Losh #if !defined(lua_writestring) 2608e3e3a7aSWarner Losh #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 2618e3e3a7aSWarner Losh #endif 2628e3e3a7aSWarner Losh 2638e3e3a7aSWarner Losh /* print a newline and flush the output */ 2648e3e3a7aSWarner Losh #if !defined(lua_writeline) 2658e3e3a7aSWarner Losh #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 2668e3e3a7aSWarner Losh #endif 2678e3e3a7aSWarner Losh 2688e3e3a7aSWarner Losh /* print an error message */ 2698e3e3a7aSWarner Losh #if !defined(lua_writestringerror) 2708e3e3a7aSWarner Losh #define lua_writestringerror(s,p) \ 2718e3e3a7aSWarner Losh (fprintf(stderr, (s), (p)), fflush(stderr)) 2728e3e3a7aSWarner Losh #endif 2738e3e3a7aSWarner Losh 2748e3e3a7aSWarner Losh /* }================================================================== */ 2758e3e3a7aSWarner Losh 2768e3e3a7aSWarner Losh 2778e3e3a7aSWarner Losh /* 2788e3e3a7aSWarner Losh ** {============================================================ 2798e3e3a7aSWarner Losh ** Compatibility with deprecated conversions 2808e3e3a7aSWarner Losh ** ============================================================= 2818e3e3a7aSWarner Losh */ 2828e3e3a7aSWarner Losh #if defined(LUA_COMPAT_APIINTCASTS) 2838e3e3a7aSWarner Losh 2848e3e3a7aSWarner Losh #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 2858e3e3a7aSWarner Losh #define luaL_optunsigned(L,a,d) \ 2868e3e3a7aSWarner Losh ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 2878e3e3a7aSWarner Losh 2888e3e3a7aSWarner Losh #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 2898e3e3a7aSWarner Losh #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 2908e3e3a7aSWarner Losh 2918e3e3a7aSWarner Losh #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 2928e3e3a7aSWarner Losh #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 2938e3e3a7aSWarner Losh 2948e3e3a7aSWarner Losh #endif 2958e3e3a7aSWarner Losh /* }============================================================ */ 2968e3e3a7aSWarner Losh 2978e3e3a7aSWarner Losh 2988e3e3a7aSWarner Losh 2998e3e3a7aSWarner Losh #endif 3008e3e3a7aSWarner Losh 3018e3e3a7aSWarner Losh 302