sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
lauxlib.h
Go to the documentation of this file.
1/*
2** $Id: lauxlib.h $
3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h
5*/
6
7
12#ifndef lauxlib_h
13#define lauxlib_h
14
15
16#include <stddef.h>
17#include <stdio.h>
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
66LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg);
67LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg,
68 lua_Integer def);
69
70LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
71LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t);
72LUALIB_API void (luaL_checkany) (lua_State *L, int arg);
73
74LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
75LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname);
76LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
77LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
78
79LUALIB_API void (luaL_where) (lua_State *L, int lvl);
80LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
81
82LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
83 const char *const lst[]);
84
85LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
86LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
87
88
89/* predefined references */
90#define LUA_NOREF (-2)
91#define LUA_REFNIL (-1)
92
93LUALIB_API int (luaL_ref) (lua_State *L, int t);
94LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
95
96LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
97 const char *mode);
98
99#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
100
101LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
102 const char *name, const char *mode);
103LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
104
105LUALIB_API lua_State *(luaL_newstate) (void);
106
107LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
108
109LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
110 const char *p, const char *r);
111LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
112 const char *p, const char *r);
113
114LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
115
116LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
117
118LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
119 const char *msg, int level);
120
121LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
122 lua_CFunction openf, int glb);
123
124/*
125** ===============================================================
126** some useful macros
127** ===============================================================
128*/
129
130
131#define luaL_newlibtable(L,l) \
132 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
133
134#define luaL_newlib(L,l) \
135 (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
136
137#define luaL_argcheck(L, cond,arg,extramsg) \
138 ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
139
140#define luaL_argexpected(L,cond,arg,tname) \
141 ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname))))
142
143#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
144#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
145
146#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
147
148#define luaL_dofile(L, fn) \
149 (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
150
151#define luaL_dostring(L, s) \
152 (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
153
154#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
155
156#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
157
158#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
159
160
161/*
162** Perform arithmetic operations on lua_Integer values with wrap-around
163** semantics, as the Lua core does.
164*/
165#define luaL_intop(op,v1,v2) \
166 ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2)))
167
168
169/* push the value used to represent failure/error */
170#define luaL_pushfail(L) lua_pushnil(L)
171
172
173/*
174** Internal assertions for in-house debugging
175*/
176#if !defined(lua_assert)
177
178#if defined LUAI_ASSERT
179 #include <assert.h>
180 #define lua_assert(c) assert(c)
181#else
182 #define lua_assert(c) ((void)0)
183#endif
184
185#endif
186
187
188
189/*
190** {======================================================
191** Generic Buffer manipulation
192** =======================================================
193*/
194
196 char *b; /* buffer address */
197 size_t size; /* buffer size */
198 size_t n; /* number of characters in buffer */
199 lua_State *L;
200 union {
201 LUAI_MAXALIGN; /* ensure maximum alignment for buffer */
202 char b[LUAL_BUFFERSIZE]; /* initial buffer */
203 } init;
204};
205
206
207#define luaL_bufflen(bf) ((bf)->n)
208#define luaL_buffaddr(bf) ((bf)->b)
209
210
211#define luaL_addchar(B,c) \
212 ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \
213 ((B)->b[(B)->n++] = (c)))
214
215#define luaL_addsize(B,s) ((B)->n += (s))
216
217#define luaL_buffsub(B,s) ((B)->n -= (s))
218
219LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
220LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
221LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
222LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
223LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
224LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
225LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz);
226LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
227
228#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
229
230/* }====================================================== */
231
232
233
234/*
235** {======================================================
236** File handles for IO library
237** =======================================================
238*/
239
240/*
241** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
242** initial structure 'luaL_Stream' (it may contain other fields
243** after that initial structure).
244*/
245
246#define LUA_FILEHANDLE "FILE*"
247
248
249typedef struct luaL_Stream {
250 FILE *f; /* stream (NULL for incompletely created streams) */
251 lua_CFunction closef; /* to close stream (NULL for closed streams) */
253
254/* }====================================================== */
255
256/*
257** {==================================================================
258** "Abstraction Layer" for basic report of messages and errors
259** ===================================================================
260*/
261
262/* print a string */
263#if !defined(lua_writestring)
264#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
265#endif
266
267/* print a newline and flush the output */
268#if !defined(lua_writeline)
269#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
270#endif
271
272/* print an error message */
273#if !defined(lua_writestringerror)
274#define lua_writestringerror(s,p) \
275 (fprintf(stderr, (s), (p)), fflush(stderr))
276#endif
277
278/* }================================================================== */
279
280
281/*
282** {============================================================
283** Compatibility with deprecated conversions
284** =============================================================
285*/
286#if defined(LUA_COMPAT_APIINTCASTS)
287
288#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
289#define luaL_optunsigned(L,a,d) \
290 ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
291
292#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
293#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
294
295#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
296#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
297
298#endif
299/* }============================================================ */
300
301
302
303#endif
304
305