lapi.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #if defined(LUA_USE_APICHECK)
  11. #include <assert.h>
  12. #define api_check(l,e,msg) assert(e)
  13. #else /* for testing */
  14. #define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg))
  15. #endif
  16. /* Increments 'L->top.p', checking for stack overflows */
  17. #define api_incr_top(L) \
  18. (L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow"))
  19. /*
  20. ** macros that are executed whenever program enters the Lua core
  21. ** ('lua_lock') and leaves the core ('lua_unlock')
  22. */
  23. #if !defined(lua_lock)
  24. #define lua_lock(L) ((void) 0)
  25. #define lua_unlock(L) ((void) 0)
  26. #endif
  27. /*
  28. ** If a call returns too many multiple returns, the callee may not have
  29. ** stack space to accommodate all results. In this case, this macro
  30. ** increases its stack space ('L->ci->top.p').
  31. */
  32. #define adjustresults(L,nres) \
  33. { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
  34. L->ci->top.p = L->top.p; }
  35. /* Ensure the stack has at least 'n' elements */
  36. #define api_checknelems(L,n) \
  37. api_check(L, (n) < (L->top.p - L->ci->func.p), \
  38. "not enough elements in the stack")
  39. /* Ensure the stack has at least 'n' elements to be popped. (Some
  40. ** functions only update a slot after checking it for popping, but that
  41. ** is only an optimization for a pop followed by a push.)
  42. */
  43. #define api_checkpop(L,n) \
  44. api_check(L, (n) < L->top.p - L->ci->func.p && \
  45. L->tbclist.p < L->top.p - (n), \
  46. "not enough free elements in the stack")
  47. /*
  48. ** To reduce the overhead of returning from C functions, the presence of
  49. ** to-be-closed variables in these functions is coded in the CallInfo's
  50. ** field 'nresults', in a way that functions with no to-be-closed variables
  51. ** with zero, one, or "all" wanted results have no overhead. Functions
  52. ** with other number of wanted results, as well as functions with
  53. ** variables to be closed, have an extra check.
  54. */
  55. #define hastocloseCfunc(n) ((n) < LUA_MULTRET)
  56. /* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
  57. #define codeNresults(n) (-(n) - 3)
  58. #define decodeNresults(n) (-(n) - 3)
  59. #endif