1 /*
2 ** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $
3 ** Auxiliary functions for building Lua libraries
4 ** See Copyright Notice in lua.h
5 */
6 
7 #ifndef lauxlib_h
8 #define lauxlib_h
9 
10 #include <stddef.h>
11 #include <stdio.h>
12 
13 #include "lua.h"
14 
15 /* extra error code for `luaL_load' */
16 #define LUA_ERRFILE (LUA_ERRERR + 1)
17 
18 typedef struct luaL_Reg
19 {
20 	const char *name;
21 	lua_CFunction func;
22 } luaL_Reg;
23 
24 LUALIB_API void(luaL_checkversion_)(lua_State *L, lua_Number ver);
25 #define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM)
26 
27 LUALIB_API int(luaL_getmetafield)(lua_State *L, int obj, const char *e);
28 LUALIB_API int(luaL_callmeta)(lua_State *L, int obj, const char *e);
29 LUALIB_API const char *(luaL_tolstring)(lua_State *L, int idx, size_t *len);
30 LUALIB_API int(luaL_argerror)(lua_State *L, int numarg, const char *extramsg);
31 LUALIB_API const char *(luaL_checklstring)(lua_State *L, int numArg,
32 										   size_t *l);
33 LUALIB_API const char *(luaL_optlstring)(lua_State *L, int numArg,
34 										 const char *def, size_t *l);
35 LUALIB_API lua_Number(luaL_checknumber)(lua_State *L, int numArg);
36 LUALIB_API lua_Number(luaL_optnumber)(lua_State *L, int nArg, lua_Number def);
37 
38 LUALIB_API lua_Integer(luaL_checkinteger)(lua_State *L, int numArg);
39 LUALIB_API lua_Integer(luaL_optinteger)(lua_State *L, int nArg,
40 										lua_Integer def);
41 LUALIB_API lua_Unsigned(luaL_checkunsigned)(lua_State *L, int numArg);
42 LUALIB_API lua_Unsigned(luaL_optunsigned)(lua_State *L, int numArg,
43 										  lua_Unsigned def);
44 
45 LUALIB_API void(luaL_checkstack)(lua_State *L, int sz, const char *msg);
46 LUALIB_API void(luaL_checktype)(lua_State *L, int narg, int t);
47 LUALIB_API void(luaL_checkany)(lua_State *L, int narg);
48 
49 LUALIB_API int(luaL_newmetatable)(lua_State *L, const char *tname);
50 LUALIB_API void(luaL_setmetatable)(lua_State *L, const char *tname);
51 LUALIB_API void *(luaL_testudata)(lua_State *L, int ud, const char *tname);
52 LUALIB_API void *(luaL_checkudata)(lua_State *L, int ud, const char *tname);
53 
54 LUALIB_API void(luaL_where)(lua_State *L, int lvl);
55 LUALIB_API int(luaL_error)(lua_State *L, const char *fmt, ...);
56 
57 LUALIB_API int(luaL_checkoption)(lua_State *L, int narg, const char *def,
58 								 const char *const lst[]);
59 
60 LUALIB_API int(luaL_fileresult)(lua_State *L, int stat, const char *fname);
61 LUALIB_API int(luaL_execresult)(lua_State *L, int stat);
62 
63 /* pre-defined references */
64 #define LUA_NOREF (-2)
65 #define LUA_REFNIL (-1)
66 
67 LUALIB_API int(luaL_ref)(lua_State *L, int t);
68 LUALIB_API void(luaL_unref)(lua_State *L, int t, int ref);
69 
70 LUALIB_API int(luaL_loadfilex)(lua_State *L, const char *filename,
71 							   const char *mode);
72 
73 #define luaL_loadfile(L, f) luaL_loadfilex(L, f, NULL)
74 
75 LUALIB_API int(luaL_loadbufferx)(lua_State *L, const char *buff, size_t sz,
76 								 const char *name, const char *mode);
77 LUALIB_API int(luaL_loadstring)(lua_State *L, const char *s);
78 
79 LUALIB_API lua_State *(luaL_newstate)(void);
80 
81 LUALIB_API int(luaL_len)(lua_State *L, int idx);
82 
83 LUALIB_API const char *(luaL_gsub)(lua_State *L, const char *s, const char *p,
84 								   const char *r);
85 
86 LUALIB_API void(luaL_setfuncs)(lua_State *L, const luaL_Reg *l, int nup);
87 
88 LUALIB_API int(luaL_getsubtable)(lua_State *L, int idx, const char *fname);
89 
90 LUALIB_API void(luaL_traceback)(lua_State *L, lua_State *L1,
91 								const char *msg, int level);
92 
93 LUALIB_API void(luaL_requiref)(lua_State *L, const char *modname,
94 							   lua_CFunction openf, int glb);
95 
96 /*
97 ** ===============================================================
98 ** some useful macros
99 ** ===============================================================
100 */
101 
102 #define luaL_newlibtable(L, l) \
103 	lua_createtable(L, 0, sizeof(l) / sizeof((l)[0]) - 1)
104 
105 #define luaL_newlib(L, l) (luaL_newlibtable(L, l), luaL_setfuncs(L, l, 0))
106 
107 #define luaL_argcheck(L, cond, numarg, extramsg) \
108 	((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
109 #define luaL_checkstring(L, n) (luaL_checklstring(L, (n), NULL))
110 #define luaL_optstring(L, n, d) (luaL_optlstring(L, (n), (d), NULL))
111 #define luaL_checkint(L, n) ((int)luaL_checkinteger(L, (n)))
112 #define luaL_optint(L, n, d) ((int)luaL_optinteger(L, (n), (d)))
113 #define luaL_checklong(L, n) ((long)luaL_checkinteger(L, (n)))
114 #define luaL_optlong(L, n, d) ((long)luaL_optinteger(L, (n), (d)))
115 
116 #define luaL_typename(L, i) lua_typename(L, lua_type(L, (i)))
117 
118 #define luaL_dofile(L, fn) \
119 	(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
120 
121 #define luaL_dostring(L, s) \
122 	(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
123 
124 #define luaL_getmetatable(L, n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
125 
126 #define luaL_opt(L, f, n, d) (lua_isnoneornil(L, (n)) ? (d) : f(L, (n)))
127 
128 #define luaL_loadbuffer(L, s, sz, n) luaL_loadbufferx(L, s, sz, n, NULL)
129 
130 /*
131 ** {======================================================
132 ** Generic Buffer manipulation
133 ** =======================================================
134 */
135 
136 typedef struct luaL_Buffer
137 {
138 	char *b;     /* buffer address */
139 	size_t size; /* buffer size */
140 	size_t n;    /* number of characters in buffer */
141 	lua_State *L;
142 	char initb[LUAL_BUFFERSIZE]; /* initial buffer */
143 } luaL_Buffer;
144 
145 #define luaL_addchar(B, c)                                    \
146 	((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \
147 	 ((B)->b[(B)->n++] = (c)))
148 
149 #define luaL_addsize(B, s) ((B)->n += (s))
150 
151 LUALIB_API void(luaL_buffinit)(lua_State *L, luaL_Buffer *B);
152 LUALIB_API char *(luaL_prepbuffsize)(luaL_Buffer *B, size_t sz);
153 LUALIB_API void(luaL_addlstring)(luaL_Buffer *B, const char *s, size_t l);
154 LUALIB_API void(luaL_addstring)(luaL_Buffer *B, const char *s);
155 LUALIB_API void(luaL_addvalue)(luaL_Buffer *B);
156 LUALIB_API void(luaL_pushresult)(luaL_Buffer *B);
157 LUALIB_API void(luaL_pushresultsize)(luaL_Buffer *B, size_t sz);
158 LUALIB_API char *(luaL_buffinitsize)(lua_State *L, luaL_Buffer *B, size_t sz);
159 
160 #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
161 
162 /* }====================================================== */
163 
164 /*
165 ** {======================================================
166 ** File handles for IO library
167 ** =======================================================
168 */
169 
170 /*
171 ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
172 ** initial structure 'luaL_Stream' (it may contain other fields
173 ** after that initial structure).
174 */
175 
176 #define LUA_FILEHANDLE "FILE*"
177 
178 typedef struct luaL_Stream
179 {
180 	FILE *f;              /* stream (NULL for incompletely created streams) */
181 	lua_CFunction closef; /* to close stream (NULL for closed streams) */
182 } luaL_Stream;
183 
184 /* }====================================================== */
185 
186 /* compatibility with old module system */
187 #if defined(LUA_COMPAT_MODULE)
188 
189 LUALIB_API void(luaL_pushmodule)(lua_State *L, const char *modname,
190 								 int sizehint);
191 LUALIB_API void(luaL_openlib)(lua_State *L, const char *libname,
192 							  const luaL_Reg *l, int nup);
193 
194 #define luaL_register(L, n, l) (luaL_openlib(L, (n), (l), 0))
195 
196 #endif
197 
198 #endif
199