xref: /freebsd/contrib/lua/src/lapi.h (revision a9490b81)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lapi.h $
38e3e3a7aSWarner Losh ** Auxiliary functions from Lua API
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #ifndef lapi_h
88e3e3a7aSWarner Losh #define lapi_h
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh 
118e3e3a7aSWarner Losh #include "llimits.h"
128e3e3a7aSWarner Losh #include "lstate.h"
138e3e3a7aSWarner Losh 
140495ed39SKyle Evans 
15*a9490b81SWarner Losh /* Increments 'L->top.p', checking for stack overflows */
16*a9490b81SWarner Losh #define api_incr_top(L)	{L->top.p++; \
17*a9490b81SWarner Losh 			 api_check(L, L->top.p <= L->ci->top.p, \
188e3e3a7aSWarner Losh 					"stack overflow");}
198e3e3a7aSWarner Losh 
208e3e3a7aSWarner Losh 
210495ed39SKyle Evans /*
220495ed39SKyle Evans ** If a call returns too many multiple returns, the callee may not have
230495ed39SKyle Evans ** stack space to accommodate all results. In this case, this macro
24*a9490b81SWarner Losh ** increases its stack space ('L->ci->top.p').
250495ed39SKyle Evans */
260495ed39SKyle Evans #define adjustresults(L,nres) \
27*a9490b81SWarner Losh     { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
28*a9490b81SWarner Losh 	L->ci->top.p = L->top.p; }
290495ed39SKyle Evans 
300495ed39SKyle Evans 
310495ed39SKyle Evans /* Ensure the stack has at least 'n' elements */
32*a9490b81SWarner Losh #define api_checknelems(L,n) \
33*a9490b81SWarner Losh 	api_check(L, (n) < (L->top.p - L->ci->func.p), \
348e3e3a7aSWarner Losh 			  "not enough elements in the stack")
358e3e3a7aSWarner Losh 
368e3e3a7aSWarner Losh 
370495ed39SKyle Evans /*
380495ed39SKyle Evans ** To reduce the overhead of returning from C functions, the presence of
390495ed39SKyle Evans ** to-be-closed variables in these functions is coded in the CallInfo's
400495ed39SKyle Evans ** field 'nresults', in a way that functions with no to-be-closed variables
410495ed39SKyle Evans ** with zero, one, or "all" wanted results have no overhead. Functions
420495ed39SKyle Evans ** with other number of wanted results, as well as functions with
430495ed39SKyle Evans ** variables to be closed, have an extra check.
440495ed39SKyle Evans */
450495ed39SKyle Evans 
460495ed39SKyle Evans #define hastocloseCfunc(n)	((n) < LUA_MULTRET)
470495ed39SKyle Evans 
488c784bb8SWarner Losh /* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
490495ed39SKyle Evans #define codeNresults(n)		(-(n) - 3)
508c784bb8SWarner Losh #define decodeNresults(n)	(-(n) - 3)
510495ed39SKyle Evans 
528e3e3a7aSWarner Losh #endif
53