1999-12-15 02:31:20 +08:00
|
|
|
/*
|
2013-08-28 02:53:35 +08:00
|
|
|
** $Id: ltests.c,v 2.149 2013/08/26 12:41:10 roberto Exp roberto $
|
1999-12-15 02:31:20 +08:00
|
|
|
** Internal Module for Debugging of the Lua Implementation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-02-07 00:01:29 +08:00
|
|
|
#include <limits.h>
|
2000-04-07 01:35:23 +08:00
|
|
|
#include <stdio.h>
|
1999-12-15 02:31:20 +08:00
|
|
|
#include <stdlib.h>
|
2000-02-22 02:30:06 +08:00
|
|
|
#include <string.h>
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2002-12-05 01:38:31 +08:00
|
|
|
#define ltests_c
|
2004-05-01 04:13:38 +08:00
|
|
|
#define LUA_CORE
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
2000-05-23 02:44:46 +08:00
|
|
|
#include "lcode.h"
|
2009-02-20 01:33:51 +08:00
|
|
|
#include "lctype.h"
|
2000-08-09 02:26:05 +08:00
|
|
|
#include "ldebug.h"
|
2000-05-11 00:33:20 +08:00
|
|
|
#include "ldo.h"
|
2000-06-29 01:06:07 +08:00
|
|
|
#include "lfunc.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lmem.h"
|
2000-04-07 01:35:23 +08:00
|
|
|
#include "lopcodes.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
2000-09-14 22:09:31 +08:00
|
|
|
#include "lualib.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-10-31 00:29:59 +08:00
|
|
|
** The whole module only makes sense with LUA_DEBUG on
|
1999-12-15 02:31:20 +08:00
|
|
|
*/
|
2005-03-24 01:51:11 +08:00
|
|
|
#if defined(LUA_DEBUG)
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2001-02-07 00:01:29 +08:00
|
|
|
|
2008-06-24 00:50:34 +08:00
|
|
|
void *l_Trick = 0;
|
2004-03-16 05:04:54 +08:00
|
|
|
|
|
|
|
|
2001-02-02 23:13:05 +08:00
|
|
|
int islocked = 0;
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2009-06-02 03:09:26 +08:00
|
|
|
#define obj_at(L,k) (L->ci->func + (k))
|
2002-03-05 05:29:41 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static void setnameval (lua_State *L, const char *name, int val) {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushstring(L, name);
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, val);
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_settable(L, -3);
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-11 23:53:29 +08:00
|
|
|
static void pushobject (lua_State *L, const TValue *o) {
|
|
|
|
setobj2s(L, L->top, o);
|
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 02:52:36 +08:00
|
|
|
static int tpanic (lua_State *L) {
|
|
|
|
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
|
|
|
|
lua_tostring(L, -1));
|
|
|
|
return (exit(EXIT_FAILURE), 0); /* do not return to Lua */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-07 00:01:29 +08:00
|
|
|
/*
|
|
|
|
** {======================================================================
|
|
|
|
** Controlled version for realloc.
|
|
|
|
** =======================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
|
|
|
|
2010-04-20 00:38:25 +08:00
|
|
|
typedef union Header {
|
|
|
|
L_Umaxalign a; /* ensures maximum alignment for Header */
|
|
|
|
struct {
|
|
|
|
size_t size;
|
|
|
|
int type;
|
|
|
|
} d;
|
|
|
|
} Header;
|
|
|
|
|
|
|
|
|
2011-05-25 22:12:28 +08:00
|
|
|
#if !defined(EXTERNMEMCHECK)
|
2010-04-20 00:38:25 +08:00
|
|
|
|
2002-11-18 23:24:27 +08:00
|
|
|
/* full memory check */
|
|
|
|
#define MARKSIZE 16 /* size of marks after each block */
|
|
|
|
#define fillmem(mem,size) memset(mem, -MARK, size)
|
2010-04-20 00:38:25 +08:00
|
|
|
|
2002-11-18 23:24:27 +08:00
|
|
|
#else
|
2010-04-20 00:38:25 +08:00
|
|
|
|
2002-11-18 23:24:27 +08:00
|
|
|
/* external memory check: don't do it twice */
|
|
|
|
#define MARKSIZE 0
|
|
|
|
#define fillmem(mem,size) /* empty */
|
2010-04-20 00:38:25 +08:00
|
|
|
|
2002-11-18 23:24:27 +08:00
|
|
|
#endif
|
2001-02-07 00:01:29 +08:00
|
|
|
|
2010-04-20 00:38:25 +08:00
|
|
|
|
2010-04-13 00:07:39 +08:00
|
|
|
Memcontrol l_memcontrol =
|
|
|
|
{0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}};
|
2001-02-07 00:01:29 +08:00
|
|
|
|
|
|
|
|
2010-04-20 00:38:25 +08:00
|
|
|
static void freeblock (Memcontrol *mc, Header *block) {
|
2001-02-07 00:01:29 +08:00
|
|
|
if (block) {
|
2010-04-20 00:38:25 +08:00
|
|
|
size_t size = block->d.size;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MARKSIZE; i++) /* check marks after block */
|
|
|
|
lua_assert(*(cast(char *, block + 1) + size + i) == MARK);
|
|
|
|
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 04:31:17 +08:00
|
|
|
mc->total -= size;
|
2001-02-07 00:01:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-20 00:38:25 +08:00
|
|
|
void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
2003-10-03 04:31:17 +08:00
|
|
|
Memcontrol *mc = cast(Memcontrol *, ud);
|
2010-04-20 00:38:25 +08:00
|
|
|
Header *block = cast(Header *, b);
|
|
|
|
int type;
|
2006-06-06 03:35:57 +08: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-20 00:38:25 +08: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-07 00:01:29 +08:00
|
|
|
if (size == 0) {
|
2010-04-20 00:38:25 +08:00
|
|
|
freeblock(mc, block);
|
2001-02-07 00:01:29 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-10-03 04:31:17 +08:00
|
|
|
else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
|
2010-04-20 00:38:25 +08:00
|
|
|
return NULL; /* fake a memory allocation error */
|
2001-02-07 00:01:29 +08:00
|
|
|
else {
|
2010-04-20 00:38:25 +08:00
|
|
|
Header *newblock;
|
2001-02-07 00:01:29 +08:00
|
|
|
int i;
|
2002-11-18 23:24:27 +08:00
|
|
|
size_t commonsize = (oldsize < size) ? oldsize : size;
|
2010-04-20 00:38:25 +08:00
|
|
|
size_t realsize = sizeof(Header) + size + MARKSIZE;
|
|
|
|
if (realsize < size) return NULL; /* arithmetic overflow! */
|
|
|
|
newblock = cast(Header *, malloc(realsize)); /* alloc a new block */
|
|
|
|
if (newblock == NULL) return NULL; /* really out of memory? */
|
2001-02-07 00:01:29 +08:00
|
|
|
if (block) {
|
2010-04-20 00:38:25 +08:00
|
|
|
memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
|
|
|
|
freeblock(mc, block); /* erase (and check) old copy */
|
2001-02-07 00:01:29 +08:00
|
|
|
}
|
|
|
|
/* initialize new part of the block with something `weird' */
|
2010-04-20 00:38:25 +08:00
|
|
|
fillmem(cast(char *, newblock + 1) + commonsize, size - commonsize);
|
|
|
|
/* initialize marks after block */
|
|
|
|
for (i = 0; i < MARKSIZE; i++)
|
|
|
|
*(cast(char *, newblock + 1) + size + i) = MARK;
|
|
|
|
newblock->d.size = size;
|
|
|
|
newblock->d.type = type;
|
2003-10-03 04:31:17 +08:00
|
|
|
mc->total += size;
|
|
|
|
if (mc->total > mc->maxmem)
|
|
|
|
mc->maxmem = mc->total;
|
|
|
|
mc->numblocks++;
|
2010-04-20 00:38:25 +08:00
|
|
|
mc->objcount[type]++;
|
|
|
|
return newblock + 1;
|
2001-02-07 00:01:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* }====================================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-17 03:09:52 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Functions to check memory consistency
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2009-12-12 03:14:59 +08:00
|
|
|
|
2004-02-17 03:09:52 +08:00
|
|
|
static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
|
|
|
|
if (isdead(g,t)) return 0;
|
2012-07-04 23:52:38 +08:00
|
|
|
if (!issweepphase(g))
|
|
|
|
return !(isblack(f) && iswhite(t));
|
2010-04-30 01:33:51 +08:00
|
|
|
else return 1;
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 02:55:49 +08:00
|
|
|
/*
|
|
|
|
** Check locality
|
|
|
|
*/
|
|
|
|
static int testobjref2 (GCObject *f, GCObject *t) {
|
|
|
|
/* not a local or pointed by a thread? */
|
|
|
|
if (!islocal(t) || gch(f)->tt == LUA_TTHREAD)
|
|
|
|
return 1; /* ok */
|
2013-08-19 22:16:33 +08:00
|
|
|
if (gch(f)->tt == LUA_TPROTO && gch(t)->tt == LUA_TLCL)
|
|
|
|
return 1; /* cache from a prototype */
|
2013-08-17 02:55:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-17 03:09:52 +08:00
|
|
|
static void printobj (global_State *g, GCObject *o) {
|
2012-05-31 00:40:29 +08:00
|
|
|
int i = 1;
|
2004-03-16 05:04:54 +08:00
|
|
|
GCObject *p;
|
2010-03-24 21:07:01 +08:00
|
|
|
for (p = g->allgc; p != o && p != NULL; p = gch(p)->next) i++;
|
2012-05-31 00:40:29 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
i = 1;
|
|
|
|
for (p = g->finobj; p != o && p != NULL; p = gch(p)->next) i++;
|
|
|
|
if (p == NULL) i = 0; /* zero means 'not found' */
|
|
|
|
else i = -i; /* negative means 'found in findobj list */
|
|
|
|
}
|
2013-08-17 02:55:49 +08:00
|
|
|
printf("||%d:%s(%p)-%s-%c(%02X)||",
|
|
|
|
i, ttypename(novariant(gch(o)->tt)), (void *)o,
|
|
|
|
islocal(o)?"L":"NL",
|
2008-02-20 02:55:09 +08:00
|
|
|
isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', gch(o)->marked);
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
2013-08-17 02:55:49 +08:00
|
|
|
int r1 = testobjref1(g,f,t);
|
|
|
|
int r2 = testobjref2(f,t);
|
|
|
|
if (!r1 || !r2) {
|
|
|
|
if (!r1)
|
|
|
|
printf("%d(%02X) - ", g->gcstate, g->currentwhite);
|
|
|
|
else
|
|
|
|
printf("local violation - ");
|
2004-03-16 05:04:54 +08:00
|
|
|
printobj(g, f);
|
2013-08-17 02:55:49 +08:00
|
|
|
printf(" -> ");
|
2004-03-16 05:04:54 +08:00
|
|
|
printobj(g, t);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2013-08-17 02:55:49 +08:00
|
|
|
return r1 && r2;
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 02:55:49 +08:00
|
|
|
#define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
|
2004-02-17 03:09:52 +08:00
|
|
|
|
|
|
|
|
2009-11-07 01:08:43 +08:00
|
|
|
static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
2013-08-17 03:02:31 +08:00
|
|
|
lua_assert(!iscollectable(t) ||
|
|
|
|
(righttt(t) && testobjref(g, f, gcvalue(t))));
|
2009-11-07 01:08:43 +08:00
|
|
|
}
|
2004-02-17 03:09:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void checktable (global_State *g, Table *h) {
|
|
|
|
int i;
|
2010-04-30 01:33:51 +08:00
|
|
|
Node *n, *limit = gnode(h, sizenode(h));
|
2004-02-17 03:09:52 +08:00
|
|
|
GCObject *hgc = obj2gco(h);
|
|
|
|
if (h->metatable)
|
|
|
|
checkobjref(g, hgc, h->metatable);
|
2010-04-30 01:33:51 +08:00
|
|
|
for (i = 0; i < h->sizearray; i++)
|
2004-02-17 03:09:52 +08:00
|
|
|
checkvalref(g, hgc, &h->array[i]);
|
2010-04-30 01:33:51 +08:00
|
|
|
for (n = gnode(h, 0); n < limit; n++) {
|
2004-02-17 03:09:52 +08:00
|
|
|
if (!ttisnil(gval(n))) {
|
|
|
|
lua_assert(!ttisnil(gkey(n)));
|
|
|
|
checkvalref(g, hgc, gkey(n));
|
|
|
|
checkvalref(g, hgc, gval(n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** All marks are conditional because a GC may happen while the
|
|
|
|
** prototype is still being created
|
|
|
|
*/
|
|
|
|
static void checkproto (global_State *g, Proto *f) {
|
|
|
|
int i;
|
|
|
|
GCObject *fgc = obj2gco(f);
|
2013-08-19 22:16:33 +08:00
|
|
|
if (f->cache) checkobjref(g, fgc, f->cache);
|
2004-02-17 03:09:52 +08:00
|
|
|
if (f->source) checkobjref(g, fgc, f->source);
|
|
|
|
for (i=0; i<f->sizek; i++) {
|
|
|
|
if (ttisstring(f->k+i))
|
|
|
|
checkobjref(g, fgc, rawtsvalue(f->k+i));
|
|
|
|
}
|
|
|
|
for (i=0; i<f->sizeupvalues; i++) {
|
2009-09-29 00:32:50 +08:00
|
|
|
if (f->upvalues[i].name)
|
|
|
|
checkobjref(g, fgc, f->upvalues[i].name);
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
for (i=0; i<f->sizep; i++) {
|
|
|
|
if (f->p[i])
|
|
|
|
checkobjref(g, fgc, f->p[i]);
|
|
|
|
}
|
|
|
|
for (i=0; i<f->sizelocvars; i++) {
|
|
|
|
if (f->locvars[i].varname)
|
|
|
|
checkobjref(g, fgc, f->locvars[i].varname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-21 06:05:50 +08:00
|
|
|
static void checkCclosure (global_State *g, CClosure *cl) {
|
2004-02-17 03:09:52 +08:00
|
|
|
GCObject *clgc = obj2gco(cl);
|
2012-01-21 06:05:50 +08: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;
|
2012-05-08 21:53:33 +08:00
|
|
|
if (cl->p) checkobjref(g, clgc, cl->p);
|
2012-01-21 06:05:50 +08:00
|
|
|
for (i=0; i<cl->nupvalues; i++) {
|
2013-08-28 02:53:35 +08:00
|
|
|
UpVal *uv = cl->upvals[i];
|
|
|
|
if (uv) {
|
|
|
|
if (!upisopen(uv)) /* only closed upvalues matter to invariant */
|
|
|
|
checkvalref(g, clgc, uv->v);
|
|
|
|
lua_assert(uv->refcount > 0);
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-18 06:00:01 +08:00
|
|
|
static int lua_checkpc (pCallInfo ci) {
|
|
|
|
if (!isLua(ci)) return 1;
|
|
|
|
else {
|
2011-06-03 03:31:40 +08:00
|
|
|
Proto *p = ci_func(ci)->p;
|
2009-04-18 06:00:01 +08:00
|
|
|
return p->code <= ci->u.l.savedpc &&
|
|
|
|
ci->u.l.savedpc <= p->code + p->sizecode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-17 03:09:52 +08:00
|
|
|
static void checkstack (global_State *g, lua_State *L1) {
|
|
|
|
StkId o;
|
|
|
|
CallInfo *ci;
|
2013-08-28 02:53:35 +08:00
|
|
|
UpVal *uv;
|
2004-02-17 03:09:52 +08:00
|
|
|
lua_assert(!isdead(g, obj2gco(L1)));
|
2013-08-28 02:53:35 +08:00
|
|
|
for (uv = L1->openupval; uv != NULL; uv = uv->u.op.next)
|
|
|
|
lua_assert(upisopen(uv)); /* must be open */
|
2009-04-17 22:28:06 +08:00
|
|
|
for (ci = L1->ci; ci != NULL; ci = ci->previous) {
|
|
|
|
lua_assert(ci->top <= L1->stack_last);
|
2009-04-18 06:00:01 +08:00
|
|
|
lua_assert(lua_checkpc(ci));
|
2004-03-16 05:04:54 +08:00
|
|
|
}
|
|
|
|
if (L1->stack) {
|
|
|
|
for (o = L1->stack; o < L1->top; o++)
|
|
|
|
checkliveness(g, o);
|
|
|
|
}
|
|
|
|
else lua_assert(L1->stacksize == 0);
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-01 04:25:42 +08:00
|
|
|
static void checkobject (global_State *g, GCObject *o, int maybedead) {
|
2004-03-16 05:04:54 +08:00
|
|
|
if (isdead(g, o))
|
2012-06-01 04:25:42 +08:00
|
|
|
lua_assert(maybedead);
|
2004-03-16 05:04:54 +08:00
|
|
|
else {
|
2013-08-17 03:02:31 +08:00
|
|
|
lua_assert(g->gcstate != GCSpause || iswhite(o));
|
2008-02-20 02:55:09 +08:00
|
|
|
switch (gch(o)->tt) {
|
2004-03-16 05:04:54 +08:00
|
|
|
case LUA_TUSERDATA: {
|
|
|
|
Table *mt = gco2u(o)->metatable;
|
|
|
|
if (mt) checkobjref(g, o, mt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TTABLE: {
|
2008-02-20 02:55:09 +08:00
|
|
|
checktable(g, gco2t(o));
|
2004-03-16 05:04:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TTHREAD: {
|
2013-08-17 02:55:49 +08:00
|
|
|
lua_assert(!islocal(o));
|
2004-03-16 05:04:54 +08:00
|
|
|
checkstack(g, gco2th(o));
|
|
|
|
break;
|
|
|
|
}
|
2012-01-21 06:05:50 +08:00
|
|
|
case LUA_TLCL: {
|
|
|
|
checkLclosure(g, gco2lcl(o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TCCL: {
|
|
|
|
checkCclosure(g, gco2ccl(o));
|
2004-03-16 05:04:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TPROTO: {
|
|
|
|
checkproto(g, gco2p(o));
|
|
|
|
break;
|
|
|
|
}
|
2012-01-26 05:05:40 +08:00
|
|
|
case LUA_TSHRSTR:
|
|
|
|
case LUA_TLNGSTR: break;
|
2004-03-16 05:04:54 +08:00
|
|
|
default: lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 02:16:57 +08:00
|
|
|
#define TESTGRAYBIT 7
|
2010-04-30 01:33:51 +08:00
|
|
|
|
2013-08-26 20:41:10 +08:00
|
|
|
static void checkgraylist (global_State *g, GCObject *l) {
|
|
|
|
UNUSED(g); /* better to keep it available if we need to print an object */
|
2010-04-30 01:33:51 +08:00
|
|
|
while (l) {
|
|
|
|
lua_assert(isgray(l));
|
2010-05-07 02:16:57 +08:00
|
|
|
lua_assert(!testbit(l->gch.marked, TESTGRAYBIT));
|
2011-06-24 22:36:21 +08:00
|
|
|
l_setbit(l->gch.marked, TESTGRAYBIT);
|
2010-04-30 01:33:51 +08:00
|
|
|
switch (gch(l)->tt) {
|
|
|
|
case LUA_TTABLE: l = gco2t(l)->gclist; break;
|
2012-05-31 00:40:29 +08:00
|
|
|
case LUA_TLCL: l = gco2lcl(l)->gclist; break;
|
|
|
|
case LUA_TCCL: l = gco2ccl(l)->gclist; break;
|
2010-04-30 01:33:51 +08:00
|
|
|
case LUA_TTHREAD: l = gco2th(l)->gclist; break;
|
|
|
|
case LUA_TPROTO: l = gco2p(l)->gclist; break;
|
|
|
|
default: lua_assert(0); /* other objects cannot be gray */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 02:16:57 +08:00
|
|
|
/*
|
|
|
|
** mark all objects in gray lists with the TESTGRAYBIT, so that
|
|
|
|
** 'checkmemory' can check that all gray objects are in a gray list
|
|
|
|
*/
|
2010-04-30 01:33:51 +08:00
|
|
|
static void markgrays (global_State *g) {
|
2010-05-01 02:37:14 +08:00
|
|
|
if (!keepinvariant(g)) return;
|
2013-08-26 20:41:10 +08:00
|
|
|
checkgraylist(g, g->gray);
|
|
|
|
checkgraylist(g, g->grayagain);
|
|
|
|
checkgraylist(g, g->weak);
|
|
|
|
checkgraylist(g, g->ephemeron);
|
|
|
|
checkgraylist(g, g->allweak);
|
2010-04-30 01:33:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-06 00:58:28 +08:00
|
|
|
static void checkgray (global_State *g, GCObject *o) {
|
2010-05-07 02:16:57 +08:00
|
|
|
for (; o != NULL; o = gch(o)->next) {
|
|
|
|
if (isgray(o)) {
|
|
|
|
lua_assert(!keepinvariant(g) || testbit(o->gch.marked, TESTGRAYBIT));
|
2011-06-24 22:36:21 +08:00
|
|
|
resetbit(o->gch.marked, TESTGRAYBIT);
|
2010-05-07 02:16:57 +08:00
|
|
|
}
|
|
|
|
lua_assert(!testbit(o->gch.marked, TESTGRAYBIT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 05:04:54 +08:00
|
|
|
int lua_checkmemory (lua_State *L) {
|
2004-02-17 03:09:52 +08:00
|
|
|
global_State *g = G(L);
|
|
|
|
GCObject *o;
|
2012-06-01 04:25:42 +08:00
|
|
|
int maybedead;
|
2013-08-07 20:18:11 +08:00
|
|
|
int isthread;
|
2010-05-08 02:09:23 +08:00
|
|
|
if (keepinvariant(g)) {
|
|
|
|
lua_assert(!iswhite(obj2gco(g->mainthread)));
|
|
|
|
lua_assert(!iswhite(gcvalue(&g->l_registry)));
|
|
|
|
}
|
2010-05-08 02:23:17 +08:00
|
|
|
lua_assert(!isdead(g, gcvalue(&g->l_registry)));
|
|
|
|
checkstack(g, g->mainthread);
|
2011-06-24 22:36:21 +08:00
|
|
|
resetbit(g->mainthread->marked, TESTGRAYBIT);
|
2010-05-07 02:16:57 +08:00
|
|
|
/* check 'allgc' list */
|
2010-04-30 01:33:51 +08:00
|
|
|
markgrays(g);
|
2013-08-06 00:58:28 +08:00
|
|
|
checkgray(g, g->allgc);
|
2012-06-01 04:25:42 +08:00
|
|
|
lua_assert(g->sweepgc == NULL || issweepphase(g));
|
|
|
|
maybedead = 0;
|
2013-08-07 20:18:11 +08:00
|
|
|
isthread = 0; /* not traversing threads (yet) */
|
2010-03-25 21:06:36 +08:00
|
|
|
for (o = g->allgc; o != NULL; o = gch(o)->next) {
|
2012-06-01 04:25:42 +08:00
|
|
|
if (g->sweepgc && o == *g->sweepgc)
|
2013-03-17 05:10:18 +08:00
|
|
|
maybedead = 1; /* part of the list not yet swept */
|
2013-08-07 20:18:11 +08:00
|
|
|
if (gch(o)->tt == LUA_TTHREAD) isthread = 1; /* now travesing threads... */
|
|
|
|
else lua_assert(!isthread); /* ... and only threads */
|
2012-06-01 04:25:42 +08:00
|
|
|
checkobject(g, o, maybedead);
|
2013-08-21 01:46:34 +08:00
|
|
|
lua_assert(!tofinalize(o));
|
2008-06-27 03:42:45 +08:00
|
|
|
}
|
2010-11-26 22:32:31 +08:00
|
|
|
/* check 'finobj' list */
|
2013-08-06 00:58:28 +08:00
|
|
|
checkgray(g, g->finobj);
|
2010-11-26 22:32:31 +08:00
|
|
|
for (o = g->finobj; o != NULL; o = gch(o)->next) {
|
2013-08-21 01:46:34 +08:00
|
|
|
lua_assert(tofinalize(o));
|
2010-11-26 22:32:31 +08:00
|
|
|
lua_assert(gch(o)->tt == LUA_TUSERDATA ||
|
|
|
|
gch(o)->tt == LUA_TTABLE);
|
2012-06-01 04:25:42 +08:00
|
|
|
checkobject(g, o, 0);
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
2010-05-07 02:16:57 +08:00
|
|
|
/* check 'tobefnz' list */
|
2013-08-06 00:58:28 +08:00
|
|
|
checkgray(g, g->tobefnz);
|
2010-05-03 19:55:40 +08:00
|
|
|
for (o = g->tobefnz; o != NULL; o = gch(o)->next) {
|
2012-07-04 23:52:38 +08:00
|
|
|
lua_assert(!iswhite(o) || g->gcstate == GCSpause);
|
2013-08-21 01:46:34 +08:00
|
|
|
lua_assert(!isdead(g, o) && tofinalize(o));
|
2010-11-26 22:32:31 +08:00
|
|
|
lua_assert(gch(o)->tt == LUA_TUSERDATA ||
|
|
|
|
gch(o)->tt == LUA_TTABLE);
|
2010-05-03 19:55:40 +08:00
|
|
|
}
|
2013-08-26 20:41:10 +08:00
|
|
|
/* check 'localgc' list */
|
|
|
|
checkgray(g, g->localgc);
|
|
|
|
for (o = g->localgc; o != NULL; o = gch(o)->next) {
|
|
|
|
checkobject(g, o, 1);
|
|
|
|
}
|
2004-03-16 05:04:54 +08:00
|
|
|
return 0;
|
2004-02-17 03:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Disassembler
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static char *buildop (Proto *p, int pc, char *buff) {
|
2000-06-29 01:06:07 +08:00
|
|
|
Instruction i = p->code[pc];
|
2000-05-23 02:44:46 +08:00
|
|
|
OpCode o = GET_OPCODE(i);
|
2001-11-29 04:13:13 +08:00
|
|
|
const char *name = luaP_opnames[o];
|
2009-06-11 00:57:53 +08:00
|
|
|
int line = getfuncline(p, pc);
|
2002-02-06 06:40:05 +08:00
|
|
|
sprintf(buff, "(%4d) %4d - ", line, pc);
|
2006-09-11 22:07:24 +08:00
|
|
|
switch (getOpMode(o)) {
|
2001-06-06 02:17:01 +08:00
|
|
|
case iABC:
|
2001-11-29 04:13:13 +08:00
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
|
2001-06-06 02:17:01 +08:00
|
|
|
GETARG_A(i), GETARG_B(i), GETARG_C(i));
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2002-04-25 04:07:46 +08:00
|
|
|
case iABx:
|
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2002-04-25 04:07:46 +08:00
|
|
|
case iAsBx:
|
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2008-04-03 01:38:54 +08:00
|
|
|
case iAx:
|
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
|
|
|
|
break;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
return buff;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
2000-05-23 02:44:46 +08:00
|
|
|
|
2002-05-13 21:10:04 +08: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 22:10:10 +08:00
|
|
|
printf("-------\n");
|
2002-05-13 21:10:04 +08:00
|
|
|
}
|
2012-06-08 02:52:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
void luaI_printinst (Proto *pt, int pc) {
|
|
|
|
char buff[100];
|
|
|
|
printf("%s\n", buildop(pt, pc, buff));
|
|
|
|
}
|
2002-05-13 21:10:04 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int listcode (lua_State *L) {
|
2000-06-29 01:06:07 +08:00
|
|
|
int pc;
|
2000-04-07 01:35:23 +08:00
|
|
|
Proto *p;
|
2002-11-14 23:41:38 +08:00
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-29 04:13:13 +08:00
|
|
|
1, "Lua function expected");
|
2009-07-16 02:37:19 +08:00
|
|
|
p = getproto(obj_at(L, 1));
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_newtable(L);
|
2001-11-29 04:13:13 +08:00
|
|
|
setnameval(L, "maxstack", p->maxstacksize);
|
|
|
|
setnameval(L, "numparams", p->numparams);
|
2001-01-16 00:13:24 +08:00
|
|
|
for (pc=0; pc<p->sizecode; pc++) {
|
2001-11-29 04:13:13 +08:00
|
|
|
char buff[100];
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, pc+1);
|
2001-06-06 02:17:01 +08:00
|
|
|
lua_pushstring(L, buildop(p, pc, buff));
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_settable(L, -3);
|
2001-01-16 00:13:24 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 03:10:01 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static int listk (lua_State *L) {
|
2000-06-03 03:10:01 +08:00
|
|
|
Proto *p;
|
|
|
|
int i;
|
2002-11-14 23:41:38 +08:00
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-29 04:13:13 +08:00
|
|
|
1, "Lua function expected");
|
2009-07-16 02:37:19 +08:00
|
|
|
p = getproto(obj_at(L, 1));
|
2003-10-10 21:29:28 +08:00
|
|
|
lua_createtable(L, p->sizek, 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
for (i=0; i<p->sizek; i++) {
|
2006-07-11 23:53:29 +08:00
|
|
|
pushobject(L, p->k+i);
|
2005-03-29 01:17:53 +08:00
|
|
|
lua_rawseti(L, -2, i+1);
|
2000-06-03 03:10:01 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
2000-06-03 03:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int listlocals (lua_State *L) {
|
2000-06-29 01:06:07 +08:00
|
|
|
Proto *p;
|
2002-11-14 23:41:38 +08:00
|
|
|
int pc = luaL_checkint(L, 2) - 1;
|
2000-08-29 01:57:04 +08:00
|
|
|
int i = 0;
|
2001-11-29 04:13:13 +08:00
|
|
|
const char *name;
|
2002-11-14 23:41:38 +08:00
|
|
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-29 04:13:13 +08:00
|
|
|
1, "Lua function expected");
|
2009-07-16 02:37:19 +08:00
|
|
|
p = getproto(obj_at(L, 1));
|
2000-08-29 01:57:04 +08:00
|
|
|
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
return i-1;
|
2000-06-29 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2001-02-14 00:52:01 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int get_limits (lua_State *L) {
|
2003-10-10 21:29:28 +08:00
|
|
|
lua_createtable(L, 0, 5);
|
2005-03-17 00:58:41 +08:00
|
|
|
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
2001-11-29 04:13:13 +08:00
|
|
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
|
|
|
setnameval(L, "MAXSTACK", MAXSTACK);
|
2003-11-05 19:59:14 +08:00
|
|
|
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int mem_query (lua_State *L) {
|
2001-12-21 05:27:12 +08:00
|
|
|
if (lua_isnone(L, 1)) {
|
2007-11-13 00:28:45 +08:00
|
|
|
lua_pushinteger(L, l_memcontrol.total);
|
|
|
|
lua_pushinteger(L, l_memcontrol.numblocks);
|
|
|
|
lua_pushinteger(L, l_memcontrol.maxmem);
|
2010-04-13 00:07:39 +08:00
|
|
|
return 3;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2010-04-13 00:07:39 +08:00
|
|
|
else if (lua_isnumber(L, 1)) {
|
2007-11-13 00:28:45 +08:00
|
|
|
l_memcontrol.memlimit = luaL_checkint(L, 1);
|
2000-08-29 01:57:04 +08:00
|
|
|
return 0;
|
2000-08-05 03:38:35 +08:00
|
|
|
}
|
2010-04-13 00:07:39 +08:00
|
|
|
else {
|
|
|
|
const char *t = luaL_checkstring(L, 1);
|
|
|
|
int i;
|
|
|
|
for (i = LUA_NUMTAGS - 1; i >= 0; i--) {
|
2010-04-14 23:13:48 +08:00
|
|
|
if (strcmp(t, ttypename(i)) == 0) {
|
2010-04-13 00:07:39 +08:00
|
|
|
lua_pushinteger(L, l_memcontrol.objcount[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return luaL_error(L, "unkown type '%s'", t);
|
|
|
|
}
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 05:04:54 +08:00
|
|
|
static int settrick (lua_State *L) {
|
2011-09-25 05:11:29 +08:00
|
|
|
if (ttisnil(obj_at(L, 1)))
|
|
|
|
l_Trick = NULL;
|
|
|
|
else
|
|
|
|
l_Trick = gcvalue(obj_at(L, 1));
|
2004-03-16 05:04:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 22:16:33 +08:00
|
|
|
static int gc_color (lua_State *L) {
|
2004-03-16 05:04:54 +08:00
|
|
|
TValue *o;
|
|
|
|
luaL_checkany(L, 1);
|
|
|
|
o = obj_at(L, 1);
|
|
|
|
if (!iscollectable(o))
|
|
|
|
lua_pushstring(L, "no collectable");
|
2013-08-17 02:55:49 +08:00
|
|
|
else
|
2004-03-16 05:04:54 +08:00
|
|
|
lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
|
|
|
|
isblack(gcvalue(o)) ? "black" : "grey");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 22:16:33 +08:00
|
|
|
static int gc_local (lua_State *L) {
|
|
|
|
TValue *o;
|
|
|
|
luaL_checkany(L, 1);
|
|
|
|
o = obj_at(L, 1);
|
|
|
|
lua_pushboolean(L, !iscollectable(o) || islocal(gcvalue(o)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-17 01:42:49 +08:00
|
|
|
static int gc_state (lua_State *L) {
|
2010-05-08 02:09:23 +08:00
|
|
|
static const char *statenames[] = {"propagate", "atomic",
|
2013-08-22 03:21:16 +08:00
|
|
|
"sweepudata", "sweep", "pause", ""};
|
2010-04-17 01:42:49 +08:00
|
|
|
int option = luaL_checkoption(L, 1, "", statenames);
|
2010-05-08 02:09:23 +08:00
|
|
|
if (option == GCSpause + 1) {
|
2010-04-17 01:42:49 +08:00
|
|
|
lua_pushstring(L, statenames[G(L)->gcstate]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
2012-07-02 23:38:36 +08:00
|
|
|
global_State *g = G(L);
|
2010-04-17 01:42:49 +08:00
|
|
|
lua_lock(L);
|
2012-07-02 23:38:36 +08:00
|
|
|
if (option < g->gcstate) { /* must cross 'pause'? */
|
|
|
|
luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */
|
|
|
|
}
|
2010-05-08 02:09:23 +08:00
|
|
|
luaC_runtilstate(L, bitmask(option));
|
|
|
|
lua_assert(G(L)->gcstate == option);
|
2010-04-17 01:42:49 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
return 0;
|
2004-03-16 05:04:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int hash_query (lua_State *L) {
|
2001-12-21 05:27:12 +08:00
|
|
|
if (lua_isnone(L, 2)) {
|
2002-11-14 23:41:38 +08:00
|
|
|
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
|
2004-03-16 05:04:54 +08:00
|
|
|
lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
else {
|
2004-03-16 05:04:54 +08:00
|
|
|
TValue *o = obj_at(L, 1);
|
2001-10-26 03:14:14 +08:00
|
|
|
Table *t;
|
2002-11-14 23:41:38 +08:00
|
|
|
luaL_checktype(L, 2, LUA_TTABLE);
|
2004-03-16 05:04:54 +08:00
|
|
|
t = hvalue(obj_at(L, 2));
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-26 06:14:54 +08:00
|
|
|
static int stacklevel (lua_State *L) {
|
2002-01-10 05:51:06 +08:00
|
|
|
unsigned long a = 0;
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, (L->top - L->stack));
|
|
|
|
lua_pushinteger(L, (L->stack_last - L->stack));
|
|
|
|
lua_pushinteger(L, (unsigned long)&a);
|
2002-01-26 06:14:54 +08:00
|
|
|
return 5;
|
2002-01-10 05:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int table_query (lua_State *L) {
|
2001-10-26 03:14:14 +08:00
|
|
|
const Table *t;
|
2002-11-14 23:41:38 +08:00
|
|
|
int i = luaL_optint(L, 2, -1);
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
2004-03-16 05:04:54 +08:00
|
|
|
t = hvalue(obj_at(L, 1));
|
1999-12-15 02:31:20 +08:00
|
|
|
if (i == -1) {
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, t->sizearray);
|
2006-01-10 21:13:06 +08:00
|
|
|
lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
|
2005-01-06 02:20:51 +08:00
|
|
|
lua_pushinteger(L, t->lastfree - t->node);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-10-26 03:14:14 +08:00
|
|
|
else if (i < t->sizearray) {
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, i);
|
2006-07-11 23:53:29 +08:00
|
|
|
pushobject(L, &t->array[i]);
|
2006-09-11 22:07:24 +08:00
|
|
|
lua_pushnil(L);
|
2001-10-26 03:14:14 +08:00
|
|
|
}
|
|
|
|
else if ((i -= t->sizearray) < sizenode(t)) {
|
2003-03-20 05:14:53 +08:00
|
|
|
if (!ttisnil(gval(gnode(t, i))) ||
|
|
|
|
ttisnil(gkey(gnode(t, i))) ||
|
|
|
|
ttisnumber(gkey(gnode(t, i)))) {
|
2010-06-25 20:18:10 +08:00
|
|
|
pushobject(L, gkey(gnode(t, i)));
|
2001-04-11 22:42:41 +08:00
|
|
|
}
|
|
|
|
else
|
2003-07-09 20:08:43 +08:00
|
|
|
lua_pushliteral(L, "<undef>");
|
2006-07-11 23:53:29 +08:00
|
|
|
pushobject(L, gval(gnode(t, i)));
|
2013-08-19 00:12:18 +08:00
|
|
|
if (gnext(&t->node[i]) != 0)
|
|
|
|
lua_pushinteger(L, gnext(&t->node[i]));
|
2000-08-29 01:57:04 +08:00
|
|
|
else
|
2001-10-26 03:14:14 +08:00
|
|
|
lua_pushnil(L);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-10-26 03:14:14 +08:00
|
|
|
return 3;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int string_query (lua_State *L) {
|
2001-06-07 02:00:19 +08:00
|
|
|
stringtable *tb = &G(L)->strt;
|
2002-11-14 23:41:38 +08:00
|
|
|
int s = luaL_optint(L, 2, 0) - 1;
|
2013-08-22 03:21:16 +08:00
|
|
|
if (s < 0) {
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L ,tb->nuse);
|
|
|
|
lua_pushinteger(L ,tb->size);
|
2000-08-29 01:57:04 +08:00
|
|
|
return 2;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2013-08-22 23:21:48 +08:00
|
|
|
else if (s < tb->size) {
|
2013-08-22 03:21:16 +08:00
|
|
|
TString *ts = tb->hash[s];
|
|
|
|
setsvalue2s(L, L->top, ts);
|
|
|
|
api_incr_top(L);
|
|
|
|
return 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2013-08-22 03:21:16 +08:00
|
|
|
else return 0;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int tref (lua_State *L) {
|
2001-04-18 01:35:54 +08:00
|
|
|
int level = lua_gettop(L);
|
2002-11-14 23:41:38 +08:00
|
|
|
luaL_checkany(L, 1);
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_pushvalue(L, 1);
|
2010-11-17 01:43:29 +08:00
|
|
|
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
|
2003-11-20 03:59:18 +08:00
|
|
|
lua_assert(lua_gettop(L) == level+1); /* +1 for result */
|
2000-08-30 03:05:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getref (lua_State *L) {
|
2001-04-18 01:35:54 +08:00
|
|
|
int level = lua_gettop(L);
|
2010-11-17 01:43:29 +08:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkint(L, 1));
|
2003-11-20 03:59:18 +08:00
|
|
|
lua_assert(lua_gettop(L) == level+1);
|
2001-10-18 05:12:57 +08:00
|
|
|
return 1;
|
2000-08-30 03:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unref (lua_State *L) {
|
2001-04-18 01:35:54 +08:00
|
|
|
int level = lua_gettop(L);
|
2010-11-17 01:43:29 +08:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, luaL_checkint(L, 1));
|
2003-11-20 03:59:18 +08:00
|
|
|
lua_assert(lua_gettop(L) == level);
|
2000-08-30 03:05:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-19 19:11:55 +08:00
|
|
|
|
|
|
|
static int upvalue (lua_State *L) {
|
|
|
|
int n = luaL_checkint(L, 2);
|
|
|
|
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-30 03:05:11 +08:00
|
|
|
static int newuserdata (lua_State *L) {
|
2002-11-14 23:41:38 +08:00
|
|
|
size_t size = luaL_checkint(L, 1);
|
2001-11-29 04:13:13 +08:00
|
|
|
char *p = cast(char *, lua_newuserdata(L, size));
|
|
|
|
while (size--) *p++ = '\0';
|
2001-06-07 02:00:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-04-06 02:54:31 +08:00
|
|
|
|
|
|
|
static int pushuserdata (lua_State *L) {
|
2013-06-21 05:59:13 +08:00
|
|
|
lua_Integer u = luaL_checkinteger(L, 1);
|
|
|
|
lua_pushlightuserdata(L, cast(void *, cast(size_t, u)));
|
2001-06-07 02:00:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
|
|
|
|
static int udataval (lua_State *L) {
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
|
2000-08-30 03:05:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-23 02:01:38 +08:00
|
|
|
|
|
|
|
static int doonnewstack (lua_State *L) {
|
2002-02-15 05:47:29 +08:00
|
|
|
lua_State *L1 = lua_newthread(L);
|
2002-05-02 04:48:12 +08:00
|
|
|
size_t l;
|
2002-11-14 23:41:38 +08:00
|
|
|
const char *s = luaL_checklstring(L, 1, &l);
|
2002-06-04 04:11:41 +08:00
|
|
|
int status = luaL_loadbuffer(L1, s, l, s);
|
2006-10-11 01:40:17 +08:00
|
|
|
if (status == LUA_OK)
|
2002-08-06 23:32:22 +08:00
|
|
|
status = lua_pcall(L1, 0, 0, 0);
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, status);
|
2001-01-23 02:01:38 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-14 00:52:01 +08:00
|
|
|
static int s2d (lua_State *L) {
|
2002-11-14 23:41:38 +08:00
|
|
|
lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
|
2001-02-14 00:52:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-11 22:00:59 +08:00
|
|
|
|
2001-02-14 00:52:01 +08:00
|
|
|
static int d2s (lua_State *L) {
|
2002-11-14 23:41:38 +08:00
|
|
|
double d = luaL_checknumber(L, 1);
|
2001-11-29 04:13:13 +08:00
|
|
|
lua_pushlstring(L, cast(char *, &d), sizeof(d));
|
2001-02-14 00:52:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-11 22:00:59 +08:00
|
|
|
static int num2int (lua_State *L) {
|
|
|
|
lua_pushinteger(L, lua_tointeger(L, 1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int newstate (lua_State *L) {
|
2004-06-03 03:08:52 +08:00
|
|
|
void *ud;
|
|
|
|
lua_Alloc f = lua_getallocf(L, &ud);
|
|
|
|
lua_State *L1 = lua_newstate(f, ud);
|
2009-03-04 02:52:36 +08:00
|
|
|
if (L1) {
|
|
|
|
lua_atpanic(L1, tpanic);
|
2009-02-18 22:52:51 +08:00
|
|
|
lua_pushlightuserdata(L, L1);
|
2009-03-04 02:52:36 +08:00
|
|
|
}
|
2000-08-30 03:05:11 +08:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-12-20 18:26:33 +08:00
|
|
|
|
2009-02-18 22:52:51 +08: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 22:09:31 +08:00
|
|
|
static int loadlib (lua_State *L) {
|
2005-08-27 01:36:32 +08:00
|
|
|
static const luaL_Reg libs[] = {
|
2010-07-02 19:38:13 +08:00
|
|
|
{"_G", luaopen_base},
|
|
|
|
{"coroutine", luaopen_coroutine},
|
|
|
|
{"debug", luaopen_debug},
|
|
|
|
{"io", luaopen_io},
|
2012-05-08 21:53:33 +08:00
|
|
|
{"os", luaopen_os},
|
2010-07-02 19:38:13 +08:00
|
|
|
{"math", luaopen_math},
|
|
|
|
{"string", luaopen_string},
|
|
|
|
{"table", luaopen_table},
|
2002-12-20 18:26:33 +08:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2009-02-18 22:52:51 +08:00
|
|
|
lua_State *L1 = getstate(L);
|
2010-07-02 19:38:13 +08:00
|
|
|
int i;
|
|
|
|
luaL_requiref(L1, "package", luaopen_package, 1);
|
2011-04-09 03:17:36 +08:00
|
|
|
luaL_getsubtable(L1, LUA_REGISTRYINDEX, "_PRELOAD");
|
2010-07-02 19:38:13 +08:00
|
|
|
for (i = 0; libs[i].name; i++) {
|
|
|
|
lua_pushcfunction(L1, libs[i].func);
|
|
|
|
lua_setfield(L1, -2, libs[i].name);
|
|
|
|
}
|
2000-09-14 22:09:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int closestate (lua_State *L) {
|
2009-02-18 22:52:51 +08:00
|
|
|
lua_State *L1 = getstate(L);
|
2001-02-22 00:51:25 +08:00
|
|
|
lua_close(L1);
|
2000-08-30 03:05:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int doremote (lua_State *L) {
|
2009-02-18 22:52:51 +08:00
|
|
|
lua_State *L1 = getstate(L);
|
2002-06-21 04:40:38 +08:00
|
|
|
size_t lcode;
|
2002-11-14 23:41:38 +08:00
|
|
|
const char *code = luaL_checklstring(L, 2, &lcode);
|
2000-08-30 03:05:11 +08:00
|
|
|
int status;
|
2002-06-21 04:40:38 +08:00
|
|
|
lua_settop(L1, 0);
|
|
|
|
status = luaL_loadbuffer(L1, code, lcode, code);
|
2006-10-11 01:40:17 +08:00
|
|
|
if (status == LUA_OK)
|
2002-08-06 23:32:22 +08:00
|
|
|
status = lua_pcall(L1, 0, LUA_MULTRET, 0);
|
2006-10-11 01:40:17 +08:00
|
|
|
if (status != LUA_OK) {
|
2000-08-30 03:05:11 +08:00
|
|
|
lua_pushnil(L);
|
2002-12-20 18:26:33 +08:00
|
|
|
lua_pushstring(L, lua_tostring(L1, -1));
|
2010-07-02 19:38:13 +08:00
|
|
|
lua_pushinteger(L, status);
|
2002-12-20 18:26:33 +08:00
|
|
|
return 3;
|
2000-08-30 03:05:11 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i = 0;
|
2001-12-21 05:27:12 +08:00
|
|
|
while (!lua_isnone(L1, ++i))
|
2000-08-30 03:05:11 +08:00
|
|
|
lua_pushstring(L, lua_tostring(L1, i));
|
2001-03-07 04:09:38 +08:00
|
|
|
lua_pop(L1, i-1);
|
2000-08-30 03:05:11 +08:00
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-26 03:14:14 +08:00
|
|
|
|
2003-02-19 00:02:56 +08:00
|
|
|
static int int2fb_aux (lua_State *L) {
|
|
|
|
int b = luaO_int2fb(luaL_checkint(L, 1));
|
2003-10-08 04:13:41 +08:00
|
|
|
lua_pushinteger(L, b);
|
2004-11-01 23:06:50 +08:00
|
|
|
lua_pushinteger(L, luaO_fb2int(b));
|
2003-02-19 00:02:56 +08:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
|
2002-10-23 02:07:55 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2001-02-23 02:59:59 +08:00
|
|
|
** function to test the API with C. It interprets a kind of assembler
|
2000-04-07 01:35:23 +08:00
|
|
|
** language with calls to the API, so the test can be driven by Lua code
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2009-12-02 00:49:48 +08:00
|
|
|
|
|
|
|
static void sethookaux (lua_State *L, int mask, int count, const char *code);
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static const char *const delimits = " \t\n,;";
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static void skip (const char **pc) {
|
2009-09-10 04:44:10 +08:00
|
|
|
for (;;) {
|
|
|
|
if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
|
|
|
else if (**pc == '#') {
|
|
|
|
while (**pc != '\n' && **pc != '\0') (*pc)++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2009-12-11 02:21:28 +08:00
|
|
|
static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
1999-12-15 02:31:20 +08:00
|
|
|
int res = 0;
|
2000-08-29 01:57:04 +08:00
|
|
|
int sig = 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
skip(pc);
|
2001-11-29 04:13:13 +08:00
|
|
|
if (**pc == '.') {
|
2009-12-11 02:21:28 +08:00
|
|
|
res = lua_tointeger(L1, -1);
|
|
|
|
lua_pop(L1, 1);
|
2000-08-29 01:57:04 +08:00
|
|
|
(*pc)++;
|
2000-08-31 21:29:47 +08:00
|
|
|
return res;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if (**pc == '-') {
|
2000-08-29 01:57:04 +08:00
|
|
|
sig = -1;
|
|
|
|
(*pc)++;
|
|
|
|
}
|
2010-12-10 21:40:22 +08:00
|
|
|
if (!lisdigit(cast_uchar(**pc)))
|
2009-09-14 22:30:39 +08:00
|
|
|
luaL_error(L, "number expected (%s)", *pc);
|
2010-12-10 21:40:22 +08:00
|
|
|
while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0';
|
2000-08-31 21:29:47 +08:00
|
|
|
return sig*res;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2006-09-11 22:07:24 +08:00
|
|
|
|
2009-12-11 02:21:28 +08:00
|
|
|
static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
|
1999-12-15 02:31:20 +08:00
|
|
|
int i = 0;
|
|
|
|
skip(pc);
|
2009-12-02 00:49:48 +08: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-29 04:13:13 +08:00
|
|
|
buff[i] = '\0';
|
1999-12-15 02:31:20 +08:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 02:21:28 +08:00
|
|
|
static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
|
2005-02-18 20:40:02 +08:00
|
|
|
skip(pc);
|
|
|
|
switch (*(*pc)++) {
|
|
|
|
case 'R': return LUA_REGISTRYINDEX;
|
2009-12-22 23:32:50 +08:00
|
|
|
case 'G': return luaL_error(L, "deprecated index 'G'");
|
2009-12-11 02:21:28 +08:00
|
|
|
case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
|
|
|
|
default: (*pc)--; return getnum_aux(L, L1, pc);
|
2005-02-18 20:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-11 02:21:28 +08:00
|
|
|
|
|
|
|
static void pushcode (lua_State *L, int code) {
|
|
|
|
static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
|
|
|
|
"ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
|
|
|
|
lua_pushstring(L, codes[code]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
#define EQ(s1) (strcmp(s1, inst) == 0)
|
|
|
|
|
2009-12-11 02:21:28 +08:00
|
|
|
#define getnum (getnum_aux(L, L1, &pc))
|
|
|
|
#define getstring (getstring_aux(L, buff, &pc))
|
|
|
|
#define getindex (getindex_aux(L, L1, &pc))
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
|
2009-04-15 03:10:17 +08:00
|
|
|
static int testC (lua_State *L);
|
2009-08-27 01:41:26 +08:00
|
|
|
static int Cfunck (lua_State *L);
|
2009-04-15 03:10:17 +08:00
|
|
|
|
|
|
|
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
2009-12-02 00:49:48 +08:00
|
|
|
char buff[300];
|
2009-12-11 02:21:28 +08:00
|
|
|
int status = 0;
|
2009-12-02 00:49:48 +08:00
|
|
|
if (pc == NULL) return luaL_error(L, "attempt to runC null script");
|
1999-12-15 02:31:20 +08:00
|
|
|
for (;;) {
|
2009-12-11 02:21:28 +08:00
|
|
|
const char *inst = getstring;
|
2001-11-29 04:13:13 +08:00
|
|
|
if EQ("") return 0;
|
2010-05-14 21:15:54 +08:00
|
|
|
else if EQ("absindex") {
|
|
|
|
lua_pushnumber(L1, lua_absindex(L1, getindex));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isnumber") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_isnumber(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isstring") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_isstring(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("istable") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_istable(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("iscfunction") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_iscfunction(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isfunction") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_isfunction(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isuserdata") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_isuserdata(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2002-07-09 22:58:28 +08:00
|
|
|
else if EQ("isudataval") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
|
2002-07-09 22:58:28 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isnil") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_isnil(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isnull") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_isnone(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tonumber") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_pushnumber(L1, lua_tonumber(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2010-04-14 23:13:48 +08:00
|
|
|
else if EQ("topointer") {
|
2010-04-17 01:42:49 +08:00
|
|
|
lua_pushnumber(L1, cast(size_t, lua_topointer(L1, getindex)));
|
2010-04-14 23:13:48 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tostring") {
|
2005-02-18 20:40:02 +08:00
|
|
|
const char *s = lua_tostring(L1, getindex);
|
2007-04-17 21:19:53 +08:00
|
|
|
const char *s1 = lua_pushstring(L1, s);
|
|
|
|
lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2004-06-01 03:41:52 +08:00
|
|
|
else if EQ("objsize") {
|
2009-12-18 00:20:01 +08:00
|
|
|
lua_pushinteger(L1, lua_rawlen(L1, getindex));
|
|
|
|
}
|
|
|
|
else if EQ("len") {
|
|
|
|
lua_len(L1, getindex);
|
|
|
|
}
|
|
|
|
else if EQ("Llen") {
|
|
|
|
lua_pushinteger(L1, luaL_len(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tocfunction") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2010-04-17 01:42:49 +08:00
|
|
|
else if EQ("func2num") {
|
2009-10-01 04:49:25 +08:00
|
|
|
lua_CFunction func = lua_tocfunction(L1, getindex);
|
2010-04-17 01:42:49 +08:00
|
|
|
lua_pushnumber(L1, cast(size_t, func));
|
2009-10-01 04:49:25 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("return") {
|
2009-12-11 02:21:28 +08:00
|
|
|
int n = getnum;
|
|
|
|
if (L1 != L) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
lua_pushstring(L, lua_tostring(L1, -(n - i)));
|
|
|
|
}
|
|
|
|
return n;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("gettop") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pushinteger(L1, lua_gettop(L1));
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("settop") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_settop(L1, getnum);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pop") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pop(L1, getnum);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushnum") {
|
2013-05-08 03:01:16 +08:00
|
|
|
lua_pushnumber(L1, (lua_Number)getnum);
|
|
|
|
}
|
|
|
|
else if EQ("pushint") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pushinteger(L1, getnum);
|
|
|
|
}
|
|
|
|
else if EQ("pushstring") {
|
2009-12-11 02:21:28 +08:00
|
|
|
lua_pushstring(L1, getstring);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2002-03-04 23:26:56 +08:00
|
|
|
else if EQ("pushnil") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pushnil(L1);
|
2002-03-04 23:26:56 +08:00
|
|
|
}
|
2002-02-08 01:26:33 +08:00
|
|
|
else if EQ("pushbool") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pushboolean(L1, getnum);
|
2002-02-08 01:26:33 +08:00
|
|
|
}
|
2008-02-12 02:04:26 +08:00
|
|
|
else if EQ("newtable") {
|
|
|
|
lua_newtable(L1);
|
|
|
|
}
|
2005-02-18 20:40:02 +08:00
|
|
|
else if EQ("newuserdata") {
|
|
|
|
lua_newuserdata(L1, getnum);
|
|
|
|
}
|
2002-03-04 23:26:56 +08:00
|
|
|
else if EQ("tobool") {
|
2008-02-12 03:04:16 +08:00
|
|
|
lua_pushboolean(L1, lua_toboolean(L1, getindex));
|
2002-03-04 23:26:56 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushvalue") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_pushvalue(L1, getindex);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushcclosure") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pushcclosure(L1, testC, getnum);
|
2001-10-18 05:12:57 +08:00
|
|
|
}
|
2009-09-10 04:44:10 +08:00
|
|
|
else if EQ("pushupvalueindex") {
|
|
|
|
lua_pushinteger(L1, lua_upvalueindex(getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("remove") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_remove(L1, getnum);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("insert") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_insert(L1, getnum);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
2002-02-06 06:40:05 +08:00
|
|
|
else if EQ("replace") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_replace(L1, getindex);
|
2002-02-06 06:40:05 +08:00
|
|
|
}
|
2009-10-06 00:44:33 +08:00
|
|
|
else if EQ("copy") {
|
|
|
|
int f = getindex;
|
|
|
|
lua_copy(L1, f, getindex);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("gettable") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_gettable(L1, getindex);
|
2000-09-25 22:48:42 +08:00
|
|
|
}
|
2010-03-27 04:58:11 +08:00
|
|
|
else if EQ("getglobal") {
|
|
|
|
lua_getglobal(L1, getstring);
|
|
|
|
}
|
2009-10-01 04:49:25 +08:00
|
|
|
else if EQ("getfield") {
|
|
|
|
int t = getindex;
|
2009-12-11 02:21:28 +08:00
|
|
|
lua_getfield(L1, t, getstring);
|
|
|
|
}
|
|
|
|
else if EQ("setfield") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_setfield(L1, t, getstring);
|
2009-10-01 04:49:25 +08:00
|
|
|
}
|
2009-09-18 02:04:21 +08:00
|
|
|
else if EQ("rawgeti") {
|
|
|
|
int t = getindex;
|
|
|
|
lua_rawgeti(L1, t, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("settable") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_settable(L1, getindex);
|
2000-09-25 22:48:42 +08:00
|
|
|
}
|
2010-03-27 04:58:11 +08:00
|
|
|
else if EQ("setglobal") {
|
|
|
|
lua_setglobal(L1, getstring);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("next") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_next(L1, -2);
|
2000-09-06 03:33:32 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("concat") {
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_concat(L1, getnum);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2009-08-27 01:41:26 +08:00
|
|
|
else if EQ("print") {
|
2009-09-10 04:44:10 +08:00
|
|
|
int n = getnum;
|
|
|
|
if (n != 0) {
|
|
|
|
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
|
|
|
lua_pop(L1, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
n = lua_gettop(L1);
|
|
|
|
for (i = 1; i <= n; i++) {
|
|
|
|
printf("%s ", luaL_tolstring(L1, i, NULL));
|
|
|
|
lua_pop(L1, 1);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2009-08-27 01:41:26 +08:00
|
|
|
}
|
2009-06-18 01:53:14 +08:00
|
|
|
else if EQ("arith") {
|
2013-04-26 21:08:29 +08:00
|
|
|
static char ops[] = "+-*/\\%^_"; /* '\' -> '//'; '_' -> '..' */
|
2009-06-18 01:53:14 +08:00
|
|
|
int op;
|
|
|
|
skip(&pc);
|
|
|
|
op = strchr(ops, *pc++) - ops;
|
2009-12-11 02:21:28 +08:00
|
|
|
lua_arith(L1, op);
|
2001-02-02 00:03:38 +08:00
|
|
|
}
|
2009-06-18 01:53:14 +08:00
|
|
|
else if EQ("compare") {
|
2005-02-18 20:40:02 +08:00
|
|
|
int a = getindex;
|
2009-06-18 01:53:14 +08:00
|
|
|
int b = getindex;
|
|
|
|
lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
|
2001-10-26 03:14:14 +08:00
|
|
|
}
|
2009-03-04 02:52:36 +08:00
|
|
|
else if EQ("call") {
|
2000-08-29 01:57:04 +08:00
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_call(L1, narg, nres);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2009-03-04 02:52:36 +08:00
|
|
|
else if EQ("pcall") {
|
2000-09-26 00:22:42 +08:00
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2009-12-11 02:21:28 +08:00
|
|
|
status = lua_pcall(L1, narg, nres, 0);
|
2000-09-26 00:22:42 +08:00
|
|
|
}
|
2009-08-27 01:41:26 +08:00
|
|
|
else if EQ("pcallk") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2009-11-27 23:39:31 +08:00
|
|
|
int i = getindex;
|
2009-12-11 02:21:28 +08:00
|
|
|
status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
|
2009-08-27 01:41:26 +08:00
|
|
|
}
|
2009-09-10 04:44:10 +08:00
|
|
|
else if EQ("callk") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2009-11-27 23:39:31 +08:00
|
|
|
int i = getindex;
|
2009-09-10 04:44:10 +08:00
|
|
|
lua_callk(L1, narg, nres, i, Cfunck);
|
|
|
|
}
|
|
|
|
else if EQ("yield") {
|
|
|
|
return lua_yield(L1, getnum);
|
|
|
|
}
|
2009-09-14 22:30:39 +08:00
|
|
|
else if EQ("yieldk") {
|
|
|
|
int nres = getnum;
|
2009-11-27 23:39:31 +08:00
|
|
|
int i = getindex;
|
2009-09-14 22:30:39 +08:00
|
|
|
return lua_yieldk(L1, nres, i, Cfunck);
|
|
|
|
}
|
2009-12-11 02:21:28 +08:00
|
|
|
else if EQ("newthread") {
|
|
|
|
lua_newthread(L1);
|
|
|
|
}
|
|
|
|
else if EQ("resume") {
|
|
|
|
int i = getindex;
|
2011-08-24 01:24:34 +08:00
|
|
|
status = lua_resume(lua_tothread(L1, i), L, getnum);
|
2009-12-11 02:21:28 +08:00
|
|
|
}
|
|
|
|
else if EQ("pushstatus") {
|
|
|
|
pushcode(L1, status);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2002-05-16 22:59:49 +08:00
|
|
|
else if EQ("loadstring") {
|
|
|
|
size_t sl;
|
2004-06-03 03:08:52 +08:00
|
|
|
const char *s = luaL_checklstring(L1, getnum, &sl);
|
|
|
|
luaL_loadbuffer(L1, s, sl, s);
|
2002-05-16 22:59:49 +08:00
|
|
|
}
|
|
|
|
else if EQ("loadfile") {
|
2004-06-03 03:08:52 +08:00
|
|
|
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
2000-09-26 00:22:42 +08:00
|
|
|
}
|
2002-01-31 01:26:44 +08:00
|
|
|
else if EQ("setmetatable") {
|
2005-02-18 20:40:02 +08:00
|
|
|
lua_setmetatable(L1, getindex);
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
2002-01-31 01:26:44 +08:00
|
|
|
else if EQ("getmetatable") {
|
2005-02-18 20:40:02 +08:00
|
|
|
if (lua_getmetatable(L1, getindex) == 0)
|
2004-06-03 03:08:52 +08:00
|
|
|
lua_pushnil(L1);
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("type") {
|
2004-07-10 02:23:17 +08:00
|
|
|
lua_pushstring(L1, luaL_typename(L1, getnum));
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2009-12-02 00:49:48 +08:00
|
|
|
else if EQ("append") {
|
|
|
|
int t = getindex;
|
2009-12-18 00:20:01 +08:00
|
|
|
int i = lua_rawlen(L1, t);
|
2009-12-02 00:49:48 +08:00
|
|
|
lua_rawseti(L1, t, i + 1);
|
|
|
|
}
|
2009-08-27 01:41:26 +08:00
|
|
|
else if EQ("getctx") {
|
|
|
|
int i = 0;
|
|
|
|
int s = lua_getctx(L1, &i);
|
2009-12-11 02:21:28 +08:00
|
|
|
pushcode(L1, s);
|
2009-08-27 01:41:26 +08:00
|
|
|
lua_pushinteger(L1, i);
|
|
|
|
}
|
2008-02-12 02:04:26 +08:00
|
|
|
else if EQ("checkstack") {
|
2011-11-10 03:08:07 +08:00
|
|
|
int sz = getnum;
|
|
|
|
luaL_checkstack(L1, sz, getstring);
|
2008-02-12 02:04:26 +08:00
|
|
|
}
|
2008-06-12 22:20:49 +08:00
|
|
|
else if EQ("newmetatable") {
|
2009-12-11 02:21:28 +08:00
|
|
|
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
2008-06-12 22:20:49 +08:00
|
|
|
}
|
|
|
|
else if EQ("testudata") {
|
|
|
|
int i = getindex;
|
2009-12-11 02:21:28 +08:00
|
|
|
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
2008-06-12 22:20:49 +08:00
|
|
|
}
|
2008-06-13 22:15:59 +08:00
|
|
|
else if EQ("gsub") {
|
|
|
|
int a = getnum; int b = getnum; int c = getnum;
|
|
|
|
luaL_gsub(L1, lua_tostring(L1, a),
|
2009-12-11 02:21:28 +08:00
|
|
|
lua_tostring(L1, b),
|
|
|
|
lua_tostring(L1, c));
|
2008-06-13 22:15:59 +08:00
|
|
|
}
|
2009-12-02 00:49:48 +08:00
|
|
|
else if EQ("sethook") {
|
|
|
|
int mask = getnum;
|
|
|
|
int count = getnum;
|
2009-12-11 02:21:28 +08:00
|
|
|
sethookaux(L1, mask, count, getstring);
|
2009-12-02 00:49:48 +08:00
|
|
|
}
|
2003-05-14 03:22:19 +08:00
|
|
|
else if EQ("throw") {
|
2005-03-24 01:51:11 +08:00
|
|
|
#if defined(__cplusplus)
|
2003-05-14 03:22:19 +08:00
|
|
|
static struct X { int x; } x;
|
|
|
|
throw x;
|
|
|
|
#else
|
2004-06-03 03:08:52 +08:00
|
|
|
luaL_error(L1, "C++");
|
2003-05-14 03:22:19 +08:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2002-06-18 23:19:27 +08:00
|
|
|
else luaL_error(L, "unknown instruction %s", buff);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 0;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2009-04-15 03:10:17 +08: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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-27 01:41:26 +08:00
|
|
|
static int Cfunck (lua_State *L) {
|
|
|
|
int i = 0;
|
|
|
|
lua_getctx(L, &i);
|
|
|
|
return runC(L, L, lua_tostring(L, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-15 03:10:17 +08:00
|
|
|
static int makeCfunc (lua_State *L) {
|
|
|
|
luaL_checkstring(L, 1);
|
2009-09-10 04:44:10 +08:00
|
|
|
lua_pushcclosure(L, Cfunc, lua_gettop(L));
|
2009-04-15 03:10:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2002-11-18 23:24:27 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2009-12-02 00:49:48 +08:00
|
|
|
** tests for C hooks
|
2002-11-18 23:24:27 +08:00
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2009-12-02 00:49:48 +08: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] */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** sets registry.C_HOOK[L] = scpt and sets Chook as a hook
|
|
|
|
*/
|
|
|
|
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 23:24:27 +08:00
|
|
|
}
|
|
|
|
|
2009-12-02 00:49:48 +08:00
|
|
|
|
|
|
|
static int sethook (lua_State *L) {
|
2002-11-18 23:24:27 +08:00
|
|
|
if (lua_isnoneornil(L, 1))
|
2002-11-26 01:47:13 +08:00
|
|
|
lua_sethook(L, NULL, 0, 0); /* turn off hooks */
|
2002-11-18 23:24:27 +08:00
|
|
|
else {
|
2009-12-02 00:49:48 +08:00
|
|
|
const char *scpt = luaL_checkstring(L, 1);
|
|
|
|
const char *smask = luaL_checkstring(L, 2);
|
|
|
|
int count = luaL_optint(L, 3, 0);
|
2002-11-26 01:47:13 +08:00
|
|
|
int mask = 0;
|
2009-12-02 00:49:48 +08:00
|
|
|
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
|
|
|
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
2002-11-26 01:47:13 +08:00
|
|
|
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
|
|
|
|
if (count > 0) mask |= LUA_MASKCOUNT;
|
2009-12-02 00:49:48 +08:00
|
|
|
sethookaux(L, mask, count, scpt);
|
2002-11-18 23:24:27 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int coresume (lua_State *L) {
|
|
|
|
int status;
|
|
|
|
lua_State *co = lua_tothread(L, 1);
|
|
|
|
luaL_argcheck(L, co, 1, "coroutine expected");
|
2011-08-24 01:24:34 +08:00
|
|
|
status = lua_resume(co, L, 0);
|
2008-08-14 01:02:12 +08:00
|
|
|
if (status != LUA_OK && status != LUA_YIELD) {
|
2002-11-18 23:24:27 +08:00
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
lua_insert(L, -2);
|
|
|
|
return 2; /* return false + error message */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2005-08-27 01:36:32 +08:00
|
|
|
static const struct luaL_Reg tests_funcs[] = {
|
2005-07-12 22:32:08 +08:00
|
|
|
{"checkmemory", lua_checkmemory},
|
|
|
|
{"closestate", closestate},
|
|
|
|
{"d2s", d2s},
|
|
|
|
{"doonnewstack", doonnewstack},
|
|
|
|
{"doremote", doremote},
|
2013-08-19 22:16:33 +08:00
|
|
|
{"gccolor", gc_color},
|
|
|
|
{"isgclocal", gc_local},
|
2010-04-17 01:42:49 +08:00
|
|
|
{"gcstate", gc_state},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"getref", getref},
|
2001-11-29 04:13:13 +08:00
|
|
|
{"hash", hash_query},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"int2fb", int2fb_aux},
|
2001-11-29 04:13:13 +08:00
|
|
|
{"limits", get_limits},
|
|
|
|
{"listcode", listcode},
|
|
|
|
{"listk", listk},
|
|
|
|
{"listlocals", listlocals},
|
|
|
|
{"loadlib", loadlib},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"newstate", newstate},
|
|
|
|
{"newuserdata", newuserdata},
|
|
|
|
{"num2int", num2int},
|
|
|
|
{"pushuserdata", pushuserdata},
|
2001-11-29 04:13:13 +08:00
|
|
|
{"querystr", string_query},
|
|
|
|
{"querytab", table_query},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"ref", tref},
|
|
|
|
{"resume", coresume},
|
|
|
|
{"s2d", s2d},
|
2009-12-02 00:49:48 +08:00
|
|
|
{"sethook", sethook},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"stacklevel", stacklevel},
|
2001-11-29 04:13:13 +08:00
|
|
|
{"testC", testC},
|
2009-04-15 03:10:17 +08:00
|
|
|
{"makeCfunc", makeCfunc},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"totalmem", mem_query},
|
2004-03-16 05:04:54 +08:00
|
|
|
{"trick", settrick},
|
2005-07-12 22:32:08 +08:00
|
|
|
{"udataval", udataval},
|
2001-11-29 04:13:13 +08:00
|
|
|
{"unref", unref},
|
2002-12-19 19:11:55 +08:00
|
|
|
{"upvalue", upvalue},
|
2002-03-20 20:54:08 +08:00
|
|
|
{NULL, NULL}
|
1999-12-15 02:31:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-06-06 03:35:57 +08:00
|
|
|
static void checkfinalmem (void) {
|
2007-11-13 00:28:45 +08:00
|
|
|
lua_assert(l_memcontrol.numblocks == 0);
|
|
|
|
lua_assert(l_memcontrol.total == 0);
|
2006-06-06 03:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 00:25:13 +08:00
|
|
|
int luaB_opentests (lua_State *L) {
|
2003-10-20 20:25:23 +08:00
|
|
|
void *ud;
|
2009-03-04 02:52:36 +08:00
|
|
|
lua_atpanic(L, &tpanic);
|
2006-06-06 03:35:57 +08:00
|
|
|
atexit(checkfinalmem);
|
2003-10-20 20:25:23 +08:00
|
|
|
lua_assert(lua_getallocf(L, &ud) == debug_realloc);
|
2007-11-13 00:28:45 +08:00
|
|
|
lua_assert(ud == cast(void *, &l_memcontrol));
|
2005-09-21 01:55:10 +08:00
|
|
|
lua_setallocf(L, lua_getallocf(L, NULL), ud);
|
2010-07-28 23:51:59 +08:00
|
|
|
luaL_newlib(L, tests_funcs);
|
|
|
|
return 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|