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