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