Update LuaJIT to the latest 2.1.0 source (f3c8569)

This commit is contained in:
Alex Szpakowski
2021-12-03 20:50:23 -04:00
parent 4f24e5473f
commit c3986a9541
216 changed files with 6026 additions and 1838 deletions
+173 -39
View File
@@ -1,6 +1,6 @@
/*
** String formatting.
** Copyright (C) 2005-2020 Mike Pall. See Copyright Notice in luajit.h
** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
*/
#include <stdio.h>
@@ -9,11 +9,17 @@
#define LUA_CORE
#include "lj_obj.h"
#include "lj_err.h"
#include "lj_buf.h"
#include "lj_str.h"
#include "lj_meta.h"
#include "lj_state.h"
#include "lj_char.h"
#include "lj_strfmt.h"
#if LJ_HASFFI
#include "lj_ctype.h"
#endif
#include "lj_lib.h"
/* -- Format parser ------------------------------------------------------- */
@@ -161,6 +167,10 @@ const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp)
if (tvisstr(o)) {
*lenp = strV(o)->len;
return strVdata(o);
} else if (tvisbuf(o)) {
SBufExt *sbx = bufV(o);
*lenp = sbufxlen(sbx);
return sbx->r;
} else if (tvisint(o)) {
sb = lj_strfmt_putint(lj_buf_tmp_(L), intV(o));
} else if (tvisnum(o)) {
@@ -169,7 +179,7 @@ const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp)
return NULL;
}
*lenp = sbuflen(sb);
return sbufB(sb);
return sb->b;
}
/* -- Unformatted conversions to buffer ----------------------------------- */
@@ -177,7 +187,7 @@ const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp)
/* Add integer to buffer. */
SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k)
{
setsbufP(sb, lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k));
sb->w = lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k);
return sb;
}
@@ -191,73 +201,86 @@ SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o)
SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v)
{
setsbufP(sb, lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), v));
sb->w = lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), v);
return sb;
}
/* Add quoted string to buffer. */
SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str)
static SBuf *strfmt_putquotedlen(SBuf *sb, const char *s, MSize len)
{
const char *s = strdata(str);
MSize len = str->len;
lj_buf_putb(sb, '"');
while (len--) {
uint32_t c = (uint32_t)(uint8_t)*s++;
char *p = lj_buf_more(sb, 4);
char *w = lj_buf_more(sb, 4);
if (c == '"' || c == '\\' || c == '\n') {
*p++ = '\\';
*w++ = '\\';
} else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */
uint32_t d;
*p++ = '\\';
*w++ = '\\';
if (c >= 100 || lj_char_isdigit((uint8_t)*s)) {
*p++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100;
*w++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100;
goto tens;
} else if (c >= 10) {
tens:
d = (c * 205) >> 11; c -= d * 10; *p++ = (char)('0'+d);
d = (c * 205) >> 11; c -= d * 10; *w++ = (char)('0'+d);
}
c += '0';
}
*p++ = (char)c;
setsbufP(sb, p);
*w++ = (char)c;
sb->w = w;
}
lj_buf_putb(sb, '"');
return sb;
}
#if LJ_HASJIT
SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str)
{
return strfmt_putquotedlen(sb, strdata(str), str->len);
}
#endif
/* -- Formatted conversions to buffer ------------------------------------- */
/* Add formatted char to buffer. */
SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat sf, int32_t c)
{
MSize width = STRFMT_WIDTH(sf);
char *p = lj_buf_more(sb, width > 1 ? width : 1);
if ((sf & STRFMT_F_LEFT)) *p++ = (char)c;
while (width-- > 1) *p++ = ' ';
if (!(sf & STRFMT_F_LEFT)) *p++ = (char)c;
setsbufP(sb, p);
char *w = lj_buf_more(sb, width > 1 ? width : 1);
if ((sf & STRFMT_F_LEFT)) *w++ = (char)c;
while (width-- > 1) *w++ = ' ';
if (!(sf & STRFMT_F_LEFT)) *w++ = (char)c;
sb->w = w;
return sb;
}
/* Add formatted string to buffer. */
SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str)
static SBuf *strfmt_putfstrlen(SBuf *sb, SFormat sf, const char *s, MSize len)
{
MSize len = str->len <= STRFMT_PREC(sf) ? str->len : STRFMT_PREC(sf);
MSize width = STRFMT_WIDTH(sf);
char *p = lj_buf_more(sb, width > len ? width : len);
if ((sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len);
while (width-- > len) *p++ = ' ';
if (!(sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len);
setsbufP(sb, p);
char *w;
if (len > STRFMT_PREC(sf)) len = STRFMT_PREC(sf);
w = lj_buf_more(sb, width > len ? width : len);
if ((sf & STRFMT_F_LEFT)) w = lj_buf_wmem(w, s, len);
while (width-- > len) *w++ = ' ';
if (!(sf & STRFMT_F_LEFT)) w = lj_buf_wmem(w, s, len);
sb->w = w;
return sb;
}
#if LJ_HASJIT
SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str)
{
return strfmt_putfstrlen(sb, sf, strdata(str), str->len);
}
#endif
/* Add formatted signed/unsigned integer to buffer. */
SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k)
{
char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *p;
char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *w;
#ifdef LUA_USE_ASSERT
char *ps;
char *ws;
#endif
MSize prefix = 0, len, prec, pprec, width, need;
@@ -301,27 +324,27 @@ SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k)
width = STRFMT_WIDTH(sf);
pprec = prec + (prefix >> 8);
need = width > pprec ? width : pprec;
p = lj_buf_more(sb, need);
w = lj_buf_more(sb, need);
#ifdef LUA_USE_ASSERT
ps = p;
ws = w;
#endif
/* Format number with leading/trailing whitespace and zeros. */
if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == 0)
while (width-- > pprec) *p++ = ' ';
while (width-- > pprec) *w++ = ' ';
if (prefix) {
if ((char)prefix >= 'X') *p++ = '0';
*p++ = (char)prefix;
if ((char)prefix >= 'X') *w++ = '0';
*w++ = (char)prefix;
}
if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == STRFMT_F_ZERO)
while (width-- > pprec) *p++ = '0';
while (prec-- > len) *p++ = '0';
while (q < buf + sizeof(buf)) *p++ = *q++; /* Add number itself. */
while (width-- > pprec) *w++ = '0';
while (prec-- > len) *w++ = '0';
while (q < buf + sizeof(buf)) *w++ = *q++; /* Add number itself. */
if ((sf & STRFMT_F_LEFT))
while (width-- > pprec) *p++ = ' ';
while (width-- > pprec) *w++ = ' ';
lj_assertX(need == (MSize)(p - ps), "miscalculated format size");
setsbufP(sb, p);
lj_assertX(need == (MSize)(w - ws), "miscalculated format size");
sb->w = w;
return sb;
}
@@ -346,6 +369,117 @@ SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n)
return lj_strfmt_putfxint(sb, sf, (uint64_t)k);
}
/* Format stack arguments to buffer. */
int lj_strfmt_putarg(lua_State *L, SBuf *sb, int arg, int retry)
{
int narg = (int)(L->top - L->base);
GCstr *fmt = lj_lib_checkstr(L, arg);
FormatState fs;
SFormat sf;
lj_strfmt_init(&fs, strdata(fmt), fmt->len);
while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {
if (sf == STRFMT_LIT) {
lj_buf_putmem(sb, fs.str, fs.len);
} else if (sf == STRFMT_ERR) {
lj_err_callerv(L, LJ_ERR_STRFMT,
strdata(lj_str_new(L, fs.str, fs.len)));
} else {
TValue *o = &L->base[arg++];
if (arg > narg)
lj_err_arg(L, arg, LJ_ERR_NOVAL);
switch (STRFMT_TYPE(sf)) {
case STRFMT_INT:
if (tvisint(o)) {
int32_t k = intV(o);
if (sf == STRFMT_INT)
lj_strfmt_putint(sb, k); /* Shortcut for plain %d. */
else
lj_strfmt_putfxint(sb, sf, k);
break;
}
#if LJ_HASFFI
if (tviscdata(o)) {
GCcdata *cd = cdataV(o);
if (cd->ctypeid == CTID_INT64 || cd->ctypeid == CTID_UINT64) {
lj_strfmt_putfxint(sb, sf, *(uint64_t *)cdataptr(cd));
break;
}
}
#endif
lj_strfmt_putfnum_int(sb, sf, lj_lib_checknum(L, arg));
break;
case STRFMT_UINT:
if (tvisint(o)) {
lj_strfmt_putfxint(sb, sf, intV(o));
break;
}
#if LJ_HASFFI
if (tviscdata(o)) {
GCcdata *cd = cdataV(o);
if (cd->ctypeid == CTID_INT64 || cd->ctypeid == CTID_UINT64) {
lj_strfmt_putfxint(sb, sf, *(uint64_t *)cdataptr(cd));
break;
}
}
#endif
lj_strfmt_putfnum_uint(sb, sf, lj_lib_checknum(L, arg));
break;
case STRFMT_NUM:
lj_strfmt_putfnum(sb, sf, lj_lib_checknum(L, arg));
break;
case STRFMT_STR: {
MSize len;
const char *s;
cTValue *mo;
if (LJ_UNLIKELY(!tvisstr(o) && !tvisbuf(o)) && retry >= 0 &&
!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
/* Call __tostring metamethod once. */
copyTV(L, L->top++, mo);
copyTV(L, L->top++, o);
lua_call(L, 1, 1);
o = &L->base[arg-1]; /* Stack may have been reallocated. */
copyTV(L, o, --L->top); /* Replace inline for retry. */
if (retry < 2) { /* Global buffer may have been overwritten. */
retry = 1;
break;
}
}
if (LJ_LIKELY(tvisstr(o))) {
len = strV(o)->len;
s = strVdata(o);
#if LJ_HASBUFFER
} else if (tvisbuf(o)) {
SBufExt *sbx = bufV(o);
if (sbx == (SBufExt *)sb) lj_err_arg(L, arg+1, LJ_ERR_BUFFER_SELF);
len = sbufxlen(sbx);
s = sbx->r;
#endif
} else {
GCstr *str = lj_strfmt_obj(L, o);
len = str->len;
s = strdata(str);
}
if ((sf & STRFMT_T_QUOTED))
strfmt_putquotedlen(sb, s, len); /* No formatting. */
else
strfmt_putfstrlen(sb, sf, s, len);
break;
}
case STRFMT_CHAR:
lj_strfmt_putfchar(sb, sf, lj_lib_checkint(L, arg));
break;
case STRFMT_PTR: /* No formatting. */
lj_strfmt_putptr(sb, lj_obj_ptr(G(L), o));
break;
default:
lj_assertL(0, "bad string format type");
break;
}
}
}
return retry;
}
/* -- Conversions to strings ---------------------------------------------- */
/* Convert integer to string. */