lapi.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. /*
  26. ** To reduce the overhead of returning from C functions, the presence of
  27. ** to-be-closed variables in these functions is coded in the CallInfo's
  28. ** field 'nresults', in a way that functions with no to-be-closed variables
  29. ** with zero, one, or "all" wanted results have no overhead. Functions
  30. ** with other number of wanted results, as well as functions with
  31. ** variables to be closed, have an extra check.
  32. */
  33. #define hastocloseCfunc(n) ((n) < LUA_MULTRET)
  34. /* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
  35. #define codeNresults(n) (-(n) - 3)
  36. #define decodeNresults(n) (-(n) - 3)
  37. #endif