From a08d82eb132bfd9db5b91e0d5ebcb81d7b26dcd0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Jun 2024 14:46:06 -0300 Subject: [PATCH] llimits.h being used by all Lua code The definitions in llimits.h are useful not only for the core. That header only defines types and '#define's, so libs and core still do not share any real code/data. --- lauxlib.c | 1 + lauxlib.h | 15 --------------- lbaselib.c | 1 + lcorolib.c | 1 + ldblib.c | 1 + linit.c | 1 + liolib.c | 3 +-- lmathlib.c | 21 +++++++-------------- loadlib.c | 1 + loslib.c | 1 + lstrlib.c | 49 +++++++++++++++++++++++-------------------------- ltablib.c | 1 + lua.c | 1 + lutf8lib.c | 24 ++++++++---------------- 14 files changed, 48 insertions(+), 73 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 5f8e8f42..fe99aca1 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -25,6 +25,7 @@ #include "lua.h" #include "lauxlib.h" +#include "llimits.h" #if !defined(MAX_SIZET) diff --git a/lauxlib.h b/lauxlib.h index 3c370686..6c5ecbb7 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -168,21 +168,6 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_pushfail(L) lua_pushnil(L) -/* -** Internal assertions for in-house debugging -*/ -#if !defined(lua_assert) - -#if defined LUAI_ASSERT - #include - #define lua_assert(c) assert(c) -#else - #define lua_assert(c) ((void)0) -#endif - -#endif - - /* ** {====================================================== diff --git a/lbaselib.c b/lbaselib.c index b2da6a77..8b034340 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" static int luaB_print (lua_State *L) { diff --git a/lcorolib.c b/lcorolib.c index c64adf08..3d95f873 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -16,6 +16,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" static lua_State *getco (lua_State *L) { diff --git a/ldblib.c b/ldblib.c index 2c941384..a0a06dd7 100644 --- a/ldblib.c +++ b/ldblib.c @@ -18,6 +18,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/linit.c b/linit.c index 140f6d75..00d06f7e 100644 --- a/linit.c +++ b/linit.c @@ -18,6 +18,7 @@ #include "lualib.h" #include "lauxlib.h" +#include "llimits.h" /* diff --git a/liolib.c b/liolib.c index c5075f3e..4349f860 100644 --- a/liolib.c +++ b/liolib.c @@ -21,8 +21,7 @@ #include "lauxlib.h" #include "lualib.h" - - +#include "llimits.h" /* diff --git a/lmathlib.c b/lmathlib.c index c1041f37..2bdcb637 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -20,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #undef PI @@ -366,25 +367,17 @@ static lua_Number I2d (Rand64 x) { #else /* no 'Rand64' }{ */ -/* get an integer with at least 32 bits */ -#if LUAI_IS32INT -typedef unsigned int lu_int32; -#else -typedef unsigned long lu_int32; -#endif - - /* ** Use two 32-bit integers to represent a 64-bit quantity. */ typedef struct Rand64 { - lu_int32 h; /* higher half */ - lu_int32 l; /* lower half */ + l_uint32 h; /* higher half */ + l_uint32 l; /* lower half */ } Rand64; /* -** If 'lu_int32' has more than 32 bits, the extra bits do not interfere +** If 'l_uint32' has more than 32 bits, the extra bits do not interfere ** with the 32 initial bits, except in a right shift and comparisons. ** Moreover, the final result has to discard the extra bits. */ @@ -398,7 +391,7 @@ typedef struct Rand64 { */ /* build a new Rand64 value */ -static Rand64 packI (lu_int32 h, lu_int32 l) { +static Rand64 packI (l_uint32 h, l_uint32 l) { Rand64 result; result.h = h; result.l = l; @@ -471,7 +464,7 @@ static Rand64 nextrand (Rand64 *state) { */ /* an unsigned 1 with proper type */ -#define UONE ((lu_int32)1) +#define UONE ((l_uint32)1) #if FIGS <= 32 @@ -522,7 +515,7 @@ static lua_Unsigned I2UInt (Rand64 x) { /* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { - return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n); + return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n); } #endif /* } */ diff --git a/loadlib.c b/loadlib.c index 7b4bb16a..45db3b72 100644 --- a/loadlib.c +++ b/loadlib.c @@ -22,6 +22,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/loslib.c b/loslib.c index ba80d72c..8280331b 100644 --- a/loslib.c +++ b/loslib.c @@ -20,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/lstrlib.c b/lstrlib.c index a90c4fd1..97d974f8 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -24,6 +24,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* @@ -36,10 +37,6 @@ #endif -/* macro to 'unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - /* ** Some sizes are better limited to fit in 'int', but must also fit in ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) @@ -128,7 +125,7 @@ static int str_lower (lua_State *L) { const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i=0; i= ms->src_end) return 0; else { - int c = uchar(*s); + int c = cast_uchar(*s); switch (*p) { case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); + case L_ESC: return match_class(c, cast_uchar(*(p+1))); case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); + default: return (cast_uchar(*p) == c); } } } @@ -612,8 +609,8 @@ static const char *match (MatchState *ms, const char *s, const char *p) { luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); - if (!matchbracketclass(uchar(previous), p, ep - 1) && - matchbracketclass(uchar(*s), p, ep - 1)) { + if (!matchbracketclass(cast_uchar(previous), p, ep - 1) && + matchbracketclass(cast_uchar(*s), p, ep - 1)) { p = ep; goto init; /* return match(ms, s, ep); */ } s = NULL; /* match failed */ @@ -622,7 +619,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p + 1))); + s = match_capture(ms, s, cast_uchar(*(p + 1))); if (s != NULL) { p += 2; goto init; /* return match(ms, s, p + 2) */ } @@ -887,7 +884,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, luaL_addchar(b, *p); else if (*p == '0') /* '%0' */ luaL_addlstring(b, s, e - s); - else if (isdigit(uchar(*p))) { /* '%n' */ + else if (isdigit(cast_uchar(*p))) { /* '%n' */ const char *cap; ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); if (resl == CAP_POSITION) @@ -1065,7 +1062,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, if (fmt[SIZELENMOD] == 'A') { int i; for (i = 0; i < n; i++) - buff[i] = toupper(uchar(buff[i])); + buff[i] = toupper(cast_uchar(buff[i])); } else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); @@ -1132,12 +1129,12 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } - else if (iscntrl(uchar(*s))) { + else if (iscntrl(cast_uchar(*s))) { char buff[10]; - if (!isdigit(uchar(*(s+1)))) - l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); + if (!isdigit(cast_uchar(*(s+1)))) + l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s)); else - l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s)); luaL_addstring(b, buff); } else @@ -1214,9 +1211,9 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { static const char *get2digits (const char *s) { - if (isdigit(uchar(*s))) { + if (isdigit(cast_uchar(*s))) { s++; - if (isdigit(uchar(*s))) s++; /* (2 digits at most) */ + if (isdigit(cast_uchar(*s))) s++; /* (2 digits at most) */ } return s; } @@ -1239,7 +1236,7 @@ static void checkformat (lua_State *L, const char *form, const char *flags, spec = get2digits(spec); /* skip precision */ } } - if (!isalpha(uchar(*spec))) /* did not go to the end? */ + if (!isalpha(cast_uchar(*spec))) /* did not go to the end? */ luaL_error(L, "invalid conversion specification: '%s'", form); } diff --git a/ltablib.c b/ltablib.c index a402daea..b5948591 100644 --- a/ltablib.c +++ b/ltablib.c @@ -18,6 +18,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/lua.c b/lua.c index d109acbf..88fb8793 100644 --- a/lua.c +++ b/lua.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #if !defined(LUA_PROGNAME) diff --git a/lutf8lib.c b/lutf8lib.c index 7b747937..243196c8 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #define MAXUNICODE 0x10FFFFu @@ -28,15 +29,6 @@ #define MSGInvalid "invalid UTF-8 code" -/* -** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. -*/ -#if (UINT_MAX >> 30) >= 1 -typedef unsigned int utfint; -#else -typedef unsigned long utfint; -#endif - #define iscont(c) (((c) & 0xC0) == 0x80) #define iscontp(p) iscont(*(p)) @@ -58,11 +50,11 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { ** entry forces an error for non-ascii bytes with no continuation ** bytes (count == 0). */ -static const char *utf8_decode (const char *s, utfint *val, int strict) { - static const utfint limits[] = - {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; +static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { + static const l_uint32 limits[] = + {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; unsigned int c = (unsigned char)s[0]; - utfint res = 0; /* final result */ + l_uint32 res = 0; /* final result */ if (c < 0x80) /* ascii? */ res = c; else { @@ -73,7 +65,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) { return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } - res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ + res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ if (count > 5 || res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ @@ -141,7 +133,7 @@ static int codepoint (lua_State *L) { n = 0; /* count the number of returns */ se = s + pose; /* string end */ for (s += posi - 1; s < se;) { - utfint code; + l_uint32 code; s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, MSGInvalid); @@ -243,7 +235,7 @@ static int iter_aux (lua_State *L, int strict) { if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { - utfint code; + l_uint32 code; const char *next = utf8_decode(s + n, &code, strict); if (next == NULL || iscontp(next)) return luaL_error(L, MSGInvalid);