2000-03-24 14:26:08 -03:00
|
|
|
/*
|
2018-08-23 14:26:12 -03:00
|
|
|
** $Id: llimits.h $
|
2014-10-25 09:50:46 -02:00
|
|
|
** Limits, basic types, and some other 'installation-dependent' definitions
|
2000-03-24 14:26:08 -03:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2000-03-24 16:49:23 -03:00
|
|
|
#ifndef llimits_h
|
|
|
|
#define llimits_h
|
2000-03-24 14:26:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
#include <limits.h>
|
2000-05-24 10:54:49 -03:00
|
|
|
#include <stddef.h>
|
2000-03-24 14:26:08 -03:00
|
|
|
|
|
|
|
|
2001-02-23 17:28:56 -03:00
|
|
|
#include "lua.h"
|
|
|
|
|
2019-05-13 14:24:10 -03:00
|
|
|
|
2014-10-29 15:07:45 -02:00
|
|
|
/*
|
2022-11-23 17:29:03 -03:00
|
|
|
** 'lu_mem' is an unsigned integer big enough to count the total memory
|
|
|
|
** used by Lua (in bytes). 'l_obj' is a signed integer big enough to
|
2022-12-13 11:55:14 -03:00
|
|
|
** count the total number of objects used by Lua. (It is signed due
|
2022-11-23 17:29:03 -03:00
|
|
|
** to the use of debt in several computations.) Usually, 'size_t' and
|
2014-10-29 15:07:45 -02:00
|
|
|
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
|
|
|
*/
|
|
|
|
#if defined(LUAI_MEM) /* { external definitions? */
|
2005-03-09 13:28:07 -03:00
|
|
|
typedef LUAI_UMEM lu_mem;
|
2022-11-23 17:29:03 -03:00
|
|
|
typedef LUAI_MEM l_obj;
|
2019-05-13 14:24:10 -03:00
|
|
|
#elif LUAI_IS32INT /* }{ */
|
2014-10-29 15:07:45 -02:00
|
|
|
typedef size_t lu_mem;
|
2022-11-23 17:29:03 -03:00
|
|
|
typedef ptrdiff_t l_obj;
|
2014-10-29 15:07:45 -02:00
|
|
|
#else /* 16-bit ints */ /* }{ */
|
|
|
|
typedef unsigned long lu_mem;
|
2022-11-23 17:29:03 -03:00
|
|
|
typedef long l_obj;
|
2014-10-29 15:07:45 -02:00
|
|
|
#endif /* } */
|
2003-12-01 14:33:30 -02:00
|
|
|
|
2023-12-20 11:06:27 -03:00
|
|
|
#define MAX_LOBJ \
|
|
|
|
cast(l_obj, (cast(lu_mem, 1) << (sizeof(l_obj) * CHAR_BIT - 1)) - 1)
|
2022-12-13 15:45:57 -03:00
|
|
|
|
2003-12-01 14:33:30 -02:00
|
|
|
|
2014-10-25 09:50:46 -02:00
|
|
|
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
2001-02-20 15:15:33 -03:00
|
|
|
typedef unsigned char lu_byte;
|
2017-06-27 08:35:01 -03:00
|
|
|
typedef signed char ls_byte;
|
2000-05-24 10:54:49 -03:00
|
|
|
|
|
|
|
|
2013-06-19 11:27:00 -03:00
|
|
|
/* maximum value for size_t */
|
2014-07-18 15:29:12 -03:00
|
|
|
#define MAX_SIZET ((size_t)(~(size_t)0))
|
2000-05-24 10:54:49 -03:00
|
|
|
|
2023-12-20 11:06:27 -03:00
|
|
|
/*
|
2024-06-20 13:43:33 -03:00
|
|
|
** Maximum size for strings and userdata visible for Lua; should be
|
|
|
|
** representable as a lua_Integer and as a size_t.
|
2023-12-20 11:06:27 -03:00
|
|
|
*/
|
2014-04-11 16:56:04 -03:00
|
|
|
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
|
2024-06-20 13:43:33 -03:00
|
|
|
: cast_sizet(LUA_MAXINTEGER))
|
2017-06-01 16:16:34 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** floor of the log2 of the maximum signed value for integral type 't'.
|
|
|
|
** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
|
|
|
|
*/
|
2022-11-23 17:17:20 -03:00
|
|
|
#define log2maxs(t) cast_int(sizeof(t) * 8 - 2)
|
2000-03-24 14:26:08 -03:00
|
|
|
|
2013-05-27 09:43:37 -03:00
|
|
|
|
2018-06-15 11:13:45 -03:00
|
|
|
/*
|
|
|
|
** test whether an unsigned value is a power of 2 (or zero)
|
|
|
|
*/
|
|
|
|
#define ispow2(x) (((x) & ((x) - 1)) == 0)
|
|
|
|
|
|
|
|
|
2019-04-04 11:45:26 -03:00
|
|
|
/* number of chars of a literal string without the ending \0 */
|
|
|
|
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
|
|
|
|
|
|
|
|
2000-03-24 14:26:08 -03:00
|
|
|
/*
|
2023-02-02 13:37:11 -03:00
|
|
|
** conversion of pointer to unsigned integer: this is for hashing only;
|
|
|
|
** there is no problem if the integer cannot hold the whole pointer
|
|
|
|
** value. (In strict ISO C this may cause undefined behavior, but no
|
|
|
|
** actual machine seems to bother.)
|
2000-03-24 14:26:08 -03:00
|
|
|
*/
|
2023-02-02 13:37:11 -03:00
|
|
|
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
|
|
|
|
__STDC_VERSION__ >= 199901L
|
|
|
|
#include <stdint.h>
|
|
|
|
#if defined(UINTPTR_MAX) /* even in C99 this type is optional */
|
|
|
|
#define L_P2I uintptr_t
|
|
|
|
#else /* no 'intptr'? */
|
2023-04-18 09:44:10 -03:00
|
|
|
#define L_P2I uintmax_t /* use the largest available integer */
|
2023-02-02 13:37:11 -03:00
|
|
|
#endif
|
|
|
|
#else /* C89 option */
|
|
|
|
#define L_P2I size_t
|
|
|
|
#endif
|
|
|
|
|
2024-03-22 14:06:11 -03:00
|
|
|
#define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX)
|
2000-03-24 14:26:08 -03:00
|
|
|
|
|
|
|
|
2000-03-31 13:28:45 -03:00
|
|
|
|
2014-04-11 16:56:04 -03:00
|
|
|
/* types of 'usual argument conversions' for lua_Number and lua_Integer */
|
2005-03-09 13:28:07 -03:00
|
|
|
typedef LUAI_UACNUMBER l_uacNumber;
|
2014-04-11 16:56:04 -03:00
|
|
|
typedef LUAI_UACINT l_uacInt;
|
2000-10-26 10:47:05 -02:00
|
|
|
|
|
|
|
|
2020-07-08 15:51:55 -03:00
|
|
|
/*
|
|
|
|
** Internal assertions for in-house debugging
|
|
|
|
*/
|
|
|
|
#if defined LUAI_ASSERT
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#define lua_assert(c) assert(c)
|
|
|
|
#endif
|
|
|
|
|
2009-12-17 10:26:09 -02:00
|
|
|
#if defined(lua_assert)
|
2005-12-27 15:12:00 -02:00
|
|
|
#define check_exp(c,e) (lua_assert(c), (e))
|
2011-05-05 16:43:14 -03:00
|
|
|
/* to avoid problems with conditions too long */
|
2015-09-08 13:53:56 -03:00
|
|
|
#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
|
2005-12-27 15:12:00 -02:00
|
|
|
#else
|
2011-09-13 14:39:23 -03:00
|
|
|
#define lua_assert(c) ((void)0)
|
2005-12-27 15:12:00 -02:00
|
|
|
#define check_exp(c,e) (e)
|
2011-09-13 14:39:23 -03:00
|
|
|
#define lua_longassert(c) ((void)0)
|
2009-12-17 10:26:09 -02:00
|
|
|
#endif
|
2005-12-27 15:12:00 -02:00
|
|
|
|
2002-06-13 10:45:31 -03:00
|
|
|
|
2015-01-16 11:26:55 -02:00
|
|
|
/* macro to avoid warnings about unused variables */
|
2009-12-17 10:26:09 -02:00
|
|
|
#if !defined(UNUSED)
|
2015-01-16 11:26:55 -02:00
|
|
|
#define UNUSED(x) ((void)(x))
|
2002-03-18 15:16:16 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-01-16 11:26:55 -02:00
|
|
|
/* type casts (a macro highlights casts in the code) */
|
2002-03-18 15:16:16 -03:00
|
|
|
#define cast(t, exp) ((t)(exp))
|
|
|
|
|
2014-03-07 13:19:00 -03:00
|
|
|
#define cast_void(i) cast(void, (i))
|
2018-01-28 13:13:26 -02:00
|
|
|
#define cast_voidp(i) cast(void *, (i))
|
2005-12-22 14:19:56 -02:00
|
|
|
#define cast_num(i) cast(lua_Number, (i))
|
|
|
|
#define cast_int(i) cast(int, (i))
|
2018-01-28 13:13:26 -02:00
|
|
|
#define cast_uint(i) cast(unsigned int, (i))
|
|
|
|
#define cast_byte(i) cast(lu_byte, (i))
|
2010-12-10 11:40:22 -02:00
|
|
|
#define cast_uchar(i) cast(unsigned char, (i))
|
2018-01-28 13:13:26 -02:00
|
|
|
#define cast_char(i) cast(char, (i))
|
|
|
|
#define cast_charp(i) cast(char *, (i))
|
|
|
|
#define cast_sizet(i) cast(size_t, (i))
|
2014-04-15 11:29:30 -03:00
|
|
|
|
|
|
|
|
2014-04-15 13:32:49 -03:00
|
|
|
/* cast a signed lua_Integer to lua_Unsigned */
|
|
|
|
#if !defined(l_castS2U)
|
|
|
|
#define l_castS2U(i) ((lua_Unsigned)(i))
|
|
|
|
#endif
|
|
|
|
|
2014-04-15 11:29:30 -03:00
|
|
|
/*
|
|
|
|
** cast a lua_Unsigned to a signed lua_Integer; this cast is
|
2014-11-02 17:33:33 -02:00
|
|
|
** not strict ISO C, but two-complement architectures should
|
2014-04-15 11:29:30 -03:00
|
|
|
** work fine.
|
|
|
|
*/
|
2014-04-15 13:32:49 -03:00
|
|
|
#if !defined(l_castU2S)
|
|
|
|
#define l_castU2S(i) ((lua_Integer)(i))
|
|
|
|
#endif
|
2005-12-22 14:19:56 -02:00
|
|
|
|
2002-03-18 15:16:16 -03:00
|
|
|
|
2011-10-07 17:45:19 -03:00
|
|
|
/*
|
|
|
|
** non-return type
|
|
|
|
*/
|
2018-05-30 11:25:52 -03:00
|
|
|
#if !defined(l_noret)
|
|
|
|
|
2011-10-07 17:45:19 -03:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define l_noret void __attribute__((noreturn))
|
2014-12-19 11:30:23 -02:00
|
|
|
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
2011-10-07 17:45:19 -03:00
|
|
|
#define l_noret void __declspec(noreturn)
|
|
|
|
#else
|
|
|
|
#define l_noret void
|
|
|
|
#endif
|
|
|
|
|
2018-05-30 11:25:52 -03:00
|
|
|
#endif
|
2011-10-07 17:45:19 -03:00
|
|
|
|
|
|
|
|
2021-09-15 11:18:41 -03:00
|
|
|
/*
|
|
|
|
** Inline functions
|
|
|
|
*/
|
|
|
|
#if !defined(LUA_USE_C89)
|
|
|
|
#define l_inline inline
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define l_inline __inline__
|
|
|
|
#else
|
|
|
|
#define l_inline /* empty */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define l_sinline static l_inline
|
|
|
|
|
|
|
|
|
2000-03-24 14:26:08 -03:00
|
|
|
/*
|
2024-06-20 13:43:33 -03:00
|
|
|
** An unsigned with (at least) 4 bytes
|
2000-03-24 14:26:08 -03:00
|
|
|
*/
|
2019-05-13 14:24:10 -03:00
|
|
|
#if LUAI_IS32INT
|
2018-12-27 14:32:29 -02:00
|
|
|
typedef unsigned int l_uint32;
|
2014-10-29 15:07:45 -02:00
|
|
|
#else
|
2018-12-27 14:32:29 -02:00
|
|
|
typedef unsigned long l_uint32;
|
2014-10-29 15:07:45 -02:00
|
|
|
#endif
|
2000-03-24 14:26:08 -03:00
|
|
|
|
2009-12-17 10:50:20 -02:00
|
|
|
|
2015-02-05 15:15:33 -02:00
|
|
|
/*
|
|
|
|
** The luai_num* macros define the primitive operations over numbers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* floor division (defined as 'floor(a/b)') */
|
|
|
|
#if !defined(luai_numidiv)
|
2015-02-09 13:41:56 -02:00
|
|
|
#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
|
2015-02-05 15:15:33 -02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* float division */
|
|
|
|
#if !defined(luai_numdiv)
|
|
|
|
#define luai_numdiv(L,a,b) ((a)/(b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2018-08-28 12:36:58 -03:00
|
|
|
** modulo: defined as 'a - floor(a/b)*b'; the direct computation
|
|
|
|
** using this definition has several problems with rounding errors,
|
|
|
|
** so it is better to use 'fmod'. 'fmod' gives the result of
|
|
|
|
** 'a - trunc(a/b)*b', and therefore must be corrected when
|
|
|
|
** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
|
2018-09-11 08:39:12 -03:00
|
|
|
** non-integer negative result: non-integer result is equivalent to
|
|
|
|
** a non-zero remainder 'm'; negative result is equivalent to 'a' and
|
|
|
|
** 'b' with different signs, or 'm' and 'b' with different signs
|
|
|
|
** (as the result 'm' of 'fmod' has the same sign of 'a').
|
2015-02-05 15:15:33 -02:00
|
|
|
*/
|
|
|
|
#if !defined(luai_nummod)
|
|
|
|
#define luai_nummod(L,a,b,m) \
|
2018-08-28 12:36:58 -03:00
|
|
|
{ (void)L; (m) = l_mathop(fmod)(a,b); \
|
|
|
|
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
|
2015-02-05 15:15:33 -02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* exponentiation */
|
|
|
|
#if !defined(luai_numpow)
|
2020-11-13 09:59:07 -03:00
|
|
|
#define luai_numpow(L,a,b) \
|
|
|
|
((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
|
2015-02-05 15:15:33 -02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* the others are quite standard operations */
|
|
|
|
#if !defined(luai_numadd)
|
|
|
|
#define luai_numadd(L,a,b) ((a)+(b))
|
|
|
|
#define luai_numsub(L,a,b) ((a)-(b))
|
|
|
|
#define luai_nummul(L,a,b) ((a)*(b))
|
|
|
|
#define luai_numunm(L,a) (-(a))
|
|
|
|
#define luai_numeq(a,b) ((a)==(b))
|
|
|
|
#define luai_numlt(a,b) ((a)<(b))
|
|
|
|
#define luai_numle(a,b) ((a)<=(b))
|
2019-03-22 13:37:17 -03:00
|
|
|
#define luai_numgt(a,b) ((a)>(b))
|
|
|
|
#define luai_numge(a,b) ((a)>=(b))
|
2015-02-05 15:15:33 -02:00
|
|
|
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2024-06-21 12:29:08 -03:00
|
|
|
/*
|
|
|
|
** {==================================================================
|
|
|
|
** "Abstraction Layer" for basic report of messages and errors
|
|
|
|
** ===================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* print a string */
|
|
|
|
#if !defined(lua_writestring)
|
|
|
|
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
2000-03-24 14:26:08 -03:00
|
|
|
#endif
|
2024-06-21 12:29:08 -03:00
|
|
|
|
|
|
|
/* print a newline and flush the output */
|
|
|
|
#if !defined(lua_writeline)
|
|
|
|
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print an error message */
|
|
|
|
#if !defined(lua_writestringerror)
|
|
|
|
#define lua_writestringerror(s,p) \
|
|
|
|
(fprintf(stderr, (s), (p)), fflush(stderr))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* }================================================================== */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|