1999-12-14 21:31:20 +03:00
|
|
|
/*
|
2004-05-31 23:41:52 +04:00
|
|
|
** $Id: ltests.c,v 2.7 2004/05/31 18:50:48 roberto Exp roberto $
|
1999-12-14 21:31:20 +03:00
|
|
|
** Internal Module for Debugging of the Lua Implementation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2001-02-06 19:01:29 +03:00
|
|
|
#include <limits.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
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define ltests_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
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"
|
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"
|
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
|
|
|
*/
|
2000-10-30 19:29:59 +03:00
|
|
|
#ifdef LUA_DEBUG
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2001-02-06 19:01:29 +03:00
|
|
|
|
2004-03-16 00:04:54 +03:00
|
|
|
int Trick = 0;
|
|
|
|
|
|
|
|
|
2002-01-11 23:26:52 +03:00
|
|
|
static lua_State *lua_state = NULL;
|
2001-02-02 18:13:05 +03:00
|
|
|
|
|
|
|
int islocked = 0;
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
|
2004-03-16 00:04:54 +03:00
|
|
|
#define obj_at(L,k) (L->ci->base+(k) - 1)
|
2002-03-05 00:29:41 +03:00
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static void setnameval (lua_State *L, const char *name, int val) {
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_pushstring(L, name);
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, val);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_settable(L, -3);
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-06 19:01:29 +03:00
|
|
|
/*
|
|
|
|
** {======================================================================
|
|
|
|
** Controlled version for realloc.
|
|
|
|
** =======================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
#ifndef EXTERNMEMCHECK
|
|
|
|
/* full memory check */
|
|
|
|
#define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
|
|
|
|
#define MARKSIZE 16 /* size of marks after each block */
|
|
|
|
#define blockhead(b) (cast(char *, b) - HEADER)
|
|
|
|
#define setsize(newblock, size) (*cast(size_t *, newblock) = size)
|
|
|
|
#define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
|
|
|
|
#define fillmem(mem,size) memset(mem, -MARK, size)
|
|
|
|
#else
|
|
|
|
/* external memory check: don't do it twice */
|
|
|
|
#define HEADER 0
|
|
|
|
#define MARKSIZE 0
|
|
|
|
#define blockhead(b) (b)
|
|
|
|
#define setsize(newblock, size) /* empty */
|
|
|
|
#define checkblocksize(b,size) (1)
|
|
|
|
#define fillmem(mem,size) /* empty */
|
|
|
|
#endif
|
2001-02-06 19:01:29 +03:00
|
|
|
|
2003-10-03 00:31:17 +04:00
|
|
|
|
|
|
|
Memcontrol memcontrol = {0L, 0L, 0L, ULONG_MAX};
|
2001-02-06 19:01:29 +03:00
|
|
|
|
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
static void *checkblock (void *block, size_t size) {
|
|
|
|
void *b = blockhead(block);
|
2001-02-06 19:01:29 +03:00
|
|
|
int i;
|
|
|
|
for (i=0;i<MARKSIZE;i++)
|
2001-08-31 23:46:07 +04:00
|
|
|
lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
2001-02-06 19:01:29 +03:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-03 00:31:17 +04:00
|
|
|
static void freeblock (Memcontrol *mc, void *block, size_t size) {
|
2001-02-06 19:01:29 +03:00
|
|
|
if (block) {
|
2002-11-18 18:24:27 +03:00
|
|
|
lua_assert(checkblocksize(block, size));
|
|
|
|
block = checkblock(block, size);
|
|
|
|
fillmem(block, size+HEADER+MARKSIZE); /* erase block */
|
2001-02-06 19:01:29 +03:00
|
|
|
free(block); /* free original block */
|
2003-10-03 00:31:17 +04:00
|
|
|
mc->numblocks--;
|
|
|
|
mc->total -= size;
|
2001-02-06 19:01:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-03 00:31:17 +04:00
|
|
|
void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
|
|
|
|
Memcontrol *mc = cast(Memcontrol *, ud);
|
2002-11-18 18:24:27 +03:00
|
|
|
lua_assert(oldsize == 0 || checkblocksize(block, oldsize));
|
2001-02-06 19:01:29 +03:00
|
|
|
if (size == 0) {
|
2003-10-03 00:31:17 +04:00
|
|
|
freeblock(mc, block, oldsize);
|
2001-02-06 19:01:29 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-10-03 00:31:17 +04:00
|
|
|
else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
|
2001-02-06 19:01:29 +03:00
|
|
|
return NULL; /* to test memory allocation errors */
|
|
|
|
else {
|
2001-08-31 00:55:22 +04:00
|
|
|
void *newblock;
|
2001-02-06 19:01:29 +03:00
|
|
|
int i;
|
|
|
|
size_t realsize = HEADER+size+MARKSIZE;
|
2002-11-18 18:24:27 +03:00
|
|
|
size_t commonsize = (oldsize < size) ? oldsize : size;
|
2001-02-06 19:01:29 +03:00
|
|
|
if (realsize < size) return NULL; /* overflow! */
|
2001-08-31 00:55:22 +04:00
|
|
|
newblock = malloc(realsize); /* alloc a new block */
|
2001-02-06 19:01:29 +03:00
|
|
|
if (newblock == NULL) return NULL;
|
|
|
|
if (block) {
|
2002-11-18 18:24:27 +03:00
|
|
|
memcpy(cast(char *, newblock)+HEADER, block, commonsize);
|
2003-10-03 00:31:17 +04:00
|
|
|
freeblock(mc, block, oldsize); /* erase (and check) old copy */
|
2001-02-06 19:01:29 +03:00
|
|
|
}
|
|
|
|
/* initialize new part of the block with something `weird' */
|
2002-11-18 18:24:27 +03:00
|
|
|
fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
|
2003-10-03 00:31:17 +04:00
|
|
|
mc->total += size;
|
|
|
|
if (mc->total > mc->maxmem)
|
|
|
|
mc->maxmem = mc->total;
|
|
|
|
mc->numblocks++;
|
2002-11-18 18:24:27 +03:00
|
|
|
setsize(newblock, size);
|
2001-02-06 19:01:29 +03:00
|
|
|
for (i=0;i<MARKSIZE;i++)
|
2001-08-31 23:46:07 +04:00
|
|
|
*(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
|
|
|
|
return cast(char *, newblock)+HEADER;
|
2001-02-06 19:01:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* }====================================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-16 22:09:52 +03:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Functions to check memory consistency
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
|
|
|
|
if (isdead(g,t)) return 0;
|
|
|
|
if (g->gcstate == GCSpropagate)
|
|
|
|
return !isblack(f) || !iswhite(t);
|
|
|
|
else if (g->gcstate == GCSfinalize)
|
|
|
|
return iswhite(f);
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void printobj (global_State *g, GCObject *o) {
|
2004-03-16 00:04:54 +03:00
|
|
|
int i = 0;
|
|
|
|
GCObject *p;
|
|
|
|
for (p = g->rootgc; p != o && p != NULL; p = p->gch.next) i++;
|
|
|
|
if (p == NULL) i = -1;
|
|
|
|
printf("%d:%s(%p)-%c(%02X)", i, luaT_typenames[o->gch.tt], (void *)o,
|
|
|
|
isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', o->gch.marked);
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
|
|
|
int r = testobjref1(g,f,t);
|
2004-03-16 00:04:54 +03:00
|
|
|
if (!r) {
|
|
|
|
printf("%d(%02X) - ", g->gcstate, g->currentwhite);
|
|
|
|
printobj(g, f);
|
|
|
|
printf("\t-> ");
|
|
|
|
printobj(g, t);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2004-02-16 22:09:52 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
|
|
|
|
|
|
|
|
#define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \
|
|
|
|
((ttype(t) == (t)->value.gc->gch.tt) && testobjref(g,f,gcvalue(t))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void checktable (global_State *g, Table *h) {
|
|
|
|
int i;
|
|
|
|
int weakkey = 0;
|
|
|
|
int weakvalue = 0;
|
|
|
|
const TValue *mode;
|
|
|
|
GCObject *hgc = obj2gco(h);
|
|
|
|
if (h->metatable)
|
|
|
|
checkobjref(g, hgc, h->metatable);
|
|
|
|
lua_assert(h->lsizenode || h->node == g->dummynode);
|
|
|
|
mode = gfasttm(g, h->metatable, TM_MODE);
|
|
|
|
if (mode && ttisstring(mode)) { /* is there a weak mode? */
|
|
|
|
weakkey = (strchr(svalue(mode), 'k') != NULL);
|
|
|
|
weakvalue = (strchr(svalue(mode), 'v') != NULL);
|
|
|
|
}
|
|
|
|
i = h->sizearray;
|
|
|
|
while (i--)
|
|
|
|
checkvalref(g, hgc, &h->array[i]);
|
|
|
|
i = sizenode(h);
|
|
|
|
while (i--) {
|
|
|
|
Node *n = gnode(h, i);
|
|
|
|
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);
|
|
|
|
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++) {
|
|
|
|
if (f->upvalues[i])
|
|
|
|
checkobjref(g, fgc, f->upvalues[i]);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void checkclosure (global_State *g, Closure *cl) {
|
|
|
|
GCObject *clgc = obj2gco(cl);
|
|
|
|
if (cl->c.isC) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<cl->c.nupvalues; i++)
|
|
|
|
checkvalref(g, clgc, &cl->c.upvalue[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
lua_assert(cl->l.nupvalues == cl->l.p->nups);
|
|
|
|
checkobjref(g, clgc, hvalue(&cl->l.g));
|
|
|
|
checkobjref(g, clgc, cl->l.p);
|
|
|
|
for (i=0; i<cl->l.nupvalues; i++) {
|
2004-03-16 00:04:54 +03:00
|
|
|
if (cl->l.upvals[i]) {
|
|
|
|
lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
|
|
|
|
checkobjref(g, clgc, cl->l.upvals[i]);
|
|
|
|
}
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void checkstack (global_State *g, lua_State *L1) {
|
|
|
|
StkId o;
|
|
|
|
CallInfo *ci;
|
|
|
|
lua_assert(!isdead(g, obj2gco(L1)));
|
|
|
|
checkliveness(g, gt(L1));
|
2004-03-16 00:04:54 +03:00
|
|
|
if (L1->base_ci) {
|
|
|
|
for (ci = L1->base_ci; ci <= L1->ci; ci++)
|
|
|
|
lua_assert(ci->top <= L1->stack_last);
|
|
|
|
}
|
|
|
|
else lua_assert(L1->size_ci == 0);
|
|
|
|
if (L1->stack) {
|
|
|
|
for (o = L1->stack; o < L1->top; o++)
|
|
|
|
checkliveness(g, o);
|
|
|
|
}
|
|
|
|
else lua_assert(L1->stacksize == 0);
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 00:04:54 +03:00
|
|
|
static void checkobject (global_State *g, GCObject *o) {
|
|
|
|
if (isdead(g, o))
|
|
|
|
lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
|
|
|
|
else {
|
|
|
|
if (g->gcstate == GCSfinalize)
|
|
|
|
lua_assert(iswhite(o));
|
|
|
|
switch (o->gch.tt) {
|
|
|
|
case LUA_TUPVAL: {
|
|
|
|
UpVal *uv = gco2uv(o);
|
|
|
|
lua_assert(uv->v == &uv->value); /* must be closed */
|
|
|
|
checkvalref(g, o, uv->v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TUSERDATA: {
|
|
|
|
Table *mt = gco2u(o)->metatable;
|
|
|
|
if (mt) checkobjref(g, o, mt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TTABLE: {
|
|
|
|
checktable(g, gco2h(o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TTHREAD: {
|
|
|
|
checkstack(g, gco2th(o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TFUNCTION: {
|
|
|
|
checkclosure(g, gco2cl(o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_TPROTO: {
|
|
|
|
checkproto(g, gco2p(o));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lua_checkmemory (lua_State *L) {
|
2004-02-16 22:09:52 +03:00
|
|
|
global_State *g = G(L);
|
|
|
|
GCObject *o;
|
|
|
|
checkstack(g, g->mainthread);
|
2004-03-16 00:04:54 +03:00
|
|
|
for (o = g->rootgc; o->gch.tt != LUA_TUSERDATA; o = o->gch.next)
|
|
|
|
checkobject(g, o);
|
|
|
|
lua_assert(o == g->firstudata);
|
|
|
|
for (; o->gch.tt != LUA_TTHREAD; o = o->gch.next)
|
|
|
|
checkobject(g, o);
|
|
|
|
lua_assert(o == obj2gco(g->mainthread));
|
|
|
|
for (; o; o = o->gch.next) {
|
|
|
|
lua_assert(o->gch.tt == LUA_TTHREAD);
|
|
|
|
checkobject(g, o);
|
2004-02-16 22:09:52 +03:00
|
|
|
}
|
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) {
|
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);
|
2001-11-28 23:13:13 +03:00
|
|
|
const char *name = luaP_opnames[o];
|
2002-05-02 17:06:20 +04:00
|
|
|
int line = getline(p, pc);
|
2002-02-06 01:40:05 +03:00
|
|
|
sprintf(buff, "(%4d) %4d - ", line, pc);
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (getOpMode(o)) {
|
|
|
|
case iABC:
|
2001-11-28 23:13:13 +03:00
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
|
2001-06-05 22:17:01 +04:00
|
|
|
GETARG_A(i), GETARG_B(i), GETARG_C(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2002-04-25 00:07:46 +04:00
|
|
|
case iABx:
|
|
|
|
sprintf(buff+strlen(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:
|
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
return buff;
|
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
|
|
|
}
|
|
|
|
#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");
|
2004-03-16 00:04:54 +03:00
|
|
|
p = clvalue(obj_at(L, 1))->l.p;
|
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
|
|
|
|
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");
|
2004-03-16 00:04:54 +03:00
|
|
|
p = clvalue(obj_at(L, 1))->l.p;
|
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++) {
|
|
|
|
luaA_pushobject(L, p->k+i);
|
2004-05-10 21:50:51 +04:00
|
|
|
lua_rawseti(L, -2, i+LUA_FIRSTINDEX);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int listlocals (lua_State *L) {
|
2000-06-28 21:06:07 +04:00
|
|
|
Proto *p;
|
2002-11-14 18:41:38 +03:00
|
|
|
int pc = luaL_checkint(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");
|
2004-03-16 00:04:54 +03:00
|
|
|
p = clvalue(obj_at(L, 1))->l.p;
|
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
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int get_limits (lua_State *L) {
|
2003-10-10 17:29:28 +04:00
|
|
|
lua_createtable(L, 0, 5);
|
2004-05-01 00:13:38 +04:00
|
|
|
setnameval(L, "BITS_INT", LUA_BITSINT);
|
2001-11-28 23:13:13 +03:00
|
|
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
2002-03-14 21:01:52 +03:00
|
|
|
setnameval(L, "MAXVARS", MAXVARS);
|
2001-11-28 23:13:13 +03:00
|
|
|
setnameval(L, "MAXSTACK", MAXSTACK);
|
|
|
|
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
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)) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, memcontrol.total);
|
|
|
|
lua_pushinteger(L, memcontrol.numblocks);
|
|
|
|
lua_pushinteger(L, memcontrol.maxmem);
|
2000-10-02 18:47:43 +04:00
|
|
|
return 3;
|
2000-08-28 21:57:04 +04:00
|
|
|
}
|
|
|
|
else {
|
2003-10-03 00:31:17 +04:00
|
|
|
memcontrol.memlimit = luaL_checkint(L, 1);
|
2000-08-28 21:57:04 +04:00
|
|
|
return 0;
|
2000-08-04 23:38:35 +04:00
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 00:04:54 +03:00
|
|
|
static int settrick (lua_State *L) {
|
|
|
|
Trick = lua_tointeger(L, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*static int set_gcstate (lua_State *L) {
|
|
|
|
static const char *const state[] = {"propagate", "sweep", "finalize"};
|
|
|
|
return 0;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
static int get_gccolor (lua_State *L) {
|
|
|
|
TValue *o;
|
|
|
|
luaL_checkany(L, 1);
|
|
|
|
o = obj_at(L, 1);
|
|
|
|
if (!iscollectable(o))
|
|
|
|
lua_pushstring(L, "no collectable");
|
|
|
|
else
|
|
|
|
lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
|
|
|
|
isblack(gcvalue(o)) ? "black" : "grey");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int gcstate (lua_State *L) {
|
|
|
|
switch(G(L)->gcstate) {
|
|
|
|
case GCSpropagate: lua_pushstring(L, "propagate"); break;
|
|
|
|
case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
|
|
|
|
case GCSsweep: lua_pushstring(L, "sweep"); break;
|
|
|
|
case GCSfinalize: lua_pushstring(L, "finalize"); break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, (L->top - L->stack));
|
|
|
|
lua_pushinteger(L, (L->stack_last - L->stack));
|
|
|
|
lua_pushinteger(L, (L->ci - L->base_ci));
|
|
|
|
lua_pushinteger(L, (L->end_ci - L->base_ci));
|
|
|
|
lua_pushinteger(L, (unsigned long)&a);
|
2002-01-26 01:14:54 +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;
|
2002-11-14 18:41:38 +03:00
|
|
|
int i = luaL_optint(L, 2, -1);
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
2004-03-16 00:04:54 +03:00
|
|
|
t = hvalue(obj_at(L, 1));
|
1999-12-14 21:31:20 +03:00
|
|
|
if (i == -1) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, t->sizearray);
|
|
|
|
lua_pushinteger(L, sizenode(t));
|
|
|
|
lua_pushinteger(L, t->firstfree - t->node);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2001-10-25 23:14:14 +04:00
|
|
|
else if (i < t->sizearray) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, i);
|
2001-10-25 23:14:14 +04:00
|
|
|
luaA_pushobject(L, &t->array[i]);
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
else if ((i -= t->sizearray) < sizenode(t)) {
|
2003-03-20 00:14:53 +03:00
|
|
|
if (!ttisnil(gval(gnode(t, i))) ||
|
|
|
|
ttisnil(gkey(gnode(t, i))) ||
|
|
|
|
ttisnumber(gkey(gnode(t, i)))) {
|
|
|
|
luaA_pushobject(L, gkey(gnode(t, i)));
|
2001-04-11 18:42:41 +04:00
|
|
|
}
|
|
|
|
else
|
2003-07-09 16:08:43 +04:00
|
|
|
lua_pushliteral(L, "<undef>");
|
2003-03-20 00:14:53 +03:00
|
|
|
luaA_pushobject(L, gval(gnode(t, i)));
|
2001-10-25 23:14:14 +04:00
|
|
|
if (t->node[i].next)
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, t->node[i].next - t->node);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2002-11-14 18:41:38 +03:00
|
|
|
int s = luaL_optint(L, 2, 0) - 1;
|
1999-12-14 21:31:20 +03:00
|
|
|
if (s==-1) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L ,tb->nuse);
|
|
|
|
lua_pushinteger(L ,tb->size);
|
2000-08-28 21:57:04 +04:00
|
|
|
return 2;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-05-10 20:33:20 +04:00
|
|
|
else if (s < tb->size) {
|
2002-08-30 23:09:21 +04:00
|
|
|
GCObject *ts;
|
2000-08-28 21:57:04 +04:00
|
|
|
int n = 0;
|
2002-08-30 23:09:21 +04:00
|
|
|
for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
|
2003-12-10 15:13:36 +03:00
|
|
|
setsvalue2s(L, L->top, gco2ts(ts));
|
2002-01-26 01:14:54 +03:00
|
|
|
incr_top(L);
|
2000-08-28 21:57:04 +04:00
|
|
|
n++;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
return n;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
int lock = luaL_optint(L, 2, 1);
|
|
|
|
luaL_checkany(L, 1);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_pushvalue(L, 1);
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_ref(L, lock));
|
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);
|
2002-11-14 18:41:38 +03:00
|
|
|
lua_getref(L, luaL_checkint(L, 1));
|
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);
|
2002-11-14 18:41:38 +03:00
|
|
|
lua_unref(L, luaL_checkint(L, 1));
|
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-01-30 20:26:44 +03:00
|
|
|
static int metatable (lua_State *L) {
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_checkany(L, 1);
|
2002-04-03 00:43:08 +04:00
|
|
|
if (lua_isnone(L, 2)) {
|
|
|
|
if (lua_getmetatable(L, 1) == 0)
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
2001-12-05 23:15:18 +03:00
|
|
|
else {
|
|
|
|
lua_settop(L, 2);
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_checktype(L, 2, LUA_TTABLE);
|
2002-01-30 20:26:44 +03:00
|
|
|
lua_setmetatable(L, 1);
|
2001-12-05 23:15:18 +03:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-12-19 14:11:55 +03: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-29 23:05:11 +04:00
|
|
|
static int newuserdata (lua_State *L) {
|
2002-11-14 18:41:38 +03:00
|
|
|
size_t size = luaL_checkint(L, 1);
|
2001-11-28 23:13:13 +03:00
|
|
|
char *p = cast(char *, lua_newuserdata(L, size));
|
|
|
|
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) {
|
2002-11-14 18:41:38 +03:00
|
|
|
lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
|
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);
|
2002-05-02 00:48:12 +04:00
|
|
|
if (status == 0)
|
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) {
|
2002-11-14 18:41:38 +03:00
|
|
|
lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
|
2001-02-13 19:52:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int d2s (lua_State *L) {
|
2002-11-14 18:41:38 +03:00
|
|
|
double d = luaL_checknumber(L, 1);
|
2001-11-28 23:13:13 +03:00
|
|
|
lua_pushlstring(L, cast(char *, &d), sizeof(d));
|
2001-02-13 19:52:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int newstate (lua_State *L) {
|
2004-03-23 20:07:53 +03:00
|
|
|
lua_State *L1 = lua_newstate(debug_realloc, &memcontrol);
|
2001-02-02 18:13:05 +03:00
|
|
|
if (L1) {
|
2002-10-26 00:05:28 +04:00
|
|
|
lua_userstateopen(L1); /* init lock */
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, (unsigned long)L1);
|
2001-02-02 18:13:05 +03:00
|
|
|
}
|
2000-08-29 23:05:11 +04:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-12-20 13:26:33 +03:00
|
|
|
|
2000-09-14 18:09:31 +04:00
|
|
|
static int loadlib (lua_State *L) {
|
2002-12-20 13:26:33 +03:00
|
|
|
static const luaL_reg libs[] = {
|
2003-03-11 15:24:34 +03:00
|
|
|
{"mathlibopen", luaopen_math},
|
|
|
|
{"strlibopen", luaopen_string},
|
|
|
|
{"iolibopen", luaopen_io},
|
|
|
|
{"tablibopen", luaopen_table},
|
|
|
|
{"dblibopen", luaopen_debug},
|
|
|
|
{"baselibopen", luaopen_base},
|
2002-12-20 13:26:33 +03:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
lua_State *L1 = cast(lua_State *,
|
|
|
|
cast(unsigned long, luaL_checknumber(L, 1)));
|
|
|
|
lua_pushvalue(L1, LUA_GLOBALSINDEX);
|
|
|
|
luaL_openlib(L1, NULL, libs, 0);
|
2000-09-14 18:09:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int closestate (lua_State *L) {
|
2002-11-14 18:41:38 +03:00
|
|
|
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
|
2001-02-21 19:51:25 +03:00
|
|
|
lua_close(L1);
|
2001-03-02 20:27:50 +03:00
|
|
|
lua_unlock(L); /* close cannot unlock that */
|
2000-08-29 23:05:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int doremote (lua_State *L) {
|
2002-11-14 18:41:38 +03:00
|
|
|
lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
|
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);
|
2002-08-06 19:32:22 +04:00
|
|
|
if (status == 0)
|
|
|
|
status = lua_pcall(L1, 0, LUA_MULTRET, 0);
|
2000-08-29 23:05:11 +04:00
|
|
|
if (status != 0) {
|
|
|
|
lua_pushnil(L);
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, status);
|
2002-12-20 13:26:33 +03:00
|
|
|
lua_pushstring(L, lua_tostring(L1, -1));
|
|
|
|
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
|
|
|
|
|
|
|
static int log2_aux (lua_State *L) {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, luaO_log2(luaL_checkint(L, 1)));
|
2001-10-25 23:14:14 +04:00
|
|
|
return 1;
|
2000-08-29 23:05:11 +04:00
|
|
|
}
|
|
|
|
|
2003-02-18 19:02:56 +03:00
|
|
|
static int int2fb_aux (lua_State *L) {
|
|
|
|
int b = luaO_int2fb(luaL_checkint(L, 1));
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, b);
|
|
|
|
lua_pushinteger(L, fb2int(b));
|
2003-02-18 19:02:56 +03:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
|
2002-10-22 22:07:55 +04:00
|
|
|
|
2000-04-06 21:35:23 +04: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
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static int getnum_aux (lua_State *L, 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 == '.') {
|
2001-08-31 23:46:07 +04:00
|
|
|
res = cast(int, lua_tonumber(L, -1));
|
2000-09-01 00:23:40 +04:00
|
|
|
lua_pop(L, 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
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if (**pc == '-') {
|
2000-08-28 21:57:04 +04:00
|
|
|
sig = -1;
|
|
|
|
(*pc)++;
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
while (isdigit(cast(int, **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
|
|
|
}
|
|
|
|
|
2001-11-28 23:13:13 +03:00
|
|
|
static const char *getname_aux (char *buff, const char **pc) {
|
1999-12-14 21:31:20 +03:00
|
|
|
int i = 0;
|
|
|
|
skip(pc);
|
2001-11-28 23:13:13 +03:00
|
|
|
while (**pc != '\0' && !strchr(delimits, **pc))
|
1999-12-14 21:31:20 +03:00
|
|
|
buff[i++] = *(*pc)++;
|
2001-11-28 23:13:13 +03:00
|
|
|
buff[i] = '\0';
|
1999-12-14 21:31:20 +03:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define EQ(s1) (strcmp(s1, inst) == 0)
|
|
|
|
|
2001-08-31 23:46:07 +04:00
|
|
|
#define getnum (getnum_aux(L, &pc))
|
|
|
|
#define getname (getname_aux(buff, &pc))
|
2000-08-28 21:57:04 +04:00
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int testC (lua_State *L) {
|
2001-11-28 23:13:13 +03:00
|
|
|
char buff[30];
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *pc = luaL_checkstring(L, 1);
|
1999-12-14 21:31:20 +03:00
|
|
|
for (;;) {
|
2001-11-28 23:13:13 +03:00
|
|
|
const char *inst = getname;
|
|
|
|
if EQ("") return 0;
|
|
|
|
else if EQ("isnumber") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_isnumber(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("isstring") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_isstring(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("istable") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_istable(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("iscfunction") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_iscfunction(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("isfunction") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_isfunction(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("isuserdata") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_isuserdata(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2002-07-09 18:58:28 +04:00
|
|
|
else if EQ("isudataval") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_islightuserdata(L, getnum));
|
2002-07-09 18:58:28 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("isnil") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_isnil(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("isnull") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_isnone(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("tonumber") {
|
2000-10-06 23:29:26 +04:00
|
|
|
lua_pushnumber(L, lua_tonumber(L, getnum));
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("tostring") {
|
|
|
|
const char *s = lua_tostring(L, getnum);
|
2001-02-09 23:22:29 +03:00
|
|
|
lua_pushstring(L, s);
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2004-05-31 23:41:52 +04:00
|
|
|
else if EQ("objsize") {
|
|
|
|
lua_pushinteger(L, lua_objsize(L, getnum));
|
2000-10-06 23:29:26 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("tocfunction") {
|
2000-10-06 23:29:26 +04:00
|
|
|
lua_pushcfunction(L, lua_tocfunction(L, getnum));
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("return") {
|
2000-08-28 21:57:04 +04:00
|
|
|
return getnum;
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("gettop") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_gettop(L));
|
2000-08-28 21:57:04 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("settop") {
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_settop(L, getnum);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("pop") {
|
2000-09-01 00:23:40 +04:00
|
|
|
lua_pop(L, getnum);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("pushnum") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2002-03-04 18:26:56 +03:00
|
|
|
else if EQ("pushnil") {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
2002-02-07 20:26:33 +03:00
|
|
|
else if EQ("pushbool") {
|
|
|
|
lua_pushboolean(L, getnum);
|
|
|
|
}
|
2002-03-04 18:26:56 +03:00
|
|
|
else if EQ("tobool") {
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, lua_toboolean(L, getnum));
|
2002-03-04 18:26:56 +03:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("pushvalue") {
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_pushvalue(L, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("pushcclosure") {
|
2001-10-18 01:12:57 +04:00
|
|
|
lua_pushcclosure(L, testC, getnum);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("remove") {
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_remove(L, getnum);
|
2000-09-01 00:23:40 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("insert") {
|
2000-09-01 00:23:40 +04:00
|
|
|
lua_insert(L, getnum);
|
|
|
|
}
|
2002-02-06 01:40:05 +03:00
|
|
|
else if EQ("replace") {
|
|
|
|
lua_replace(L, getnum);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("gettable") {
|
2000-09-25 18:48:42 +04:00
|
|
|
lua_gettable(L, getnum);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("settable") {
|
2000-09-25 18:48:42 +04:00
|
|
|
lua_settable(L, getnum);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("next") {
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_next(L, -2);
|
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("concat") {
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_concat(L, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("lessthan") {
|
2001-02-01 19:03:38 +03:00
|
|
|
int a = getnum;
|
2001-12-12 01:48:44 +03:00
|
|
|
lua_pushboolean(L, lua_lessthan(L, a, getnum));
|
2001-02-01 19:03:38 +03:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("equal") {
|
2001-10-25 23:14:14 +04:00
|
|
|
int a = getnum;
|
2001-12-12 01:48:44 +03:00
|
|
|
lua_pushboolean(L, lua_equal(L, a, getnum));
|
2001-10-25 23:14:14 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("rawcall") {
|
2000-08-28 21:57:04 +04:00
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2002-06-25 23:18:49 +04:00
|
|
|
lua_call(L, narg, nres);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("call") {
|
2000-09-25 20:22:42 +04:00
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2002-08-06 19:32:22 +04:00
|
|
|
lua_pcall(L, narg, nres, 0);
|
2000-09-25 20:22:42 +04:00
|
|
|
}
|
2002-05-16 18:59:49 +04:00
|
|
|
else if EQ("loadstring") {
|
|
|
|
size_t sl;
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *s = luaL_checklstring(L, getnum, &sl);
|
2002-06-04 00:11:41 +04:00
|
|
|
luaL_loadbuffer(L, s, sl, s);
|
2002-05-16 18:59:49 +04:00
|
|
|
}
|
|
|
|
else if EQ("loadfile") {
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_loadfile(L, luaL_checkstring(L, getnum));
|
2000-09-25 20:22:42 +04:00
|
|
|
}
|
2002-01-30 20:26:44 +03:00
|
|
|
else if EQ("setmetatable") {
|
|
|
|
lua_setmetatable(L, getnum);
|
2000-10-05 17:00:17 +04:00
|
|
|
}
|
2002-01-30 20:26:44 +03:00
|
|
|
else if EQ("getmetatable") {
|
2002-04-03 00:43:08 +04:00
|
|
|
if (lua_getmetatable(L, getnum) == 0)
|
|
|
|
lua_pushnil(L);
|
2000-10-05 17:00:17 +04:00
|
|
|
}
|
2001-11-28 23:13:13 +03:00
|
|
|
else if EQ("type") {
|
2001-12-05 23:15:18 +03:00
|
|
|
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2003-04-07 18:35:00 +04:00
|
|
|
else if EQ("getn") {
|
|
|
|
int i = getnum;
|
2003-10-08 00:13:41 +04:00
|
|
|
lua_pushinteger(L, luaL_getn(L, i));
|
2003-04-07 18:35:00 +04:00
|
|
|
}
|
|
|
|
else if EQ("setn") {
|
|
|
|
int i = getnum;
|
|
|
|
int n = cast(int, lua_tonumber(L, -1));
|
|
|
|
luaL_setn(L, i, n);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2003-05-13 23:22:19 +04:00
|
|
|
else if EQ("throw") {
|
2004-05-01 00:13:38 +04:00
|
|
|
#ifdef __cplusplus
|
2003-05-13 23:22:19 +04:00
|
|
|
static struct X { int x; } x;
|
|
|
|
throw x;
|
|
|
|
#else
|
|
|
|
luaL_error(L, "C++");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2002-11-18 18:24:27 +03:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** tests for yield inside hooks
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void yieldf (lua_State *L, lua_Debug *ar) {
|
|
|
|
lua_yield(L, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int setyhook (lua_State *L) {
|
|
|
|
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 {
|
|
|
|
const char *smask = luaL_checkstring(L, 1);
|
2002-11-25 20:47:13 +03:00
|
|
|
int count = luaL_optint(L, 2, 0);
|
|
|
|
int mask = 0;
|
|
|
|
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
|
|
|
|
if (count > 0) mask |= LUA_MASKCOUNT;
|
|
|
|
lua_sethook(L, yieldf, mask, count);
|
2002-11-18 18:24:27 +03: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");
|
|
|
|
status = lua_resume(co, 0);
|
|
|
|
if (status != 0) {
|
|
|
|
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
|
|
|
|
|
|
|
static const struct luaL_reg tests_funcs[] = {
|
2001-11-28 23:13:13 +03:00
|
|
|
{"hash", hash_query},
|
|
|
|
{"limits", get_limits},
|
|
|
|
{"listcode", listcode},
|
|
|
|
{"listk", listk},
|
|
|
|
{"listlocals", listlocals},
|
|
|
|
{"loadlib", loadlib},
|
2002-01-26 01:14:54 +03:00
|
|
|
{"stacklevel", stacklevel},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"querystr", string_query},
|
|
|
|
{"querytab", table_query},
|
|
|
|
{"testC", testC},
|
2004-03-16 00:04:54 +03:00
|
|
|
{"checkmemory", lua_checkmemory},
|
|
|
|
{"gccolor", get_gccolor},
|
|
|
|
{"gcstate", gcstate},
|
|
|
|
{"trick", settrick},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"ref", tref},
|
|
|
|
{"getref", getref},
|
|
|
|
{"unref", unref},
|
|
|
|
{"d2s", d2s},
|
|
|
|
{"s2d", s2d},
|
2002-01-30 20:26:44 +03:00
|
|
|
{"metatable", metatable},
|
2002-12-19 14:11:55 +03:00
|
|
|
{"upvalue", upvalue},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"newuserdata", newuserdata},
|
2002-04-05 22:54:31 +04:00
|
|
|
{"pushuserdata", pushuserdata},
|
2001-11-28 23:13:13 +03:00
|
|
|
{"udataval", udataval},
|
|
|
|
{"doonnewstack", doonnewstack},
|
|
|
|
{"newstate", newstate},
|
|
|
|
{"closestate", closestate},
|
|
|
|
{"doremote", doremote},
|
|
|
|
{"log2", log2_aux},
|
2003-02-18 19:02:56 +03:00
|
|
|
{"int2fb", int2fb_aux},
|
2002-03-20 15:54:08 +03:00
|
|
|
{"totalmem", mem_query},
|
2002-11-18 18:24:27 +03:00
|
|
|
{"resume", coresume},
|
|
|
|
{"setyhook", setyhook},
|
2002-03-20 15:54:08 +03:00
|
|
|
{NULL, NULL}
|
1999-12-14 21:31:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-01-11 23:26:52 +03:00
|
|
|
static void fim (void) {
|
|
|
|
if (!islocked)
|
|
|
|
lua_close(lua_state);
|
2003-10-03 00:31:17 +04:00
|
|
|
lua_assert(memcontrol.numblocks == 0);
|
|
|
|
lua_assert(memcontrol.total == 0);
|
2002-01-11 23:26:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-29 13:27:53 +03:00
|
|
|
static int l_panic (lua_State *L) {
|
|
|
|
UNUSED(L);
|
|
|
|
fprintf(stderr, "unable to recover; exiting\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 20:25:13 +04:00
|
|
|
int luaB_opentests (lua_State *L) {
|
2003-10-20 16:25:23 +04:00
|
|
|
void *ud;
|
|
|
|
lua_assert(lua_getallocf(L, &ud) == debug_realloc);
|
|
|
|
lua_assert(ud == cast(void *, &memcontrol));
|
2003-01-29 13:27:53 +03:00
|
|
|
lua_atpanic(L, l_panic);
|
2002-10-26 00:05:28 +04:00
|
|
|
lua_userstateopen(L); /* init lock */
|
2001-02-02 18:13:05 +03:00
|
|
|
lua_state = L; /* keep first state to be opened */
|
2002-11-14 18:41:38 +03:00
|
|
|
luaL_openlib(L, "T", tests_funcs, 0);
|
2002-01-11 23:26:52 +03:00
|
|
|
atexit(fim);
|
2002-07-17 20:25:13 +04:00
|
|
|
return 0;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
2002-12-04 20:29:05 +03:00
|
|
|
|
|
|
|
#undef main
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
char *limit = getenv("MEMLIMIT");
|
|
|
|
if (limit)
|
2003-10-03 00:31:17 +04:00
|
|
|
memcontrol.memlimit = strtoul(limit, NULL, 10);
|
2004-05-31 22:50:48 +04:00
|
|
|
return l_main(argc, argv);
|
2002-12-04 20:29:05 +03:00
|
|
|
}
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
#endif
|