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