lua/ltests.c

687 lines
17 KiB
C
Raw Normal View History

/*
2001-03-02 20:27:50 +03:00
** $Id: ltests.c,v 1.72 2001/02/23 17:17:25 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
lua_State *lua_state = NULL;
int islocked = 0;
2001-02-23 20:17:25 +03:00
static void setnameval (lua_State *L, const l_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-02-23 20:17:25 +03:00
#define blocksize(b) ((size_t *)((l_char *)(b) - HEADER))
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-02-23 20:17:25 +03:00
lua_assert(*(((l_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 : oldsize == *blocksize(block));
if (size == 0) {
freeblock(block);
return NULL;
}
else if (memdebug_total+size-oldsize > memdebug_memlimit)
return NULL; /* to test memory allocation errors */
else {
2001-02-23 20:17:25 +03:00
l_char *newblock;
int i;
size_t realsize = HEADER+size+MARKSIZE;
if (realsize < size) return NULL; /* overflow! */
2001-02-23 20:17:25 +03:00
newblock = (l_char *)malloc(realsize); /* alloc a new block */
if (newblock == NULL) return NULL;
if (oldsize > size) oldsize = size;
if (block) {
memcpy(newblock+HEADER, block, oldsize);
freeblock(block); /* erase (and check) old copy */
}
/* initialize new part of the block with something `weird' */
memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
memdebug_total += size;
if (memdebug_total > memdebug_maxmem)
memdebug_maxmem = memdebug_total;
memdebug_numblocks++;
*(size_t *)newblock = size;
for (i=0;i<MARKSIZE;i++)
2001-02-23 20:17:25 +03:00
*(newblock+HEADER+size+i) = (l_char)(MARK+i);
return newblock+HEADER;
}
}
/* }====================================================================== */
/*
** {======================================================
** Disassembler
** =======================================================
*/
2001-02-23 20:17:25 +03:00
static const l_char *const instrname[NUM_OPCODES] = {
l_s("RETURN"), l_s("CALL"), l_s("TAILCALL"), l_s("PUSHNIL"), l_s("POP"), l_s("PUSHINT"),
l_s("PUSHSTRING"), l_s("PUSHNUM"), l_s("PUSHNEGNUM"), l_s("PUSHUPVALUE"), l_s("GETLOCAL"),
l_s("GETGLOBAL"), l_s("GETTABLE"), l_s("GETDOTTED"), l_s("GETINDEXED"), l_s("PUSHSELF"),
l_s("CREATETABLE"), l_s("SETLOCAL"), l_s("SETGLOBAL"), l_s("SETTABLE"), l_s("SETLIST"), l_s("SETMAP"),
l_s("ADD"), l_s("ADDI"), l_s("SUB"), l_s("MULT"), l_s("DIV"), l_s("POW"), l_s("CONCAT"), l_s("MINUS"), l_s("NOT"),
l_s("JMPNE"), l_s("JMPEQ"), l_s("JMPLT"), l_s("JMPLE"), l_s("JMPGT"), l_s("JMPGE"), l_s("JMPT"), l_s("JMPF"),
l_s("JMPONT"), l_s("JMPONF"), l_s("JMP"), l_s("PUSHNILJMP"), l_s("FORPREP"), l_s("FORLOOP"), l_s("LFORPREP"),
l_s("LFORLOOP"), l_s("CLOSURE")
};
2001-01-15 19:13:24 +03:00
static void pushop (lua_State *L, Proto *p, int pc) {
2001-02-23 20:17:25 +03:00
l_char buff[100];
2000-06-28 21:06:07 +04:00
Instruction i = p->code[pc];
OpCode o = GET_OPCODE(i);
2001-02-23 20:17:25 +03:00
const l_char *name = instrname[o];
sprintf(buff, l_s("%5d - "), luaG_getline(p->lineinfo, pc, 1, NULL));
switch ((enum Mode)luaK_opproperties[o].mode) {
case iO:
2001-02-23 20:17:25 +03:00
sprintf(buff+8, l_s("%-12s"), name);
break;
case iU:
2001-02-23 20:17:25 +03:00
sprintf(buff+8, l_s("%-12s%4u"), name, GETARG_U(i));
break;
case iS:
2001-02-23 20:17:25 +03:00
sprintf(buff+8, l_s("%-12s%4d"), name, GETARG_S(i));
break;
case iAB:
2001-02-23 20:17:25 +03:00
sprintf(buff+8, l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_B(i));
break;
}
2000-08-28 21:57:04 +04:00
lua_pushstring(L, buff);
}
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),
2001-02-23 20:17:25 +03:00
1, l_s("Lua function expected"));
2000-08-28 21:57:04 +04:00
p = clvalue(luaA_index(L, 1))->f.l;
lua_newtable(L);
2001-02-23 20:17:25 +03:00
setnameval(L, l_s("maxstack"), p->maxstacksize);
setnameval(L, l_s("numparams"), p->numparams);
2001-01-15 19:13:24 +03:00
for (pc=0; pc<p->sizecode; pc++) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, pc+1);
2001-01-15 19:13:24 +03:00
pushop(L, p, pc);
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-08-28 21:57:04 +04:00
static int liststrings (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),
2001-02-23 20:17:25 +03:00
1, l_s("Lua function expected"));
2000-08-28 21:57:04 +04:00
p = clvalue(luaA_index(L, 1))->f.l;
lua_newtable(L);
for (i=0; i<p->sizekstr; i++) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, i+1);
lua_pushstring(L, getstr(p->kstr[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;
2001-02-23 20:17:25 +03:00
const l_char *name;
2000-10-05 16:14:08 +04:00
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
2001-02-23 20:17:25 +03:00
1, l_s("Lua function expected"));
2000-08-28 21:57:04 +04:00
p = clvalue(luaA_index(L, 1))->f.l;
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
static int pushbool (lua_State *L, int b) {
if (b) lua_pushnumber(L, 1);
else lua_pushnil(L);
return 1;
}
2000-08-28 21:57:04 +04:00
static int get_limits (lua_State *L) {
lua_newtable(L);
2001-02-23 20:17:25 +03:00
setnameval(L, l_s("BITS_INT"), BITS_INT);
setnameval(L, l_s("LFPF"), LFIELDS_PER_FLUSH);
setnameval(L, l_s("MAXARG_A"), MAXARG_A);
setnameval(L, l_s("MAXARG_B"), MAXARG_B);
setnameval(L, l_s("MAXARG_S"), MAXARG_S);
setnameval(L, l_s("MAXARG_U"), MAXARG_U);
setnameval(L, l_s("MAXLOCALS"), MAXLOCALS);
setnameval(L, l_s("MAXPARAMS"), MAXPARAMS);
setnameval(L, l_s("MAXSTACK"), MAXSTACK);
setnameval(L, l_s("MAXUPVALUES"), MAXUPVALUES);
setnameval(L, l_s("MAXVARSLH"), MAXVARSLH);
setnameval(L, l_s("RFPF"), RFIELDS_PER_FLUSH);
setnameval(L, l_s("SIZE_A"), SIZE_A);
setnameval(L, l_s("SIZE_B"), SIZE_B);
setnameval(L, l_s("SIZE_OP"), SIZE_OP);
setnameval(L, l_s("SIZE_U"), SIZE_U);
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) {
if (lua_isnull(L, 1)) {
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) {
if (lua_isnull(L, 2)) {
2001-02-23 20:17:25 +03:00
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, l_s("string expected"));
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
}
else {
2000-08-28 21:57:04 +04:00
Hash *t;
Node n;
TObject *o = luaA_index(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
2000-08-28 21:57:04 +04:00
t = hvalue(luaA_index(L, 2));
setobj2key(&n, o);
lua_pushnumber(L, luaH_mainposition(t, &n) - t->node);
}
2000-08-28 21:57:04 +04:00
return 1;
}
2000-08-28 21:57:04 +04:00
static int table_query (lua_State *L) {
const Hash *t;
int i = luaL_opt_int(L, 2, -1);
luaL_checktype(L, 1, LUA_TTABLE);
2000-08-28 21:57:04 +04:00
t = hvalue(luaA_index(L, 1));
if (i == -1) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, t->size);
lua_pushnumber(L, t->firstfree - t->node);
return 2;
}
else if (i < t->size) {
TObject o;
setkey2obj(&o, &t->node[i]);
luaA_pushobject(L, &o);
2000-08-28 21:57:04 +04:00
luaA_pushobject(L, &t->node[i].val);
if (t->node[i].next) {
lua_pushnumber(L, t->node[i].next - t->node);
return 3;
}
else
return 2;
}
2000-08-28 21:57:04 +04:00
return 0;
}
2000-08-28 21:57:04 +04:00
static int string_query (lua_State *L) {
2001-02-23 20:17:25 +03:00
stringtable *tb = (*luaL_check_string(L, 1) == l_c('s')) ? &G(L)->strt :
&G(L)->udt;
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;
2000-05-10 20:33:20 +04:00
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
setsvalue(L->top, ts);
2000-05-10 20:33:20 +04:00
incr_top;
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
static int tref (lua_State *L) {
luaL_checkany(L, 1);
2000-09-05 23:33:32 +04:00
lua_pushvalue(L, 1);
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
return 1;
}
static int getref (lua_State *L) {
if (lua_getref(L, luaL_check_int(L, 1)))
return 1;
else
return 0;
}
static int unref (lua_State *L) {
lua_unref(L, luaL_check_int(L, 1));
return 0;
}
static int newuserdata (lua_State *L) {
if (lua_isnumber(L, 2)) {
int tag = luaL_check_int(L, 2);
int res = lua_pushuserdata(L, (void *)luaL_check_int(L, 1));
if (tag) lua_settag(L, tag);
2001-02-13 19:52:01 +03:00
pushbool(L, res);
return 2;
}
else {
2001-02-09 23:29:33 +03:00
size_t size = luaL_check_int(L, 1);
2001-02-23 20:17:25 +03:00
l_char *p = (l_char *)lua_newuserdata(L, size);
while (size--) *p++ = l_c('\0');
return 1;
}
}
static int udataval (lua_State *L) {
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_pushnumber(L, (int)lua_touserdata(L, 1));
return 1;
}
static int newtag (lua_State *L) {
2001-02-20 21:18:00 +03:00
lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1),
(int)lua_tonumber(L, 2)));
return 1;
}
static int doonnewstack (lua_State *L) {
lua_State *L1 = lua_open(L, luaL_check_int(L, 1));
if (L1 == NULL) return 0;
*((int **)L1) = &islocked; /* initialize the lock */
lua_dostring(L1, luaL_check_string(L, 2));
lua_pushnumber(L, 1);
lua_close(L1);
return 1;
}
2001-02-13 19:52:01 +03:00
static int s2d (lua_State *L) {
lua_pushnumber(L, *(double *)luaL_check_string(L, 1));
return 1;
}
static int d2s (lua_State *L) {
double d = luaL_check_number(L, 1);
2001-02-23 20:17:25 +03:00
lua_pushlstring(L, (l_char *)&d, sizeof(d));
2001-02-13 19:52:01 +03:00
return 1;
}
static int newstate (lua_State *L) {
lua_State *L1 = lua_open(NULL, luaL_check_int(L, 1));
if (L1) {
*((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) {
lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
switch (*luaL_check_string(L, 2)) {
2001-02-23 20:17:25 +03:00
case l_c('m'): lua_mathlibopen(L1); break;
case l_c('s'): lua_strlibopen(L1); break;
case l_c('i'): lua_iolibopen(L1); break;
case l_c('d'): lua_dblibopen(L1); break;
case l_c('b'): lua_baselibopen(L1); break;
default: luaL_argerror(L, 2, l_s("invalid option"));
2000-09-14 18:09:31 +04:00
}
return 0;
}
static int closestate (lua_State *L) {
lua_State *L1 = (lua_State *)(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) {
lua_State *L1;
2001-02-23 20:17:25 +03:00
const l_char *code = luaL_check_string(L, 2);
int status;
L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
status = lua_dostring(L1, code);
if (status != 0) {
lua_pushnil(L);
lua_pushnumber(L, status);
return 2;
}
else {
int i = 0;
while (!lua_isnull(L1, ++i))
lua_pushstring(L, lua_tostring(L1, i));
return i-1;
}
}
static int settagmethod (lua_State *L) {
int tag = luaL_check_int(L, 1);
2001-02-23 20:17:25 +03:00
const l_char *event = luaL_check_string(L, 2);
luaL_checkany(L, 3);
lua_gettagmethod(L, tag, event);
lua_pushvalue(L, 3);
lua_settagmethod(L, tag, event);
return 1;
}
static int equal (lua_State *L) {
return pushbool(L, lua_equal(L, 1, 2));
}
/*
** {======================================================
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
** =======================================================
*/
2001-02-23 20:17:25 +03:00
static const l_char *const delimits = l_s(" \t\n,;");
2001-02-23 20:17:25 +03:00
static void skip (const l_char **pc) {
while (**pc != l_c('\0') && strchr(delimits, **pc)) (*pc)++;
}
2001-02-23 20:17:25 +03:00
static int getnum (lua_State *L, const l_char **pc) {
int res = 0;
2000-08-28 21:57:04 +04:00
int sig = 1;
skip(pc);
2001-02-23 20:17:25 +03:00
if (**pc == l_c('.')) {
2000-08-31 17:29:47 +04:00
res = (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
}
2001-02-23 20:17:25 +03:00
else if (**pc == l_c('-')) {
2000-08-28 21:57:04 +04:00
sig = -1;
(*pc)++;
}
2001-02-23 20:17:25 +03:00
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - l_c('0');
2000-08-31 17:29:47 +04:00
return sig*res;
}
2001-02-23 20:17:25 +03:00
static const l_char *getname (l_char *buff, const l_char **pc) {
int i = 0;
skip(pc);
2001-02-23 20:17:25 +03:00
while (**pc != l_c('\0') && !strchr(delimits, **pc))
buff[i++] = *(*pc)++;
2001-02-23 20:17:25 +03:00
buff[i] = l_c('\0');
return buff;
}
#define EQ(s1) (strcmp(s1, inst) == 0)
2000-08-31 17:29:47 +04:00
#define getnum ((getnum)(L, &pc))
2000-08-28 21:57:04 +04:00
#define getname ((getname)(buff, &pc))
2000-08-28 21:57:04 +04:00
static int testC (lua_State *L) {
2001-02-23 20:17:25 +03:00
l_char buff[30];
const l_char *pc = luaL_check_string(L, 1);
for (;;) {
2001-02-23 20:17:25 +03:00
const l_char *inst = getname;
if EQ(l_s("")) return 0;
else if EQ(l_s("isnumber")) {
lua_pushnumber(L, lua_isnumber(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("isstring")) {
lua_pushnumber(L, lua_isstring(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("istable")) {
lua_pushnumber(L, lua_istable(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("iscfunction")) {
lua_pushnumber(L, lua_iscfunction(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("isfunction")) {
lua_pushnumber(L, lua_isfunction(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("isuserdata")) {
lua_pushnumber(L, lua_isuserdata(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("isnil")) {
lua_pushnumber(L, lua_isnil(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("isnull")) {
lua_pushnumber(L, lua_isnull(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("tonumber")) {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("tostring")) {
const l_char *s = lua_tostring(L, getnum);
lua_pushstring(L, s);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("tonumber")) {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("strlen")) {
lua_pushnumber(L, lua_strlen(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("tocfunction")) {
lua_pushcfunction(L, lua_tocfunction(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("return")) {
2000-08-28 21:57:04 +04:00
return getnum;
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("gettop")) {
2000-08-31 17:29:47 +04:00
lua_pushnumber(L, lua_gettop(L));
2000-08-28 21:57:04 +04:00
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("settop")) {
2000-08-28 21:57:04 +04:00
lua_settop(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("pop")) {
lua_pop(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("pushnum")) {
2000-08-28 21:57:04 +04:00
lua_pushnumber(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("pushvalue")) {
2000-09-05 23:33:32 +04:00
lua_pushvalue(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("remove")) {
2000-09-05 23:33:32 +04:00
lua_remove(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("insert")) {
lua_insert(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("gettable")) {
lua_gettable(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("settable")) {
lua_settable(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("next")) {
2000-09-05 23:33:32 +04:00
lua_next(L, -2);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("concat")) {
2000-09-05 23:33:32 +04:00
lua_concat(L, getnum);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("lessthan")) {
int a = getnum;
if (lua_lessthan(L, a, getnum))
lua_pushnumber(L, 1);
else
lua_pushnil(L);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("rawcall")) {
2000-08-28 21:57:04 +04:00
int narg = getnum;
int nres = getnum;
2000-09-14 18:09:31 +04:00
lua_rawcall(L, narg, nres);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("call")) {
2000-09-25 20:22:42 +04:00
int narg = getnum;
int nres = getnum;
lua_call(L, narg, nres);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("dostring")) {
2000-09-25 20:22:42 +04:00
lua_dostring(L, luaL_check_string(L, getnum));
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("settagmethod")) {
int tag = getnum;
2001-02-23 20:17:25 +03:00
const l_char *event = getname;
lua_settagmethod(L, tag, event);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("gettagmethod")) {
int tag = getnum;
2001-02-23 20:17:25 +03:00
const l_char *event = getname;
lua_gettagmethod(L, tag, event);
}
2001-02-23 20:17:25 +03:00
else if EQ(l_s("type")) {
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
}
2001-02-23 20:17:25 +03:00
else luaL_verror(L, l_s("unknown instruction %.30s"), buff);
}
2000-08-28 21:57:04 +04:00
return 0;
}
/* }====================================================== */
static const struct luaL_reg tests_funcs[] = {
2001-02-23 20:17:25 +03:00
{l_s("hash"), hash_query},
{l_s("limits"), get_limits},
{l_s("listcode"), listcode},
{l_s("liststrings"), liststrings},
{l_s("listlocals"), listlocals},
{l_s("loadlib"), loadlib},
{l_s("querystr"), string_query},
{l_s("querytab"), table_query},
{l_s("testC"), testC},
{l_s("ref"), tref},
{l_s("getref"), getref},
{l_s("unref"), unref},
{l_s("d2s"), d2s},
{l_s("s2d"), s2d},
{l_s("newuserdata"), newuserdata},
{l_s("udataval"), udataval},
{l_s("newtag"), newtag},
{l_s("doonnewstack"), doonnewstack},
{l_s("newstate"), newstate},
{l_s("closestate"), closestate},
{l_s("doremote"), doremote},
{l_s("settagmethod"), settagmethod},
{l_s("equal"), equal},
{l_s("totalmem"), mem_query}
};
void luaB_opentests (lua_State *L) {
*((int **)L) = &islocked; /* init lock */
lua_state = L; /* keep first state to be opened */
/* open lib in a new table */
lua_newtable(L);
lua_getglobals(L);
2000-09-05 23:33:32 +04:00
lua_pushvalue(L, -2);
lua_setglobals(L);
luaL_openl(L, tests_funcs); /* open functions inside new table */
lua_setglobals(L); /* restore old table of globals */
2001-02-23 20:17:25 +03:00
lua_setglobal(L, l_s("T")); /* set new table as global T */
}
#endif