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