1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** table.c
|
|
|
|
** Module to control static tables
|
|
|
|
*/
|
|
|
|
|
1995-11-03 18:30:50 +03:00
|
|
|
char *rcs_table="$Id: table.c,v 2.37 1995/10/26 14:21:56 roberto Exp roberto $";
|
1993-12-17 21:41:19 +03:00
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
/*#include <string.h>*/
|
1993-07-28 17:18:00 +04:00
|
|
|
|
1994-11-16 20:39:16 +03:00
|
|
|
#include "mem.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
#include "opcode.h"
|
1994-07-20 01:27:18 +04:00
|
|
|
#include "tree.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
#include "hash.h"
|
|
|
|
#include "table.h"
|
1995-05-02 22:43:03 +04:00
|
|
|
#include "inout.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
#include "lua.h"
|
1994-11-08 23:07:54 +03:00
|
|
|
#include "fallback.h"
|
1995-10-26 17:21:56 +03:00
|
|
|
#include "luadebug.h"
|
1993-07-28 17:18:00 +04:00
|
|
|
|
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
#define BUFFER_BLOCK 256
|
|
|
|
|
1995-09-16 00:47:53 +04:00
|
|
|
Symbol *lua_table = NULL;
|
1994-07-20 01:27:18 +04:00
|
|
|
static Word lua_ntable = 0;
|
1994-08-03 18:15:46 +04:00
|
|
|
static Long lua_maxsymbol = 0;
|
1994-07-20 01:27:18 +04:00
|
|
|
|
1995-09-16 00:47:53 +04:00
|
|
|
TaggedString **lua_constant = NULL;
|
1994-07-20 01:27:18 +04:00
|
|
|
static Word lua_nconstant = 0;
|
1994-08-03 18:15:46 +04:00
|
|
|
static Long lua_maxconstant = 0;
|
1994-07-20 01:27:18 +04:00
|
|
|
|
|
|
|
|
1995-05-16 23:23:55 +04:00
|
|
|
#define GARBAGE_BLOCK 1024
|
|
|
|
#define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
|
1994-04-21 02:07:57 +04:00
|
|
|
|
1994-11-16 19:03:48 +03:00
|
|
|
static void lua_nextvar (void);
|
1994-11-22 00:41:09 +03:00
|
|
|
static void setglobal (void);
|
|
|
|
static void getglobal (void);
|
1994-11-16 19:03:48 +03:00
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
/*
|
|
|
|
** Initialise symbol table with internal functions
|
|
|
|
*/
|
|
|
|
static void lua_initsymbol (void)
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
Word n;
|
1994-07-20 01:27:18 +04:00
|
|
|
lua_maxsymbol = BUFFER_BLOCK;
|
1994-11-16 20:39:16 +03:00
|
|
|
lua_table = newvector(lua_maxsymbol, Symbol);
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("next");
|
1994-11-02 23:29:09 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
|
1994-11-22 00:41:09 +03:00
|
|
|
n = luaI_findsymbolbyname("dofile");
|
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
|
|
|
n = luaI_findsymbolbyname("setglobal");
|
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
|
|
|
|
n = luaI_findsymbolbyname("getglobal");
|
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("nextvar");
|
1994-11-02 23:29:09 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("type");
|
1994-11-04 20:20:00 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("tonumber");
|
1994-11-04 20:20:00 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("print");
|
1994-11-02 23:29:09 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("dostring");
|
1994-11-02 23:29:09 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("setfallback");
|
1994-11-04 20:20:00 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
|
1994-11-15 00:40:14 +03:00
|
|
|
n = luaI_findsymbolbyname("error");
|
1994-11-08 23:07:54 +03:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
|
1994-07-20 01:27:18 +04:00
|
|
|
}
|
1994-04-21 02:07:57 +04:00
|
|
|
|
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
/*
|
|
|
|
** Initialise constant table with pre-defined constants
|
|
|
|
*/
|
|
|
|
void lua_initconstant (void)
|
|
|
|
{
|
|
|
|
lua_maxconstant = BUFFER_BLOCK;
|
1994-11-23 17:32:00 +03:00
|
|
|
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
1994-07-20 01:27:18 +04:00
|
|
|
}
|
1994-04-21 02:07:57 +04:00
|
|
|
|
1994-11-04 01:33:40 +03:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** Given a name, search it at symbol table and return its index. If not
|
1994-07-20 01:27:18 +04:00
|
|
|
** found, allocate it.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-21 00:20:36 +03:00
|
|
|
Word luaI_findsymbol (TreeNode *t)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-07-20 01:27:18 +04:00
|
|
|
if (lua_table == NULL)
|
|
|
|
lua_initsymbol();
|
1994-11-23 17:32:00 +03:00
|
|
|
if (t->varindex == NOT_USED)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-07-20 01:27:18 +04:00
|
|
|
if (lua_ntable == lua_maxsymbol)
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
if (lua_maxsymbol >= MAX_WORD)
|
1994-11-10 23:41:37 +03:00
|
|
|
lua_error("symbol table overflow");
|
1994-12-21 00:20:36 +03:00
|
|
|
lua_maxsymbol *= 2;
|
|
|
|
if (lua_maxsymbol >= MAX_WORD)
|
|
|
|
lua_maxsymbol = MAX_WORD;
|
1994-11-16 20:39:16 +03:00
|
|
|
lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
|
1994-07-20 01:27:18 +04:00
|
|
|
}
|
1994-11-15 00:40:14 +03:00
|
|
|
t->varindex = lua_ntable;
|
1994-11-02 23:29:09 +03:00
|
|
|
s_tag(lua_ntable) = LUA_T_NIL;
|
1994-07-20 01:27:18 +04:00
|
|
|
lua_ntable++;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
1994-11-15 00:40:14 +03:00
|
|
|
return t->varindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-21 00:20:36 +03:00
|
|
|
Word luaI_findsymbolbyname (char *name)
|
1994-11-15 00:40:14 +03:00
|
|
|
{
|
|
|
|
return luaI_findsymbol(lua_constcreate(name));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-07-20 01:27:18 +04:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1995-10-13 18:16:25 +03:00
|
|
|
** Given a tree node, check it is has a correspondent constant index. If not,
|
|
|
|
** allocate it.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-12-21 00:20:36 +03:00
|
|
|
Word luaI_findconstant (TreeNode *t)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-07-20 01:27:18 +04:00
|
|
|
if (lua_constant == NULL)
|
|
|
|
lua_initconstant();
|
1994-11-23 17:32:00 +03:00
|
|
|
if (t->constindex == NOT_USED)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-07-20 01:27:18 +04:00
|
|
|
if (lua_nconstant == lua_maxconstant)
|
|
|
|
{
|
1994-12-21 00:20:36 +03:00
|
|
|
if (lua_maxconstant >= MAX_WORD)
|
1994-11-10 23:41:37 +03:00
|
|
|
lua_error("constant table overflow");
|
1994-12-21 00:20:36 +03:00
|
|
|
lua_maxconstant *= 2;
|
|
|
|
if (lua_maxconstant >= MAX_WORD)
|
|
|
|
lua_maxconstant = MAX_WORD;
|
1994-11-23 17:32:00 +03:00
|
|
|
lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
|
1994-07-20 01:27:18 +04:00
|
|
|
}
|
1994-11-15 00:40:14 +03:00
|
|
|
t->constindex = lua_nconstant;
|
1994-11-23 17:32:00 +03:00
|
|
|
lua_constant[lua_nconstant] = &(t->ts);
|
1994-07-20 01:27:18 +04:00
|
|
|
lua_nconstant++;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
1994-11-15 00:40:14 +03:00
|
|
|
return t->constindex;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-13 18:16:25 +03:00
|
|
|
Word luaI_findconstantbyname (char *name)
|
|
|
|
{
|
|
|
|
return luaI_findconstant(lua_constcreate(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-04-21 02:07:57 +04:00
|
|
|
/*
|
|
|
|
** Traverse symbol table objects
|
|
|
|
*/
|
1995-10-17 14:58:41 +03:00
|
|
|
static char *lua_travsymbol (int (*fn)(Object *))
|
1994-04-21 02:07:57 +04:00
|
|
|
{
|
1994-11-11 17:00:08 +03:00
|
|
|
Word i;
|
1994-04-21 02:07:57 +04:00
|
|
|
for (i=0; i<lua_ntable; i++)
|
1995-10-17 14:58:41 +03:00
|
|
|
if (fn(&s_object(i)))
|
|
|
|
return luaI_nodebysymbol(i)->ts.str;
|
|
|
|
return NULL;
|
1994-04-21 02:07:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
|
|
|
** Mark an object if it is a string or a unmarked array.
|
|
|
|
*/
|
1995-10-17 14:58:41 +03:00
|
|
|
int lua_markobject (Object *o)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1994-11-23 17:32:00 +03:00
|
|
|
if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
|
|
|
|
tsvalue(o)->marked = 1;
|
1994-11-02 23:29:09 +03:00
|
|
|
else if (tag(o) == LUA_T_ARRAY)
|
1994-11-23 17:32:00 +03:00
|
|
|
lua_hashmark (avalue(o));
|
1995-10-17 14:58:41 +03:00
|
|
|
else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
|
|
|
|
&& !o->value.tf->marked)
|
1995-10-04 20:13:02 +03:00
|
|
|
o->value.tf->marked = 1;
|
1995-10-17 14:58:41 +03:00
|
|
|
return 0;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
1994-04-21 02:07:57 +04:00
|
|
|
|
1993-07-28 17:18:00 +04:00
|
|
|
/*
|
1994-04-21 02:07:57 +04:00
|
|
|
** Garbage collection.
|
|
|
|
** Delete all unused strings and arrays.
|
1993-07-28 17:18:00 +04:00
|
|
|
*/
|
1994-04-21 02:07:57 +04:00
|
|
|
void lua_pack (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-01-12 17:19:04 +03:00
|
|
|
static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
|
|
|
|
static Long nentity = 0; /* counter of new entities (strings and arrays) */
|
|
|
|
Long recovered = 0;
|
1994-11-17 16:58:57 +03:00
|
|
|
if (nentity++ < block) return;
|
|
|
|
lua_travstack(lua_markobject); /* mark stack objects */
|
|
|
|
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
|
|
|
luaI_travlock(lua_markobject); /* mark locked objects */
|
1995-10-04 20:13:02 +03:00
|
|
|
luaI_travfallbacks(lua_markobject); /* mark fallbacks */
|
1994-11-17 16:58:57 +03:00
|
|
|
recovered += lua_strcollector();
|
|
|
|
recovered += lua_hashcollector();
|
1995-10-04 20:13:02 +03:00
|
|
|
recovered += luaI_funccollector();
|
1994-11-17 16:58:57 +03:00
|
|
|
nentity = 0; /* reset counter */
|
1995-01-18 23:15:54 +03:00
|
|
|
block=(16*block-7*recovered)/12; /* adapt block size */
|
1994-11-17 16:58:57 +03:00
|
|
|
if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Internal function: return next global variable
|
|
|
|
*/
|
1994-11-16 19:03:48 +03:00
|
|
|
static void lua_nextvar (void)
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-10-17 14:58:41 +03:00
|
|
|
Word next;
|
1994-11-07 19:34:44 +03:00
|
|
|
lua_Object o = lua_getparam(1);
|
1994-12-16 18:55:04 +03:00
|
|
|
if (o == LUA_NOOBJECT)
|
1995-05-02 22:43:03 +04:00
|
|
|
lua_error("too few arguments to function `nextvar'");
|
1994-12-16 18:55:04 +03:00
|
|
|
if (lua_getparam(2) != LUA_NOOBJECT)
|
1995-05-02 22:43:03 +04:00
|
|
|
lua_error("too many arguments to function `nextvar'");
|
1994-11-07 19:34:44 +03:00
|
|
|
if (lua_isnil(o))
|
1995-10-17 14:58:41 +03:00
|
|
|
next = 0;
|
1994-11-07 19:34:44 +03:00
|
|
|
else if (!lua_isstring(o))
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-05-02 22:43:03 +04:00
|
|
|
lua_error("incorrect argument to function `nextvar'");
|
1994-11-10 23:41:37 +03:00
|
|
|
return; /* to avoid warnings */
|
1994-07-20 01:27:18 +04:00
|
|
|
}
|
1994-11-10 23:41:37 +03:00
|
|
|
else
|
1995-10-17 14:58:41 +03:00
|
|
|
next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
|
|
|
|
while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
|
|
|
|
if (next >= lua_ntable)
|
1994-07-20 01:27:18 +04:00
|
|
|
{
|
|
|
|
lua_pushnil();
|
|
|
|
lua_pushnil();
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
1994-07-20 01:27:18 +04:00
|
|
|
else
|
1993-07-28 17:18:00 +04:00
|
|
|
{
|
1995-10-17 14:58:41 +03:00
|
|
|
TreeNode *t = luaI_nodebysymbol(next);
|
1993-07-28 17:18:00 +04:00
|
|
|
Object name;
|
1994-11-02 23:29:09 +03:00
|
|
|
tag(&name) = LUA_T_STRING;
|
1995-10-17 14:58:41 +03:00
|
|
|
tsvalue(&name) = &(t->ts);
|
1994-11-07 19:34:44 +03:00
|
|
|
luaI_pushobject(&name);
|
1995-10-17 14:58:41 +03:00
|
|
|
luaI_pushobject(&s_object(next));
|
1993-07-28 17:18:00 +04:00
|
|
|
}
|
|
|
|
}
|
1994-11-22 00:41:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void setglobal (void)
|
|
|
|
{
|
|
|
|
lua_Object name = lua_getparam(1);
|
|
|
|
lua_Object value = lua_getparam(2);
|
|
|
|
if (!lua_isstring(name))
|
1995-05-02 22:43:03 +04:00
|
|
|
lua_error("incorrect argument to function `setglobal'");
|
1994-11-22 00:41:09 +03:00
|
|
|
lua_pushobject(value);
|
|
|
|
lua_storeglobal(lua_getstring(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void getglobal (void)
|
|
|
|
{
|
|
|
|
lua_Object name = lua_getparam(1);
|
|
|
|
if (!lua_isstring(name))
|
1995-05-02 22:43:03 +04:00
|
|
|
lua_error("incorrect argument to function `getglobal'");
|
1994-11-22 00:41:09 +03:00
|
|
|
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
|
|
|
}
|
1995-10-17 14:58:41 +03:00
|
|
|
|
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
static Object *functofind;
|
1995-10-17 14:58:41 +03:00
|
|
|
static int checkfunc (Object *o)
|
|
|
|
{
|
1995-10-26 17:21:56 +03:00
|
|
|
if (o->tag == LUA_T_FUNCTION)
|
|
|
|
return
|
|
|
|
((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
|
|
|
|
&& (functofind->value.tf == o->value.tf));
|
|
|
|
if (o->tag == LUA_T_CFUNCTION)
|
|
|
|
return
|
|
|
|
((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
|
|
|
|
&& (functofind->value.f == o->value.f));
|
|
|
|
return 0;
|
1995-10-17 14:58:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-26 17:21:56 +03:00
|
|
|
char *getobjname (lua_Object o, char **name)
|
|
|
|
{ /* try to find a name for given function */
|
|
|
|
functofind = luaI_Address(o);
|
1995-11-03 18:30:50 +03:00
|
|
|
if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
|
1995-10-26 17:21:56 +03:00
|
|
|
return "fallback";
|
1995-11-03 18:30:50 +03:00
|
|
|
else if ((*name = lua_travsymbol(checkfunc)) != NULL)
|
|
|
|
return "global";
|
1995-10-26 17:21:56 +03:00
|
|
|
else return "";
|
1995-10-17 14:58:41 +03:00
|
|
|
}
|
|
|
|
|