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