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