'onelua' can use the test library

Just add -DLUA_USER_H='"ltests.h"' when compiling it.
This commit is contained in:
Roberto Ierusalimschy
2025-07-29 14:35:04 -03:00
parent c33bb08ffe
commit 8fddca81e7
4 changed files with 34 additions and 13 deletions

View File

@@ -1177,7 +1177,11 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
} }
LUALIB_API lua_State *luaL_newstate (void) { /*
** Use the name with parentheses so that headers can redefine it
** as a macro.
*/
LUALIB_API lua_State *(luaL_newstate) (void) {
lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed());
if (l_likely(L)) { if (l_likely(L)) {
lua_atpanic(L, &panic); lua_atpanic(L, &panic);

View File

@@ -164,13 +164,13 @@ static void warnf (void *ud, const char *msg, int tocont) {
#define MARK 0x55 /* 01010101 (a nice pattern) */ #define MARK 0x55 /* 01010101 (a nice pattern) */
typedef union Header { typedef union memHeader {
LUAI_MAXALIGN; LUAI_MAXALIGN;
struct { struct {
size_t size; size_t size;
int type; int type;
} d; } d;
} Header; } memHeader;
#if !defined(EXTERNMEMCHECK) #if !defined(EXTERNMEMCHECK)
@@ -193,14 +193,14 @@ Memcontrol l_memcontrol =
{0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
static void freeblock (Memcontrol *mc, Header *block) { static void freeblock (Memcontrol *mc, memHeader *block) {
if (block) { if (block) {
size_t size = block->d.size; size_t size = block->d.size;
int i; int i;
for (i = 0; i < MARKSIZE; i++) /* check marks after block */ for (i = 0; i < MARKSIZE; i++) /* check marks after block */
lua_assert(*(cast_charp(block + 1) + size + i) == MARK); lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
mc->objcount[block->d.type]--; mc->objcount[block->d.type]--;
fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */ fillmem(block, sizeof(memHeader) + size + MARKSIZE); /* erase block */
free(block); /* actually free block */ free(block); /* actually free block */
mc->numblocks--; /* update counts */ mc->numblocks--; /* update counts */
mc->total -= size; mc->total -= size;
@@ -210,7 +210,7 @@ static void freeblock (Memcontrol *mc, Header *block) {
void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
Memcontrol *mc = cast(Memcontrol *, ud); Memcontrol *mc = cast(Memcontrol *, ud);
Header *block = cast(Header *, b); memHeader *block = cast(memHeader *, b);
int type; int type;
if (mc->memlimit == 0) { /* first time? */ if (mc->memlimit == 0) { /* first time? */
char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
@@ -241,12 +241,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
if (size > oldsize && mc->total+size-oldsize > mc->memlimit) if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
return NULL; /* fake a memory allocation error */ return NULL; /* fake a memory allocation error */
else { else {
Header *newblock; memHeader *newblock;
int i; int i;
size_t commonsize = (oldsize < size) ? oldsize : size; size_t commonsize = (oldsize < size) ? oldsize : size;
size_t realsize = sizeof(Header) + size + MARKSIZE; size_t realsize = sizeof(memHeader) + size + MARKSIZE;
if (realsize < size) return NULL; /* arithmetic overflow! */ if (realsize < size) return NULL; /* arithmetic overflow! */
newblock = cast(Header *, malloc(realsize)); /* alloc a new block */ newblock = cast(memHeader *, malloc(realsize)); /* alloc a new block */
if (newblock == NULL) if (newblock == NULL)
return NULL; /* really out of memory? */ return NULL; /* really out of memory? */
if (block) { if (block) {
@@ -480,7 +480,7 @@ static int lua_checkpc (CallInfo *ci) {
} }
static void checkstack (global_State *g, lua_State *L1) { static void check_stack (global_State *g, lua_State *L1) {
StkId o; StkId o;
CallInfo *ci; CallInfo *ci;
UpVal *uv; UpVal *uv;
@@ -517,7 +517,7 @@ static void checkrefs (global_State *g, GCObject *o) {
break; break;
} }
case LUA_VTHREAD: { case LUA_VTHREAD: {
checkstack(g, gco2th(o)); check_stack(g, gco2th(o));
break; break;
} }
case LUA_VLCL: { case LUA_VLCL: {
@@ -908,6 +908,17 @@ static int get_limits (lua_State *L) {
} }
static int get_sizes (lua_State *L) {
lua_newtable(L);
setnameval(L, "Lua state", sizeof(lua_State));
setnameval(L, "global state", sizeof(global_State));
setnameval(L, "TValue", sizeof(TValue));
setnameval(L, "Node", sizeof(Node));
setnameval(L, "stack Value", sizeof(StackValue));
return 1;
}
static int mem_query (lua_State *L) { static int mem_query (lua_State *L) {
if (lua_isnone(L, 1)) { if (lua_isnone(L, 1)) {
lua_pushinteger(L, cast_Integer(l_memcontrol.total)); lua_pushinteger(L, cast_Integer(l_memcontrol.total));
@@ -2171,6 +2182,7 @@ static const struct luaL_Reg tests_funcs[] = {
{"s2d", s2d}, {"s2d", s2d},
{"sethook", sethook}, {"sethook", sethook},
{"stacklevel", stacklevel}, {"stacklevel", stacklevel},
{"sizes", get_sizes},
{"testC", testC}, {"testC", testC},
{"makeCfunc", makeCfunc}, {"makeCfunc", makeCfunc},
{"totalmem", mem_query}, {"totalmem", mem_query},

View File

@@ -122,14 +122,14 @@ LUA_API int luaB_opentests (lua_State *L);
LUA_API void *debug_realloc (void *ud, void *block, LUA_API void *debug_realloc (void *ud, void *block,
size_t osize, size_t nsize); size_t osize, size_t nsize);
#if defined(lua_c)
#define luaL_newstate() \ #define luaL_newstate() \
lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL)) lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL))
#define luai_openlibs(L) \ #define luai_openlibs(L) \
{ luaL_openlibs(L); \ { luaL_openlibs(L); \
luaL_requiref(L, "T", luaB_opentests, 1); \ luaL_requiref(L, "T", luaB_opentests, 1); \
lua_pop(L, 1); } lua_pop(L, 1); }
#endif

View File

@@ -110,6 +110,11 @@
#include "linit.c" #include "linit.c"
#endif #endif
/* test library -- used only for internal development */
#if defined(LUA_DEBUG)
#include "ltests.c"
#endif
/* lua */ /* lua */
#ifdef MAKE_LUA #ifdef MAKE_LUA
#include "lua.c" #include "lua.c"