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.
This commit is contained in:
Roberto Ierusalimschy 2024-06-20 14:46:06 -03:00
parent 55ac40f859
commit a08d82eb13
14 changed files with 48 additions and 73 deletions

View File

@ -25,6 +25,7 @@
#include "lua.h"
#include "lauxlib.h"
#include "llimits.h"
#if !defined(MAX_SIZET)

View File

@ -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 <assert.h>
#define lua_assert(c) assert(c)
#else
#define lua_assert(c) ((void)0)
#endif
#endif
/*
** {======================================================

View File

@ -19,6 +19,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
static int luaB_print (lua_State *L) {

View File

@ -16,6 +16,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
static lua_State *getco (lua_State *L) {

View File

@ -18,6 +18,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
/*

View File

@ -18,6 +18,7 @@
#include "lualib.h"
#include "lauxlib.h"
#include "llimits.h"
/*

View File

@ -21,8 +21,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
/*

View File

@ -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 /* } */

View File

@ -22,6 +22,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
/*

View File

@ -20,6 +20,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
/*

View File

@ -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<l; i++)
p[i] = tolower(uchar(s[i]));
p[i] = tolower(cast_uchar(s[i]));
luaL_pushresultsize(&b, l);
return 1;
}
@ -141,7 +138,7 @@ static int str_upper (lua_State *L) {
const char *s = luaL_checklstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i=0; i<l; i++)
p[i] = toupper(uchar(s[i]));
p[i] = toupper(cast_uchar(s[i]));
luaL_pushresultsize(&b, l);
return 1;
}
@ -187,7 +184,7 @@ static int str_byte (lua_State *L) {
n = (int)(pose - posi) + 1;
luaL_checkstack(L, n, "string slice too long");
for (i=0; i<n; i++)
lua_pushinteger(L, uchar(s[posi+i-1]));
lua_pushinteger(L, cast_uchar(s[posi+i-1]));
return n;
}
@ -200,7 +197,7 @@ static int str_char (lua_State *L) {
for (i=1; i<=n; i++) {
lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
p[i - 1] = uchar(c);
p[i - 1] = cast_uchar(c);
}
luaL_pushresultsize(&b, n);
return 1;
@ -459,15 +456,15 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
while (++p < ec) {
if (*p == L_ESC) {
p++;
if (match_class(c, uchar(*p)))
if (match_class(c, cast_uchar(*p)))
return sig;
}
else if ((*(p+1) == '-') && (p+2 < ec)) {
p+=2;
if (uchar(*(p-2)) <= c && c <= uchar(*p))
if (cast_uchar(*(p-2)) <= c && c <= cast_uchar(*p))
return sig;
}
else if (uchar(*p) == c) return sig;
else if (cast_uchar(*p) == c) return sig;
}
return !sig;
}
@ -478,12 +475,12 @@ static int singlematch (MatchState *ms, const char *s, const char *p,
if (s >= 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);
}

View File

@ -18,6 +18,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
/*

1
lua.c
View File

@ -19,6 +19,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "llimits.h"
#if !defined(LUA_PROGNAME)

View File

@ -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);