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