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