1999-12-14 21:31:20 +03:00
|
|
|
/*
|
2018-08-23 20:26:12 +03:00
|
|
|
** $Id: ltests.c $
|
1999-12-14 21:31:20 +03:00
|
|
|
** Internal Module for Debugging of the Lua Implementation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define ltests_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2001-02-06 19:01:29 +03:00
|
|
|
#include <limits.h>
|
2014-11-10 20:41:36 +03:00
|
|
|
#include <setjmp.h>
|
2000-04-06 21:35:23 +04:00
|
|
|
#include <stdio.h>
|
1999-12-14 21:31:20 +03:00
|
|
|
#include <stdlib.h>
|
2000-02-21 21:30:06 +03:00
|
|
|
#include <string.h>
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
2000-05-22 22:44:46 +04:00
|
|
|
#include "lcode.h"
|
2009-02-19 20:33:51 +03:00
|
|
|
#include "lctype.h"
|
2000-08-08 22:26:05 +04:00
|
|
|
#include "ldebug.h"
|
2000-05-10 20:33:20 +04:00
|
|
|
#include "ldo.h"
|
2000-06-28 21:06:07 +04:00
|
|
|
#include "lfunc.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
#include "lmem.h"
|
2000-04-06 21:35:23 +04:00
|
|
|
#include "lopcodes.h"
|
2018-07-09 18:50:51 +03:00
|
|
|
#include "lopnames.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
2000-09-14 18:09:31 +04:00
|
|
|
#include "lualib.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-10-30 19:29:59 +03:00
|
|
|
** The whole module only makes sense with LUA_DEBUG on
|
1999-12-14 21:31:20 +03:00
|
|
|
*/
|
2005-03-23 20:51:11 +03:00
|
|
|
#if defined(LUA_DEBUG)
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2001-02-06 19:01:29 +03:00
|
|
|
|
2008-06-23 20:50:34 +04:00
|
|
|
void *l_Trick = 0;
|
2004-03-16 00:04:54 +03:00
|
|
|
|
|
|
|
|
2022-10-29 18:06:37 +03:00
|
|
|
#define obj_at(L,k) s2v(L->ci->func.p + (k))
|
2002-03-05 00:29:41 +03:00
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2014-11-10 20:41:36 +03:00
|
|
|
static int runC (lua_State *L, lua_State *L1, const char *pc);
|
|
|
|
|
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static void setnameval (lua_State *L, const char *name, int val) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, val);
|
2019-05-13 22:20:40 +03:00
|
|
|
lua_setfield(L, -2, name);
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-11 19:53:29 +04:00
|
|
|
static void pushobject (lua_State *L, const TValue *o) {
|
2022-10-29 18:06:37 +03:00
|
|
|
setobj2s(L, L->top.p, o);
|
2006-07-11 19:53:29 +04:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:46 +03:00
|
|
|
static void badexit (const char *fmt, const char *s1, const char *s2) {
|
|
|
|
fprintf(stderr, fmt, s1);
|
|
|
|
if (s2)
|
|
|
|
fprintf(stderr, "extra info: %s\n", s2);
|
2018-12-28 20:42:34 +03:00
|
|
|
/* avoid assertion failures when exiting */
|
|
|
|
l_memcontrol.numblocks = l_memcontrol.total = 0;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-03 21:52:36 +03:00
|
|
|
static int tpanic (lua_State *L) {
|
2020-06-09 22:12:01 +03:00
|
|
|
const char *msg = lua_tostring(L, -1);
|
|
|
|
if (msg == NULL) msg = "error object is not a string";
|
2019-01-01 17:14:56 +03:00
|
|
|
return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
|
2020-06-09 22:12:01 +03:00
|
|
|
msg, NULL),
|
2019-01-01 17:14:56 +03:00
|
|
|
0); /* do not return to Lua */
|
2018-12-28 20:42:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-01 17:14:56 +03:00
|
|
|
/*
|
2019-10-22 20:08:22 +03:00
|
|
|
** Warning function for tests. First, it concatenates all parts of
|
2019-08-15 19:44:36 +03:00
|
|
|
** a warning in buffer 'buff'. Then, it has three modes:
|
|
|
|
** - 0.normal: messages starting with '#' are shown on standard output;
|
2019-03-14 21:30:54 +03:00
|
|
|
** - other messages abort the tests (they represent real warning
|
|
|
|
** conditions; the standard tests should not generate these conditions
|
2019-08-15 19:44:36 +03:00
|
|
|
** unexpectedly);
|
|
|
|
** - 1.allow: all messages are shown;
|
|
|
|
** - 2.store: all warnings go to the global '_WARN';
|
2019-01-01 17:14:56 +03:00
|
|
|
*/
|
2019-03-14 21:30:54 +03:00
|
|
|
static void warnf (void *ud, const char *msg, int tocont) {
|
2019-08-18 23:29:46 +03:00
|
|
|
lua_State *L = cast(lua_State *, ud);
|
2019-03-14 21:30:54 +03:00
|
|
|
static char buff[200] = ""; /* should be enough for tests... */
|
2019-08-20 19:42:26 +03:00
|
|
|
static int onoff = 0;
|
2019-08-15 19:44:36 +03:00
|
|
|
static int mode = 0; /* start in normal mode */
|
|
|
|
static int lasttocont = 0;
|
|
|
|
if (!lasttocont && !tocont && *msg == '@') { /* control message? */
|
|
|
|
if (buff[0] != '\0')
|
2019-08-18 23:29:46 +03:00
|
|
|
badexit("Control warning during warning: %s\naborting...\n", msg, buff);
|
2019-08-16 15:51:54 +03:00
|
|
|
if (strcmp(msg, "@off") == 0)
|
2019-08-15 19:44:36 +03:00
|
|
|
onoff = 0;
|
2019-08-16 15:51:54 +03:00
|
|
|
else if (strcmp(msg, "@on") == 0)
|
2019-08-15 19:44:36 +03:00
|
|
|
onoff = 1;
|
2019-08-16 15:51:54 +03:00
|
|
|
else if (strcmp(msg, "@normal") == 0)
|
2019-08-15 19:44:36 +03:00
|
|
|
mode = 0;
|
2019-08-16 15:51:54 +03:00
|
|
|
else if (strcmp(msg, "@allow") == 0)
|
2019-08-15 19:44:36 +03:00
|
|
|
mode = 1;
|
2019-08-16 15:51:54 +03:00
|
|
|
else if (strcmp(msg, "@store") == 0)
|
2019-08-15 19:44:36 +03:00
|
|
|
mode = 2;
|
|
|
|
else
|
2019-08-18 23:29:46 +03:00
|
|
|
badexit("Invalid control warning in test mode: %s\naborting...\n",
|
|
|
|
msg, NULL);
|
2019-08-15 19:44:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
lasttocont = tocont;
|
2019-03-14 21:30:54 +03:00
|
|
|
if (strlen(msg) >= sizeof(buff) - strlen(buff))
|
2019-08-18 23:29:46 +03:00
|
|
|
badexit("warnf-buffer overflow (%s)\n", msg, buff);
|
2019-03-14 21:30:54 +03:00
|
|
|
strcat(buff, msg); /* add new message to current warning */
|
|
|
|
if (!tocont) { /* message finished? */
|
2019-08-18 23:29:46 +03:00
|
|
|
lua_unlock(L);
|
2021-03-02 17:35:40 +03:00
|
|
|
luaL_checkstack(L, 1, "warn stack space");
|
2020-07-04 22:40:18 +03:00
|
|
|
lua_getglobal(L, "_WARN");
|
|
|
|
if (!lua_toboolean(L, -1))
|
2019-08-18 23:29:46 +03:00
|
|
|
lua_pop(L, 1); /* ok, no previous unexpected warning */
|
|
|
|
else {
|
|
|
|
badexit("Unhandled warning in store mode: %s\naborting...\n",
|
|
|
|
lua_tostring(L, -1), buff);
|
|
|
|
}
|
|
|
|
lua_lock(L);
|
2019-08-15 19:44:36 +03:00
|
|
|
switch (mode) {
|
|
|
|
case 0: { /* normal */
|
|
|
|
if (buff[0] != '#' && onoff) /* unexpected warning? */
|
2019-08-18 23:29:46 +03:00
|
|
|
badexit("Unexpected warning in test mode: %s\naborting...\n",
|
|
|
|
buff, NULL);
|
2020-03-16 20:13:13 +03:00
|
|
|
} /* FALLTHROUGH */
|
2019-08-15 19:44:36 +03:00
|
|
|
case 1: { /* allow */
|
|
|
|
if (onoff)
|
|
|
|
fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: { /* store */
|
|
|
|
lua_unlock(L);
|
2021-03-02 17:35:40 +03:00
|
|
|
luaL_checkstack(L, 1, "warn stack space");
|
2019-08-15 19:44:36 +03:00
|
|
|
lua_pushstring(L, buff);
|
|
|
|
lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
|
|
|
|
lua_lock(L);
|
|
|
|
break;
|
|
|
|
}
|
2019-01-01 17:14:56 +03:00
|
|
|
}
|
2019-03-14 21:30:54 +03:00
|
|
|
buff[0] = '\0'; /* prepare buffer for next warning */
|
2018-12-28 20:42:34 +03:00
|
|
|
}
|
2009-03-03 21:52:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-06 19:01:29 +03:00
|
|
|
/*
|
|
|
|
** {======================================================================
|
|
|
|
** Controlled version for realloc.
|
|
|
|
** =======================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
|
|
|
|
2010-04-19 20:38:25 +04:00
|
|
|
typedef union Header {
|
2017-04-24 21:06:12 +03:00
|
|
|
LUAI_MAXALIGN;
|
2010-04-19 20:38:25 +04:00
|
|
|
struct {
|
|
|
|
size_t size;
|
|
|
|
int type;
|
|
|
|
} d;
|
|
|
|
} Header;
|
|
|
|
|
|
|
|
|
2011-05-25 18:12:28 +04:00
|
|
|
#if !defined(EXTERNMEMCHECK)
|
2010-04-19 20:38:25 +04:00
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
/* full memory check */
|
|
|
|
#define MARKSIZE 16 /* size of marks after each block */
|
|
|
|
#define fillmem(mem,size) memset(mem, -MARK, size)
|
2010-04-19 20:38:25 +04:00
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
#else
|
2010-04-19 20:38:25 +04:00
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
/* external memory check: don't do it twice */
|
|
|
|
#define MARKSIZE 0
|
|
|
|
#define fillmem(mem,size) /* empty */
|
2010-04-19 20:38:25 +04:00
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
#endif
|
2001-02-06 19:01:29 +03:00
|
|
|
|
2010-04-19 20:38:25 +04:00
|
|
|
|
2010-04-12 20:07:39 +04:00
|
|
|
Memcontrol l_memcontrol =
|
2020-08-03 19:22:57 +03:00
|
|
|
{0, 0UL, 0UL, 0UL, 0UL, (~0UL),
|
|
|
|
{0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
|
2001-02-06 19:01:29 +03:00
|
|
|
|
|
|
|
|
2010-04-19 20:38:25 +04:00
|
|
|
static void freeblock (Memcontrol *mc, Header *block) {
|
2001-02-06 19:01:29 +03:00
|
|
|
if (block) {
|
2010-04-19 20:38:25 +04:00
|
|
|
size_t size = block->d.size;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MARKSIZE; i++) /* check marks after block */
|
2018-01-28 18:13:26 +03:00
|
|
|
lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
|
2010-04-19 20:38:25 +04:00
|
|
|
mc->objcount[block->d.type]--;
|
|
|
|
fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */
|
|
|
|
free(block); /* actually free block */
|
|
|
|
mc->numblocks--; /* update counts */
|
2003-10-03 00:31:17 +04:00
|
|
|
mc->total -= size;
|
2001-02-06 19:01:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 20:38:25 +04:00
|
|
|
void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
2003-10-03 00:31:17 +04:00
|
|
|
Memcontrol *mc = cast(Memcontrol *, ud);
|
2010-04-19 20:38:25 +04:00
|
|
|
Header *block = cast(Header *, b);
|
|
|
|
int type;
|
2006-06-05 23:35:57 +04:00
|
|
|
if (mc->memlimit == 0) { /* first time? */
|
|
|
|
char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
|
|
|
|
mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
|
|
|
|
}
|
2010-04-19 20:38:25 +04:00
|
|
|
if (block == NULL) {
|
|
|
|
type = (oldsize < LUA_NUMTAGS) ? oldsize : 0;
|
|
|
|
oldsize = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
block--; /* go to real header */
|
|
|
|
type = block->d.type;
|
|
|
|
lua_assert(oldsize == block->d.size);
|
|
|
|
}
|
2001-02-06 19:01:29 +03:00
|
|
|
if (size == 0) {
|
2010-04-19 20:38:25 +04:00
|
|
|
freeblock(mc, block);
|
2001-02-06 19:01:29 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-08-03 19:22:57 +03:00
|
|
|
if (mc->failnext) {
|
|
|
|
mc->failnext = 0;
|
|
|
|
return NULL; /* fake a single memory allocation error */
|
|
|
|
}
|
2018-11-24 16:59:15 +03:00
|
|
|
if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */
|
2017-12-07 21:51:39 +03:00
|
|
|
if (mc->countlimit == 0)
|
|
|
|
return NULL; /* fake a memory allocation error */
|
|
|
|
mc->countlimit--;
|
|
|
|
}
|
|
|
|
if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
|
2010-04-19 20:38:25 +04:00
|
|
|
return NULL; /* fake a memory allocation error */
|
2001-02-06 19:01:29 +03:00
|
|
|
else {
|
2010-04-19 20:38:25 +04:00
|
|
|
Header *newblock;
|
2001-02-06 19:01:29 +03:00
|
|
|
int i;
|
2002-11-18 18:24:27 +03:00
|
|
|
size_t commonsize = (oldsize < size) ? oldsize : size;
|
2010-04-19 20:38:25 +04:00
|
|
|
size_t realsize = sizeof(Header) + size + MARKSIZE;
|
|
|
|
if (realsize < size) return NULL; /* arithmetic overflow! */
|
|
|
|
newblock = cast(Header *, malloc(realsize)); /* alloc a new block */
|
2018-12-17 18:56:22 +03:00
|
|
|
if (newblock == NULL)
|
|
|
|
return NULL; /* really out of memory? */
|
2001-02-06 19:01:29 +03:00
|
|
|
if (block) {
|
2010-04-19 20:38:25 +04:00
|
|
|
memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
|
|
|
|
freeblock(mc, block); /* erase (and check) old copy */
|
2001-02-06 19:01:29 +03:00
|
|
|
}
|
2014-10-25 15:50:46 +04:00
|
|
|
/* initialize new part of the block with something weird */
|
2018-01-28 18:13:26 +03:00
|
|
|
fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize);
|
2010-04-19 20:38:25 +04:00
|
|
|
/* initialize marks after block */
|
|
|
|
for (i = 0; i < MARKSIZE; i++)
|
2018-01-28 18:13:26 +03:00
|
|
|
*(cast_charp(newblock + 1) + size + i) = MARK;
|
2010-04-19 20:38:25 +04:00
|
|
|
newblock->d.size = size;
|
|
|
|
newblock->d.type = type;
|
2003-10-03 00:31:17 +04:00
|
|
|
mc->total += size;
|
|
|
|
if (mc->total > mc->maxmem)
|
|
|
|
mc->maxmem = mc->total;
|
|
|
|
mc->numblocks++;
|
2010-04-19 20:38:25 +04:00
|
|
|
mc->objcount[type]++;
|
|
|
|
return newblock + 1;
|
2001-02-06 19:01:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* }====================================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-16 22:09:52 +03:00
|
|
|
/*
|
2020-12-08 17:54:21 +03:00
|
|
|
** {=====================================================================
|
|
|
|
** Functions to check memory consistency.
|
|
|
|
** Most of these checks are done through asserts, so this code does
|
|
|
|
** not make sense with asserts off. For this reason, it uses 'assert'
|
|
|
|
** directly, instead of 'lua_assert'.
|
|
|
|
** ======================================================================
|
2004-02-16 22:09:52 +03:00
|
|
|
*/
|
|
|
|
|
2020-12-08 17:54:21 +03:00
|
|
|
#include <assert.h>
|
2009-12-11 22:14:59 +03:00
|
|
|
|
2017-04-18 22:42:12 +03:00
|
|
|
/*
|
|
|
|
** Check GC invariants. For incremental mode, a black object cannot
|
|
|
|
** point to a white one. For generational mode, really old objects
|
2017-04-19 21:46:47 +03:00
|
|
|
** cannot point to young objects. Both old1 and touched2 objects
|
|
|
|
** cannot point to new objects (but can point to survivals).
|
|
|
|
** (Threads and open upvalues, despite being marked "really old",
|
|
|
|
** continue to be visited in all collections, and therefore can point to
|
|
|
|
** new objects. They, and only they, are old but gray.)
|
2017-04-18 22:42:12 +03:00
|
|
|
*/
|
2004-02-16 22:09:52 +03:00
|
|
|
static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
|
|
|
|
if (isdead(g,t)) return 0;
|
2017-04-18 22:42:12 +03:00
|
|
|
if (issweepphase(g))
|
|
|
|
return 1; /* no invariants */
|
2023-12-06 16:49:56 +03:00
|
|
|
else if (g->gckind != KGC_GENMINOR)
|
2017-04-18 22:42:12 +03:00
|
|
|
return !(isblack(f) && iswhite(t)); /* basic incremental invariant */
|
2017-04-19 21:46:47 +03:00
|
|
|
else { /* generational mode */
|
|
|
|
if ((getage(f) == G_OLD && isblack(f)) && !isold(t))
|
|
|
|
return 0;
|
2023-11-29 22:22:09 +03:00
|
|
|
if ((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) &&
|
|
|
|
getage(t) == G_NEW)
|
2017-04-19 21:46:47 +03:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void printobj (global_State *g, GCObject *o) {
|
2017-02-24 00:07:34 +03:00
|
|
|
printf("||%s(%p)-%c%c(%02X)||",
|
2014-07-17 21:27:49 +04:00
|
|
|
ttypename(novariant(o->tt)), (void *)o,
|
2017-02-24 00:07:34 +03:00
|
|
|
isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
|
2017-04-18 22:42:12 +03:00
|
|
|
"ns01oTt"[getage(o)], o->marked);
|
2020-01-31 17:09:53 +03:00
|
|
|
if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR)
|
2017-02-24 00:07:34 +03:00
|
|
|
printf(" '%s'", getstr(gco2ts(o)));
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-27 17:24:03 +03:00
|
|
|
void lua_printobj (lua_State *L, struct GCObject *o) {
|
|
|
|
printobj(G(L), o);
|
|
|
|
}
|
|
|
|
|
2004-02-16 22:09:52 +03:00
|
|
|
static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
2014-02-13 16:11:34 +04:00
|
|
|
int r1 = testobjref1(g, f, t);
|
|
|
|
if (!r1) {
|
|
|
|
printf("%d(%02X) - ", g->gcstate, g->currentwhite);
|
2004-03-16 00:04:54 +03:00
|
|
|
printobj(g, f);
|
2013-08-16 22:55:49 +04:00
|
|
|
printf(" -> ");
|
2004-03-16 00:04:54 +03:00
|
|
|
printobj(g, t);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2014-02-13 16:11:34 +04:00
|
|
|
return r1;
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 17:54:21 +03:00
|
|
|
|
|
|
|
static void checkobjref (global_State *g, GCObject *f, GCObject *t) {
|
|
|
|
assert(testobjref(g, f, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Version where 't' can be NULL. In that case, it should not apply the
|
|
|
|
** macro 'obj2gco' over the object. ('t' may have several types, so this
|
|
|
|
** definition must be a macro.) Most checks need this version, because
|
|
|
|
** the check may run while an object is still being created.
|
|
|
|
*/
|
|
|
|
#define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); }
|
2014-07-18 16:17:54 +04:00
|
|
|
|
2004-02-16 22:09:52 +03:00
|
|
|
|
2009-11-06 20:08:43 +03:00
|
|
|
static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t))));
|
2009-11-06 20:08:43 +03:00
|
|
|
}
|
2004-02-16 22:09:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void checktable (global_State *g, Table *h) {
|
2014-09-04 22:15:29 +04:00
|
|
|
unsigned int i;
|
2018-06-15 17:14:20 +03:00
|
|
|
unsigned int asize = luaH_realasize(h);
|
2010-04-29 21:33:51 +04:00
|
|
|
Node *n, *limit = gnode(h, sizenode(h));
|
2004-02-16 22:09:52 +03:00
|
|
|
GCObject *hgc = obj2gco(h);
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, hgc, h->metatable);
|
2023-10-30 20:25:59 +03:00
|
|
|
for (i = 0; i < asize; i++) {
|
|
|
|
TValue aux;
|
|
|
|
arr2obj(h, i + 1, &aux);
|
|
|
|
checkvalref(g, hgc, &aux);
|
|
|
|
}
|
2010-04-29 21:33:51 +04:00
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
2018-02-23 16:16:18 +03:00
|
|
|
if (!isempty(gval(n))) {
|
2017-06-09 19:48:44 +03:00
|
|
|
TValue k;
|
|
|
|
getnodekey(g->mainthread, &k, n);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!keyisnil(n));
|
2017-06-09 19:48:44 +03:00
|
|
|
checkvalref(g, hgc, &k);
|
2004-02-16 22:09:52 +03:00
|
|
|
checkvalref(g, hgc, gval(n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-20 19:52:50 +03:00
|
|
|
static void checkudata (global_State *g, Udata *u) {
|
|
|
|
int i;
|
|
|
|
GCObject *hgc = obj2gco(u);
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, hgc, u->metatable);
|
2018-02-20 19:52:50 +03:00
|
|
|
for (i = 0; i < u->nuvalue; i++)
|
|
|
|
checkvalref(g, hgc, &u->uv[i].uv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-16 22:09:52 +03:00
|
|
|
static void checkproto (global_State *g, Proto *f) {
|
|
|
|
int i;
|
|
|
|
GCObject *fgc = obj2gco(f);
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, fgc, f->source);
|
2004-02-16 22:09:52 +03:00
|
|
|
for (i=0; i<f->sizek; i++) {
|
2020-12-08 17:54:21 +03:00
|
|
|
if (iscollectable(f->k + i))
|
|
|
|
checkobjref(g, fgc, gcvalue(f->k + i));
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
2014-06-17 21:13:29 +04:00
|
|
|
for (i=0; i<f->sizeupvalues; i++)
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, fgc, f->upvalues[i].name);
|
2014-06-17 21:13:29 +04:00
|
|
|
for (i=0; i<f->sizep; i++)
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, fgc, f->p[i]);
|
2014-06-17 21:13:29 +04:00
|
|
|
for (i=0; i<f->sizelocvars; i++)
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, fgc, f->locvars[i].varname);
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 02:05:50 +04:00
|
|
|
static void checkCclosure (global_State *g, CClosure *cl) {
|
2004-02-16 22:09:52 +03:00
|
|
|
GCObject *clgc = obj2gco(cl);
|
2012-01-21 02:05:50 +04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < cl->nupvalues; i++)
|
|
|
|
checkvalref(g, clgc, &cl->upvalue[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void checkLclosure (global_State *g, LClosure *cl) {
|
|
|
|
GCObject *clgc = obj2gco(cl);
|
|
|
|
int i;
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, clgc, cl->p);
|
2012-01-21 02:05:50 +04:00
|
|
|
for (i=0; i<cl->nupvalues; i++) {
|
2013-08-27 22:53:35 +04:00
|
|
|
UpVal *uv = cl->upvals[i];
|
|
|
|
if (uv) {
|
2020-12-08 17:54:21 +03:00
|
|
|
checkobjrefN(g, clgc, uv);
|
2017-04-18 22:42:12 +03:00
|
|
|
if (!upisopen(uv))
|
2022-10-29 18:06:37 +03:00
|
|
|
checkvalref(g, obj2gco(uv), uv->v.p);
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-07 16:25:26 +03:00
|
|
|
static int lua_checkpc (CallInfo *ci) {
|
|
|
|
if (!isLua(ci)) return 1;
|
2009-04-18 02:00:01 +04:00
|
|
|
else {
|
2022-10-29 18:06:37 +03:00
|
|
|
StkId f = ci->func.p;
|
2017-11-07 16:25:26 +03:00
|
|
|
Proto *p = clLvalue(s2v(f))->p;
|
|
|
|
return p->code <= ci->u.l.savedpc &&
|
|
|
|
ci->u.l.savedpc <= p->code + p->sizecode;
|
2009-04-18 02:00:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-16 22:09:52 +03:00
|
|
|
static void checkstack (global_State *g, lua_State *L1) {
|
|
|
|
StkId o;
|
2017-11-07 16:25:26 +03:00
|
|
|
CallInfo *ci;
|
2013-08-27 22:53:35 +04:00
|
|
|
UpVal *uv;
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!isdead(g, L1));
|
2022-10-29 18:06:37 +03:00
|
|
|
if (L1->stack.p == NULL) { /* incomplete thread? */
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(L1->openupval == NULL && L1->ci == NULL);
|
2020-02-27 18:59:22 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-02-15 17:12:01 +04:00
|
|
|
for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(upisopen(uv)); /* must be open */
|
2022-10-29 18:06:37 +03:00
|
|
|
assert(L1->top.p <= L1->stack_last.p);
|
|
|
|
assert(L1->tbclist.p <= L1->top.p);
|
2017-11-07 16:25:26 +03:00
|
|
|
for (ci = L1->ci; ci != NULL; ci = ci->previous) {
|
2022-10-29 18:06:37 +03:00
|
|
|
assert(ci->top.p <= L1->stack_last.p);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(lua_checkpc(ci));
|
2017-11-03 20:22:54 +03:00
|
|
|
}
|
2022-10-29 18:06:37 +03:00
|
|
|
for (o = L1->stack.p; o < L1->stack_last.p; o++)
|
2020-02-27 18:59:22 +03:00
|
|
|
checkliveness(L1, s2v(o)); /* entire stack must have valid values */
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 22:42:12 +03:00
|
|
|
static void checkrefs (global_State *g, GCObject *o) {
|
|
|
|
switch (o->tt) {
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VUSERDATA: {
|
2018-02-20 19:52:50 +03:00
|
|
|
checkudata(g, gco2u(o));
|
2017-04-18 22:42:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VUPVAL: {
|
2022-10-29 18:06:37 +03:00
|
|
|
checkvalref(g, o, gco2upv(o)->v.p);
|
2017-04-18 22:42:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VTABLE: {
|
2017-04-18 22:42:12 +03:00
|
|
|
checktable(g, gco2t(o));
|
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VTHREAD: {
|
2017-04-18 22:42:12 +03:00
|
|
|
checkstack(g, gco2th(o));
|
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VLCL: {
|
2017-04-18 22:42:12 +03:00
|
|
|
checkLclosure(g, gco2lcl(o));
|
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VCCL: {
|
2017-04-18 22:42:12 +03:00
|
|
|
checkCclosure(g, gco2ccl(o));
|
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VPROTO: {
|
2017-04-18 22:42:12 +03:00
|
|
|
checkproto(g, gco2p(o));
|
|
|
|
break;
|
|
|
|
}
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VSHRSTR:
|
|
|
|
case LUA_VLNGSTR: {
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!isgray(o)); /* strings are never gray */
|
2017-04-18 22:42:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-12-08 17:54:21 +03:00
|
|
|
default: assert(0);
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-19 21:46:47 +03:00
|
|
|
/*
|
|
|
|
** Check consistency of an object:
|
|
|
|
** - Dead objects can only happen in the 'allgc' list during a sweep
|
2019-12-30 17:45:08 +03:00
|
|
|
** phase (controlled by the caller through 'maybedead').
|
2017-04-19 21:46:47 +03:00
|
|
|
** - During pause, all objects must be white.
|
|
|
|
** - In generational mode:
|
|
|
|
** * objects must be old enough for their lists ('listage').
|
|
|
|
** * old objects cannot be white.
|
|
|
|
** * old objects must be black, except for 'touched1', 'old0',
|
2023-11-29 22:22:09 +03:00
|
|
|
** threads, and open upvalues.
|
|
|
|
** * 'touched1' objects must be gray.
|
2017-04-19 21:46:47 +03:00
|
|
|
*/
|
2017-04-18 22:42:12 +03:00
|
|
|
static void checkobject (global_State *g, GCObject *o, int maybedead,
|
|
|
|
int listage) {
|
2004-03-16 00:04:54 +03:00
|
|
|
if (isdead(g, o))
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(maybedead);
|
2004-03-16 00:04:54 +03:00
|
|
|
else {
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(g->gcstate != GCSpause || iswhite(o));
|
2023-12-06 16:49:56 +03:00
|
|
|
if (g->gckind == KGC_GENMINOR) { /* generational mode? */
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(getage(o) >= listage);
|
2017-04-18 22:42:12 +03:00
|
|
|
if (isold(o)) {
|
2023-11-29 22:22:09 +03:00
|
|
|
assert(!iswhite(o));
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(isblack(o) ||
|
2017-04-18 22:42:12 +03:00
|
|
|
getage(o) == G_TOUCHED1 ||
|
2017-04-19 21:46:47 +03:00
|
|
|
getage(o) == G_OLD0 ||
|
2020-01-31 17:09:53 +03:00
|
|
|
o->tt == LUA_VTHREAD ||
|
|
|
|
(o->tt == LUA_VUPVAL && upisopen(gco2upv(o))));
|
2004-03-16 00:04:54 +03:00
|
|
|
}
|
2023-11-29 22:22:09 +03:00
|
|
|
assert(getage(o) != G_TOUCHED1 || isgray(o));
|
2004-03-16 00:04:54 +03:00
|
|
|
}
|
2017-04-18 22:42:12 +03:00
|
|
|
checkrefs(g, o);
|
2004-03-16 00:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-23 23:29:03 +03:00
|
|
|
static l_obj checkgraylist (global_State *g, GCObject *o) {
|
2020-08-03 19:22:57 +03:00
|
|
|
int total = 0; /* count number of elements in the list */
|
2022-10-19 22:29:54 +03:00
|
|
|
cast_void(g); /* better to keep it if we need to print an object */
|
2013-09-03 19:37:10 +04:00
|
|
|
while (o) {
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2));
|
|
|
|
assert(!testbit(o->marked, TESTBIT));
|
2020-08-07 20:45:20 +03:00
|
|
|
if (keepinvariant(g))
|
|
|
|
l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */
|
2020-08-03 19:22:57 +03:00
|
|
|
total++;
|
2014-07-17 21:27:49 +04:00
|
|
|
switch (o->tt) {
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VTABLE: o = gco2t(o)->gclist; break;
|
|
|
|
case LUA_VLCL: o = gco2lcl(o)->gclist; break;
|
|
|
|
case LUA_VCCL: o = gco2ccl(o)->gclist; break;
|
|
|
|
case LUA_VTHREAD: o = gco2th(o)->gclist; break;
|
|
|
|
case LUA_VPROTO: o = gco2p(o)->gclist; break;
|
2020-06-15 18:01:36 +03:00
|
|
|
case LUA_VUSERDATA:
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(gco2u(o)->nuvalue > 0);
|
2020-06-15 18:01:36 +03:00
|
|
|
o = gco2u(o)->gclist;
|
|
|
|
break;
|
2020-12-08 17:54:21 +03:00
|
|
|
default: assert(0); /* other objects cannot be in a gray list */
|
2010-04-29 21:33:51 +04:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 19:22:57 +03:00
|
|
|
return total;
|
2010-04-29 21:33:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-06 22:16:57 +04:00
|
|
|
/*
|
2018-06-11 17:19:50 +03:00
|
|
|
** Check objects in gray lists.
|
2010-05-06 22:16:57 +04:00
|
|
|
*/
|
2022-11-23 23:29:03 +03:00
|
|
|
static l_obj checkgrays (global_State *g) {
|
2020-08-03 19:22:57 +03:00
|
|
|
int total = 0; /* count number of elements in all lists */
|
|
|
|
if (!keepinvariant(g)) return total;
|
|
|
|
total += checkgraylist(g, g->gray);
|
|
|
|
total += checkgraylist(g, g->grayagain);
|
|
|
|
total += checkgraylist(g, g->weak);
|
|
|
|
total += checkgraylist(g, g->allweak);
|
|
|
|
total += checkgraylist(g, g->ephemeron);
|
|
|
|
return total;
|
2010-04-29 21:33:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-07 20:45:20 +03:00
|
|
|
/*
|
|
|
|
** Check whether 'o' should be in a gray list. If so, increment
|
|
|
|
** 'count' and check its TESTBIT. (It must have been previously set by
|
|
|
|
** 'checkgraylist'.)
|
|
|
|
*/
|
2022-11-23 23:29:03 +03:00
|
|
|
static void incifingray (global_State *g, GCObject *o, l_obj *count) {
|
2020-08-07 20:45:20 +03:00
|
|
|
if (!keepinvariant(g))
|
|
|
|
return; /* gray lists not being kept in these phases */
|
|
|
|
if (o->tt == LUA_VUPVAL) {
|
|
|
|
/* only open upvalues can be gray */
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!isgray(o) || upisopen(gco2upv(o)));
|
2020-08-07 20:45:20 +03:00
|
|
|
return; /* upvalues are never in gray lists */
|
|
|
|
}
|
|
|
|
/* these are the ones that must be in gray lists */
|
|
|
|
if (isgray(o) || getage(o) == G_TOUCHED2) {
|
|
|
|
(*count)++;
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(testbit(o->marked, TESTBIT));
|
2020-08-07 20:45:20 +03:00
|
|
|
resetbit(o->marked, TESTBIT); /* prepare for next cycle */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:22:57 +03:00
|
|
|
|
2022-11-23 23:29:03 +03:00
|
|
|
static l_obj checklist (global_State *g, int maybedead, int tof,
|
2017-04-19 21:46:47 +03:00
|
|
|
GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
|
2017-04-18 22:42:12 +03:00
|
|
|
GCObject *o;
|
2022-11-23 23:29:03 +03:00
|
|
|
l_obj total = 0; /* number of object that should be in gray lists */
|
2017-04-19 21:46:47 +03:00
|
|
|
for (o = newl; o != survival; o = o->next) {
|
2017-04-18 22:42:12 +03:00
|
|
|
checkobject(g, o, maybedead, G_NEW);
|
2020-08-07 20:45:20 +03:00
|
|
|
incifingray(g, o, &total);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!tof == !tofinalize(o));
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
|
|
|
for (o = survival; o != old; o = o->next) {
|
|
|
|
checkobject(g, o, 0, G_SURVIVAL);
|
2020-08-07 20:45:20 +03:00
|
|
|
incifingray(g, o, &total);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!tof == !tofinalize(o));
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
|
|
|
for (o = old; o != reallyold; o = o->next) {
|
|
|
|
checkobject(g, o, 0, G_OLD1);
|
2020-08-07 20:45:20 +03:00
|
|
|
incifingray(g, o, &total);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!tof == !tofinalize(o));
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
|
|
|
for (o = reallyold; o != NULL; o = o->next) {
|
|
|
|
checkobject(g, o, 0, G_OLD);
|
2020-08-07 20:45:20 +03:00
|
|
|
incifingray(g, o, &total);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!tof == !tofinalize(o));
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
2020-08-03 19:22:57 +03:00
|
|
|
return total;
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 00:04:54 +03:00
|
|
|
int lua_checkmemory (lua_State *L) {
|
2004-02-16 22:09:52 +03:00
|
|
|
global_State *g = G(L);
|
|
|
|
GCObject *o;
|
2012-06-01 00:25:42 +04:00
|
|
|
int maybedead;
|
2022-11-23 23:29:03 +03:00
|
|
|
l_obj totalin; /* total of objects that are in gray lists */
|
|
|
|
l_obj totalshould; /* total of objects that should be in gray lists */
|
2010-05-07 22:09:23 +04:00
|
|
|
if (keepinvariant(g)) {
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!iswhite(g->mainthread));
|
|
|
|
assert(!iswhite(gcvalue(&g->l_registry)));
|
2010-05-07 22:09:23 +04:00
|
|
|
}
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(!isdead(g, gcvalue(&g->l_registry)));
|
|
|
|
assert(g->sweepgc == NULL || issweepphase(g));
|
2020-08-03 19:22:57 +03:00
|
|
|
totalin = checkgrays(g);
|
2017-04-18 22:42:12 +03:00
|
|
|
|
2014-02-13 16:11:34 +04:00
|
|
|
/* check 'fixedgc' list */
|
2014-07-17 21:27:49 +04:00
|
|
|
for (o = g->fixedgc; o != NULL; o = o->next) {
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD);
|
2013-09-04 19:34:24 +04:00
|
|
|
}
|
2017-04-18 22:42:12 +03:00
|
|
|
|
2013-09-04 19:34:24 +04:00
|
|
|
/* check 'allgc' list */
|
2013-09-11 18:47:08 +04:00
|
|
|
maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc);
|
2020-08-03 19:22:57 +03:00
|
|
|
totalshould = checklist(g, maybedead, 0, g->allgc,
|
|
|
|
g->survival, g->old1, g->reallyold);
|
2017-04-18 22:42:12 +03:00
|
|
|
|
2010-11-26 17:32:31 +03:00
|
|
|
/* check 'finobj' list */
|
2020-08-03 19:22:57 +03:00
|
|
|
totalshould += checklist(g, 0, 1, g->finobj,
|
|
|
|
g->finobjsur, g->finobjold1, g->finobjrold);
|
2017-04-18 22:42:12 +03:00
|
|
|
|
2010-05-06 22:16:57 +04:00
|
|
|
/* check 'tobefnz' list */
|
2014-07-17 21:27:49 +04:00
|
|
|
for (o = g->tobefnz; o != NULL; o = o->next) {
|
2017-04-18 22:42:12 +03:00
|
|
|
checkobject(g, o, 0, G_NEW);
|
2020-08-07 20:45:20 +03:00
|
|
|
incifingray(g, o, &totalshould);
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(tofinalize(o));
|
|
|
|
assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE);
|
2010-05-03 15:55:40 +04:00
|
|
|
}
|
2020-08-03 19:22:57 +03:00
|
|
|
if (keepinvariant(g))
|
2020-12-08 17:54:21 +03:00
|
|
|
assert(totalin == totalshould);
|
2004-03-16 00:04:54 +03:00
|
|
|
return 0;
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Disassembler
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static char *buildop (Proto *p, int pc, char *buff) {
|
2018-10-08 16:42:07 +03:00
|
|
|
char *obuff = buff;
|
2000-06-28 21:06:07 +04:00
|
|
|
Instruction i = p->code[pc];
|
2000-05-22 22:44:46 +04:00
|
|
|
OpCode o = GET_OPCODE(i);
|
2018-07-09 18:50:51 +03:00
|
|
|
const char *name = opnames[o];
|
2017-06-27 14:35:31 +03:00
|
|
|
int line = luaG_getfuncline(p, pc);
|
2018-07-11 18:53:23 +03:00
|
|
|
int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0;
|
2018-10-08 16:42:07 +03:00
|
|
|
if (lineinfo == ABSLINEINFO)
|
|
|
|
buff += sprintf(buff, "(__");
|
|
|
|
else
|
|
|
|
buff += sprintf(buff, "(%2d", lineinfo);
|
|
|
|
buff += sprintf(buff, " - %4d) %4d - ", line, pc);
|
2006-09-11 18:07:24 +04:00
|
|
|
switch (getOpMode(o)) {
|
2001-06-05 22:17:01 +04:00
|
|
|
case iABC:
|
2018-10-08 16:42:07 +03:00
|
|
|
sprintf(buff, "%-12s%4d %4d %4d%s", name,
|
2017-10-05 00:56:32 +03:00
|
|
|
GETARG_A(i), GETARG_B(i), GETARG_C(i),
|
|
|
|
GETARG_k(i) ? " (k)" : "");
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2002-04-25 00:07:46 +04:00
|
|
|
case iABx:
|
2018-10-08 16:42:07 +03:00
|
|
|
sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2002-04-25 00:07:46 +04:00
|
|
|
case iAsBx:
|
2018-10-08 16:42:07 +03:00
|
|
|
sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2008-04-02 21:38:54 +04:00
|
|
|
case iAx:
|
2018-10-08 16:42:07 +03:00
|
|
|
sprintf(buff, "%-12s%4d", name, GETARG_Ax(i));
|
2008-04-02 21:38:54 +04:00
|
|
|
break;
|
2017-11-07 20:20:42 +03:00
|
|
|
case isJ:
|
2018-10-29 20:26:48 +03:00
|
|
|
sprintf(buff, "%-12s%4d", name, GETARG_sJ(i));
|
2017-11-07 20:20:42 +03:00
|
|
|
break;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
2018-10-08 16:42:07 +03:00
|
|
|
return obuff;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
2000-05-22 22:44:46 +04:00
|
|
|
|
2002-05-13 17:10:04 +04:00
|
|
|
#if 0
|
|
|
|
void luaI_printcode (Proto *pt, int size) {
|
|
|
|
int pc;
|
|
|
|
for (pc=0; pc<size; pc++) {
|
|
|
|
char buff[100];
|
|
|
|
printf("%s\n", buildop(pt, pc, buff));
|
|
|
|
}
|
2002-08-05 18:10:10 +04:00
|
|
|
printf("-------\n");
|
2002-05-13 17:10:04 +04:00
|
|
|
}
|
2012-06-07 22:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
void luaI_printinst (Proto *pt, int pc) {
|
|
|
|
char buff[100];
|
|
|
|
printf("%s\n", buildop(pt, pc, buff));
|
|
|
|
}
|
2002-05-13 17:10:04 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int listcode (lua_State *L) {
|
2000-06-28 21:06:07 +04:00
|
|
|
int pc;
|
2000-04-06 21:35:23 +04:00
|
|
|
Proto *p;
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-28 23:13:13 +03:00
|
|
|
1, "Lua function expected");
|
2009-07-15 22:37:19 +04:00
|
|
|
p = getproto(obj_at(L, 1));
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_newtable(L);
|
2001-11-28 23:13:13 +03:00
|
|
|
setnameval(L, "maxstack", p->maxstacksize);
|
|
|
|
setnameval(L, "numparams", p->numparams);
|
2001-01-15 19:13:24 +03:00
|
|
|
for (pc=0; pc<p->sizecode; pc++) {
|
2001-11-28 23:13:13 +03:00
|
|
|
char buff[100];
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, pc+1);
|
2001-06-05 22:17:01 +04:00
|
|
|
lua_pushstring(L, buildop(p, pc, buff));
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_settable(L, -3);
|
2001-01-15 19:13:24 +03:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
return 1;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
2000-06-02 23:10:01 +04:00
|
|
|
|
2017-10-01 22:17:51 +03:00
|
|
|
static int printcode (lua_State *L) {
|
|
|
|
int pc;
|
|
|
|
Proto *p;
|
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
|
|
|
1, "Lua function expected");
|
|
|
|
p = getproto(obj_at(L, 1));
|
|
|
|
printf("maxstack: %d\n", p->maxstacksize);
|
|
|
|
printf("numparams: %d\n", p->numparams);
|
|
|
|
for (pc=0; pc<p->sizecode; pc++) {
|
|
|
|
char buff[100];
|
2018-07-09 18:50:51 +03:00
|
|
|
printf("%s\n", buildop(p, pc, buff));
|
2017-10-01 22:17:51 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
static int listk (lua_State *L) {
|
2000-06-02 23:10:01 +04:00
|
|
|
Proto *p;
|
|
|
|
int i;
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-28 23:13:13 +03:00
|
|
|
1, "Lua function expected");
|
2009-07-15 22:37:19 +04:00
|
|
|
p = getproto(obj_at(L, 1));
|
2003-10-10 17:29:28 +04:00
|
|
|
lua_createtable(L, p->sizek, 0);
|
2001-06-05 22:17:01 +04:00
|
|
|
for (i=0; i<p->sizek; i++) {
|
2006-07-11 19:53:29 +04:00
|
|
|
pushobject(L, p->k+i);
|
2005-03-28 21:17:53 +04:00
|
|
|
lua_rawseti(L, -2, i+1);
|
2000-06-02 23:10:01 +04:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
return 1;
|
2000-06-02 23:10:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-11 18:53:23 +03:00
|
|
|
static int listabslineinfo (lua_State *L) {
|
|
|
|
Proto *p;
|
|
|
|
int i;
|
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
|
|
|
1, "Lua function expected");
|
|
|
|
p = getproto(obj_at(L, 1));
|
|
|
|
luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info");
|
|
|
|
lua_createtable(L, 2 * p->sizeabslineinfo, 0);
|
|
|
|
for (i=0; i < p->sizeabslineinfo; i++) {
|
|
|
|
lua_pushinteger(L, p->abslineinfo[i].pc);
|
|
|
|
lua_rawseti(L, -2, 2 * i + 1);
|
|
|
|
lua_pushinteger(L, p->abslineinfo[i].line);
|
|
|
|
lua_rawseti(L, -2, 2 * i + 2);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int listlocals (lua_State *L) {
|
2000-06-28 21:06:07 +04:00
|
|
|
Proto *p;
|
2014-11-29 20:38:33 +03:00
|
|
|
int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
|
2000-08-28 21:57:04 +04:00
|
|
|
int i = 0;
|
2001-11-28 23:13:13 +03:00
|
|
|
const char *name;
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-28 23:13:13 +03:00
|
|
|
1, "Lua function expected");
|
2009-07-15 22:37:19 +04:00
|
|
|
p = getproto(obj_at(L, 1));
|
2000-08-28 21:57:04 +04:00
|
|
|
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
return i-1;
|
2000-06-28 21:06:07 +04:00
|
|
|
}
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2001-02-13 19:52:01 +03:00
|
|
|
|
2014-11-11 20:15:06 +03:00
|
|
|
static void printstack (lua_State *L) {
|
|
|
|
int i;
|
|
|
|
int n = lua_gettop(L);
|
2020-07-06 19:54:01 +03:00
|
|
|
printf("stack: >>\n");
|
2014-11-11 20:15:06 +03:00
|
|
|
for (i = 1; i <= n; i++) {
|
|
|
|
printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2020-07-06 19:54:01 +03:00
|
|
|
printf("<<\n");
|
2014-11-11 20:15:06 +03:00
|
|
|
}
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int get_limits (lua_State *L) {
|
2019-05-13 22:20:40 +03:00
|
|
|
lua_createtable(L, 0, 6);
|
|
|
|
setnameval(L, "IS32INT", LUAI_IS32INT);
|
2014-10-27 20:00:02 +03:00
|
|
|
setnameval(L, "MAXARG_Ax", MAXARG_Ax);
|
|
|
|
setnameval(L, "MAXARG_Bx", MAXARG_Bx);
|
2017-12-15 21:53:48 +03:00
|
|
|
setnameval(L, "OFFSET_sBx", OFFSET_sBx);
|
2001-11-28 23:13:13 +03:00
|
|
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
2003-11-05 14:59:14 +03:00
|
|
|
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
2000-08-28 21:57:04 +04:00
|
|
|
return 1;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int mem_query (lua_State *L) {
|
2001-12-21 00:27:12 +03:00
|
|
|
if (lua_isnone(L, 1)) {
|
2007-11-12 19:28:45 +03:00
|
|
|
lua_pushinteger(L, l_memcontrol.total);
|
|
|
|
lua_pushinteger(L, l_memcontrol.numblocks);
|
|
|
|
lua_pushinteger(L, l_memcontrol.maxmem);
|
2010-04-12 20:07:39 +04:00
|
|
|
return 3;
|
2000-08-28 21:57:04 +04:00
|
|
|
}
|
2010-04-12 20:07:39 +04:00
|
|
|
else if (lua_isnumber(L, 1)) {
|
2014-05-08 23:08:46 +04:00
|
|
|
unsigned long limit = cast(unsigned long, luaL_checkinteger(L, 1));
|
|
|
|
if (limit == 0) limit = ULONG_MAX;
|
|
|
|
l_memcontrol.memlimit = limit;
|
2000-08-28 21:57:04 +04:00
|
|
|
return 0;
|
2000-08-04 23:38:35 +04:00
|
|
|
}
|
2010-04-12 20:07:39 +04:00
|
|
|
else {
|
|
|
|
const char *t = luaL_checkstring(L, 1);
|
|
|
|
int i;
|
|
|
|
for (i = LUA_NUMTAGS - 1; i >= 0; i--) {
|
2010-04-14 19:13:48 +04:00
|
|
|
if (strcmp(t, ttypename(i)) == 0) {
|
2010-04-12 20:07:39 +04:00
|
|
|
lua_pushinteger(L, l_memcontrol.objcount[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 17:45:08 +03:00
|
|
|
return luaL_error(L, "unknown type '%s'", t);
|
2010-04-12 20:07:39 +04:00
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-07 21:51:39 +03:00
|
|
|
static int alloc_count (lua_State *L) {
|
|
|
|
if (lua_isnone(L, 1))
|
|
|
|
l_memcontrol.countlimit = ~0L;
|
|
|
|
else
|
|
|
|
l_memcontrol.countlimit = luaL_checkinteger(L, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-03 19:22:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
static int alloc_failnext (lua_State *L) {
|
|
|
|
UNUSED(L);
|
|
|
|
l_memcontrol.failnext = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-03 22:34:36 +03:00
|
|
|
|
2017-12-07 21:51:39 +03:00
|
|
|
|
2004-03-16 00:04:54 +03:00
|
|
|
static int settrick (lua_State *L) {
|
2011-09-25 01:11:29 +04:00
|
|
|
if (ttisnil(obj_at(L, 1)))
|
|
|
|
l_Trick = NULL;
|
|
|
|
else
|
|
|
|
l_Trick = gcvalue(obj_at(L, 1));
|
2004-03-16 00:04:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 18:16:33 +04:00
|
|
|
static int gc_color (lua_State *L) {
|
2004-03-16 00:04:54 +03:00
|
|
|
TValue *o;
|
|
|
|
luaL_checkany(L, 1);
|
|
|
|
o = obj_at(L, 1);
|
2017-04-19 21:46:47 +03:00
|
|
|
if (!iscollectable(o))
|
2004-03-16 00:04:54 +03:00
|
|
|
lua_pushstring(L, "no collectable");
|
2014-09-01 21:58:55 +04:00
|
|
|
else {
|
|
|
|
GCObject *obj = gcvalue(o);
|
|
|
|
lua_pushstring(L, isdead(G(L), obj) ? "dead" :
|
|
|
|
iswhite(obj) ? "white" :
|
2018-02-20 19:52:50 +03:00
|
|
|
isblack(obj) ? "black" : "gray");
|
2017-04-19 21:46:47 +03:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int gc_age (lua_State *L) {
|
|
|
|
TValue *o;
|
|
|
|
luaL_checkany(L, 1);
|
|
|
|
o = obj_at(L, 1);
|
|
|
|
if (!iscollectable(o))
|
|
|
|
lua_pushstring(L, "no collectable");
|
|
|
|
else {
|
|
|
|
static const char *gennames[] = {"new", "survival", "old0", "old1",
|
|
|
|
"old", "touched1", "touched2"};
|
|
|
|
GCObject *obj = gcvalue(o);
|
2017-04-18 22:42:12 +03:00
|
|
|
lua_pushstring(L, gennames[getage(obj)]);
|
2014-09-01 21:58:55 +04:00
|
|
|
}
|
2017-04-19 21:46:47 +03:00
|
|
|
return 1;
|
2017-04-18 22:42:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int gc_printobj (lua_State *L) {
|
|
|
|
TValue *o;
|
|
|
|
luaL_checkany(L, 1);
|
|
|
|
o = obj_at(L, 1);
|
|
|
|
if (!iscollectable(o))
|
|
|
|
printf("no collectable\n");
|
|
|
|
else {
|
|
|
|
GCObject *obj = gcvalue(o);
|
|
|
|
printobj(G(L), obj);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return 0;
|
2004-03-16 00:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-16 21:42:49 +04:00
|
|
|
static int gc_state (lua_State *L) {
|
2017-05-31 21:54:58 +03:00
|
|
|
static const char *statenames[] = {
|
|
|
|
"propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj",
|
|
|
|
"sweeptobefnz", "sweepend", "callfin", "pause", ""};
|
|
|
|
static const int states[] = {
|
|
|
|
GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj,
|
|
|
|
GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1};
|
2014-02-11 16:18:12 +04:00
|
|
|
int option = states[luaL_checkoption(L, 1, "", statenames)];
|
|
|
|
if (option == -1) {
|
2010-04-16 21:42:49 +04:00
|
|
|
lua_pushstring(L, statenames[G(L)->gcstate]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
2012-07-02 19:38:36 +04:00
|
|
|
global_State *g = G(L);
|
2023-12-06 16:49:56 +03:00
|
|
|
if (G(L)->gckind != KGC_INC)
|
2017-04-18 22:42:12 +03:00
|
|
|
luaL_error(L, "cannot change states in generational mode");
|
2010-04-16 21:42:49 +04:00
|
|
|
lua_lock(L);
|
2012-07-02 19:38:36 +04:00
|
|
|
if (option < g->gcstate) { /* must cross 'pause'? */
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */
|
|
|
|
}
|
2010-05-07 22:09:23 +04:00
|
|
|
luaC_runtilstate(L, bitmask(option));
|
|
|
|
lua_assert(G(L)->gcstate == option);
|
2010-04-16 21:42:49 +04:00
|
|
|
lua_unlock(L);
|
|
|
|
return 0;
|
2004-03-16 00:04:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int hash_query (lua_State *L) {
|
2001-12-21 00:27:12 +03:00
|
|
|
if (lua_isnone(L, 2)) {
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
|
2004-03-16 00:04:54 +03:00
|
|
|
lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else {
|
2004-03-16 00:04:54 +03:00
|
|
|
TValue *o = obj_at(L, 1);
|
2001-10-25 23:14:14 +04:00
|
|
|
Table *t;
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_checktype(L, 2, LUA_TTABLE);
|
2004-03-16 00:04:54 +03:00
|
|
|
t = hvalue(obj_at(L, 2));
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
return 1;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-26 01:14:54 +03:00
|
|
|
static int stacklevel (lua_State *L) {
|
2002-01-10 00:51:06 +03:00
|
|
|
unsigned long a = 0;
|
2022-10-29 18:06:37 +03:00
|
|
|
lua_pushinteger(L, (L->top.p - L->stack.p));
|
2020-10-06 21:50:24 +03:00
|
|
|
lua_pushinteger(L, stacksize(L));
|
2017-11-23 18:38:42 +03:00
|
|
|
lua_pushinteger(L, L->nCcalls);
|
|
|
|
lua_pushinteger(L, L->nci);
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, (unsigned long)&a);
|
2017-11-23 18:38:42 +03:00
|
|
|
return 5;
|
2002-01-10 00:51:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int table_query (lua_State *L) {
|
2001-10-25 23:14:14 +04:00
|
|
|
const Table *t;
|
2014-11-29 20:38:33 +03:00
|
|
|
int i = cast_int(luaL_optinteger(L, 2, -1));
|
2018-06-15 17:14:20 +03:00
|
|
|
unsigned int asize;
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
2004-03-16 00:04:54 +03:00
|
|
|
t = hvalue(obj_at(L, 1));
|
2018-06-15 17:14:20 +03:00
|
|
|
asize = luaH_realasize(t);
|
1999-12-14 21:31:20 +03:00
|
|
|
if (i == -1) {
|
2018-06-15 17:14:20 +03:00
|
|
|
lua_pushinteger(L, asize);
|
2016-11-07 15:38:35 +03:00
|
|
|
lua_pushinteger(L, allocsizenode(t));
|
2018-06-15 17:14:20 +03:00
|
|
|
lua_pushinteger(L, t->alimit);
|
2022-11-01 21:42:08 +03:00
|
|
|
return 3;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2018-06-15 17:14:20 +03:00
|
|
|
else if ((unsigned int)i < asize) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, i);
|
2023-10-30 20:25:59 +03:00
|
|
|
arr2obj(t, i + 1, s2v(L->top.p));
|
|
|
|
api_incr_top(L);
|
2006-09-11 18:07:24 +04:00
|
|
|
lua_pushnil(L);
|
2001-10-25 23:14:14 +04:00
|
|
|
}
|
2018-06-15 17:14:20 +03:00
|
|
|
else if ((i -= asize) < sizenode(t)) {
|
2017-06-09 19:48:44 +03:00
|
|
|
TValue k;
|
|
|
|
getnodekey(L, &k, gnode(t, i));
|
2018-02-23 16:16:18 +03:00
|
|
|
if (!isempty(gval(gnode(t, i))) ||
|
2017-06-09 19:48:44 +03:00
|
|
|
ttisnil(&k) ||
|
|
|
|
ttisnumber(&k)) {
|
|
|
|
pushobject(L, &k);
|
2001-04-11 18:42:41 +04:00
|
|
|
}
|
|
|
|
else
|
2003-07-09 16:08:43 +04:00
|
|
|
lua_pushliteral(L, "<undef>");
|
2006-07-11 19:53:29 +04:00
|
|
|
pushobject(L, gval(gnode(t, i)));
|
2013-08-18 20:12:18 +04:00
|
|
|
if (gnext(&t->node[i]) != 0)
|
|
|
|
lua_pushinteger(L, gnext(&t->node[i]));
|
2000-08-28 21:57:04 +04:00
|
|
|
else
|
2001-10-25 23:14:14 +04:00
|
|
|
lua_pushnil(L);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2001-10-25 23:14:14 +04:00
|
|
|
return 3;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-23 23:17:20 +03:00
|
|
|
static int query_inc (lua_State *L) {
|
|
|
|
global_State *g = G(L);
|
|
|
|
lua_pushinteger(L, gettotalobjs(g));
|
|
|
|
lua_pushinteger(L, g->GCdebt);
|
2022-12-13 17:55:14 +03:00
|
|
|
lua_pushinteger(L, applygcparam(g, gcpause, 100));
|
|
|
|
lua_pushinteger(L, applygcparam(g, gcstepmul, 100));
|
2022-11-23 23:29:03 +03:00
|
|
|
lua_pushinteger(L, cast(l_obj, 1) << g->gcstepsize);
|
2022-11-23 23:17:20 +03:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int string_query (lua_State *L) {
|
2001-06-06 22:00:19 +04:00
|
|
|
stringtable *tb = &G(L)->strt;
|
2014-11-29 20:38:33 +03:00
|
|
|
int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
|
2013-09-05 23:31:49 +04:00
|
|
|
if (s == -1) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L ,tb->size);
|
2013-09-05 23:31:49 +04:00
|
|
|
lua_pushinteger(L ,tb->nuse);
|
2000-08-28 21:57:04 +04:00
|
|
|
return 2;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2013-08-22 19:21:48 +04:00
|
|
|
else if (s < tb->size) {
|
2013-09-05 23:31:49 +04:00
|
|
|
TString *ts;
|
|
|
|
int n = 0;
|
2015-01-16 19:54:37 +03:00
|
|
|
for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
|
2022-10-29 18:06:37 +03:00
|
|
|
setsvalue2s(L, L->top.p, ts);
|
2013-09-05 23:31:49 +04:00
|
|
|
api_incr_top(L);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2013-08-21 23:21:16 +04:00
|
|
|
else return 0;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int tref (lua_State *L) {
|
2001-04-17 21:35:54 +04:00
|
|
|
int level = lua_gettop(L);
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_checkany(L, 1);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_pushvalue(L, 1);
|
2010-11-16 20:43:29 +03:00
|
|
|
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
|
2022-10-19 22:29:54 +03:00
|
|
|
cast_void(level); /* to avoid warnings */
|
2003-11-19 22:59:18 +03:00
|
|
|
lua_assert(lua_gettop(L) == level+1); /* +1 for result */
|
2000-08-29 23:05:11 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getref (lua_State *L) {
|
2001-04-17 21:35:54 +04:00
|
|
|
int level = lua_gettop(L);
|
2014-10-01 15:54:56 +04:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
|
2022-10-19 22:29:54 +03:00
|
|
|
cast_void(level); /* to avoid warnings */
|
2003-11-19 22:59:18 +03:00
|
|
|
lua_assert(lua_gettop(L) == level+1);
|
2001-10-18 01:12:57 +04:00
|
|
|
return 1;
|
2000-08-29 23:05:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unref (lua_State *L) {
|
2001-04-17 21:35:54 +04:00
|
|
|
int level = lua_gettop(L);
|
2014-11-29 20:38:33 +03:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
2022-10-19 22:29:54 +03:00
|
|
|
cast_void(level); /* to avoid warnings */
|
2003-11-19 22:59:18 +03:00
|
|
|
lua_assert(lua_gettop(L) == level);
|
2000-08-29 23:05:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-19 14:11:55 +03:00
|
|
|
|
|
|
|
static int upvalue (lua_State *L) {
|
2014-11-29 20:38:33 +03:00
|
|
|
int n = cast_int(luaL_checkinteger(L, 2));
|
2002-12-19 14:11:55 +03:00
|
|
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
|
|
|
if (lua_isnone(L, 3)) {
|
|
|
|
const char *name = lua_getupvalue(L, 1, n);
|
|
|
|
if (name == NULL) return 0;
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *name = lua_setupvalue(L, 1, n);
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int newuserdata (lua_State *L) {
|
2018-02-20 19:52:50 +03:00
|
|
|
size_t size = cast_sizet(luaL_optinteger(L, 1, 0));
|
|
|
|
int nuv = luaL_optinteger(L, 2, 0);
|
|
|
|
char *p = cast_charp(lua_newuserdatauv(L, size, nuv));
|
2001-11-28 23:13:13 +03:00
|
|
|
while (size--) *p++ = '\0';
|
2001-06-06 22:00:19 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-04-05 22:54:31 +04:00
|
|
|
|
|
|
|
static int pushuserdata (lua_State *L) {
|
2013-06-21 01:59:13 +04:00
|
|
|
lua_Integer u = luaL_checkinteger(L, 1);
|
2018-01-28 18:13:26 +03:00
|
|
|
lua_pushlightuserdata(L, cast_voidp(cast_sizet(u)));
|
2001-06-06 22:00:19 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
|
|
|
|
static int udataval (lua_State *L) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
|
2000-08-29 23:05:11 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-22 21:01:38 +03:00
|
|
|
|
|
|
|
static int doonnewstack (lua_State *L) {
|
2002-02-15 00:47:29 +03:00
|
|
|
lua_State *L1 = lua_newthread(L);
|
2002-05-02 00:48:12 +04:00
|
|
|
size_t l;
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *s = luaL_checklstring(L, 1, &l);
|
2002-06-04 00:11:41 +04:00
|
|
|
int status = luaL_loadbuffer(L1, s, l, s);
|
2006-10-10 21:40:17 +04:00
|
|
|
if (status == LUA_OK)
|
2002-08-06 19:32:22 +04:00
|
|
|
status = lua_pcall(L1, 0, 0, 0);
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, status);
|
2001-01-22 21:01:38 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-13 19:52:01 +03:00
|
|
|
static int s2d (lua_State *L) {
|
2018-03-09 22:24:45 +03:00
|
|
|
lua_pushnumber(L, cast_num(*cast(const double *, luaL_checkstring(L, 1))));
|
2001-02-13 19:52:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-11 18:00:59 +04:00
|
|
|
|
2001-02-13 19:52:01 +03:00
|
|
|
static int d2s (lua_State *L) {
|
2018-03-09 22:24:45 +03:00
|
|
|
double d = cast(double, luaL_checknumber(L, 1));
|
2018-01-28 18:13:26 +03:00
|
|
|
lua_pushlstring(L, cast_charp(&d), sizeof(d));
|
2001-02-13 19:52:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-11 18:00:59 +04:00
|
|
|
static int num2int (lua_State *L) {
|
|
|
|
lua_pushinteger(L, lua_tointeger(L, 1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-15 16:28:32 +03:00
|
|
|
static int makeseed (lua_State *L) {
|
|
|
|
lua_pushinteger(L, luaL_makeseed(L));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int newstate (lua_State *L) {
|
2004-06-02 23:08:52 +04:00
|
|
|
void *ud;
|
|
|
|
lua_Alloc f = lua_getallocf(L, &ud);
|
2023-03-20 22:13:17 +03:00
|
|
|
lua_State *L1 = lua_newstate(f, ud, 0);
|
2009-03-03 21:52:36 +03:00
|
|
|
if (L1) {
|
|
|
|
lua_atpanic(L1, tpanic);
|
2009-02-18 17:52:51 +03:00
|
|
|
lua_pushlightuserdata(L, L1);
|
2009-03-03 21:52:36 +03:00
|
|
|
}
|
2000-08-29 23:05:11 +04:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-12-20 13:26:33 +03:00
|
|
|
|
2009-02-18 17:52:51 +03:00
|
|
|
static lua_State *getstate (lua_State *L) {
|
|
|
|
lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
|
|
|
|
luaL_argcheck(L, L1 != NULL, 1, "state expected");
|
|
|
|
return L1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 18:09:31 +04:00
|
|
|
static int loadlib (lua_State *L) {
|
2009-02-18 17:52:51 +03:00
|
|
|
lua_State *L1 = getstate(L);
|
2022-12-07 21:12:52 +03:00
|
|
|
int what = luaL_checkinteger(L, 2);
|
|
|
|
luaL_openselectedlibs(L1, what);
|
|
|
|
luaL_requiref(L1, "T", luaB_opentests, 0);
|
2014-07-16 18:51:36 +04:00
|
|
|
lua_assert(lua_type(L1, -1) == LUA_TTABLE);
|
|
|
|
/* 'requiref' should not reload module already loaded... */
|
2022-12-07 21:12:52 +03:00
|
|
|
luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */
|
2014-07-16 18:51:36 +04:00
|
|
|
/* ...but should return the same module */
|
|
|
|
lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
|
2000-09-14 18:09:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int closestate (lua_State *L) {
|
2009-02-18 17:52:51 +03:00
|
|
|
lua_State *L1 = getstate(L);
|
2001-02-21 19:51:25 +03:00
|
|
|
lua_close(L1);
|
2000-08-29 23:05:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int doremote (lua_State *L) {
|
2009-02-18 17:52:51 +03:00
|
|
|
lua_State *L1 = getstate(L);
|
2002-06-21 00:40:38 +04:00
|
|
|
size_t lcode;
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *code = luaL_checklstring(L, 2, &lcode);
|
2000-08-29 23:05:11 +04:00
|
|
|
int status;
|
2002-06-21 00:40:38 +04:00
|
|
|
lua_settop(L1, 0);
|
|
|
|
status = luaL_loadbuffer(L1, code, lcode, code);
|
2006-10-10 21:40:17 +04:00
|
|
|
if (status == LUA_OK)
|
2002-08-06 19:32:22 +04:00
|
|
|
status = lua_pcall(L1, 0, LUA_MULTRET, 0);
|
2006-10-10 21:40:17 +04:00
|
|
|
if (status != LUA_OK) {
|
2000-08-29 23:05:11 +04:00
|
|
|
lua_pushnil(L);
|
2002-12-20 13:26:33 +03:00
|
|
|
lua_pushstring(L, lua_tostring(L1, -1));
|
2010-07-02 15:38:13 +04:00
|
|
|
lua_pushinteger(L, status);
|
2002-12-20 13:26:33 +03:00
|
|
|
return 3;
|
2000-08-29 23:05:11 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i = 0;
|
2001-12-21 00:27:12 +03:00
|
|
|
while (!lua_isnone(L1, ++i))
|
2000-08-29 23:05:11 +04:00
|
|
|
lua_pushstring(L, lua_tostring(L1, i));
|
2001-03-06 23:09:38 +03:00
|
|
|
lua_pop(L1, i-1);
|
2000-08-29 23:05:11 +04:00
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-25 23:14:14 +04:00
|
|
|
|
2015-04-03 00:10:21 +03:00
|
|
|
static int log2_aux (lua_State *L) {
|
|
|
|
unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
|
|
|
|
lua_pushinteger(L, luaO_ceillog2(x));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 20:15:06 +03:00
|
|
|
struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; };
|
2014-11-10 20:41:36 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** does a long-jump back to "main program".
|
|
|
|
*/
|
|
|
|
static int panicback (lua_State *L) {
|
|
|
|
struct Aux *b;
|
2014-11-11 20:15:06 +03:00
|
|
|
lua_checkstack(L, 1); /* open space for 'Aux' struct */
|
2014-11-10 20:41:36 +03:00
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */
|
|
|
|
b = (struct Aux *)lua_touserdata(L, -1);
|
|
|
|
lua_pop(L, 1); /* remove 'Aux' struct */
|
2014-11-11 20:15:06 +03:00
|
|
|
runC(b->L, L, b->paniccode); /* run optional panic code */
|
2014-11-10 20:41:36 +03:00
|
|
|
longjmp(b->jb, 1);
|
|
|
|
return 1; /* to avoid warnings */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int checkpanic (lua_State *L) {
|
|
|
|
struct Aux b;
|
|
|
|
void *ud;
|
2014-11-11 20:15:06 +03:00
|
|
|
lua_State *L1;
|
|
|
|
const char *code = luaL_checkstring(L, 1);
|
2014-11-10 20:41:36 +03:00
|
|
|
lua_Alloc f = lua_getallocf(L, &ud);
|
2014-11-11 20:15:06 +03:00
|
|
|
b.paniccode = luaL_optstring(L, 2, "");
|
|
|
|
b.L = L;
|
2023-03-20 22:13:17 +03:00
|
|
|
L1 = lua_newstate(f, ud, 0); /* create new state */
|
2014-11-10 20:41:36 +03:00
|
|
|
if (L1 == NULL) { /* error? */
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_atpanic(L1, panicback); /* set its panic function */
|
|
|
|
lua_pushlightuserdata(L1, &b);
|
|
|
|
lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */
|
|
|
|
if (setjmp(b.jb) == 0) { /* set jump buffer */
|
|
|
|
runC(L, L1, code); /* run code unprotected */
|
|
|
|
lua_pushliteral(L, "no errors");
|
|
|
|
}
|
|
|
|
else { /* error handling */
|
|
|
|
/* move error message to original state */
|
2014-11-11 20:15:06 +03:00
|
|
|
lua_pushstring(L, lua_tostring(L1, -1));
|
2014-11-10 20:41:36 +03:00
|
|
|
}
|
|
|
|
lua_close(L1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-09 23:05:42 +03:00
|
|
|
static int externKstr (lua_State *L) {
|
|
|
|
size_t len;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &len);
|
|
|
|
lua_pushextlstring(L, s, len, NULL, NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Create a buffer with the content of a given string and then
|
|
|
|
** create an external string using that buffer. Use the allocation
|
|
|
|
** function from Lua to create and free the buffer.
|
|
|
|
*/
|
|
|
|
static int externstr (lua_State *L) {
|
|
|
|
size_t len;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &len);
|
|
|
|
void *ud;
|
|
|
|
lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */
|
|
|
|
/* create the buffer */
|
|
|
|
char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1));
|
|
|
|
if (buff == NULL) { /* memory error? */
|
|
|
|
lua_pushliteral(L, "not enough memory");
|
|
|
|
lua_error(L); /* raise a memory error */
|
|
|
|
}
|
|
|
|
/* copy string content to buffer, including ending 0 */
|
|
|
|
memcpy(buff, s, (len + 1) * sizeof(char));
|
|
|
|
/* create external string */
|
|
|
|
lua_pushextlstring(L, buff, len, allocf, ud);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-10-22 22:07:55 +04:00
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/*
|
2014-12-10 14:30:51 +03:00
|
|
|
** {====================================================================
|
2001-02-22 21:59:59 +03:00
|
|
|
** function to test the API with C. It interprets a kind of assembler
|
2000-04-06 21:35:23 +04:00
|
|
|
** language with calls to the API, so the test can be driven by Lua code
|
2014-12-10 14:30:51 +03:00
|
|
|
** =====================================================================
|
2000-04-06 21:35:23 +04:00
|
|
|
*/
|
|
|
|
|
2009-12-01 19:49:48 +03:00
|
|
|
|
|
|
|
static void sethookaux (lua_State *L, int mask, int count, const char *code);
|
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static const char *const delimits = " \t\n,;";
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static void skip (const char **pc) {
|
2009-09-10 00:44:10 +04:00
|
|
|
for (;;) {
|
|
|
|
if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
2019-01-04 18:09:47 +03:00
|
|
|
else if (**pc == '#') { /* comment? */
|
|
|
|
while (**pc != '\n' && **pc != '\0') (*pc)++; /* until end-of-line */
|
2009-09-10 00:44:10 +04:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
2009-12-10 21:21:28 +03:00
|
|
|
static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
1999-12-14 21:31:20 +03:00
|
|
|
int res = 0;
|
2000-08-28 21:57:04 +04:00
|
|
|
int sig = 1;
|
1999-12-14 21:31:20 +03:00
|
|
|
skip(pc);
|
2001-11-28 23:13:13 +03:00
|
|
|
if (**pc == '.') {
|
2014-11-29 20:38:33 +03:00
|
|
|
res = cast_int(lua_tointeger(L1, -1));
|
2009-12-10 21:21:28 +03:00
|
|
|
lua_pop(L1, 1);
|
2000-08-28 21:57:04 +04:00
|
|
|
(*pc)++;
|
2000-08-31 17:29:47 +04:00
|
|
|
return res;
|
2000-08-28 21:57:04 +04:00
|
|
|
}
|
2015-10-12 19:38:19 +03:00
|
|
|
else if (**pc == '*') {
|
|
|
|
res = lua_gettop(L1);
|
|
|
|
(*pc)++;
|
|
|
|
return res;
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if (**pc == '-') {
|
2000-08-28 21:57:04 +04:00
|
|
|
sig = -1;
|
|
|
|
(*pc)++;
|
|
|
|
}
|
2010-12-10 16:40:22 +03:00
|
|
|
if (!lisdigit(cast_uchar(**pc)))
|
2009-09-14 18:30:39 +04:00
|
|
|
luaL_error(L, "number expected (%s)", *pc);
|
2010-12-10 16:40:22 +03:00
|
|
|
while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0';
|
2000-08-31 17:29:47 +04:00
|
|
|
return sig*res;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2006-09-11 18:07:24 +04:00
|
|
|
|
2009-12-10 21:21:28 +03:00
|
|
|
static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
|
1999-12-14 21:31:20 +03:00
|
|
|
int i = 0;
|
|
|
|
skip(pc);
|
2009-12-01 19:49:48 +03:00
|
|
|
if (**pc == '"' || **pc == '\'') { /* quoted string? */
|
|
|
|
int quote = *(*pc)++;
|
|
|
|
while (**pc != quote) {
|
|
|
|
if (**pc == '\0') luaL_error(L, "unfinished string in C script");
|
|
|
|
buff[i++] = *(*pc)++;
|
|
|
|
}
|
|
|
|
(*pc)++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (**pc != '\0' && !strchr(delimits, **pc))
|
|
|
|
buff[i++] = *(*pc)++;
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
buff[i] = '\0';
|
1999-12-14 21:31:20 +03:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-10 21:21:28 +03:00
|
|
|
static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
|
2005-02-18 15:40:02 +03:00
|
|
|
skip(pc);
|
|
|
|
switch (*(*pc)++) {
|
|
|
|
case 'R': return LUA_REGISTRYINDEX;
|
2009-12-22 18:32:50 +03:00
|
|
|
case 'G': return luaL_error(L, "deprecated index 'G'");
|
2009-12-10 21:21:28 +03:00
|
|
|
case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
|
|
|
|
default: (*pc)--; return getnum_aux(L, L1, pc);
|
2005-02-18 15:40:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-10 21:21:28 +03:00
|
|
|
|
2020-07-04 22:40:18 +03:00
|
|
|
static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN",
|
|
|
|
"ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Avoid these stat codes from being collected, to avoid possible
|
|
|
|
** memory error when pushing them.
|
|
|
|
*/
|
|
|
|
static void regcodes (lua_State *L) {
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) {
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]);
|
|
|
|
}
|
2009-12-10 21:21:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
#define EQ(s1) (strcmp(s1, inst) == 0)
|
|
|
|
|
2009-12-10 21:21:28 +03:00
|
|
|
#define getnum (getnum_aux(L, L1, &pc))
|
|
|
|
#define getstring (getstring_aux(L, buff, &pc))
|
|
|
|
#define getindex (getindex_aux(L, L1, &pc))
|
2000-08-28 21:57:04 +04:00
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2009-04-14 23:10:17 +04:00
|
|
|
static int testC (lua_State *L);
|
2014-10-07 22:29:13 +04:00
|
|
|
static int Cfunck (lua_State *L, int status, lua_KContext ctx);
|
2009-04-14 23:10:17 +04:00
|
|
|
|
2013-12-31 00:47:58 +04:00
|
|
|
/*
|
|
|
|
** arithmetic operation encoding for 'arith' instruction
|
|
|
|
** LUA_OPIDIV -> \
|
|
|
|
** LUA_OPSHL -> <
|
|
|
|
** LUA_OPSHR -> >
|
|
|
|
** LUA_OPUNM -> _
|
|
|
|
** LUA_OPBNOT -> !
|
|
|
|
*/
|
2014-11-25 17:51:33 +03:00
|
|
|
static const char ops[] = "+-*%^/\\&|~<>_!";
|
2013-12-31 00:47:58 +04:00
|
|
|
|
2009-04-14 23:10:17 +04:00
|
|
|
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
2009-12-01 19:49:48 +03:00
|
|
|
char buff[300];
|
2009-12-10 21:21:28 +03:00
|
|
|
int status = 0;
|
2009-12-01 19:49:48 +03:00
|
|
|
if (pc == NULL) return luaL_error(L, "attempt to runC null script");
|
1999-12-14 21:31:20 +03:00
|
|
|
for (;;) {
|
2009-12-10 21:21:28 +03:00
|
|
|
const char *inst = getstring;
|
2001-11-28 23:13:13 +03:00
|
|
|
if EQ("") return 0;
|
2010-05-14 17:15:54 +04:00
|
|
|
else if EQ("absindex") {
|
|
|
|
lua_pushnumber(L1, lua_absindex(L1, getindex));
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("append") {
|
|
|
|
int t = getindex;
|
|
|
|
int i = lua_rawlen(L1, t);
|
|
|
|
lua_rawseti(L1, t, i + 1);
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("arith") {
|
|
|
|
int op;
|
|
|
|
skip(&pc);
|
|
|
|
op = strchr(ops, *pc++) - ops;
|
|
|
|
lua_arith(L1, op);
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("call") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
|
|
|
lua_call(L1, narg, nres);
|
2010-04-14 19:13:48 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("callk") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
|
|
|
int i = getindex;
|
|
|
|
lua_callk(L1, narg, nres, i, Cfunck);
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("checkstack") {
|
|
|
|
int sz = getnum;
|
2014-11-14 21:15:17 +03:00
|
|
|
const char *msg = getstring;
|
|
|
|
if (*msg == '\0')
|
|
|
|
msg = NULL; /* to test 'luaL_checkstack' with no message */
|
|
|
|
luaL_checkstack(L1, sz, msg);
|
2009-12-17 19:20:01 +03:00
|
|
|
}
|
2017-12-08 18:19:13 +03:00
|
|
|
else if EQ("rawcheckstack") {
|
|
|
|
int sz = getnum;
|
|
|
|
lua_pushboolean(L1, lua_checkstack(L1, sz));
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("compare") {
|
2014-11-25 17:51:33 +03:00
|
|
|
const char *opt = getstring; /* EQ, LT, or LE */
|
|
|
|
int op = (opt[0] == 'E') ? LUA_OPEQ
|
|
|
|
: (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE;
|
2014-04-14 22:42:44 +04:00
|
|
|
int a = getindex;
|
|
|
|
int b = getindex;
|
2014-11-25 17:51:33 +03:00
|
|
|
lua_pushboolean(L1, lua_compare(L1, a, b, op));
|
2009-12-17 19:20:01 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("concat") {
|
|
|
|
lua_concat(L1, getnum);
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("copy") {
|
|
|
|
int f = getindex;
|
|
|
|
lua_copy(L1, f, getindex);
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2010-04-16 21:42:49 +04:00
|
|
|
else if EQ("func2num") {
|
2009-10-01 00:49:25 +04:00
|
|
|
lua_CFunction func = lua_tocfunction(L1, getindex);
|
2018-01-28 18:13:26 +03:00
|
|
|
lua_pushnumber(L1, cast_sizet(func));
|
2009-10-01 00:49:25 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("getfield") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_getfield(L1, t, getstring);
|
2000-08-28 21:57:04 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("getglobal") {
|
|
|
|
lua_getglobal(L1, getstring);
|
2000-09-01 00:23:40 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("getmetatable") {
|
|
|
|
if (lua_getmetatable(L1, getindex) == 0)
|
|
|
|
lua_pushnil(L1);
|
2013-05-07 23:01:16 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("gettable") {
|
|
|
|
lua_gettable(L1, getindex);
|
2004-06-02 23:08:52 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("gettop") {
|
|
|
|
lua_pushinteger(L1, lua_gettop(L1));
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("gsub") {
|
|
|
|
int a = getnum; int b = getnum; int c = getnum;
|
|
|
|
luaL_gsub(L1, lua_tostring(L1, a),
|
|
|
|
lua_tostring(L1, b),
|
|
|
|
lua_tostring(L1, c));
|
2002-03-04 18:26:56 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("insert") {
|
|
|
|
lua_insert(L1, getnum);
|
2002-02-07 20:26:33 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("iscfunction") {
|
|
|
|
lua_pushboolean(L1, lua_iscfunction(L1, getindex));
|
2008-02-11 21:04:26 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isfunction") {
|
|
|
|
lua_pushboolean(L1, lua_isfunction(L1, getindex));
|
2005-02-18 15:40:02 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isnil") {
|
|
|
|
lua_pushboolean(L1, lua_isnil(L1, getindex));
|
2002-03-04 18:26:56 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isnull") {
|
|
|
|
lua_pushboolean(L1, lua_isnone(L1, getindex));
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isnumber") {
|
|
|
|
lua_pushboolean(L1, lua_isnumber(L1, getindex));
|
2001-10-18 01:12:57 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isstring") {
|
|
|
|
lua_pushboolean(L1, lua_isstring(L1, getindex));
|
2009-09-10 00:44:10 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("istable") {
|
|
|
|
lua_pushboolean(L1, lua_istable(L1, getindex));
|
2000-09-01 00:23:40 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isudataval") {
|
|
|
|
lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
|
2000-09-01 00:23:40 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("isuserdata") {
|
|
|
|
lua_pushboolean(L1, lua_isuserdata(L1, getindex));
|
2002-02-06 01:40:05 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("len") {
|
|
|
|
lua_len(L1, getindex);
|
2009-10-05 20:44:33 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("Llen") {
|
|
|
|
lua_pushinteger(L1, luaL_len(L1, getindex));
|
2000-09-25 18:48:42 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("loadfile") {
|
|
|
|
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
2010-03-26 23:58:11 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("loadstring") {
|
2023-09-05 21:30:45 +03:00
|
|
|
size_t slen;
|
|
|
|
const char *s = luaL_checklstring(L1, getnum, &slen);
|
|
|
|
const char *name = getstring;
|
|
|
|
const char *mode = getstring;
|
|
|
|
luaL_loadbufferx(L1, s, slen, name, mode);
|
2009-12-10 21:21:28 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("newmetatable") {
|
|
|
|
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
2009-10-01 00:49:25 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("newtable") {
|
|
|
|
lua_newtable(L1);
|
2009-09-17 22:04:21 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("newthread") {
|
|
|
|
lua_newthread(L1);
|
2000-09-25 18:48:42 +04:00
|
|
|
}
|
2018-12-13 18:07:53 +03:00
|
|
|
else if EQ("resetthread") {
|
2023-05-02 22:41:43 +03:00
|
|
|
lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */
|
2018-12-13 18:07:53 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("newuserdata") {
|
|
|
|
lua_newuserdata(L1, getnum);
|
2010-03-26 23:58:11 +03:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("next") {
|
2004-06-02 23:08:52 +04:00
|
|
|
lua_next(L1, -2);
|
2000-09-05 23:33:32 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("objsize") {
|
|
|
|
lua_pushinteger(L1, lua_rawlen(L1, getindex));
|
|
|
|
}
|
|
|
|
else if EQ("pcall") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2014-12-10 14:30:51 +03:00
|
|
|
status = lua_pcall(L1, narg, nres, getnum);
|
2014-04-14 22:42:44 +04:00
|
|
|
}
|
|
|
|
else if EQ("pcallk") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
|
|
|
int i = getindex;
|
|
|
|
status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
|
|
|
|
}
|
|
|
|
else if EQ("pop") {
|
|
|
|
lua_pop(L1, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2017-12-29 18:59:37 +03:00
|
|
|
else if EQ("printstack") {
|
2009-09-10 00:44:10 +04:00
|
|
|
int n = getnum;
|
|
|
|
if (n != 0) {
|
|
|
|
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
|
|
|
lua_pop(L1, 1);
|
|
|
|
}
|
2014-11-11 20:15:06 +03:00
|
|
|
else printstack(L1);
|
2009-08-26 21:41:26 +04:00
|
|
|
}
|
2017-12-29 18:59:37 +03:00
|
|
|
else if EQ("print") {
|
|
|
|
const char *msg = getstring;
|
|
|
|
printf("%s\n", msg);
|
|
|
|
}
|
2019-03-14 21:30:54 +03:00
|
|
|
else if EQ("warningC") {
|
|
|
|
const char *msg = getstring;
|
|
|
|
lua_warning(L1, msg, 1);
|
|
|
|
}
|
2018-12-28 20:42:34 +03:00
|
|
|
else if EQ("warning") {
|
|
|
|
const char *msg = getstring;
|
2019-03-14 21:30:54 +03:00
|
|
|
lua_warning(L1, msg, 0);
|
2018-12-28 20:42:34 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushbool") {
|
|
|
|
lua_pushboolean(L1, getnum);
|
2001-02-01 19:03:38 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushcclosure") {
|
|
|
|
lua_pushcclosure(L1, testC, getnum);
|
2001-10-25 23:14:14 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushint") {
|
|
|
|
lua_pushinteger(L1, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushnil") {
|
|
|
|
lua_pushnil(L1);
|
2000-09-25 20:22:42 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushnum") {
|
|
|
|
lua_pushnumber(L1, (lua_Number)getnum);
|
2009-08-26 21:41:26 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushstatus") {
|
2020-07-04 22:40:18 +03:00
|
|
|
lua_pushstring(L1, statcodes[status]);
|
2009-09-10 00:44:10 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushstring") {
|
|
|
|
lua_pushstring(L1, getstring);
|
2009-09-10 00:44:10 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushupvalueindex") {
|
|
|
|
lua_pushinteger(L1, lua_upvalueindex(getnum));
|
2009-09-14 18:30:39 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("pushvalue") {
|
|
|
|
lua_pushvalue(L1, getindex);
|
|
|
|
}
|
2019-04-24 20:01:20 +03:00
|
|
|
else if EQ("pushfstringI") {
|
|
|
|
lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1));
|
|
|
|
}
|
|
|
|
else if EQ("pushfstringS") {
|
|
|
|
lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1));
|
|
|
|
}
|
|
|
|
else if EQ("pushfstringP") {
|
|
|
|
lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
|
|
|
|
}
|
2019-06-25 23:38:58 +03:00
|
|
|
else if EQ("rawget") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_rawget(L1, t);
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("rawgeti") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_rawgeti(L1, t, getnum);
|
|
|
|
}
|
2014-11-10 17:47:29 +03:00
|
|
|
else if EQ("rawgetp") {
|
|
|
|
int t = getindex;
|
2018-01-28 18:13:26 +03:00
|
|
|
lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
2014-11-10 17:47:29 +03:00
|
|
|
}
|
2019-06-25 23:38:58 +03:00
|
|
|
else if EQ("rawset") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_rawset(L1, t);
|
|
|
|
}
|
|
|
|
else if EQ("rawseti") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_rawseti(L1, t, getnum);
|
|
|
|
}
|
2014-11-10 17:47:29 +03:00
|
|
|
else if EQ("rawsetp") {
|
|
|
|
int t = getindex;
|
2018-01-28 18:13:26 +03:00
|
|
|
lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
2014-11-10 17:47:29 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("remove") {
|
|
|
|
lua_remove(L1, getnum);
|
|
|
|
}
|
|
|
|
else if EQ("replace") {
|
|
|
|
lua_replace(L1, getindex);
|
2009-12-10 21:21:28 +03:00
|
|
|
}
|
|
|
|
else if EQ("resume") {
|
|
|
|
int i = getindex;
|
2017-11-02 14:28:56 +03:00
|
|
|
int nres;
|
|
|
|
status = lua_resume(lua_tothread(L1, i), L, getnum, &nres);
|
2009-12-10 21:21:28 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("return") {
|
|
|
|
int n = getnum;
|
|
|
|
if (L1 != L) {
|
|
|
|
int i;
|
2017-12-08 18:19:13 +03:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
int idx = -(n - i);
|
|
|
|
switch (lua_type(L1, idx)) {
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
lua_pushboolean(L, lua_toboolean(L1, idx));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_pushstring(L, lua_tostring(L1, idx));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
}
|
|
|
|
return n;
|
2009-12-10 21:21:28 +03:00
|
|
|
}
|
2014-05-13 23:40:28 +04:00
|
|
|
else if EQ("rotate") {
|
|
|
|
int i = getindex;
|
|
|
|
lua_rotate(L1, i, getnum);
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("setfield") {
|
2009-12-10 21:21:28 +03:00
|
|
|
int t = getindex;
|
2019-01-04 18:09:47 +03:00
|
|
|
const char *s = getstring;
|
|
|
|
lua_setfield(L1, t, s);
|
2009-12-10 21:21:28 +03:00
|
|
|
}
|
2019-06-25 23:38:58 +03:00
|
|
|
else if EQ("seti") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_seti(L1, t, getnum);
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("setglobal") {
|
2019-01-04 18:09:47 +03:00
|
|
|
const char *s = getstring;
|
|
|
|
lua_setglobal(L1, s);
|
2002-05-16 18:59:49 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("sethook") {
|
|
|
|
int mask = getnum;
|
|
|
|
int count = getnum;
|
2019-01-04 18:09:47 +03:00
|
|
|
const char *s = getstring;
|
|
|
|
sethookaux(L1, mask, count, s);
|
2000-09-25 20:22:42 +04:00
|
|
|
}
|
2002-01-30 20:26:44 +03:00
|
|
|
else if EQ("setmetatable") {
|
2019-01-04 18:09:47 +03:00
|
|
|
int idx = getindex;
|
|
|
|
lua_setmetatable(L1, idx);
|
2000-10-05 17:00:17 +04:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("settable") {
|
|
|
|
lua_settable(L1, getindex);
|
2008-02-11 21:04:26 +03:00
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("settop") {
|
|
|
|
lua_settop(L1, getnum);
|
2008-06-12 18:20:49 +04:00
|
|
|
}
|
|
|
|
else if EQ("testudata") {
|
|
|
|
int i = getindex;
|
2009-12-10 21:21:28 +03:00
|
|
|
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
2008-06-12 18:20:49 +04:00
|
|
|
}
|
2014-11-10 20:41:36 +03:00
|
|
|
else if EQ("error") {
|
|
|
|
lua_error(L1);
|
|
|
|
}
|
2019-07-31 16:43:51 +03:00
|
|
|
else if EQ("abort") {
|
|
|
|
abort();
|
|
|
|
}
|
2003-05-13 23:22:19 +04:00
|
|
|
else if EQ("throw") {
|
2005-03-23 20:51:11 +03:00
|
|
|
#if defined(__cplusplus)
|
2003-05-13 23:22:19 +04:00
|
|
|
static struct X { int x; } x;
|
|
|
|
throw x;
|
|
|
|
#else
|
2004-06-02 23:08:52 +04:00
|
|
|
luaL_error(L1, "C++");
|
2003-05-13 23:22:19 +04:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("tobool") {
|
|
|
|
lua_pushboolean(L1, lua_toboolean(L1, getindex));
|
|
|
|
}
|
|
|
|
else if EQ("tocfunction") {
|
|
|
|
lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
|
|
|
|
}
|
|
|
|
else if EQ("tointeger") {
|
|
|
|
lua_pushinteger(L1, lua_tointeger(L1, getindex));
|
|
|
|
}
|
|
|
|
else if EQ("tonumber") {
|
|
|
|
lua_pushnumber(L1, lua_tonumber(L1, getindex));
|
|
|
|
}
|
|
|
|
else if EQ("topointer") {
|
2019-03-13 19:16:53 +03:00
|
|
|
lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex)));
|
|
|
|
}
|
|
|
|
else if EQ("touserdata") {
|
|
|
|
lua_pushlightuserdata(L1, lua_touserdata(L1, getindex));
|
2014-04-14 22:42:44 +04:00
|
|
|
}
|
|
|
|
else if EQ("tostring") {
|
|
|
|
const char *s = lua_tostring(L1, getindex);
|
|
|
|
const char *s1 = lua_pushstring(L1, s);
|
2022-10-19 22:29:54 +03:00
|
|
|
cast_void(s1); /* to avoid warnings */
|
2015-06-18 17:25:26 +03:00
|
|
|
lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
|
2014-04-14 22:42:44 +04:00
|
|
|
}
|
2021-07-22 19:48:43 +03:00
|
|
|
else if EQ("Ltolstring") {
|
|
|
|
luaL_tolstring(L1, getindex, NULL);
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("type") {
|
|
|
|
lua_pushstring(L1, luaL_typename(L1, getnum));
|
|
|
|
}
|
|
|
|
else if EQ("xmove") {
|
|
|
|
int f = getindex;
|
|
|
|
int t = getindex;
|
|
|
|
lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
|
|
|
|
lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
|
|
|
|
int n = getnum;
|
|
|
|
if (n == 0) n = lua_gettop(fs);
|
|
|
|
lua_xmove(fs, ts, n);
|
|
|
|
}
|
2020-07-06 19:54:01 +03:00
|
|
|
else if EQ("isyieldable") {
|
|
|
|
lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex)));
|
|
|
|
}
|
2014-04-14 22:42:44 +04:00
|
|
|
else if EQ("yield") {
|
|
|
|
return lua_yield(L1, getnum);
|
|
|
|
}
|
|
|
|
else if EQ("yieldk") {
|
|
|
|
int nres = getnum;
|
|
|
|
int i = getindex;
|
|
|
|
return lua_yieldk(L1, nres, i, Cfunck);
|
|
|
|
}
|
2018-11-07 15:03:05 +03:00
|
|
|
else if EQ("toclose") {
|
2019-01-04 18:09:47 +03:00
|
|
|
lua_toclose(L1, getnum);
|
2018-10-25 21:30:15 +03:00
|
|
|
}
|
2021-01-11 21:03:01 +03:00
|
|
|
else if EQ("closeslot") {
|
|
|
|
lua_closeslot(L1, getnum);
|
|
|
|
}
|
2002-06-18 19:19:27 +04:00
|
|
|
else luaL_error(L, "unknown instruction %s", buff);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
return 0;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
2009-04-14 23:10:17 +04:00
|
|
|
|
|
|
|
static int testC (lua_State *L) {
|
|
|
|
lua_State *L1;
|
|
|
|
const char *pc;
|
|
|
|
if (lua_isuserdata(L, 1)) {
|
|
|
|
L1 = getstate(L);
|
|
|
|
pc = luaL_checkstring(L, 2);
|
|
|
|
}
|
|
|
|
else if (lua_isthread(L, 1)) {
|
|
|
|
L1 = lua_tothread(L, 1);
|
|
|
|
pc = luaL_checkstring(L, 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
L1 = L;
|
|
|
|
pc = luaL_checkstring(L, 1);
|
|
|
|
}
|
|
|
|
return runC(L, L1, pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int Cfunc (lua_State *L) {
|
|
|
|
return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-07 22:29:13 +04:00
|
|
|
static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
|
2020-07-04 22:40:18 +03:00
|
|
|
lua_pushstring(L, statcodes[status]);
|
2014-06-10 21:41:38 +04:00
|
|
|
lua_setglobal(L, "status");
|
|
|
|
lua_pushinteger(L, ctx);
|
|
|
|
lua_setglobal(L, "ctx");
|
|
|
|
return runC(L, L, lua_tostring(L, ctx));
|
2009-08-26 21:41:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-14 23:10:17 +04:00
|
|
|
static int makeCfunc (lua_State *L) {
|
|
|
|
luaL_checkstring(L, 1);
|
2009-09-10 00:44:10 +04:00
|
|
|
lua_pushcclosure(L, Cfunc, lua_gettop(L));
|
2009-04-14 23:10:17 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2009-12-01 19:49:48 +03:00
|
|
|
** tests for C hooks
|
2002-11-18 18:24:27 +03:00
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2009-12-01 19:49:48 +03:00
|
|
|
/*
|
|
|
|
** C hook that runs the C script stored in registry.C_HOOK[L]
|
|
|
|
*/
|
|
|
|
static void Chook (lua_State *L, lua_Debug *ar) {
|
|
|
|
const char *scpt;
|
|
|
|
const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
|
|
|
|
lua_pushlightuserdata(L, L);
|
|
|
|
lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
|
|
|
|
scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
|
|
|
|
lua_pop(L, 2); /* remove C_HOOK and script */
|
|
|
|
lua_pushstring(L, events[ar->event]); /* may be used by script */
|
|
|
|
lua_pushinteger(L, ar->currentline); /* may be used by script */
|
|
|
|
runC(L, L, scpt); /* run script from C_HOOK[L] */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2014-10-30 21:53:28 +03:00
|
|
|
** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook
|
2009-12-01 19:49:48 +03:00
|
|
|
*/
|
|
|
|
static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
|
|
|
|
if (*scpt == '\0') { /* no script? */
|
|
|
|
lua_sethook(L, NULL, 0, 0); /* turn off hooks */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
|
|
|
|
if (!lua_istable(L, -1)) { /* no hook table? */
|
|
|
|
lua_pop(L, 1); /* remove previous value */
|
|
|
|
lua_newtable(L); /* create new C_HOOK table */
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
|
|
|
|
}
|
|
|
|
lua_pushlightuserdata(L, L);
|
|
|
|
lua_pushstring(L, scpt);
|
|
|
|
lua_settable(L, -3); /* C_HOOK[L] = script */
|
|
|
|
lua_sethook(L, Chook, mask, count);
|
2002-11-18 18:24:27 +03:00
|
|
|
}
|
|
|
|
|
2009-12-01 19:49:48 +03:00
|
|
|
|
|
|
|
static int sethook (lua_State *L) {
|
2002-11-18 18:24:27 +03:00
|
|
|
if (lua_isnoneornil(L, 1))
|
2002-11-25 20:47:13 +03:00
|
|
|
lua_sethook(L, NULL, 0, 0); /* turn off hooks */
|
2002-11-18 18:24:27 +03:00
|
|
|
else {
|
2009-12-01 19:49:48 +03:00
|
|
|
const char *scpt = luaL_checkstring(L, 1);
|
|
|
|
const char *smask = luaL_checkstring(L, 2);
|
2014-11-29 20:38:33 +03:00
|
|
|
int count = cast_int(luaL_optinteger(L, 3, 0));
|
2002-11-25 20:47:13 +03:00
|
|
|
int mask = 0;
|
2009-12-01 19:49:48 +03:00
|
|
|
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
|
|
|
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
2002-11-25 20:47:13 +03:00
|
|
|
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
|
|
|
|
if (count > 0) mask |= LUA_MASKCOUNT;
|
2009-12-01 19:49:48 +03:00
|
|
|
sethookaux(L, mask, count, scpt);
|
2002-11-18 18:24:27 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int coresume (lua_State *L) {
|
2017-11-02 14:28:56 +03:00
|
|
|
int status, nres;
|
2002-11-18 18:24:27 +03:00
|
|
|
lua_State *co = lua_tothread(L, 1);
|
|
|
|
luaL_argcheck(L, co, 1, "coroutine expected");
|
2017-11-02 14:28:56 +03:00
|
|
|
status = lua_resume(co, L, 0, &nres);
|
2008-08-13 21:02:12 +04:00
|
|
|
if (status != LUA_OK && status != LUA_YIELD) {
|
2002-11-18 18:24:27 +03:00
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
lua_insert(L, -2);
|
|
|
|
return 2; /* return false + error message */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2005-08-26 21:36:32 +04:00
|
|
|
static const struct luaL_Reg tests_funcs[] = {
|
2005-07-12 18:32:08 +04:00
|
|
|
{"checkmemory", lua_checkmemory},
|
|
|
|
{"closestate", closestate},
|
|
|
|
{"d2s", d2s},
|
|
|
|
{"doonnewstack", doonnewstack},
|
|
|
|
{"doremote", doremote},
|
2013-08-19 18:16:33 +04:00
|
|
|
{"gccolor", gc_color},
|
2017-04-19 21:46:47 +03:00
|
|
|
{"gcage", gc_age},
|
2010-04-16 21:42:49 +04:00
|
|
|
{"gcstate", gc_state},
|
2017-04-18 22:42:12 +03:00
|
|
|
{"pobj", gc_printobj},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"getref", getref},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"hash", hash_query},
|
2015-04-03 00:10:21 +03:00
|
|
|
{"log2", log2_aux},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"limits", get_limits},
|
|
|
|
{"listcode", listcode},
|
2017-10-01 22:17:51 +03:00
|
|
|
{"printcode", printcode},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"listk", listk},
|
2018-07-11 18:53:23 +03:00
|
|
|
{"listabslineinfo", listabslineinfo},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"listlocals", listlocals},
|
|
|
|
{"loadlib", loadlib},
|
2014-11-10 20:41:36 +03:00
|
|
|
{"checkpanic", checkpanic},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"newstate", newstate},
|
|
|
|
{"newuserdata", newuserdata},
|
|
|
|
{"num2int", num2int},
|
2023-11-15 16:28:32 +03:00
|
|
|
{"makeseed", makeseed},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"pushuserdata", pushuserdata},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"querystr", string_query},
|
|
|
|
{"querytab", table_query},
|
2022-11-23 23:17:20 +03:00
|
|
|
{"queryinc", query_inc},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"ref", tref},
|
|
|
|
{"resume", coresume},
|
|
|
|
{"s2d", s2d},
|
2009-12-01 19:49:48 +03:00
|
|
|
{"sethook", sethook},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"stacklevel", stacklevel},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"testC", testC},
|
2009-04-14 23:10:17 +04:00
|
|
|
{"makeCfunc", makeCfunc},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"totalmem", mem_query},
|
2017-12-07 21:51:39 +03:00
|
|
|
{"alloccount", alloc_count},
|
2020-08-03 19:22:57 +03:00
|
|
|
{"allocfailnext", alloc_failnext},
|
2004-03-16 00:04:54 +03:00
|
|
|
{"trick", settrick},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"udataval", udataval},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"unref", unref},
|
2002-12-19 14:11:55 +03:00
|
|
|
{"upvalue", upvalue},
|
2023-11-09 23:05:42 +03:00
|
|
|
{"externKstr", externKstr},
|
|
|
|
{"externstr", externstr},
|
2002-03-20 15:54:08 +03:00
|
|
|
{NULL, NULL}
|
1999-12-14 21:31:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-06-05 23:35:57 +04:00
|
|
|
static void checkfinalmem (void) {
|
2007-11-12 19:28:45 +03:00
|
|
|
lua_assert(l_memcontrol.numblocks == 0);
|
|
|
|
lua_assert(l_memcontrol.total == 0);
|
2006-06-05 23:35:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 20:25:13 +04:00
|
|
|
int luaB_opentests (lua_State *L) {
|
2003-10-20 16:25:23 +04:00
|
|
|
void *ud;
|
2020-12-08 17:54:21 +03:00
|
|
|
lua_Alloc f = lua_getallocf(L, &ud);
|
2009-03-03 21:52:36 +03:00
|
|
|
lua_atpanic(L, &tpanic);
|
2018-12-28 20:42:34 +03:00
|
|
|
lua_setwarnf(L, &warnf, L);
|
2020-07-04 22:40:18 +03:00
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
lua_setglobal(L, "_WARN"); /* _WARN = false */
|
|
|
|
regcodes(L);
|
2006-06-05 23:35:57 +04:00
|
|
|
atexit(checkfinalmem);
|
2020-12-08 17:54:21 +03:00
|
|
|
lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol));
|
|
|
|
lua_setallocf(L, f, ud); /* exercise this function */
|
2010-07-28 19:51:59 +04:00
|
|
|
luaL_newlib(L, tests_funcs);
|
|
|
|
return 1;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-02-13 16:11:34 +04:00
|
|
|
|