1*0a6a1f1dSLionel Sambuc /* $NetBSD: lauxlib.h,v 1.3 2015/02/02 14:03:05 lneto Exp $ */ 211be35a1SLionel Sambuc 311be35a1SLionel Sambuc /* 4*0a6a1f1dSLionel Sambuc ** Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp 511be35a1SLionel Sambuc ** Auxiliary functions for building Lua libraries 611be35a1SLionel Sambuc ** See Copyright Notice in lua.h 711be35a1SLionel Sambuc */ 811be35a1SLionel Sambuc 911be35a1SLionel Sambuc 1011be35a1SLionel Sambuc #ifndef lauxlib_h 1111be35a1SLionel Sambuc #define lauxlib_h 1211be35a1SLionel Sambuc 1311be35a1SLionel Sambuc 14*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 1511be35a1SLionel Sambuc #include <stddef.h> 1611be35a1SLionel Sambuc #include <stdio.h> 17*0a6a1f1dSLionel Sambuc #endif 1811be35a1SLionel Sambuc 1911be35a1SLionel Sambuc #include "lua.h" 2011be35a1SLionel Sambuc 2111be35a1SLionel Sambuc 2211be35a1SLionel Sambuc 23*0a6a1f1dSLionel Sambuc /* extra error code for 'luaL_load' */ 2411be35a1SLionel Sambuc #define LUA_ERRFILE (LUA_ERRERR+1) 2511be35a1SLionel Sambuc 2611be35a1SLionel Sambuc 2711be35a1SLionel Sambuc typedef struct luaL_Reg { 2811be35a1SLionel Sambuc const char *name; 2911be35a1SLionel Sambuc lua_CFunction func; 3011be35a1SLionel Sambuc } luaL_Reg; 3111be35a1SLionel Sambuc 3211be35a1SLionel Sambuc 33*0a6a1f1dSLionel Sambuc #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 3411be35a1SLionel Sambuc 35*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 36*0a6a1f1dSLionel Sambuc #define luaL_checkversion(L) \ 37*0a6a1f1dSLionel Sambuc luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 38*0a6a1f1dSLionel Sambuc 3911be35a1SLionel Sambuc LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 4011be35a1SLionel Sambuc LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 41*0a6a1f1dSLionel Sambuc LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 42*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 43*0a6a1f1dSLionel Sambuc LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 4411be35a1SLionel Sambuc size_t *l); 45*0a6a1f1dSLionel Sambuc LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 4611be35a1SLionel Sambuc const char *def, size_t *l); 47*0a6a1f1dSLionel Sambuc LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 48*0a6a1f1dSLionel Sambuc LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 4911be35a1SLionel Sambuc 50*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 51*0a6a1f1dSLionel Sambuc LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 52*0a6a1f1dSLionel Sambuc LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 5311be35a1SLionel Sambuc lua_Integer def); 54*0a6a1f1dSLionel Sambuc #else /* _KERNEL */ 55*0a6a1f1dSLionel Sambuc #define luaL_checkinteger luaL_checknumber 56*0a6a1f1dSLionel Sambuc #define luaL_optinteger(L,a,d) luaL_optnumber(L, (a), (lua_Number)(d)) 57*0a6a1f1dSLionel Sambuc #endif 5811be35a1SLionel Sambuc 5911be35a1SLionel Sambuc LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 60*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 61*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 6211be35a1SLionel Sambuc 6311be35a1SLionel Sambuc LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 64*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 65*0a6a1f1dSLionel Sambuc LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 6611be35a1SLionel Sambuc LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 6711be35a1SLionel Sambuc 6811be35a1SLionel Sambuc LUALIB_API void (luaL_where) (lua_State *L, int lvl); 6911be35a1SLionel Sambuc LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 7011be35a1SLionel Sambuc 71*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 7211be35a1SLionel Sambuc const char *const lst[]); 7311be35a1SLionel Sambuc 74*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 75*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 76*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 77*0a6a1f1dSLionel Sambuc #endif 78*0a6a1f1dSLionel Sambuc 79*0a6a1f1dSLionel Sambuc /* pre-defined references */ 80*0a6a1f1dSLionel Sambuc #define LUA_NOREF (-2) 81*0a6a1f1dSLionel Sambuc #define LUA_REFNIL (-1) 82*0a6a1f1dSLionel Sambuc 8311be35a1SLionel Sambuc LUALIB_API int (luaL_ref) (lua_State *L, int t); 8411be35a1SLionel Sambuc LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 8511be35a1SLionel Sambuc 86*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 87*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 88*0a6a1f1dSLionel Sambuc const char *mode); 89*0a6a1f1dSLionel Sambuc 90*0a6a1f1dSLionel Sambuc #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 91*0a6a1f1dSLionel Sambuc #endif 92*0a6a1f1dSLionel Sambuc 93*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 94*0a6a1f1dSLionel Sambuc const char *name, const char *mode); 9511be35a1SLionel Sambuc LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 9611be35a1SLionel Sambuc 9711be35a1SLionel Sambuc LUALIB_API lua_State *(luaL_newstate) (void); 9811be35a1SLionel Sambuc 99*0a6a1f1dSLionel Sambuc LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 10011be35a1SLionel Sambuc 10111be35a1SLionel Sambuc LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 10211be35a1SLionel Sambuc const char *r); 10311be35a1SLionel Sambuc 104*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 10511be35a1SLionel Sambuc 106*0a6a1f1dSLionel Sambuc LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 10711be35a1SLionel Sambuc 108*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 109*0a6a1f1dSLionel Sambuc const char *msg, int level); 11011be35a1SLionel Sambuc 111*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 112*0a6a1f1dSLionel Sambuc lua_CFunction openf, int glb); 11311be35a1SLionel Sambuc 11411be35a1SLionel Sambuc /* 11511be35a1SLionel Sambuc ** =============================================================== 11611be35a1SLionel Sambuc ** some useful macros 11711be35a1SLionel Sambuc ** =============================================================== 11811be35a1SLionel Sambuc */ 11911be35a1SLionel Sambuc 120*0a6a1f1dSLionel Sambuc 121*0a6a1f1dSLionel Sambuc #define luaL_newlibtable(L,l) \ 122*0a6a1f1dSLionel Sambuc lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 123*0a6a1f1dSLionel Sambuc 124*0a6a1f1dSLionel Sambuc #define luaL_newlib(L,l) \ 125*0a6a1f1dSLionel Sambuc (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 126*0a6a1f1dSLionel Sambuc 127*0a6a1f1dSLionel Sambuc #define luaL_argcheck(L, cond,arg,extramsg) \ 128*0a6a1f1dSLionel Sambuc ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) 12911be35a1SLionel Sambuc #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 13011be35a1SLionel Sambuc #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 13111be35a1SLionel Sambuc 13211be35a1SLionel Sambuc #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 13311be35a1SLionel Sambuc 134*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 13511be35a1SLionel Sambuc #define luaL_dofile(L, fn) \ 13611be35a1SLionel Sambuc (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 137*0a6a1f1dSLionel Sambuc #endif 13811be35a1SLionel Sambuc 13911be35a1SLionel Sambuc #define luaL_dostring(L, s) \ 14011be35a1SLionel Sambuc (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 14111be35a1SLionel Sambuc 14211be35a1SLionel Sambuc #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 14311be35a1SLionel Sambuc 14411be35a1SLionel Sambuc #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 14511be35a1SLionel Sambuc 146*0a6a1f1dSLionel Sambuc #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 147*0a6a1f1dSLionel Sambuc 148*0a6a1f1dSLionel Sambuc 14911be35a1SLionel Sambuc /* 15011be35a1SLionel Sambuc ** {====================================================== 15111be35a1SLionel Sambuc ** Generic Buffer manipulation 15211be35a1SLionel Sambuc ** ======================================================= 15311be35a1SLionel Sambuc */ 15411be35a1SLionel Sambuc 15511be35a1SLionel Sambuc typedef struct luaL_Buffer { 156*0a6a1f1dSLionel Sambuc char *b; /* buffer address */ 157*0a6a1f1dSLionel Sambuc size_t size; /* buffer size */ 158*0a6a1f1dSLionel Sambuc size_t n; /* number of characters in buffer */ 15911be35a1SLionel Sambuc lua_State *L; 160*0a6a1f1dSLionel Sambuc char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 16111be35a1SLionel Sambuc } luaL_Buffer; 16211be35a1SLionel Sambuc 163*0a6a1f1dSLionel Sambuc 16411be35a1SLionel Sambuc #define luaL_addchar(B,c) \ 165*0a6a1f1dSLionel Sambuc ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 166*0a6a1f1dSLionel Sambuc ((B)->b[(B)->n++] = (c))) 16711be35a1SLionel Sambuc 168*0a6a1f1dSLionel Sambuc #define luaL_addsize(B,s) ((B)->n += (s)) 16911be35a1SLionel Sambuc 17011be35a1SLionel Sambuc LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 171*0a6a1f1dSLionel Sambuc LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 17211be35a1SLionel Sambuc LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 17311be35a1SLionel Sambuc LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 17411be35a1SLionel Sambuc LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 17511be35a1SLionel Sambuc LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 176*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 177*0a6a1f1dSLionel Sambuc LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 17811be35a1SLionel Sambuc 179*0a6a1f1dSLionel Sambuc #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 18011be35a1SLionel Sambuc 18111be35a1SLionel Sambuc /* }====================================================== */ 18211be35a1SLionel Sambuc 18311be35a1SLionel Sambuc 18411be35a1SLionel Sambuc 185*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 186*0a6a1f1dSLionel Sambuc /* 187*0a6a1f1dSLionel Sambuc ** {====================================================== 188*0a6a1f1dSLionel Sambuc ** File handles for IO library 189*0a6a1f1dSLionel Sambuc ** ======================================================= 190*0a6a1f1dSLionel Sambuc */ 19111be35a1SLionel Sambuc 192*0a6a1f1dSLionel Sambuc /* 193*0a6a1f1dSLionel Sambuc ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 194*0a6a1f1dSLionel Sambuc ** initial structure 'luaL_Stream' (it may contain other fields 195*0a6a1f1dSLionel Sambuc ** after that initial structure). 196*0a6a1f1dSLionel Sambuc */ 19711be35a1SLionel Sambuc 198*0a6a1f1dSLionel Sambuc #define LUA_FILEHANDLE "FILE*" 19911be35a1SLionel Sambuc 20011be35a1SLionel Sambuc 201*0a6a1f1dSLionel Sambuc typedef struct luaL_Stream { 202*0a6a1f1dSLionel Sambuc FILE *f; /* stream (NULL for incompletely created streams) */ 203*0a6a1f1dSLionel Sambuc lua_CFunction closef; /* to close stream (NULL for closed streams) */ 204*0a6a1f1dSLionel Sambuc } luaL_Stream; 205*0a6a1f1dSLionel Sambuc 206*0a6a1f1dSLionel Sambuc /* }====================================================== */ 207*0a6a1f1dSLionel Sambuc #endif 208*0a6a1f1dSLionel Sambuc 209*0a6a1f1dSLionel Sambuc 210*0a6a1f1dSLionel Sambuc 211*0a6a1f1dSLionel Sambuc /* compatibility with old module system */ 212*0a6a1f1dSLionel Sambuc #if defined(LUA_COMPAT_MODULE) 213*0a6a1f1dSLionel Sambuc 214*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 215*0a6a1f1dSLionel Sambuc int sizehint); 216*0a6a1f1dSLionel Sambuc LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 217*0a6a1f1dSLionel Sambuc const luaL_Reg *l, int nup); 218*0a6a1f1dSLionel Sambuc 219*0a6a1f1dSLionel Sambuc #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 220*0a6a1f1dSLionel Sambuc 221*0a6a1f1dSLionel Sambuc #endif 222*0a6a1f1dSLionel Sambuc 223*0a6a1f1dSLionel Sambuc 224*0a6a1f1dSLionel Sambuc #ifndef _KERNEL 225*0a6a1f1dSLionel Sambuc /* 226*0a6a1f1dSLionel Sambuc ** {================================================================== 227*0a6a1f1dSLionel Sambuc ** "Abstraction Layer" for basic report of messages and errors 228*0a6a1f1dSLionel Sambuc ** =================================================================== 229*0a6a1f1dSLionel Sambuc */ 230*0a6a1f1dSLionel Sambuc 231*0a6a1f1dSLionel Sambuc /* print a string */ 232*0a6a1f1dSLionel Sambuc #if !defined(lua_writestring) 233*0a6a1f1dSLionel Sambuc #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 234*0a6a1f1dSLionel Sambuc #endif 235*0a6a1f1dSLionel Sambuc 236*0a6a1f1dSLionel Sambuc /* print a newline and flush the output */ 237*0a6a1f1dSLionel Sambuc #if !defined(lua_writeline) 238*0a6a1f1dSLionel Sambuc #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 239*0a6a1f1dSLionel Sambuc #endif 240*0a6a1f1dSLionel Sambuc 241*0a6a1f1dSLionel Sambuc /* print an error message */ 242*0a6a1f1dSLionel Sambuc #if !defined(lua_writestringerror) 243*0a6a1f1dSLionel Sambuc #define lua_writestringerror(s,p) \ 244*0a6a1f1dSLionel Sambuc (fprintf(stderr, (s), (p)), fflush(stderr)) 245*0a6a1f1dSLionel Sambuc #endif 246*0a6a1f1dSLionel Sambuc 247*0a6a1f1dSLionel Sambuc /* }================================================================== */ 248*0a6a1f1dSLionel Sambuc #endif 249*0a6a1f1dSLionel Sambuc 250*0a6a1f1dSLionel Sambuc 251*0a6a1f1dSLionel Sambuc /* 252*0a6a1f1dSLionel Sambuc ** {============================================================ 253*0a6a1f1dSLionel Sambuc ** Compatibility with deprecated conversions 254*0a6a1f1dSLionel Sambuc ** ============================================================= 255*0a6a1f1dSLionel Sambuc */ 256*0a6a1f1dSLionel Sambuc #if defined(LUA_COMPAT_APIINTCASTS) 257*0a6a1f1dSLionel Sambuc 258*0a6a1f1dSLionel Sambuc #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 259*0a6a1f1dSLionel Sambuc #define luaL_optunsigned(L,a,d) \ 260*0a6a1f1dSLionel Sambuc ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 261*0a6a1f1dSLionel Sambuc 262*0a6a1f1dSLionel Sambuc #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 263*0a6a1f1dSLionel Sambuc #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 264*0a6a1f1dSLionel Sambuc 265*0a6a1f1dSLionel Sambuc #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 266*0a6a1f1dSLionel Sambuc #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 267*0a6a1f1dSLionel Sambuc 268*0a6a1f1dSLionel Sambuc #endif 269*0a6a1f1dSLionel Sambuc /* }============================================================ */ 270*0a6a1f1dSLionel Sambuc 271*0a6a1f1dSLionel Sambuc 27211be35a1SLionel Sambuc 27311be35a1SLionel Sambuc #endif 27411be35a1SLionel Sambuc 27511be35a1SLionel Sambuc 276