1999-12-15 02:31:20 +08:00
|
|
|
/*
|
2001-12-06 04:15:18 +08:00
|
|
|
** $Id: ltests.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
|
1999-12-15 02:31:20 +08:00
|
|
|
** Internal Module for Debugging of the Lua Implementation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2001-02-07 00:01:29 +08:00
|
|
|
#include <limits.h>
|
2000-04-07 01:35:23 +08:00
|
|
|
#include <stdio.h>
|
1999-12-15 02:31:20 +08:00
|
|
|
#include <stdlib.h>
|
2000-02-22 02:30:06 +08:00
|
|
|
#include <string.h>
|
1999-12-15 02:31:20 +08:00
|
|
|
|
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
2000-05-23 02:44:46 +08:00
|
|
|
#include "lcode.h"
|
2000-08-09 02:26:05 +08:00
|
|
|
#include "ldebug.h"
|
2000-05-11 00:33:20 +08:00
|
|
|
#include "ldo.h"
|
2000-06-29 01:06:07 +08:00
|
|
|
#include "lfunc.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lmem.h"
|
2000-04-07 01:35:23 +08:00
|
|
|
#include "lopcodes.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
1999-12-24 02:19:57 +08:00
|
|
|
#include "luadebug.h"
|
2000-09-14 22:09:31 +08:00
|
|
|
#include "lualib.h"
|
1999-12-15 02:31:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-10-31 00:29:59 +08:00
|
|
|
** The whole module only makes sense with LUA_DEBUG on
|
1999-12-15 02:31:20 +08:00
|
|
|
*/
|
2000-10-31 00:29:59 +08:00
|
|
|
#ifdef LUA_DEBUG
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2001-02-07 00:01:29 +08:00
|
|
|
|
2001-02-02 23:13:05 +08:00
|
|
|
lua_State *lua_state = NULL;
|
|
|
|
|
|
|
|
int islocked = 0;
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static void setnameval (lua_State *L, const char *name, int val) {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_pushnumber(L, val);
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_settable(L, -3);
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-07 00:01:29 +08:00
|
|
|
/*
|
|
|
|
** {======================================================================
|
|
|
|
** 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-09-01 03:46:07 +08:00
|
|
|
#define blocksize(b) (cast(size_t *, b) - HEADER/sizeof(size_t))
|
2001-02-07 00:01:29 +08:00
|
|
|
|
|
|
|
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-09-01 03:46:07 +08:00
|
|
|
lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
2001-02-07 00:01:29 +08:00
|
|
|
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;
|
|
|
|
}
|
2001-02-22 00:51:25 +08:00
|
|
|
else if (memdebug_total+size-oldsize > memdebug_memlimit)
|
2001-02-07 00:01:29 +08:00
|
|
|
return NULL; /* to test memory allocation errors */
|
|
|
|
else {
|
2001-08-31 04:55:22 +08:00
|
|
|
void *newblock;
|
2001-02-07 00:01:29 +08:00
|
|
|
int i;
|
|
|
|
size_t realsize = HEADER+size+MARKSIZE;
|
|
|
|
if (realsize < size) return NULL; /* overflow! */
|
2001-08-31 04:55:22 +08:00
|
|
|
newblock = malloc(realsize); /* alloc a new block */
|
2001-02-07 00:01:29 +08:00
|
|
|
if (newblock == NULL) return NULL;
|
|
|
|
if (oldsize > size) oldsize = size;
|
|
|
|
if (block) {
|
2001-09-01 03:46:07 +08:00
|
|
|
memcpy(cast(char *, newblock)+HEADER, block, oldsize);
|
2001-02-07 00:01:29 +08:00
|
|
|
freeblock(block); /* erase (and check) old copy */
|
|
|
|
}
|
|
|
|
/* initialize new part of the block with something `weird' */
|
2001-09-01 03:46:07 +08:00
|
|
|
memset(cast(char *, newblock)+HEADER+oldsize, -MARK, size-oldsize);
|
2001-02-07 00:01:29 +08:00
|
|
|
memdebug_total += size;
|
|
|
|
if (memdebug_total > memdebug_maxmem)
|
|
|
|
memdebug_maxmem = memdebug_total;
|
|
|
|
memdebug_numblocks++;
|
2001-09-01 03:46:07 +08:00
|
|
|
*cast(size_t *, newblock) = size;
|
2001-02-07 00:01:29 +08:00
|
|
|
for (i=0;i<MARKSIZE;i++)
|
2001-09-01 03:46:07 +08:00
|
|
|
*(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
|
|
|
|
return cast(char *, newblock)+HEADER;
|
2001-02-07 00:01:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* }====================================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Disassembler
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static char *buildop (Proto *p, int pc, char *buff) {
|
2000-06-29 01:06:07 +08:00
|
|
|
Instruction i = p->code[pc];
|
2000-05-23 02:44:46 +08:00
|
|
|
OpCode o = GET_OPCODE(i);
|
2001-11-29 04:13:13 +08:00
|
|
|
const char *name = luaP_opnames[o];
|
|
|
|
sprintf(buff, "%4d - ", pc);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (getOpMode(o)) {
|
|
|
|
case iABC:
|
2001-11-29 04:13:13 +08:00
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
|
2001-06-06 02:17:01 +08:00
|
|
|
GETARG_A(i), GETARG_B(i), GETARG_C(i));
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2001-06-06 02:17:01 +08:00
|
|
|
case iABc:
|
2001-11-29 04:13:13 +08:00
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bc(i));
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2001-06-06 02:17:01 +08:00
|
|
|
case iAsBc:
|
2001-11-29 04:13:13 +08:00
|
|
|
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBc(i));
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
return buff;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
2000-05-23 02:44:46 +08:00
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int listcode (lua_State *L) {
|
2000-06-29 01:06:07 +08:00
|
|
|
int pc;
|
2000-04-07 01:35:23 +08:00
|
|
|
Proto *p;
|
2000-10-05 20:14:08 +08:00
|
|
|
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-29 04:13:13 +08:00
|
|
|
1, "Lua function expected");
|
2001-10-03 00:45:03 +08:00
|
|
|
p = clvalue(luaA_index(L, 1))->l.p;
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_newtable(L);
|
2001-11-29 04:13:13 +08:00
|
|
|
setnameval(L, "maxstack", p->maxstacksize);
|
|
|
|
setnameval(L, "numparams", p->numparams);
|
2001-01-16 00:13:24 +08:00
|
|
|
for (pc=0; pc<p->sizecode; pc++) {
|
2001-11-29 04:13:13 +08:00
|
|
|
char buff[100];
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L, pc+1);
|
2001-06-06 02:17:01 +08:00
|
|
|
lua_pushstring(L, buildop(p, pc, buff));
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_settable(L, -3);
|
2001-01-16 00:13:24 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
2000-06-03 03:10:01 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static int listk (lua_State *L) {
|
2000-06-03 03:10:01 +08:00
|
|
|
Proto *p;
|
|
|
|
int i;
|
2000-10-05 20:14:08 +08:00
|
|
|
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-29 04:13:13 +08:00
|
|
|
1, "Lua function expected");
|
2001-10-03 00:45:03 +08:00
|
|
|
p = clvalue(luaA_index(L, 1))->l.p;
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_newtable(L);
|
2001-06-06 02:17:01 +08:00
|
|
|
for (i=0; i<p->sizek; i++) {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L, i+1);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaA_pushobject(L, p->k+i);
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_settable(L, -3);
|
2000-06-03 03:10:01 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
2000-06-03 03:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int listlocals (lua_State *L) {
|
2000-06-29 01:06:07 +08:00
|
|
|
Proto *p;
|
2000-08-29 01:57:04 +08:00
|
|
|
int pc = luaL_check_int(L, 2) - 1;
|
|
|
|
int i = 0;
|
2001-11-29 04:13:13 +08:00
|
|
|
const char *name;
|
2000-10-05 20:14:08 +08:00
|
|
|
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
2001-11-29 04:13:13 +08:00
|
|
|
1, "Lua function expected");
|
2001-10-03 00:45:03 +08:00
|
|
|
p = clvalue(luaA_index(L, 1))->l.p;
|
2000-08-29 01:57:04 +08:00
|
|
|
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
return i-1;
|
2000-06-29 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2001-02-14 00:52:01 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int get_limits (lua_State *L) {
|
|
|
|
lua_newtable(L);
|
2001-11-29 04:13:13 +08:00
|
|
|
setnameval(L, "BITS_INT", BITS_INT);
|
|
|
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
|
|
|
setnameval(L, "MAXLOCALS", MAXLOCALS);
|
|
|
|
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
|
|
|
setnameval(L, "MAXSTACK", MAXSTACK);
|
|
|
|
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
2000-04-07 01:35:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int mem_query (lua_State *L) {
|
2001-12-21 05:27:12 +08:00
|
|
|
if (lua_isnone(L, 1)) {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L, memdebug_total);
|
|
|
|
lua_pushnumber(L, memdebug_numblocks);
|
|
|
|
lua_pushnumber(L, memdebug_maxmem);
|
2000-10-02 22:47:43 +08:00
|
|
|
return 3;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
memdebug_memlimit = luaL_check_int(L, 1);
|
|
|
|
return 0;
|
2000-08-05 03:38:35 +08:00
|
|
|
}
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int hash_query (lua_State *L) {
|
2001-12-21 05:27:12 +08:00
|
|
|
if (lua_isnone(L, 2)) {
|
2001-12-06 04:15:18 +08:00
|
|
|
luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
|
2001-06-16 04:36:57 +08:00
|
|
|
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->tsv.hash);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
else {
|
2001-01-30 01:17:26 +08:00
|
|
|
TObject *o = luaA_index(L, 1);
|
2001-10-26 03:14:14 +08:00
|
|
|
Table *t;
|
2001-12-06 04:15:18 +08:00
|
|
|
luaL_check_type(L, 2, LUA_TTABLE);
|
2000-08-29 01:57:04 +08:00
|
|
|
t = hvalue(luaA_index(L, 2));
|
2001-07-06 04:31:14 +08:00
|
|
|
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int table_query (lua_State *L) {
|
2001-10-26 03:14:14 +08:00
|
|
|
const Table *t;
|
2000-08-29 01:57:04 +08:00
|
|
|
int i = luaL_opt_int(L, 2, -1);
|
2001-12-06 04:15:18 +08:00
|
|
|
luaL_check_type(L, 1, LUA_TTABLE);
|
2000-08-29 01:57:04 +08:00
|
|
|
t = hvalue(luaA_index(L, 1));
|
1999-12-15 02:31:20 +08:00
|
|
|
if (i == -1) {
|
2001-10-26 03:14:14 +08:00
|
|
|
lua_pushnumber(L, t->sizearray);
|
|
|
|
lua_pushnumber(L, sizenode(t));
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L, t->firstfree - t->node);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-10-26 03:14:14 +08: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 22:42:41 +08:00
|
|
|
if (ttype(val(node(t, i))) != LUA_TNIL ||
|
2001-06-26 21:20:45 +08:00
|
|
|
ttype(key(node(t, i))) == LUA_TNIL ||
|
|
|
|
ttype(key(node(t, i))) == LUA_TNUMBER) {
|
|
|
|
luaA_pushobject(L, key(node(t, i)));
|
2001-04-11 22:42:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pushstring(L, "<undef>");
|
2001-11-30 04:21:46 +08:00
|
|
|
luaA_pushobject(L, val(&t->node[i]));
|
2001-10-26 03:14:14 +08:00
|
|
|
if (t->node[i].next)
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L, t->node[i].next - t->node);
|
|
|
|
else
|
2001-10-26 03:14:14 +08:00
|
|
|
lua_pushnil(L);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-10-26 03:14:14 +08:00
|
|
|
return 3;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int string_query (lua_State *L) {
|
2001-06-07 02:00:19 +08:00
|
|
|
stringtable *tb = &G(L)->strt;
|
2000-08-29 01:57:04 +08:00
|
|
|
int s = luaL_opt_int(L, 2, 0) - 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
if (s==-1) {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L ,tb->nuse);
|
|
|
|
lua_pushnumber(L ,tb->size);
|
|
|
|
return 2;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-05-11 00:33:20 +08:00
|
|
|
else if (s < tb->size) {
|
|
|
|
TString *ts;
|
2000-08-29 01:57:04 +08:00
|
|
|
int n = 0;
|
2001-06-16 04:36:57 +08:00
|
|
|
for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
|
2001-01-18 23:59:09 +08:00
|
|
|
setsvalue(L->top, ts);
|
2000-05-11 00:33:20 +08:00
|
|
|
incr_top;
|
2000-08-29 01:57:04 +08:00
|
|
|
n++;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return n;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 0;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int tref (lua_State *L) {
|
2001-04-18 01:35:54 +08:00
|
|
|
int level = lua_gettop(L);
|
2001-11-07 05:41:43 +08:00
|
|
|
int lock = luaL_opt_int(L, 2, 1);
|
2001-10-27 01:33:30 +08:00
|
|
|
luaL_check_any(L, 1);
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_pushvalue(L, 1);
|
2001-11-07 05:41:43 +08:00
|
|
|
lua_pushnumber(L, lua_ref(L, lock));
|
2001-04-18 01:35:54 +08:00
|
|
|
assert(lua_gettop(L) == level+1); /* +1 for result */
|
2000-08-30 03:05:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getref (lua_State *L) {
|
2001-04-18 01:35:54 +08:00
|
|
|
int level = lua_gettop(L);
|
2001-10-18 05:12:57 +08:00
|
|
|
lua_getref(L, luaL_check_int(L, 1));
|
|
|
|
assert(lua_gettop(L) == level+1);
|
|
|
|
return 1;
|
2000-08-30 03:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unref (lua_State *L) {
|
2001-04-18 01:35:54 +08:00
|
|
|
int level = lua_gettop(L);
|
2000-08-30 03:05:11 +08:00
|
|
|
lua_unref(L, luaL_check_int(L, 1));
|
2001-04-18 01:35:54 +08:00
|
|
|
assert(lua_gettop(L) == level);
|
2000-08-30 03:05:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-06 04:15:18 +08:00
|
|
|
static int eventtable (lua_State *L) {
|
|
|
|
luaL_check_any(L, 1);
|
2001-12-21 05:27:12 +08:00
|
|
|
if (lua_isnone(L, 2))
|
2001-12-06 04:15:18 +08:00
|
|
|
lua_geteventtable(L, 1);
|
|
|
|
else {
|
|
|
|
lua_settop(L, 2);
|
|
|
|
luaL_check_type(L, 2, LUA_TTABLE);
|
|
|
|
lua_seteventtable(L, 1);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int newuserdata (lua_State *L) {
|
2001-06-07 02:00:19 +08:00
|
|
|
size_t size = luaL_check_int(L, 1);
|
2001-11-29 04:13:13 +08:00
|
|
|
char *p = cast(char *, lua_newuserdata(L, size));
|
|
|
|
while (size--) *p++ = '\0';
|
2001-06-07 02:00:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int newuserdatabox (lua_State *L) {
|
2001-09-01 03:46:07 +08:00
|
|
|
lua_newuserdatabox(L, cast(void *, luaL_check_int(L, 1)));
|
2001-06-07 02:00:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
|
|
|
|
static int udataval (lua_State *L) {
|
2001-12-06 04:15:18 +08:00
|
|
|
luaL_check_type(L, 1, LUA_TUSERDATA);
|
2001-09-01 03:46:07 +08:00
|
|
|
lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
|
2000-08-30 03:05:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-23 02:01:38 +08:00
|
|
|
|
|
|
|
static int doonnewstack (lua_State *L) {
|
2001-03-10 02:05:05 +08:00
|
|
|
lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
|
2001-01-23 02:01:38 +08:00
|
|
|
if (L1 == NULL) return 0;
|
2001-09-01 03:46:07 +08:00
|
|
|
*cast(int **, L1) = &islocked; /* initialize the lock */
|
2001-01-23 02:01:38 +08:00
|
|
|
lua_dostring(L1, luaL_check_string(L, 2));
|
|
|
|
lua_pushnumber(L, 1);
|
|
|
|
lua_close(L1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-14 00:52:01 +08:00
|
|
|
static int s2d (lua_State *L) {
|
2001-09-01 03:46:07 +08:00
|
|
|
lua_pushnumber(L, *cast(double *, luaL_check_string(L, 1)));
|
2001-02-14 00:52:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int d2s (lua_State *L) {
|
|
|
|
double d = luaL_check_number(L, 1);
|
2001-11-29 04:13:13 +08:00
|
|
|
lua_pushlstring(L, cast(char *, &d), sizeof(d));
|
2001-02-14 00:52:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int newstate (lua_State *L) {
|
2001-03-10 02:05:05 +08:00
|
|
|
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
2001-02-02 23:13:05 +08:00
|
|
|
if (L1) {
|
2001-09-01 03:46:07 +08:00
|
|
|
*cast(int **, L1) = &islocked; /* initialize the lock */
|
2001-02-22 00:51:25 +08:00
|
|
|
lua_pushnumber(L, (unsigned long)L1);
|
2001-02-02 23:13:05 +08:00
|
|
|
}
|
2000-08-30 03:05:11 +08:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-09-14 22:09:31 +08:00
|
|
|
static int loadlib (lua_State *L) {
|
2001-09-01 03:46:07 +08:00
|
|
|
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
|
2001-03-07 04:09:38 +08: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 22:09:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-08-30 03:05:11 +08:00
|
|
|
static int closestate (lua_State *L) {
|
2001-09-01 03:46:07 +08:00
|
|
|
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
|
2001-02-22 00:51:25 +08:00
|
|
|
lua_close(L1);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L); /* close cannot unlock that */
|
2000-08-30 03:05:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int doremote (lua_State *L) {
|
|
|
|
lua_State *L1;
|
2001-11-29 04:13:13 +08:00
|
|
|
const char *code = luaL_check_string(L, 2);
|
2000-08-30 03:05:11 +08:00
|
|
|
int status;
|
2001-09-01 03:46:07 +08:00
|
|
|
L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
|
2000-08-30 03:05:11 +08:00
|
|
|
status = lua_dostring(L1, code);
|
|
|
|
if (status != 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushnumber(L, status);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i = 0;
|
2001-12-21 05:27:12 +08:00
|
|
|
while (!lua_isnone(L1, ++i))
|
2000-08-30 03:05:11 +08:00
|
|
|
lua_pushstring(L, lua_tostring(L1, i));
|
2001-03-07 04:09:38 +08:00
|
|
|
lua_pop(L1, i-1);
|
2000-08-30 03:05:11 +08:00
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-26 03:14:14 +08:00
|
|
|
|
|
|
|
static int log2_aux (lua_State *L) {
|
|
|
|
lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
|
|
|
|
return 1;
|
2000-08-30 03:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2001-02-23 02:59:59 +08:00
|
|
|
** function to test the API with C. It interprets a kind of assembler
|
2000-04-07 01:35:23 +08:00
|
|
|
** language with calls to the API, so the test can be driven by Lua code
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static const char *const delimits = " \t\n,;";
|
1999-12-15 02:31:20 +08:00
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static void skip (const char **pc) {
|
|
|
|
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static int getnum_aux (lua_State *L, const char **pc) {
|
1999-12-15 02:31:20 +08:00
|
|
|
int res = 0;
|
2000-08-29 01:57:04 +08:00
|
|
|
int sig = 1;
|
1999-12-15 02:31:20 +08:00
|
|
|
skip(pc);
|
2001-11-29 04:13:13 +08:00
|
|
|
if (**pc == '.') {
|
2001-09-01 03:46:07 +08:00
|
|
|
res = cast(int, lua_tonumber(L, -1));
|
2000-09-01 04:23:40 +08:00
|
|
|
lua_pop(L, 1);
|
2000-08-29 01:57:04 +08:00
|
|
|
(*pc)++;
|
2000-08-31 21:29:47 +08:00
|
|
|
return res;
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if (**pc == '-') {
|
2000-08-29 01:57:04 +08:00
|
|
|
sig = -1;
|
|
|
|
(*pc)++;
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
|
2000-08-31 21:29:47 +08:00
|
|
|
return sig*res;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
static const char *getname_aux (char *buff, const char **pc) {
|
1999-12-15 02:31:20 +08:00
|
|
|
int i = 0;
|
|
|
|
skip(pc);
|
2001-11-29 04:13:13 +08:00
|
|
|
while (**pc != '\0' && !strchr(delimits, **pc))
|
1999-12-15 02:31:20 +08:00
|
|
|
buff[i++] = *(*pc)++;
|
2001-11-29 04:13:13 +08:00
|
|
|
buff[i] = '\0';
|
1999-12-15 02:31:20 +08:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define EQ(s1) (strcmp(s1, inst) == 0)
|
|
|
|
|
2001-09-01 03:46:07 +08:00
|
|
|
#define getnum (getnum_aux(L, &pc))
|
|
|
|
#define getname (getname_aux(buff, &pc))
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
static int testC (lua_State *L) {
|
2001-11-29 04:13:13 +08:00
|
|
|
char buff[30];
|
|
|
|
const char *pc = luaL_check_string(L, 1);
|
1999-12-15 02:31:20 +08:00
|
|
|
for (;;) {
|
2001-11-29 04:13:13 +08:00
|
|
|
const char *inst = getname;
|
|
|
|
if EQ("") return 0;
|
|
|
|
else if EQ("isnumber") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_isnumber(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isstring") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_isstring(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("istable") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_istable(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("iscfunction") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_iscfunction(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isfunction") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_isfunction(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isuserdata") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_isuserdata(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isnil") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_isnil(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("isnull") {
|
2001-12-21 05:27:12 +08:00
|
|
|
lua_pushnumber(L, lua_isnone(L, getnum));
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tonumber") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_tonumber(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tostring") {
|
|
|
|
const char *s = lua_tostring(L, getnum);
|
2001-02-10 04:22:29 +08:00
|
|
|
lua_pushstring(L, s);
|
2000-10-07 03:29:26 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tonumber") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_tonumber(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("strlen") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushnumber(L, lua_strlen(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("tocfunction") {
|
2000-10-07 03:29:26 +08:00
|
|
|
lua_pushcfunction(L, lua_tocfunction(L, getnum));
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("return") {
|
2000-08-29 01:57:04 +08:00
|
|
|
return getnum;
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("gettop") {
|
2000-08-31 21:29:47 +08:00
|
|
|
lua_pushnumber(L, lua_gettop(L));
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("settop") {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_settop(L, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pop") {
|
2000-09-01 04:23:40 +08:00
|
|
|
lua_pop(L, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushnum") {
|
2000-08-29 01:57:04 +08:00
|
|
|
lua_pushnumber(L, getnum);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushvalue") {
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_pushvalue(L, getnum);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushcclosure") {
|
2001-10-18 05:12:57 +08:00
|
|
|
lua_pushcclosure(L, testC, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("pushupvalues") {
|
2001-10-18 05:12:57 +08:00
|
|
|
lua_pushupvalues(L);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("remove") {
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_remove(L, getnum);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("insert") {
|
2000-09-01 04:23:40 +08:00
|
|
|
lua_insert(L, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("gettable") {
|
2000-09-25 22:48:42 +08:00
|
|
|
lua_gettable(L, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("settable") {
|
2000-09-25 22:48:42 +08:00
|
|
|
lua_settable(L, getnum);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("next") {
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_next(L, -2);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("concat") {
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_concat(L, getnum);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("lessthan") {
|
2001-02-02 00:03:38 +08:00
|
|
|
int a = getnum;
|
2001-12-12 06:48:44 +08:00
|
|
|
lua_pushboolean(L, lua_lessthan(L, a, getnum));
|
2001-02-02 00:03:38 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("equal") {
|
2001-10-26 03:14:14 +08:00
|
|
|
int a = getnum;
|
2001-12-12 06:48:44 +08:00
|
|
|
lua_pushboolean(L, lua_equal(L, a, getnum));
|
2001-10-26 03:14:14 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("rawcall") {
|
2000-08-29 01:57:04 +08:00
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
2000-09-14 22:09:31 +08:00
|
|
|
lua_rawcall(L, narg, nres);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("call") {
|
2000-09-26 00:22:42 +08:00
|
|
|
int narg = getnum;
|
|
|
|
int nres = getnum;
|
|
|
|
lua_call(L, narg, nres);
|
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("dostring") {
|
2000-09-26 00:22:42 +08:00
|
|
|
lua_dostring(L, luaL_check_string(L, getnum));
|
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
else if EQ("seteventtable") {
|
|
|
|
lua_seteventtable(L, getnum);
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
else if EQ("geteventtable") {
|
|
|
|
lua_geteventtable(L, getnum);
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else if EQ("type") {
|
2001-12-06 04:15:18 +08:00
|
|
|
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2001-11-29 04:13:13 +08:00
|
|
|
else luaL_verror(L, "unknown instruction %.30s", buff);
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
2000-08-29 01:57:04 +08:00
|
|
|
return 0;
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
2000-04-07 01:35:23 +08:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
1999-12-15 02:31:20 +08:00
|
|
|
|
|
|
|
static const struct luaL_reg tests_funcs[] = {
|
2001-11-29 04:13:13 +08:00
|
|
|
{"hash", hash_query},
|
|
|
|
{"limits", get_limits},
|
|
|
|
{"listcode", listcode},
|
|
|
|
{"listk", listk},
|
|
|
|
{"listlocals", listlocals},
|
|
|
|
{"loadlib", loadlib},
|
|
|
|
{"querystr", string_query},
|
|
|
|
{"querytab", table_query},
|
|
|
|
{"testC", testC},
|
|
|
|
{"ref", tref},
|
|
|
|
{"getref", getref},
|
|
|
|
{"unref", unref},
|
|
|
|
{"d2s", d2s},
|
|
|
|
{"s2d", s2d},
|
2001-12-06 04:15:18 +08:00
|
|
|
{"eventtable", eventtable},
|
2001-11-29 04:13:13 +08:00
|
|
|
{"newuserdata", newuserdata},
|
|
|
|
{"newuserdatabox", newuserdatabox},
|
|
|
|
{"udataval", udataval},
|
|
|
|
{"doonnewstack", doonnewstack},
|
|
|
|
{"newstate", newstate},
|
|
|
|
{"closestate", closestate},
|
|
|
|
{"doremote", doremote},
|
|
|
|
{"log2", log2_aux},
|
|
|
|
{"totalmem", mem_query}
|
1999-12-15 02:31:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void luaB_opentests (lua_State *L) {
|
2001-09-01 03:46:07 +08:00
|
|
|
*cast(int **, L) = &islocked; /* init lock */
|
2001-02-02 23:13:05 +08:00
|
|
|
lua_state = L; /* keep first state to be opened */
|
|
|
|
/* open lib in a new table */
|
2000-08-30 03:05:11 +08:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_getglobals(L);
|
2000-09-06 03:33:32 +08:00
|
|
|
lua_pushvalue(L, -2);
|
2000-08-30 03:05:11 +08:00
|
|
|
lua_setglobals(L);
|
|
|
|
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
|
|
|
lua_setglobals(L); /* restore old table of globals */
|
2001-11-29 04:13:13 +08:00
|
|
|
lua_setglobal(L, "T"); /* set new table as global T */
|
1999-12-15 02:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|