3 ** Limits, basic types, and some other 'installation-dependent' definitions
4 ** See Copyright Notice in lua.h
19 ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
20 ** the total memory used by Lua (in bytes). Usually, 'size_t' and
21 ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
23 #if defined(LUAI_MEM) /* { external definitions? */
24 typedef LUAI_UMEM lu_mem
;
25 typedef LUAI_MEM l_mem
;
26 #elif LUAI_IS32INT /* }{ */
27 typedef size_t lu_mem
;
28 typedef ptrdiff_t l_mem
;
29 #else /* 16-bit ints */ /* }{ */
30 typedef unsigned long lu_mem
;
35 /* chars used as small naturals (so that 'char' is reserved for characters) */
36 typedef unsigned char lu_byte
;
37 typedef signed char ls_byte
;
40 /* maximum value for size_t */
41 #define MAX_SIZET ((size_t)(~(size_t)0))
43 /* maximum size visible for Lua (must be representable in a lua_Integer) */
44 #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
45 : (size_t)(LUA_MAXINTEGER))
48 #define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
50 #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
53 #define MAX_INT INT_MAX /* maximum value of an int */
57 ** floor of the log2 of the maximum signed value for integral type 't'.
58 ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
60 #define log2maxs(t) (sizeof(t) * 8 - 2)
64 ** test whether an unsigned value is a power of 2 (or zero)
66 #define ispow2(x) (((x) & ((x) - 1)) == 0)
69 /* number of chars of a literal string without the ending \0 */
70 #define LL(x) (sizeof(x)/sizeof(char) - 1)
74 ** conversion of pointer to unsigned integer: this is for hashing only;
75 ** there is no problem if the integer cannot hold the whole pointer
76 ** value. (In strict ISO C this may cause undefined behavior, but no
77 ** actual machine seems to bother.)
79 #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
80 __STDC_VERSION__ >= 199901L
82 #if defined(UINTPTR_MAX) /* even in C99 this type is optional */
83 #define L_P2I uintptr_t
84 #else /* no 'intptr'? */
85 #define L_P2I uintmax_t /* use the largest available integer */
87 #else /* C89 option */
91 #define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))
95 /* types of 'usual argument conversions' for lua_Number and lua_Integer */
96 typedef LUAI_UACNUMBER l_uacNumber
;
97 typedef LUAI_UACINT l_uacInt
;
101 ** Internal assertions for in-house debugging
103 #if defined LUAI_ASSERT
106 #define lua_assert(c) assert(c)
109 #if defined(lua_assert)
110 #define check_exp(c,e) (lua_assert(c), (e))
111 /* to avoid problems with conditions too long */
112 #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
114 #define lua_assert(c) ((void)0)
115 #define check_exp(c,e) (e)
116 #define lua_longassert(c) ((void)0)
120 ** assertion for checking API calls
122 #if !defined(luai_apicheck)
123 #define luai_apicheck(l,e) ((void)l, lua_assert(e))
126 #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
129 /* macro to avoid warnings about unused variables */
131 #define UNUSED(x) ((void)(x))
135 /* type casts (a macro highlights casts in the code) */
136 #define cast(t, exp) ((t)(exp))
138 #define cast_void(i) cast(void, (i))
139 #define cast_voidp(i) cast(void *, (i))
140 #define cast_num(i) cast(lua_Number, (i))
141 #define cast_int(i) cast(int, (i))
142 #define cast_uint(i) cast(unsigned int, (i))
143 #define cast_byte(i) cast(lu_byte, (i))
144 #define cast_uchar(i) cast(unsigned char, (i))
145 #define cast_char(i) cast(char, (i))
146 #define cast_charp(i) cast(char *, (i))
147 #define cast_sizet(i) cast(size_t, (i))
150 /* cast a signed lua_Integer to lua_Unsigned */
151 #if !defined(l_castS2U)
152 #define l_castS2U(i) ((lua_Unsigned)(i))
156 ** cast a lua_Unsigned to a signed lua_Integer; this cast is
157 ** not strict ISO C, but two-complement architectures should
160 #if !defined(l_castU2S)
161 #define l_castU2S(i) ((lua_Integer)(i))
168 #if !defined(l_noret)
170 #if defined(__GNUC__)
171 #define l_noret void __attribute__((noreturn))
172 #elif defined(_MSC_VER) && _MSC_VER >= 1200
173 #define l_noret void __declspec(noreturn)
184 #if !defined(LUA_USE_C89)
185 #define l_inline inline
186 #elif defined(__GNUC__)
187 #define l_inline __inline__
189 #define l_inline /* empty */
192 #define l_sinline static l_inline
196 ** type for virtual-machine instructions;
197 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
200 typedef unsigned int l_uint32
;
202 typedef unsigned long l_uint32
;
205 typedef l_uint32 Instruction
;
210 ** Maximum length for short strings, that is, strings that are
211 ** internalized. (Cannot be smaller than reserved words or tags for
212 ** metamethods, as these strings must be internalized;
213 ** #("function") = 8, #("__newindex") = 10.)
215 #if !defined(LUAI_MAXSHORTLEN)
216 #define LUAI_MAXSHORTLEN 40
221 ** Initial size for the string table (must be power of 2).
222 ** The Lua core alone registers ~50 strings (reserved words +
223 ** metaevent keys + a few others). Libraries would typically add
224 ** a few dozens more.
226 #if !defined(MINSTRTABSIZE)
227 #define MINSTRTABSIZE 128
232 ** Size of cache for strings in the API. 'N' is the number of
233 ** sets (better be a prime) and "M" is the size of each set (M == 1
234 ** makes a direct cache.)
236 #if !defined(STRCACHE_N)
237 #define STRCACHE_N 53
242 /* minimum size for string buffer */
243 #if !defined(LUA_MINBUFFER)
244 #define LUA_MINBUFFER 32
249 ** Maximum depth for nested C calls, syntactical nested non-terminals,
250 ** and other features implemented through recursion in C. (Value must
251 ** fit in a 16-bit unsigned integer. It must also be compatible with
252 ** the size of the C stack.)
254 #if !defined(LUAI_MAXCCALLS)
255 #define LUAI_MAXCCALLS 200
260 ** macros that are executed whenever program enters the Lua core
261 ** ('lua_lock') and leaves the core ('lua_unlock')
263 #if !defined(lua_lock)
264 #define lua_lock(L) ((void) 0)
265 #define lua_unlock(L) ((void) 0)
269 ** macro executed during Lua functions at points where the
270 ** function can yield.
272 #if !defined(luai_threadyield)
273 #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
278 ** these macros allow user-specific actions when a thread is
279 ** created/deleted/resumed/yielded.
281 #if !defined(luai_userstateopen)
282 #define luai_userstateopen(L) ((void)L)
285 #if !defined(luai_userstateclose)
286 #define luai_userstateclose(L) ((void)L)
289 #if !defined(luai_userstatethread)
290 #define luai_userstatethread(L,L1) ((void)L)
293 #if !defined(luai_userstatefree)
294 #define luai_userstatefree(L,L1) ((void)L)
297 #if !defined(luai_userstateresume)
298 #define luai_userstateresume(L,n) ((void)L)
301 #if !defined(luai_userstateyield)
302 #define luai_userstateyield(L,n) ((void)L)
308 ** The luai_num* macros define the primitive operations over numbers.
311 /* floor division (defined as 'floor(a/b)') */
312 #if !defined(luai_numidiv)
313 #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
317 #if !defined(luai_numdiv)
318 #define luai_numdiv(L,a,b) ((a)/(b))
322 ** modulo: defined as 'a - floor(a/b)*b'; the direct computation
323 ** using this definition has several problems with rounding errors,
324 ** so it is better to use 'fmod'. 'fmod' gives the result of
325 ** 'a - trunc(a/b)*b', and therefore must be corrected when
326 ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
327 ** non-integer negative result: non-integer result is equivalent to
328 ** a non-zero remainder 'm'; negative result is equivalent to 'a' and
329 ** 'b' with different signs, or 'm' and 'b' with different signs
330 ** (as the result 'm' of 'fmod' has the same sign of 'a').
332 #if !defined(luai_nummod)
333 #define luai_nummod(L,a,b,m) \
334 { (void)L; (m) = l_mathop(fmod)(a,b); \
335 if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
339 #if !defined(luai_numpow)
340 #define luai_numpow(L,a,b) \
341 ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
344 /* the others are quite standard operations */
345 #if !defined(luai_numadd)
346 #define luai_numadd(L,a,b) ((a)+(b))
347 #define luai_numsub(L,a,b) ((a)-(b))
348 #define luai_nummul(L,a,b) ((a)*(b))
349 #define luai_numunm(L,a) (-(a))
350 #define luai_numeq(a,b) ((a)==(b))
351 #define luai_numlt(a,b) ((a)<(b))
352 #define luai_numle(a,b) ((a)<=(b))
353 #define luai_numgt(a,b) ((a)>(b))
354 #define luai_numge(a,b) ((a)>=(b))
355 #define luai_numisnan(a) (!luai_numeq((a), (a)))
363 ** macro to control inclusion of some hard tests on stack reallocation
365 #if !defined(HARDSTACKTESTS)
366 #define condmovestack(L,pre,pos) ((void)0)
368 /* realloc stack keeping its size */
369 #define condmovestack(L,pre,pos) \
370 { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
373 #if !defined(HARDMEMTESTS)
374 #define condchangemem(L,pre,pos) ((void)0)
376 #define condchangemem(L,pre,pos) \
377 { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }