Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
lua.h
1/*
2** $Id: lua.h $
3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (www.lua.org)
5** See Copyright Notice at the end of this file
6*/
7
8
9#ifndef lua_h
10#define lua_h
11
12#include <stdarg.h>
13#include <stddef.h>
14
15
16#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio"
17#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
18
19
20#define LUA_VERSION_MAJOR_N 5
21#define LUA_VERSION_MINOR_N 5
22#define LUA_VERSION_RELEASE_N 0
23
24#define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N)
25#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N)
26
27
28#include "luaconf.h"
29
30
31/* mark for precompiled code ('<esc>Lua') */
32#define LUA_SIGNATURE "\x1bLua"
33
34/* option for multiple returns in 'lua_pcall' and 'lua_call' */
35#define LUA_MULTRET (-1)
36
37
38/*
39** Pseudo-indices
40** (The stack size is limited to INT_MAX/2; we keep some free empty
41** space after that to help overflow detection.)
42*/
43#define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000))
44#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
45
46
47/* thread status */
48#define LUA_OK 0
49#define LUA_YIELD 1
50#define LUA_ERRRUN 2
51#define LUA_ERRSYNTAX 3
52#define LUA_ERRMEM 4
53#define LUA_ERRERR 5
54
55
56typedef struct lua_State lua_State;
57
58
59/*
60** basic types
61*/
62#define LUA_TNONE (-1)
63
64#define LUA_TNIL 0
65#define LUA_TBOOLEAN 1
66#define LUA_TLIGHTUSERDATA 2
67#define LUA_TNUMBER 3
68#define LUA_TSTRING 4
69#define LUA_TTABLE 5
70#define LUA_TFUNCTION 6
71#define LUA_TUSERDATA 7
72#define LUA_TTHREAD 8
73
74#define LUA_NUMTYPES 9
75
76
77
78/* minimum Lua stack available to a C function */
79#define LUA_MINSTACK 20
80
81
82/* predefined values in the registry */
83/* index 1 is reserved for the reference mechanism */
84#define LUA_RIDX_GLOBALS 2
85#define LUA_RIDX_MAINTHREAD 3
86#define LUA_RIDX_LAST 3
87
88
89/* type of numbers in Lua */
90typedef LUA_NUMBER lua_Number;
91
92
93/* type for integer functions */
94typedef LUA_INTEGER lua_Integer;
95
96/* unsigned integer type */
97typedef LUA_UNSIGNED lua_Unsigned;
98
99/* type for continuation-function contexts */
100typedef LUA_KCONTEXT lua_KContext;
101
102
103/*
104** Type for C functions registered with Lua
105*/
106typedef int (*lua_CFunction) (lua_State *L);
107
108/*
109** Type for continuation functions
110*/
111typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
112
113
114/*
115** Type for functions that read/write blocks when loading/dumping Lua chunks
116*/
117typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
118
119typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
120
121
122/*
123** Type for memory-allocation functions
124*/
125typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
126
127
128/*
129** Type for warning functions
130*/
131typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
132
133
134/*
135** Type used by the debug API to collect debug information
136*/
137typedef struct lua_Debug lua_Debug;
138
139
140/*
141** Functions to be called by the debugger in specific events
142*/
143typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
144
145
146/*
147** generic extra include file
148*/
149#if defined(LUA_USER_H)
150#include LUA_USER_H
151#endif
152
153
154/*
155** RCS ident string
156*/
157extern const char lua_ident[];
158
159
160/*
161** state manipulation
162*/
163LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, unsigned seed);
164LUA_API void (lua_close) (lua_State *L);
165LUA_API lua_State *(lua_newthread) (lua_State *L);
166LUA_API int (lua_closethread) (lua_State *L, lua_State *from);
167
168LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
169
170
171LUA_API lua_Number (lua_version) (lua_State *L);
172
173
174/*
175** basic stack manipulation
176*/
177LUA_API int (lua_absindex) (lua_State *L, int idx);
178LUA_API int (lua_gettop) (lua_State *L);
179LUA_API void (lua_settop) (lua_State *L, int idx);
180LUA_API void (lua_pushvalue) (lua_State *L, int idx);
181LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
182LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
183LUA_API int (lua_checkstack) (lua_State *L, int n);
184
185LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
186
187
188/*
189** access functions (stack -> C)
190*/
191
192LUA_API int (lua_isnumber) (lua_State *L, int idx);
193LUA_API int (lua_isstring) (lua_State *L, int idx);
194LUA_API int (lua_iscfunction) (lua_State *L, int idx);
195LUA_API int (lua_isinteger) (lua_State *L, int idx);
196LUA_API int (lua_isuserdata) (lua_State *L, int idx);
197LUA_API int (lua_type) (lua_State *L, int idx);
198LUA_API const char *(lua_typename) (lua_State *L, int tp);
199
200LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
201LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
202LUA_API int (lua_toboolean) (lua_State *L, int idx);
203LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
204LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx);
205LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
206LUA_API void *(lua_touserdata) (lua_State *L, int idx);
207LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
208LUA_API const void *(lua_topointer) (lua_State *L, int idx);
209
210
211/*
212** Comparison and arithmetic functions
213*/
214
215#define LUA_OPADD 0 /* ORDER TM, ORDER OP */
216#define LUA_OPSUB 1
217#define LUA_OPMUL 2
218#define LUA_OPMOD 3
219#define LUA_OPPOW 4
220#define LUA_OPDIV 5
221#define LUA_OPIDIV 6
222#define LUA_OPBAND 7
223#define LUA_OPBOR 8
224#define LUA_OPBXOR 9
225#define LUA_OPSHL 10
226#define LUA_OPSHR 11
227#define LUA_OPUNM 12
228#define LUA_OPBNOT 13
229
230LUA_API void (lua_arith) (lua_State *L, int op);
231
232#define LUA_OPEQ 0
233#define LUA_OPLT 1
234#define LUA_OPLE 2
235
236LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
237LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
238
239
240/*
241** push functions (C -> stack)
242*/
243LUA_API void (lua_pushnil) (lua_State *L);
244LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
245LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
246LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
247LUA_API const char *(lua_pushexternalstring) (lua_State *L,
248 const char *s, size_t len, lua_Alloc falloc, void *ud);
249LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
250LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
251 va_list argp);
252LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
253LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
254LUA_API void (lua_pushboolean) (lua_State *L, int b);
255LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
256LUA_API int (lua_pushthread) (lua_State *L);
257
258
259/*
260** get functions (Lua -> stack)
261*/
262LUA_API int (lua_getglobal) (lua_State *L, const char *name);
263LUA_API int (lua_gettable) (lua_State *L, int idx);
264LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
265LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
266LUA_API int (lua_rawget) (lua_State *L, int idx);
267LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
268LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
269
270LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
271LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
272LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
273LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n);
274
275
276/*
277** set functions (stack -> Lua)
278*/
279LUA_API void (lua_setglobal) (lua_State *L, const char *name);
280LUA_API void (lua_settable) (lua_State *L, int idx);
281LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
282LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
283LUA_API void (lua_rawset) (lua_State *L, int idx);
284LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
285LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
286LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
287LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n);
288
289
290/*
291** 'load' and 'call' functions (load and run Lua code)
292*/
293LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults,
294 lua_KContext ctx, lua_KFunction k);
295#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
296
297LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
298 lua_KContext ctx, lua_KFunction k);
299#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
300
301LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
302 const char *chunkname, const char *mode);
303
304LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
305
306
307/*
308** coroutine functions
309*/
310LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
311 lua_KFunction k);
312LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg,
313 int *nres);
314LUA_API int (lua_status) (lua_State *L);
315LUA_API int (lua_isyieldable) (lua_State *L);
316
317#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
318
319
320/*
321** Warning-related functions
322*/
323LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud);
324LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
325
326
327/*
328** garbage-collection options
329*/
330
331#define LUA_GCSTOP 0
332#define LUA_GCRESTART 1
333#define LUA_GCCOLLECT 2
334#define LUA_GCCOUNT 3
335#define LUA_GCCOUNTB 4
336#define LUA_GCSTEP 5
337#define LUA_GCISRUNNING 6
338#define LUA_GCGEN 7
339#define LUA_GCINC 8
340#define LUA_GCPARAM 9
341
342
343/*
344** garbage-collection parameters
345*/
346/* parameters for generational mode */
347#define LUA_GCPMINORMUL 0 /* control minor collections */
348#define LUA_GCPMAJORMINOR 1 /* control shift major->minor */
349#define LUA_GCPMINORMAJOR 2 /* control shift minor->major */
350
351/* parameters for incremental mode */
352#define LUA_GCPPAUSE 3 /* size of pause between successive GCs */
353#define LUA_GCPSTEPMUL 4 /* GC "speed" */
354#define LUA_GCPSTEPSIZE 5 /* GC granularity */
355
356/* number of parameters */
357#define LUA_GCPN 6
358
359
360LUA_API int (lua_gc) (lua_State *L, int what, ...);
361
362
363/*
364** miscellaneous functions
365*/
366
367LUA_API int (lua_error) (lua_State *L);
368
369LUA_API int (lua_next) (lua_State *L, int idx);
370
371LUA_API void (lua_concat) (lua_State *L, int n);
372LUA_API void (lua_len) (lua_State *L, int idx);
373
374#define LUA_N2SBUFFSZ 64
375LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff);
376LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
377
378LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
379LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
380
381LUA_API void (lua_toclose) (lua_State *L, int idx);
382LUA_API void (lua_closeslot) (lua_State *L, int idx);
383
384
385/*
386** {==============================================================
387** some useful macros
388** ===============================================================
389*/
390
391#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE))
392
393#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
394#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
395
396#define lua_pop(L,n) lua_settop(L, -(n)-1)
397
398#define lua_newtable(L) lua_createtable(L, 0, 0)
399
400#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
401
402#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
403
404#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
405#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
406#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
407#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
408#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
409#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
410#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
411#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
412
413#define lua_pushliteral(L, s) lua_pushstring(L, "" s)
414
415#define lua_pushglobaltable(L) \
416 ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS))
417
418#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
419
420
421#define lua_insert(L,idx) lua_rotate(L, (idx), 1)
422
423#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
424
425#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1))
426
427/* }============================================================== */
428
429
430/*
431** {==============================================================
432** compatibility macros
433** ===============================================================
434*/
435
436#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1)
437#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
438#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
439
440#define lua_resetthread(L) lua_closethread(L,NULL)
441
442/* }============================================================== */
443
444/*
445** {======================================================================
446** Debug API
447** =======================================================================
448*/
449
450
451/*
452** Event codes
453*/
454#define LUA_HOOKCALL 0
455#define LUA_HOOKRET 1
456#define LUA_HOOKLINE 2
457#define LUA_HOOKCOUNT 3
458#define LUA_HOOKTAILCALL 4
459
460
461/*
462** Event masks
463*/
464#define LUA_MASKCALL (1 << LUA_HOOKCALL)
465#define LUA_MASKRET (1 << LUA_HOOKRET)
466#define LUA_MASKLINE (1 << LUA_HOOKLINE)
467#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
468
469
470LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
471LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
472LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n);
473LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n);
474LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n);
475LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
476
477LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n);
478LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1,
479 int fidx2, int n2);
480
481LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);
482LUA_API lua_Hook (lua_gethook) (lua_State *L);
483LUA_API int (lua_gethookmask) (lua_State *L);
484LUA_API int (lua_gethookcount) (lua_State *L);
485
486
487struct lua_Debug {
488 int event;
489 const char *name; /* (n) */
490 const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */
491 const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */
492 const char *source; /* (S) */
493 size_t srclen; /* (S) */
494 int currentline; /* (l) */
495 int linedefined; /* (S) */
496 int lastlinedefined; /* (S) */
497 unsigned char nups; /* (u) number of upvalues */
498 unsigned char nparams;/* (u) number of parameters */
499 char isvararg; /* (u) */
500 unsigned char extraargs; /* (t) number of extra arguments */
501 char istailcall; /* (t) */
502 int ftransfer; /* (r) index of first value transferred */
503 int ntransfer; /* (r) number of transferred values */
504 char short_src[LUA_IDSIZE]; /* (S) */
505 /* private part */
506 struct CallInfo *i_ci; /* active function */
507};
508
509/* }====================================================================== */
510
511
512#define LUAI_TOSTRAUX(x) #x
513#define LUAI_TOSTR(x) LUAI_TOSTRAUX(x)
514
515#define LUA_VERSION_MAJOR LUAI_TOSTR(LUA_VERSION_MAJOR_N)
516#define LUA_VERSION_MINOR LUAI_TOSTR(LUA_VERSION_MINOR_N)
517#define LUA_VERSION_RELEASE LUAI_TOSTR(LUA_VERSION_RELEASE_N)
518
519#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
520#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
521
522
523/******************************************************************************
524* Copyright (C) 1994-2025 Lua.org, PUC-Rio.
525*
526* Permission is hereby granted, free of charge, to any person obtaining
527* a copy of this software and associated documentation files (the
528* "Software"), to deal in the Software without restriction, including
529* without limitation the rights to use, copy, modify, merge, publish,
530* distribute, sublicense, and/or sell copies of the Software, and to
531* permit persons to whom the Software is furnished to do so, subject to
532* the following conditions:
533*
534* The above copyright notice and this permission notice shall be
535* included in all copies or substantial portions of the Software.
536*
537* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
538* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
539* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
540* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
541* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
542* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
543* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
544******************************************************************************/
545
546
547#endif