1999-12-14 21:31:20 +03:00
|
|
|
/*
|
2001-01-15 19:13:24 +03:00
|
|
|
** $Id: ltests.c,v 1.55 2000/12/28 12:55:41 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>
|
2000-04-06 21:35:23 +04:00
|
|
|
#include <stdio.h>
|
1999-12-14 21:31:20 +03:00
|
|
|
#include <stdlib.h>
|
2000-02-21 21:30:06 +03:00
|
|
|
#include <string.h>
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
2000-05-22 22:44:46 +04:00
|
|
|
#include "lcode.h"
|
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"
|
1999-12-23 21:19:57 +03:00
|
|
|
#include "luadebug.h"
|
2000-09-14 18:09:31 +04:00
|
|
|
#include "lualib.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
void luaB_opentests (lua_State *L);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static void setnameval (lua_State *L, const char *name, int val) {
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_pushnumber(L, val);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_settable(L, -3);
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Disassembler
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-06-21 21:05:49 +04:00
|
|
|
static const char *const instrname[NUM_OPCODES] = {
|
2001-01-15 19:13:24 +03:00
|
|
|
"RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
|
2000-05-22 22:44:46 +04:00
|
|
|
"PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
|
|
|
|
"GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
|
|
|
|
"CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
|
|
|
|
"ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
|
|
|
|
"JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
|
|
|
|
"JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
|
2000-06-26 23:28:31 +04:00
|
|
|
"LFORLOOP", "CLOSURE"
|
2000-05-22 22:44:46 +04:00
|
|
|
};
|
2000-04-06 21:35:23 +04:00
|
|
|
|
|
|
|
|
2001-01-15 19:13:24 +03:00
|
|
|
static void pushop (lua_State *L, Proto *p, int pc) {
|
2000-04-06 21:35:23 +04:00
|
|
|
char buff[100];
|
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);
|
|
|
|
const char *name = instrname[o];
|
2000-08-09 00:42:07 +04:00
|
|
|
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
|
2000-05-22 22:44:46 +04:00
|
|
|
switch ((enum Mode)luaK_opproperties[o].mode) {
|
|
|
|
case iO:
|
2000-10-20 20:39:03 +04:00
|
|
|
sprintf(buff+8, "%-12s", name);
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
|
|
|
case iU:
|
2000-06-28 21:06:07 +04:00
|
|
|
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
|
|
|
case iS:
|
2000-06-28 21:06:07 +04:00
|
|
|
sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
|
|
|
case iAB:
|
2000-06-28 21:06:07 +04:00
|
|
|
sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_pushstring(L, buff);
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
2000-05-22 22:44:46 +04:00
|
|
|
|
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;
|
2000-10-05 16:14:08 +04:00
|
|
|
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
|
|
|
1, "Lua function expected");
|
2000-08-28 21:57:04 +04:00
|
|
|
p = clvalue(luaA_index(L, 1))->f.l;
|
|
|
|
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++) {
|
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-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
2000-06-02 23:10:01 +04:00
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int liststrings (lua_State *L) {
|
2000-06-02 23:10:01 +04:00
|
|
|
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");
|
2000-08-28 21:57:04 +04:00
|
|
|
p = clvalue(luaA_index(L, 1))->f.l;
|
|
|
|
lua_newtable(L);
|
2000-12-28 15:55:41 +03:00
|
|
|
for (i=0; i<p->sizekstr; i++) {
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_pushnumber(L, i+1);
|
|
|
|
lua_pushstring(L, p->kstr[i]->str);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_settable(L, -3);
|
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;
|
2000-08-28 21:57:04 +04:00
|
|
|
int pc = luaL_check_int(L, 2) - 1;
|
|
|
|
int i = 0;
|
2000-06-28 21:06:07 +04:00
|
|
|
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");
|
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
|
|
|
}
|
|
|
|
|
2000-04-06 21:35:23 +04: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);
|
|
|
|
setnameval(L, "MAXARG_A", MAXARG_A);
|
|
|
|
setnameval(L, "MAXARG_B", MAXARG_B);
|
|
|
|
setnameval(L, "MAXARG_S", MAXARG_S);
|
|
|
|
setnameval(L, "MAXARG_U", MAXARG_U);
|
|
|
|
setnameval(L, "MAXLOCALS", MAXLOCALS);
|
|
|
|
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
|
|
|
setnameval(L, "MAXSTACK", MAXSTACK);
|
|
|
|
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
|
|
|
setnameval(L, "MAXVARSLH", MAXVARSLH);
|
|
|
|
setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
|
|
|
|
setnameval(L, "SIZE_A", SIZE_A);
|
|
|
|
setnameval(L, "SIZE_B", SIZE_B);
|
|
|
|
setnameval(L, "SIZE_OP", SIZE_OP);
|
|
|
|
setnameval(L, "SIZE_U", SIZE_U);
|
|
|
|
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) {
|
|
|
|
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-04 23:38:35 +04:00
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static int hash_query (lua_State *L) {
|
|
|
|
if (lua_isnull(L, 2)) {
|
2000-10-05 16:14:08 +04:00
|
|
|
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-08-28 21:57:04 +04:00
|
|
|
Hash *t;
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checktype(L, 2, LUA_TTABLE);
|
2000-08-28 21:57:04 +04:00
|
|
|
t = hvalue(luaA_index(L, 2));
|
|
|
|
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
2000-08-28 21:57:04 +04:00
|
|
|
t = hvalue(luaA_index(L, 1));
|
1999-12-14 21:31:20 +03:00
|
|
|
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;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else if (i < t->size) {
|
2000-08-28 21:57:04 +04:00
|
|
|
luaA_pushobject(L, &t->node[i].key);
|
|
|
|
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;
|
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
|
|
|
static int string_query (lua_State *L) {
|
|
|
|
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
|
|
|
|
int s = luaL_opt_int(L, 2, 0) - 1;
|
1999-12-14 21:31:20 +03:00
|
|
|
if (s==-1) {
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_pushnumber(L ,tb->nuse);
|
|
|
|
lua_pushnumber(L ,tb->size);
|
|
|
|
return 2;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
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) {
|
2000-10-05 16:14:08 +04:00
|
|
|
ttype(L->top) = LUA_TSTRING;
|
2000-05-10 20:33:20 +04:00
|
|
|
tsvalue(L->top) = ts;
|
|
|
|
incr_top;
|
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) {
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checkany(L, 1);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_pushvalue(L, 1);
|
2000-08-29 23:05:11 +04:00
|
|
|
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) {
|
2000-10-26 16:47:05 +04:00
|
|
|
if (lua_isnumber(L, 2))
|
|
|
|
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
|
|
|
|
else
|
|
|
|
lua_newuserdata(L, luaL_check_int(L, 1));
|
2000-08-29 23:05:11 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int udataval (lua_State *L) {
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
2000-08-29 23:05:11 +04:00
|
|
|
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int newstate (lua_State *L) {
|
2000-09-11 23:42:57 +04:00
|
|
|
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
2000-08-29 23:05:11 +04:00
|
|
|
if (L1)
|
|
|
|
lua_pushuserdata(L, 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)) {
|
|
|
|
case 'm': lua_mathlibopen(L1); break;
|
|
|
|
case 's': lua_strlibopen(L1); break;
|
|
|
|
case 'i': lua_iolibopen(L1); break;
|
|
|
|
case 'd': lua_dblibopen(L1); break;
|
|
|
|
case 'b': lua_baselibopen(L1); break;
|
|
|
|
default: luaL_argerror(L, 2, "invalid option");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:05:11 +04:00
|
|
|
static int closestate (lua_State *L) {
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
2000-08-29 23:05:11 +04:00
|
|
|
lua_close((lua_State *)lua_touserdata(L, 1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int doremote (lua_State *L) {
|
|
|
|
lua_State *L1;
|
|
|
|
const char *code = luaL_check_string(L, 2);
|
|
|
|
int status;
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
2000-08-29 23:05:11 +04:00
|
|
|
L1 = (lua_State *)lua_touserdata(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) {
|
2000-10-31 16:10:24 +03:00
|
|
|
int tag = luaL_check_int(L, 1);
|
|
|
|
const char *event = luaL_check_string(L, 2);
|
2000-10-03 00:10:55 +04:00
|
|
|
luaL_checkany(L, 3);
|
2000-10-31 16:10:24 +03:00
|
|
|
lua_gettagmethod(L, tag, event);
|
|
|
|
lua_pushvalue(L, 3);
|
|
|
|
lua_settagmethod(L, tag, event);
|
2000-08-29 23:05:11 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pushbool (lua_State *L, int b) {
|
|
|
|
if (b) lua_pushnumber(L, 1);
|
|
|
|
else lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int equal (lua_State *L) {
|
|
|
|
return pushbool(L, lua_equal(L, 1, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-04-06 21:35:23 +04: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,;";
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
static void skip (const char **pc) {
|
|
|
|
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
|
|
|
}
|
|
|
|
|
2000-08-31 17:29:47 +04:00
|
|
|
static int getnum (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);
|
2000-08-31 17:29:47 +04:00
|
|
|
if (**pc == '.') {
|
|
|
|
res = (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
|
|
|
}
|
|
|
|
else if (**pc == '-') {
|
|
|
|
sig = -1;
|
|
|
|
(*pc)++;
|
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
while (isdigit(**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
|
|
|
}
|
|
|
|
|
2000-08-28 21:57:04 +04:00
|
|
|
static const char *getname (char *buff, const char **pc) {
|
1999-12-14 21:31:20 +03:00
|
|
|
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)
|
|
|
|
|
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-04-06 21:35:23 +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);
|
1999-12-14 21:31:20 +03:00
|
|
|
for (;;) {
|
2000-08-28 21:57:04 +04:00
|
|
|
const char *inst = getname;
|
|
|
|
if EQ("") return 0;
|
2000-10-06 23:29:26 +04:00
|
|
|
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") {
|
|
|
|
lua_pushnumber(L, lua_isnull(L, getnum));
|
|
|
|
}
|
|
|
|
else if EQ("tonumber") {
|
|
|
|
lua_pushnumber(L, lua_tonumber(L, getnum));
|
|
|
|
}
|
|
|
|
else if EQ("tostring") {
|
|
|
|
lua_pushstring(L, lua_tostring(L, getnum));
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
else if EQ("return") {
|
|
|
|
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") {
|
|
|
|
lua_settop(L, getnum);
|
|
|
|
}
|
2000-09-01 00:23:40 +04:00
|
|
|
else if EQ("pop") {
|
|
|
|
lua_pop(L, getnum);
|
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
else if EQ("pushnum") {
|
2000-08-28 21:57:04 +04:00
|
|
|
lua_pushnumber(L, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-09-05 23:33:32 +04:00
|
|
|
else if EQ("pushvalue") {
|
|
|
|
lua_pushvalue(L, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-09-05 23:33:32 +04:00
|
|
|
else if EQ("remove") {
|
|
|
|
lua_remove(L, getnum);
|
2000-09-01 00:23:40 +04:00
|
|
|
}
|
|
|
|
else if EQ("insert") {
|
|
|
|
lua_insert(L, getnum);
|
|
|
|
}
|
2000-09-25 18:48:42 +04:00
|
|
|
else if EQ("gettable") {
|
|
|
|
lua_gettable(L, getnum);
|
|
|
|
}
|
|
|
|
else if EQ("settable") {
|
|
|
|
lua_settable(L, getnum);
|
|
|
|
}
|
2000-08-31 17:29:47 +04:00
|
|
|
else if EQ("next") {
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_next(L, -2);
|
|
|
|
}
|
|
|
|
else if EQ("concat") {
|
|
|
|
lua_concat(L, getnum);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-09-25 20:22:42 +04:00
|
|
|
else if EQ("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);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-09-25 20:22:42 +04:00
|
|
|
else if EQ("call") {
|
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
|
|
|
lua_call(L, narg, nres);
|
|
|
|
}
|
|
|
|
else if EQ("dostring") {
|
|
|
|
lua_dostring(L, luaL_check_string(L, getnum));
|
|
|
|
}
|
2000-10-05 17:00:17 +04:00
|
|
|
else if EQ("settagmethod") {
|
|
|
|
int tag = getnum;
|
|
|
|
const char *event = getname;
|
|
|
|
lua_settagmethod(L, tag, event);
|
|
|
|
}
|
|
|
|
else if EQ("gettagmethod") {
|
|
|
|
int tag = getnum;
|
|
|
|
const char *event = getname;
|
|
|
|
lua_gettagmethod(L, tag, event);
|
|
|
|
}
|
1999-12-23 21:19:57 +03:00
|
|
|
else if EQ("type") {
|
2000-10-03 00:10:55 +04:00
|
|
|
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-08-28 21:57:04 +04:00
|
|
|
else luaL_verror(L, "unknown instruction %.30s", 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
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
static const struct luaL_reg tests_funcs[] = {
|
2000-08-28 21:57:04 +04:00
|
|
|
{"hash", hash_query},
|
|
|
|
{"limits", get_limits},
|
|
|
|
{"listcode", listcode},
|
|
|
|
{"liststrings", liststrings},
|
|
|
|
{"listlocals", listlocals},
|
2000-09-14 18:09:31 +04:00
|
|
|
{"loadlib", loadlib},
|
2000-08-28 21:57:04 +04:00
|
|
|
{"querystr", string_query},
|
|
|
|
{"querytab", table_query},
|
|
|
|
{"testC", testC},
|
2000-08-29 23:05:11 +04:00
|
|
|
{"ref", tref},
|
|
|
|
{"getref", getref},
|
|
|
|
{"unref", unref},
|
|
|
|
{"newuserdata", newuserdata},
|
|
|
|
{"udataval", udataval},
|
|
|
|
{"newstate", newstate},
|
|
|
|
{"closestate", closestate},
|
|
|
|
{"doremote", doremote},
|
|
|
|
{"settagmethod", settagmethod},
|
|
|
|
{"equal", equal},
|
2000-08-28 21:57:04 +04:00
|
|
|
{"totalmem", mem_query}
|
1999-12-14 21:31:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void luaB_opentests (lua_State *L) {
|
2000-08-29 23:05:11 +04:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_getglobals(L);
|
2000-09-05 23:33:32 +04:00
|
|
|
lua_pushvalue(L, -2);
|
2000-08-29 23:05:11 +04:00
|
|
|
lua_setglobals(L);
|
|
|
|
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
|
|
|
lua_setglobals(L); /* restore old table of globals */
|
|
|
|
lua_setglobal(L, "T"); /* set new table as global T */
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|