lapi.h 1.8 KB

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