3 ** Auxiliary functions for building Lua libraries
4 ** See Copyright Notice in lua.h
20 #define LUA_GNAME "_G"
23 typedef struct luaL_Buffer luaL_Buffer
;
26 /* extra error code for 'luaL_loadfilex' */
27 #define LUA_ERRFILE (LUA_ERRERR+1)
30 /* key, in the registry, for table of loaded modules */
31 #define LUA_LOADED_TABLE "_LOADED"
34 /* key, in the registry, for table of preloaded loaders */
35 #define LUA_PRELOAD_TABLE "_PRELOAD"
38 typedef struct luaL_Reg
{
44 #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
46 LUALIB_API
void (luaL_checkversion_
)(lua_State
*L
, lua_Number ver
, size_t sz
);
47 #define luaL_checkversion(L) \
48 luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
50 LUALIB_API
int (luaL_getmetafield
)(lua_State
*L
, int obj
, const char *e
);
51 LUALIB_API
int (luaL_callmeta
)(lua_State
*L
, int obj
, const char *e
);
52 LUALIB_API
const char *(luaL_tolstring
)(lua_State
*L
, int idx
, size_t *len
);
53 LUALIB_API
int (luaL_argerror
)(lua_State
*L
, int arg
, const char *extramsg
);
54 LUALIB_API
int (luaL_typeerror
)(lua_State
*L
, int arg
, const char *tname
);
55 LUALIB_API
const char *(luaL_checklstring
)(lua_State
*L
, int arg
,
57 LUALIB_API
const char *(luaL_optlstring
)(lua_State
*L
, int arg
,
58 const char *def
, size_t *l
);
59 LUALIB_API
lua_Number(luaL_checknumber
)(lua_State
*L
, int arg
);
60 LUALIB_API
lua_Number(luaL_optnumber
)(lua_State
*L
, int arg
, lua_Number def
);
62 LUALIB_API
lua_Integer(luaL_checkinteger
)(lua_State
*L
, int arg
);
63 LUALIB_API
lua_Integer(luaL_optinteger
)(lua_State
*L
, int arg
,
66 LUALIB_API
void (luaL_checkstack
)(lua_State
*L
, int sz
, const char *msg
);
67 LUALIB_API
void (luaL_checktype
)(lua_State
*L
, int arg
, int t
);
68 LUALIB_API
void (luaL_checkany
)(lua_State
*L
, int arg
);
70 LUALIB_API
int (luaL_newmetatable
)(lua_State
*L
, const char *tname
);
71 LUALIB_API
void (luaL_setmetatable
)(lua_State
*L
, const char *tname
);
72 LUALIB_API
void *(luaL_testudata
)(lua_State
*L
, int ud
, const char *tname
);
73 LUALIB_API
void *(luaL_checkudata
)(lua_State
*L
, int ud
, const char *tname
);
75 LUALIB_API
void (luaL_where
)(lua_State
*L
, int lvl
);
76 LUALIB_API
int (luaL_error
)(lua_State
*L
, const char *fmt
, ...);
78 LUALIB_API
int (luaL_checkoption
)(lua_State
*L
, int arg
, const char *def
,
79 const char *const lst
[]);
81 LUALIB_API
int (luaL_fileresult
)(lua_State
*L
, int stat
, const char *fname
);
82 LUALIB_API
int (luaL_execresult
)(lua_State
*L
, int stat
);
85 /* predefined references */
86 #define LUA_NOREF (-2)
87 #define LUA_REFNIL (-1)
89 LUALIB_API
int (luaL_ref
)(lua_State
*L
, int t
);
90 LUALIB_API
void (luaL_unref
)(lua_State
*L
, int t
, int ref
);
92 LUALIB_API
int (luaL_loadfilex
)(lua_State
*L
, const char *filename
,
95 #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
97 LUALIB_API
int (luaL_loadbufferx
)(lua_State
*L
, const char *buff
, size_t sz
,
98 const char *name
, const char *mode
);
99 LUALIB_API
int (luaL_loadstring
)(lua_State
*L
, const char *s
);
101 LUALIB_API lua_State
*(luaL_newstate
)(void);
103 LUALIB_API
lua_Integer(luaL_len
)(lua_State
*L
, int idx
);
105 LUALIB_API
void (luaL_addgsub
)(luaL_Buffer
*b
, const char *s
,
106 const char *p
, const char *r
);
107 LUALIB_API
const char *(luaL_gsub
)(lua_State
*L
, const char *s
,
108 const char *p
, const char *r
);
110 LUALIB_API
void (luaL_setfuncs
)(lua_State
*L
, const luaL_Reg
*l
, int nup
);
112 LUALIB_API
int (luaL_getsubtable
)(lua_State
*L
, int idx
, const char *fname
);
114 LUALIB_API
void (luaL_traceback
)(lua_State
*L
, lua_State
*L1
,
115 const char *msg
, int level
);
117 LUALIB_API
void (luaL_requiref
)(lua_State
*L
, const char *modname
,
118 lua_CFunction openf
, int glb
);
121 ** ===============================================================
122 ** some useful macros
123 ** ===============================================================
127 #define luaL_newlibtable(L,l) \
128 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
130 #define luaL_newlib(L,l) \
131 (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
133 #define luaL_argcheck(L, cond,arg,extramsg) \
134 ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
136 #define luaL_argexpected(L,cond,arg,tname) \
137 ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname))))
139 #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
140 #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
142 #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
144 #define luaL_dofile(L, fn) \
145 (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
147 #define luaL_dostring(L, s) \
148 (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
150 #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
152 #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
154 #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
158 ** Perform arithmetic operations on lua_Integer values with wrap-around
159 ** semantics, as the Lua core does.
161 #define luaL_intop(op,v1,v2) \
162 ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2)))
165 /* push the value used to represent failure/error */
166 #define luaL_pushfail(L) lua_pushnil(L)
170 ** Internal assertions for in-house debugging
172 #if !defined(lua_assert)
174 #if defined LUAI_ASSERT
176 #define lua_assert(c) assert(c)
178 #define lua_assert(c) ((void)0)
186 ** {======================================================
187 ** Generic Buffer manipulation
188 ** =======================================================
192 char *b
; /* buffer address */
193 size_t size
; /* buffer size */
194 size_t n
; /* number of characters in buffer */
197 LUAI_MAXALIGN
; /* ensure maximum alignment for buffer */
198 char b
[LUAL_BUFFERSIZE
]; /* initial buffer */
203 #define luaL_bufflen(bf) ((bf)->n)
204 #define luaL_buffaddr(bf) ((bf)->b)
207 #define luaL_addchar(B,c) \
208 ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \
209 ((B)->b[(B)->n++] = (c)))
211 #define luaL_addsize(B,s) ((B)->n += (s))
213 #define luaL_buffsub(B,s) ((B)->n -= (s))
215 LUALIB_API
void (luaL_buffinit
)(lua_State
*L
, luaL_Buffer
*B
);
216 LUALIB_API
char *(luaL_prepbuffsize
)(luaL_Buffer
*B
, size_t sz
);
217 LUALIB_API
void (luaL_addlstring
)(luaL_Buffer
*B
, const char *s
, size_t l
);
218 LUALIB_API
void (luaL_addstring
)(luaL_Buffer
*B
, const char *s
);
219 LUALIB_API
void (luaL_addvalue
)(luaL_Buffer
*B
);
220 LUALIB_API
void (luaL_pushresult
)(luaL_Buffer
*B
);
221 LUALIB_API
void (luaL_pushresultsize
)(luaL_Buffer
*B
, size_t sz
);
222 LUALIB_API
char *(luaL_buffinitsize
)(lua_State
*L
, luaL_Buffer
*B
, size_t sz
);
224 #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
226 /* }====================================================== */
231 ** {======================================================
232 ** File handles for IO library
233 ** =======================================================
237 ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
238 ** initial structure 'luaL_Stream' (it may contain other fields
239 ** after that initial structure).
242 #define LUA_FILEHANDLE "FILE*"
245 typedef struct luaL_Stream
{
246 FILE *f
; /* stream (NULL for incompletely created streams) */
247 lua_CFunction closef
; /* to close stream (NULL for closed streams) */
250 /* }====================================================== */
253 ** {==================================================================
254 ** "Abstraction Layer" for basic report of messages and errors
255 ** ===================================================================
259 #if !defined(lua_writestring)
260 #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
263 /* print a newline and flush the output */
264 #if !defined(lua_writeline)
265 #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
268 /* print an error message */
269 #if !defined(lua_writestringerror)
270 #define lua_writestringerror(s,p) \
271 (fprintf(stderr, (s), (p)), fflush(stderr))
274 /* }================================================================== */
278 ** {============================================================
279 ** Compatibility with deprecated conversions
280 ** =============================================================
282 #if defined(LUA_COMPAT_APIINTCASTS)
284 #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
285 #define luaL_optunsigned(L,a,d) \
286 ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
288 #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
289 #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
291 #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
292 #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
295 /* }============================================================ */