lua/ltests.c

749 lines
18 KiB
C
Raw Normal View History

/*
2002-06-21 00:40:38 +04:00
** $Id: ltests.c,v 1.126 2002/06/18 15:19:27 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
2000-02-21 21:30:06 +03:00
#include <string.h>
#include "lua.h"
#include "lapi.h"
#include "lauxlib.h"
#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"
#include "lmem.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "luadebug.h"
2000-09-14 18:09:31 +04:00
#include "lualib.h"
/*
2000-10-30 19:29:59 +03:00
** The whole module only makes sense with LUA_DEBUG on
*/
2000-10-30 19:29:59 +03:00
#ifdef LUA_DEBUG
static lua_State *lua_state = NULL;
int islocked = 0;
2002-03-05 00:29:41 +03:00
#define index(L,k) (L->ci->base+(k) - 1)
static void setnameval (lua_State *L, const char *name, int val) {
2000-08-28 21:57:04 +04:00
lua_pushstring(L, name);
lua_pushnumber(L, val);
2000-09-05 23:33:32 +04:00
lua_settable(L, -3);
}
/*
** {======================================================================
** Controlled version for realloc.
** =======================================================================
*/
/* ensures maximum alignment for HEADER */
#define HEADER (sizeof(union L_Umaxalign))
#define MARKSIZE 32
#define MARK 0x55 /* 01010101 (a nice pattern) */
2001-08-31 23:46:07 +04:00
#define blocksize(b) (cast(size_t *, b) - HEADER/sizeof(size_t))
unsigned long memdebug_numblocks = 0;
unsigned long memdebug_total = 0;
unsigned long memdebug_maxmem = 0;
unsigned long memdebug_memlimit = ULONG_MAX;
static void *checkblock (void *block) {
size_t *b = blocksize(block);
size_t size = *b;
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? */
return b;
}
static void freeblock (void *block) {
if (block) {
size_t size = *blocksize(block);
block = checkblock(block);
memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
free(block); /* free original block */
memdebug_numblocks--;
memdebug_total -= size;
}
}
void *debug_realloc (void *block, size_t oldsize, size_t size) {
lua_assert((oldsize == 0) == (block == NULL));
lua_assert(oldsize == 0 || oldsize == *blocksize(block));
/* ISO does not specify what realloc(NULL, 0) does */
lua_assert(block != NULL || size > 0);
if (size == 0) {
freeblock(block);
return NULL;
}
else if (memdebug_total+size-oldsize > memdebug_memlimit)
return NULL; /* to test memory allocation errors */
else {
2001-08-31 00:55:22 +04:00
void *newblock;
int i;
size_t realsize = HEADER+size+MARKSIZE;
if (realsize < size) return NULL; /* overflow! */
2001-08-31 00:55:22 +04:00
newblock = malloc(realsize); /* alloc a new block */
if (newblock == NULL) return NULL;
if (oldsize > size) oldsize = size;
if (block) {
2001-08-31 23:46:07 +04:00
memcpy(cast(char *, newblock)+HEADER, block, oldsize);
freeblock(block); /* erase (and check) old copy */
}
/* initialize new part of the block with something `weird' */
2001-08-31 23:46:07 +04:00
memset(cast(char *, newblock)+HEADER+oldsize, -MARK, size-oldsize);
memdebug_total += size;
if (memdebug_total > memdebug_maxmem)
memdebug_maxmem = memdebug_total;
memdebug_numblocks++;
2001-08-31 23:46:07 +04:00
*cast(size_t *, newblock) = size;
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;
}
}
/* }====================================================================== */
/*
** {======================================================
** Disassembler
** =======================================================
*/
static char *buildop (Proto *p, int pc, char *buff) {
2000-06-28 21:06:07 +04:00
Instruction i = p->code[pc];
OpCode o = GET_OPCODE(i);
const char *name = luaP_opnames[o];
int line = getline(p, pc);
sprintf(buff, "(%4d) %4d - ", line, pc);
switch (getOpMode(o)) {
case iABC:
sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
GETARG_A(i), GETARG_B(i), GETARG_C(i));
break;
case iABx:
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
break;
case iAsBx:
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
break;
}
return buff;
}
#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));
}
printf("-------\n");
}
#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;
Proto *p;
2000-10-05 16:14:08 +04:00
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
2002-03-05 00:29:41 +03:00
p = clvalue(index(L, 1))->l.p;
2000-08-28 21:57:04 +04:00
lua_newtable(L);
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++) {
char buff[100];
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, pc+1);
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;
}
static int listk (lua_State *L) {
Proto *p;
int i;
2000-10-05 16:14:08 +04:00
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
2002-03-05 00:29:41 +03:00
p = clvalue(index(L, 1))->l.p;
2000-08-28 21:57:04 +04:00
lua_newtable(L);
for (i=0; i<p->sizek; i++) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, i+1);
luaA_pushobject(L, p->k+i);
2000-09-05 23:33:32 +04:00
lua_settable(L, -3);
}
2000-08-28 21:57:04 +04:00
return 1;
}
2000-08-28 21:57:04 +04:00
static int listlocals (lua_State *L) {
2000-06-28 21:06:07 +04:00
Proto *p;
2000-08-28 21:57:04 +04:00
int pc = luaL_check_int(L, 2) - 1;
int i = 0;
const char *name;
2000-10-05 16:14:08 +04:00
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
2002-03-05 00:29:41 +03:00
p = clvalue(index(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
}
/* }====================================================== */
2001-02-13 19:52:01 +03:00
2000-08-28 21:57:04 +04:00
static int get_limits (lua_State *L) {
lua_newtable(L);
setnameval(L, "BITS_INT", BITS_INT);
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
2002-03-14 21:01:52 +03:00
setnameval(L, "MAXVARS", MAXVARS);
setnameval(L, "MAXPARAMS", MAXPARAMS);
setnameval(L, "MAXSTACK", MAXSTACK);
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
2000-08-28 21:57:04 +04:00
return 1;
}
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)) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, memdebug_total);
lua_pushnumber(L, memdebug_numblocks);
lua_pushnumber(L, memdebug_maxmem);
2000-10-02 18:47:43 +04:00
return 3;
2000-08-28 21:57:04 +04:00
}
else {
memdebug_memlimit = luaL_check_int(L, 1);
return 0;
}
}
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)) {
2001-12-05 23:15:18 +03:00
luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
2002-03-05 00:29:41 +03:00
lua_pushnumber(L, tsvalue(index(L, 1))->tsv.hash);
}
else {
2002-03-05 00:29:41 +03:00
TObject *o = index(L, 1);
2001-10-25 23:14:14 +04:00
Table *t;
2001-12-05 23:15:18 +03:00
luaL_check_type(L, 2, LUA_TTABLE);
2002-03-05 00:29:41 +03:00
t = hvalue(index(L, 2));
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
}
2000-08-28 21:57:04 +04:00
return 1;
}
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;
2002-01-26 01:14:54 +03:00
lua_pushnumber(L, (int)(L->top - L->stack));
lua_pushnumber(L, (int)(L->stack_last - L->stack));
lua_pushnumber(L, (int)(L->ci - L->base_ci));
lua_pushnumber(L, (int)(L->end_ci - L->base_ci));
2002-01-10 00:51:06 +03:00
lua_pushnumber(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;
2000-08-28 21:57:04 +04:00
int i = luaL_opt_int(L, 2, -1);
2001-12-05 23:15:18 +03:00
luaL_check_type(L, 1, LUA_TTABLE);
2002-03-05 00:29:41 +03:00
t = hvalue(index(L, 1));
if (i == -1) {
2001-10-25 23:14:14 +04:00
lua_pushnumber(L, t->sizearray);
lua_pushnumber(L, sizenode(t));
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, t->firstfree - t->node);
}
2001-10-25 23:14:14 +04:00
else if (i < t->sizearray) {
lua_pushnumber(L, i);
luaA_pushobject(L, &t->array[i]);
lua_pushnil(L);
}
else if ((i -= t->sizearray) < sizenode(t)) {
2001-04-11 18:42:41 +04:00
if (ttype(val(node(t, i))) != LUA_TNIL ||
ttype(key(node(t, i))) == LUA_TNIL ||
ttype(key(node(t, i))) == LUA_TNUMBER) {
luaA_pushobject(L, key(node(t, i)));
2001-04-11 18:42:41 +04:00
}
else
lua_pushstring(L, "<undef>");
luaA_pushobject(L, val(&t->node[i]));
2001-10-25 23:14:14 +04:00
if (t->node[i].next)
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, t->node[i].next - t->node);
else
2001-10-25 23:14:14 +04:00
lua_pushnil(L);
}
2001-10-25 23:14:14 +04:00
return 3;
}
2000-08-28 21:57:04 +04:00
static int string_query (lua_State *L) {
stringtable *tb = &G(L)->strt;
2000-08-28 21:57:04 +04:00
int s = luaL_opt_int(L, 2, 0) - 1;
if (s==-1) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L ,tb->nuse);
lua_pushnumber(L ,tb->size);
return 2;
}
2000-05-10 20:33:20 +04:00
else if (s < tb->size) {
TString *ts;
2000-08-28 21:57:04 +04:00
int n = 0;
for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
setsvalue(L->top, ts);
2002-01-26 01:14:54 +03:00
incr_top(L);
2000-08-28 21:57:04 +04:00
n++;
}
2000-08-28 21:57:04 +04:00
return n;
}
2000-08-28 21:57:04 +04:00
return 0;
}
2000-08-28 21:57:04 +04:00
2002-06-18 19:19:27 +04:00
static int xpcall (lua_State *L) {
int status;
luaL_check_type(L, 1, LUA_TFUNCTION);
luaL_check_any(L, 2);
lua_pushliteral(L, LUA_TRACEBACK);
lua_gettable(L, LUA_REGISTRYINDEX);
lua_pushliteral(L, LUA_TRACEBACK);
lua_pushvalue(L, 1);
lua_settable(L, LUA_REGISTRYINDEX);
lua_replace(L, 1);
status = lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET);
lua_pushliteral(L, LUA_TRACEBACK);
lua_pushvalue(L, 1);
lua_settable(L, LUA_REGISTRYINDEX);
if (status != 0) {
int numres = (status == LUA_ERRRUN) ? 3 : 2;
lua_pushnil(L);
lua_insert(L, -numres);
return numres;
}
else {
lua_pushboolean(L, 1);
lua_insert(L, 2);
return lua_gettop(L) - 1; /* return `true' + all results */
}
}
static int tref (lua_State *L) {
int level = lua_gettop(L);
2001-11-07 00:41:43 +03:00
int lock = luaL_opt_int(L, 2, 1);
luaL_check_any(L, 1);
2000-09-05 23:33:32 +04:00
lua_pushvalue(L, 1);
2001-11-07 00:41:43 +03:00
lua_pushnumber(L, lua_ref(L, lock));
assert(lua_gettop(L) == level+1); /* +1 for result */
return 1;
}
static int getref (lua_State *L) {
int level = lua_gettop(L);
lua_getref(L, luaL_check_int(L, 1));
assert(lua_gettop(L) == level+1);
return 1;
}
static int unref (lua_State *L) {
int level = lua_gettop(L);
lua_unref(L, luaL_check_int(L, 1));
assert(lua_gettop(L) == level);
return 0;
}
2002-01-30 20:26:44 +03:00
static int metatable (lua_State *L) {
2001-12-05 23:15:18 +03:00
luaL_check_any(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);
luaL_check_type(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;
}
static int newuserdata (lua_State *L) {
size_t size = luaL_check_int(L, 1);
char *p = cast(char *, lua_newuserdata(L, size));
while (size--) *p++ = '\0';
return 1;
}
static int pushuserdata (lua_State *L) {
lua_pushudataval(L, cast(void *, luaL_check_int(L, 1)));
return 1;
}
static int udataval (lua_State *L) {
2001-08-31 23:46:07 +04:00
lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
return 1;
}
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;
const char *s = luaL_check_lstr(L, 1, &l);
int status = luaL_loadbuffer(L1, s, l, s);
2002-05-02 00:48:12 +04:00
if (status == 0)
2002-06-18 19:19:27 +04:00
status = lua_pcall(L1, 0, 0);
2002-02-15 00:47:29 +03:00
lua_pushnumber(L, status);
lua_closethread(L, L1);
return 1;
}
2001-02-13 19:52:01 +03:00
static int s2d (lua_State *L) {
2002-02-09 01:41:50 +03:00
lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
2001-02-13 19:52:01 +03:00
return 1;
}
static int d2s (lua_State *L) {
double d = luaL_check_number(L, 1);
lua_pushlstring(L, cast(char *, &d), sizeof(d));
2001-02-13 19:52:01 +03:00
return 1;
}
static int newstate (lua_State *L) {
2002-02-15 00:47:29 +03:00
lua_State *L1 = lua_open();
if (L1) {
2001-08-31 23:46:07 +04:00
*cast(int **, L1) = &islocked; /* initialize the lock */
lua_pushnumber(L, (unsigned long)L1);
}
else
lua_pushnil(L);
return 1;
}
2000-09-14 18:09:31 +04:00
static int loadlib (lua_State *L) {
2001-08-31 23:46:07 +04:00
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
2001-03-06 23:09:38 +03:00
lua_register(L1, "mathlibopen", lua_mathlibopen);
lua_register(L1, "strlibopen", lua_strlibopen);
lua_register(L1, "iolibopen", lua_iolibopen);
lua_register(L1, "dblibopen", lua_dblibopen);
lua_register(L1, "baselibopen", lua_baselibopen);
2000-09-14 18:09:31 +04:00
return 0;
}
static int closestate (lua_State *L) {
2001-08-31 23:46:07 +04:00
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
lua_close(L1);
2001-03-02 20:27:50 +03:00
lua_unlock(L); /* close cannot unlock that */
return 0;
}
static int doremote (lua_State *L) {
2002-06-21 00:40:38 +04:00
lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_check_number(L, 1)));
size_t lcode;
const char *code = luaL_check_lstr(L, 2, &lcode);
int status;
2002-06-21 00:40:38 +04:00
lua_settop(L1, 0);
status = luaL_loadbuffer(L1, code, lcode, code);
if (status == 0)
status = lua_pcall(L1, 0, LUA_MULTRET);
if (status != 0) {
lua_pushnil(L);
lua_pushnumber(L, status);
return 2;
}
else {
int i = 0;
2001-12-21 00:27:12 +03:00
while (!lua_isnone(L1, ++i))
lua_pushstring(L, lua_tostring(L1, i));
2001-03-06 23:09:38 +03:00
lua_pop(L1, i-1);
return i-1;
}
}
2001-10-25 23:14:14 +04:00
static int log2_aux (lua_State *L) {
lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
return 1;
}
/*
** {======================================================
2001-02-22 21:59:59 +03:00
** function to test the API with C. It interprets a kind of assembler
** language with calls to the API, so the test can be driven by Lua code
** =======================================================
*/
static const char *const delimits = " \t\n,;";
static void skip (const char **pc) {
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
}
static int getnum_aux (lua_State *L, const char **pc) {
int res = 0;
2000-08-28 21:57:04 +04:00
int sig = 1;
skip(pc);
if (**pc == '.') {
2001-08-31 23:46:07 +04:00
res = cast(int, lua_tonumber(L, -1));
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
}
else if (**pc == '-') {
2000-08-28 21:57:04 +04:00
sig = -1;
(*pc)++;
}
while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
2000-08-31 17:29:47 +04:00
return sig*res;
}
static const char *getname_aux (char *buff, const char **pc) {
int i = 0;
skip(pc);
while (**pc != '\0' && !strchr(delimits, **pc))
buff[i++] = *(*pc)++;
buff[i] = '\0';
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-08-28 21:57:04 +04:00
static int testC (lua_State *L) {
char buff[30];
const char *pc = luaL_check_string(L, 1);
for (;;) {
const char *inst = getname;
if EQ("") return 0;
else if EQ("isnumber") {
lua_pushnumber(L, lua_isnumber(L, getnum));
}
else if EQ("isstring") {
lua_pushnumber(L, lua_isstring(L, getnum));
}
else if EQ("istable") {
lua_pushnumber(L, lua_istable(L, getnum));
}
else if EQ("iscfunction") {
lua_pushnumber(L, lua_iscfunction(L, getnum));
}
else if EQ("isfunction") {
lua_pushnumber(L, lua_isfunction(L, getnum));
}
else if EQ("isuserdata") {
lua_pushnumber(L, lua_isuserdata(L, getnum));
}
else if EQ("isnil") {
lua_pushnumber(L, lua_isnil(L, getnum));
}
else if EQ("isnull") {
2001-12-21 00:27:12 +03:00
lua_pushnumber(L, lua_isnone(L, getnum));
}
else if EQ("tonumber") {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
else if EQ("tostring") {
const char *s = lua_tostring(L, getnum);
lua_pushstring(L, s);
}
else if EQ("tonumber") {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
else if EQ("strlen") {
lua_pushnumber(L, lua_strlen(L, getnum));
}
else if EQ("tocfunction") {
lua_pushcfunction(L, lua_tocfunction(L, getnum));
}
else if EQ("return") {
2000-08-28 21:57:04 +04:00
return getnum;
}
else if EQ("gettop") {
2000-08-31 17:29:47 +04:00
lua_pushnumber(L, lua_gettop(L));
2000-08-28 21:57:04 +04:00
}
else if EQ("settop") {
2000-08-28 21:57:04 +04:00
lua_settop(L, getnum);
}
else if EQ("pop") {
lua_pop(L, getnum);
}
else if EQ("pushnum") {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, getnum);
}
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") {
lua_pushnumber(L, lua_toboolean(L, getnum));
}
else if EQ("pushvalue") {
2000-09-05 23:33:32 +04:00
lua_pushvalue(L, getnum);
}
else if EQ("pushcclosure") {
lua_pushcclosure(L, testC, getnum);
}
else if EQ("pushupvalues") {
lua_pushupvalues(L);
}
else if EQ("remove") {
2000-09-05 23:33:32 +04:00
lua_remove(L, getnum);
}
else if EQ("insert") {
lua_insert(L, getnum);
}
else if EQ("replace") {
lua_replace(L, getnum);
}
else if EQ("gettable") {
lua_gettable(L, getnum);
}
else if EQ("settable") {
lua_settable(L, getnum);
}
else if EQ("next") {
2000-09-05 23:33:32 +04:00
lua_next(L, -2);
}
else if EQ("concat") {
2000-09-05 23:33:32 +04:00
lua_concat(L, getnum);
}
else if EQ("lessthan") {
int a = getnum;
2001-12-12 01:48:44 +03:00
lua_pushboolean(L, lua_lessthan(L, a, getnum));
}
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
}
else if EQ("rawcall") {
2000-08-28 21:57:04 +04:00
int narg = getnum;
int nres = getnum;
lua_upcall(L, narg, nres);
}
else if EQ("call") {
2000-09-25 20:22:42 +04:00
int narg = getnum;
int nres = getnum;
2002-06-18 19:19:27 +04:00
lua_pcall(L, narg, nres);
2000-09-25 20:22:42 +04:00
}
2002-05-16 18:59:49 +04:00
else if EQ("loadstring") {
size_t sl;
const char *s = luaL_check_lstr(L, getnum, &sl);
luaL_loadbuffer(L, s, sl, s);
2002-05-16 18:59:49 +04:00
}
else if EQ("loadfile") {
luaL_loadfile(L, luaL_check_string(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);
}
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);
}
else if EQ("type") {
2001-12-05 23:15:18 +03:00
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
}
2002-06-18 19:19:27 +04:00
else luaL_error(L, "unknown instruction %s", buff);
}
2000-08-28 21:57:04 +04:00
return 0;
}
/* }====================================================== */
static const struct luaL_reg tests_funcs[] = {
{"hash", hash_query},
{"limits", get_limits},
{"listcode", listcode},
{"listk", listk},
{"listlocals", listlocals},
{"loadlib", loadlib},
2002-01-26 01:14:54 +03:00
{"stacklevel", stacklevel},
{"querystr", string_query},
2002-06-18 19:19:27 +04:00
{"xpcall", xpcall},
{"querytab", table_query},
{"testC", testC},
{"ref", tref},
{"getref", getref},
{"unref", unref},
{"d2s", d2s},
{"s2d", s2d},
2002-01-30 20:26:44 +03:00
{"metatable", metatable},
{"newuserdata", newuserdata},
{"pushuserdata", pushuserdata},
{"udataval", udataval},
{"doonnewstack", doonnewstack},
{"newstate", newstate},
{"closestate", closestate},
{"doremote", doremote},
{"log2", log2_aux},
2002-03-20 15:54:08 +03:00
{"totalmem", mem_query},
{NULL, NULL}
};
static void fim (void) {
if (!islocked)
lua_close(lua_state);
lua_assert(memdebug_numblocks == 0);
lua_assert(memdebug_total == 0);
}
void luaB_opentests (lua_State *L) {
2001-08-31 23:46:07 +04:00
*cast(int **, L) = &islocked; /* init lock */
lua_state = L; /* keep first state to be opened */
2002-04-03 00:43:08 +04:00
luaL_opennamedlib(L, "T", tests_funcs, 0);
atexit(fim);
}
#endif