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