xref: /netbsd/external/mit/lua/dist/src/lapi.h (revision f13f21ab)
1 /*	$NetBSD: lapi.h,v 1.11 2023/06/08 21:12:08 nikita Exp $	*/
2 
3 /*
4 ** Id: lapi.h
5 ** Auxiliary functions from Lua API
6 ** See Copyright Notice in lua.h
7 */
8 
9 #ifndef lapi_h
10 #define lapi_h
11 
12 
13 #include "llimits.h"
14 #include "lstate.h"
15 
16 
17 /* Increments 'L->top.p', checking for stack overflows */
18 #define api_incr_top(L)	{L->top.p++; \
19 			 api_check(L, L->top.p <= L->ci->top.p, \
20 					"stack overflow");}
21 
22 
23 /*
24 ** If a call returns too many multiple returns, the callee may not have
25 ** stack space to accommodate all results. In this case, this macro
26 ** increases its stack space ('L->ci->top.p').
27 */
28 #define adjustresults(L,nres) \
29     { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
30 	L->ci->top.p = L->top.p; }
31 
32 
33 /* Ensure the stack has at least 'n' elements */
34 #define api_checknelems(L,n) \
35 	api_check(L, (n) < (L->top.p - L->ci->func.p), \
36 			  "not enough elements in the stack")
37 
38 
39 /*
40 ** To reduce the overhead of returning from C functions, the presence of
41 ** to-be-closed variables in these functions is coded in the CallInfo's
42 ** field 'nresults', in a way that functions with no to-be-closed variables
43 ** with zero, one, or "all" wanted results have no overhead. Functions
44 ** with other number of wanted results, as well as functions with
45 ** variables to be closed, have an extra check.
46 */
47 
48 #define hastocloseCfunc(n)	((n) < LUA_MULTRET)
49 
50 /* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
51 #define codeNresults(n)		(-(n) - 3)
52 #define decodeNresults(n)	(-(n) - 3)
53 
54 #endif
55