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