1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** table.c
|
|
|
|
** Module to control static tables
|
|
|
|
*/
|
|
|
|
|
1995-05-17 01:23:58 +08:00
|
|
|
char *rcs_table="$Id: table.c,v 2.29 1995/05/02 18:43:03 roberto Exp roberto $";
|
1993-12-18 02:41:19 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
1994-11-17 01:39:16 +08:00
|
|
|
#include "mem.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
#include "opcode.h"
|
1994-07-20 05:27:18 +08:00
|
|
|
#include "tree.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
#include "hash.h"
|
|
|
|
#include "table.h"
|
1995-05-03 02:43:03 +08:00
|
|
|
#include "inout.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
#include "lua.h"
|
1994-11-09 04:07:54 +08:00
|
|
|
#include "fallback.h"
|
1993-07-28 21:18:00 +08:00
|
|
|
|
|
|
|
|
1994-07-20 05:27:18 +08:00
|
|
|
#define BUFFER_BLOCK 256
|
|
|
|
|
|
|
|
Symbol *lua_table;
|
|
|
|
static Word lua_ntable = 0;
|
1994-08-03 22:15:46 +08:00
|
|
|
static Long lua_maxsymbol = 0;
|
1994-07-20 05:27:18 +08:00
|
|
|
|
1994-11-23 22:32:00 +08:00
|
|
|
TaggedString **lua_constant;
|
1994-07-20 05:27:18 +08:00
|
|
|
static Word lua_nconstant = 0;
|
1994-08-03 22:15:46 +08:00
|
|
|
static Long lua_maxconstant = 0;
|
1994-07-20 05:27:18 +08:00
|
|
|
|
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
|
|
|
|
#define MAXFILE 20
|
|
|
|
char *lua_file[MAXFILE];
|
|
|
|
int lua_nfile;
|
|
|
|
|
1994-07-20 05:27:18 +08:00
|
|
|
#define GARBAGE_BLOCK 256
|
1994-11-17 21:58:57 +08:00
|
|
|
#define MIN_GARBAGE_BLOCK 10
|
1994-04-21 06:07:57 +08:00
|
|
|
|
1994-11-17 00:03:48 +08:00
|
|
|
static void lua_nextvar (void);
|
1994-11-22 05:41:09 +08:00
|
|
|
static void setglobal (void);
|
|
|
|
static void getglobal (void);
|
1994-11-17 00:03:48 +08:00
|
|
|
|
1994-07-20 05:27:18 +08:00
|
|
|
/*
|
|
|
|
** Initialise symbol table with internal functions
|
|
|
|
*/
|
|
|
|
static void lua_initsymbol (void)
|
|
|
|
{
|
1994-12-21 05:20:36 +08:00
|
|
|
Word n;
|
1994-07-20 05:27:18 +08:00
|
|
|
lua_maxsymbol = BUFFER_BLOCK;
|
1994-11-17 01:39:16 +08:00
|
|
|
lua_table = newvector(lua_maxsymbol, Symbol);
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("next");
|
1994-11-03 04:29:09 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
|
1994-11-22 05:41:09 +08: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 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("nextvar");
|
1994-11-03 04:29:09 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("type");
|
1994-11-05 01:20:00 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("tonumber");
|
1994-11-05 01:20:00 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("print");
|
1994-11-03 04:29:09 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("dostring");
|
1994-11-03 04:29:09 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("setfallback");
|
1994-11-05 01:20:00 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
|
1995-05-17 01:23:58 +08:00
|
|
|
n = luaI_findsymbolbyname("getstack");
|
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_getstack;
|
1994-11-15 05:40:14 +08:00
|
|
|
n = luaI_findsymbolbyname("error");
|
1994-11-09 04:07:54 +08:00
|
|
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
|
1994-07-20 05:27:18 +08:00
|
|
|
}
|
1994-04-21 06:07:57 +08:00
|
|
|
|
|
|
|
|
1994-07-20 05:27:18 +08:00
|
|
|
/*
|
|
|
|
** Initialise constant table with pre-defined constants
|
|
|
|
*/
|
|
|
|
void lua_initconstant (void)
|
|
|
|
{
|
|
|
|
lua_maxconstant = BUFFER_BLOCK;
|
1994-11-23 22:32:00 +08:00
|
|
|
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
1994-07-20 05:27:18 +08:00
|
|
|
}
|
1994-04-21 06:07:57 +08:00
|
|
|
|
1994-11-04 06:33:40 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** Given a name, search it at symbol table and return its index. If not
|
1994-07-20 05:27:18 +08:00
|
|
|
** found, allocate it.
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
1994-12-21 05:20:36 +08:00
|
|
|
Word luaI_findsymbol (TreeNode *t)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1994-07-20 05:27:18 +08:00
|
|
|
if (lua_table == NULL)
|
|
|
|
lua_initsymbol();
|
1994-11-23 22:32:00 +08:00
|
|
|
if (t->varindex == NOT_USED)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1994-07-20 05:27:18 +08:00
|
|
|
if (lua_ntable == lua_maxsymbol)
|
|
|
|
{
|
1994-12-21 05:20:36 +08:00
|
|
|
if (lua_maxsymbol >= MAX_WORD)
|
1994-11-11 04:41:37 +08:00
|
|
|
lua_error("symbol table overflow");
|
1994-12-21 05:20:36 +08:00
|
|
|
lua_maxsymbol *= 2;
|
|
|
|
if (lua_maxsymbol >= MAX_WORD)
|
|
|
|
lua_maxsymbol = MAX_WORD;
|
1994-11-17 01:39:16 +08:00
|
|
|
lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
|
1994-07-20 05:27:18 +08:00
|
|
|
}
|
1994-11-15 05:40:14 +08:00
|
|
|
t->varindex = lua_ntable;
|
1994-11-03 04:29:09 +08:00
|
|
|
s_tag(lua_ntable) = LUA_T_NIL;
|
1994-07-20 05:27:18 +08:00
|
|
|
lua_ntable++;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
1994-11-15 05:40:14 +08:00
|
|
|
return t->varindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-21 05:20:36 +08:00
|
|
|
Word luaI_findsymbolbyname (char *name)
|
1994-11-15 05:40:14 +08:00
|
|
|
{
|
|
|
|
return luaI_findsymbol(lua_constcreate(name));
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
1994-07-20 05:27:18 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
1994-07-20 05:27:18 +08:00
|
|
|
** Given a name, search it at constant table and return its index. If not
|
|
|
|
** found, allocate it.
|
|
|
|
** On error, return -1.
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
1994-12-21 05:20:36 +08:00
|
|
|
Word luaI_findconstant (TreeNode *t)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1994-07-20 05:27:18 +08:00
|
|
|
if (lua_constant == NULL)
|
|
|
|
lua_initconstant();
|
1994-11-23 22:32:00 +08:00
|
|
|
if (t->constindex == NOT_USED)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1994-07-20 05:27:18 +08:00
|
|
|
if (lua_nconstant == lua_maxconstant)
|
|
|
|
{
|
1994-12-21 05:20:36 +08:00
|
|
|
if (lua_maxconstant >= MAX_WORD)
|
1994-11-11 04:41:37 +08:00
|
|
|
lua_error("constant table overflow");
|
1994-12-21 05:20:36 +08:00
|
|
|
lua_maxconstant *= 2;
|
|
|
|
if (lua_maxconstant >= MAX_WORD)
|
|
|
|
lua_maxconstant = MAX_WORD;
|
1994-11-23 22:32:00 +08:00
|
|
|
lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
|
1994-07-20 05:27:18 +08:00
|
|
|
}
|
1994-11-15 05:40:14 +08:00
|
|
|
t->constindex = lua_nconstant;
|
1994-11-23 22:32:00 +08:00
|
|
|
lua_constant[lua_nconstant] = &(t->ts);
|
1994-07-20 05:27:18 +08:00
|
|
|
lua_nconstant++;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
1994-11-15 05:40:14 +08:00
|
|
|
return t->constindex;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-04-21 06:07:57 +08:00
|
|
|
/*
|
|
|
|
** Traverse symbol table objects
|
|
|
|
*/
|
|
|
|
void lua_travsymbol (void (*fn)(Object *))
|
|
|
|
{
|
1994-11-11 22:00:08 +08:00
|
|
|
Word i;
|
1994-04-21 06:07:57 +08:00
|
|
|
for (i=0; i<lua_ntable; i++)
|
|
|
|
fn(&s_object(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** Mark an object if it is a string or a unmarked array.
|
|
|
|
*/
|
|
|
|
void lua_markobject (Object *o)
|
|
|
|
{
|
1994-11-23 22:32:00 +08:00
|
|
|
if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
|
|
|
|
tsvalue(o)->marked = 1;
|
1994-11-03 04:29:09 +08:00
|
|
|
else if (tag(o) == LUA_T_ARRAY)
|
1994-11-23 22:32:00 +08:00
|
|
|
lua_hashmark (avalue(o));
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
1994-04-21 06:07:57 +08:00
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
1994-04-21 06:07:57 +08:00
|
|
|
** Garbage collection.
|
|
|
|
** Delete all unused strings and arrays.
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
1994-04-21 06:07:57 +08:00
|
|
|
void lua_pack (void)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1995-01-12 22:19:04 +08: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 21:58:57 +08: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 */
|
|
|
|
recovered += lua_strcollector();
|
|
|
|
recovered += lua_hashcollector();
|
|
|
|
nentity = 0; /* reset counter */
|
1995-01-19 04:15:54 +08:00
|
|
|
block=(16*block-7*recovered)/12; /* adapt block size */
|
1994-11-17 21:58:57 +08:00
|
|
|
if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Add a file name at file table, checking overflow. This function also set
|
|
|
|
** the external variable "lua_filename" with the function filename set.
|
1994-11-04 05:48:36 +08:00
|
|
|
** Return 0 on success or error message on error.
|
1993-07-28 21:18:00 +08:00
|
|
|
*/
|
1994-11-04 05:48:36 +08:00
|
|
|
char *lua_addfile (char *fn)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1994-11-10 02:11:47 +08:00
|
|
|
if (lua_nfile >= MAXFILE)
|
1994-11-04 05:48:36 +08:00
|
|
|
return "too many files";
|
1995-01-14 23:40:26 +08:00
|
|
|
if ((lua_file[lua_nfile++] = luaI_strdup (fn)) == NULL)
|
1994-11-04 05:48:36 +08:00
|
|
|
return "not enough memory";
|
|
|
|
return NULL;
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
|
1993-12-18 02:41:19 +08:00
|
|
|
/*
|
|
|
|
** Delete a file from file stack
|
|
|
|
*/
|
|
|
|
int lua_delfile (void)
|
|
|
|
{
|
1994-11-17 01:39:16 +08:00
|
|
|
luaI_free(lua_file[--lua_nfile]);
|
1993-12-18 02:41:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1993-07-28 21:18:00 +08:00
|
|
|
/*
|
|
|
|
** Return the last file name set.
|
|
|
|
*/
|
|
|
|
char *lua_filename (void)
|
|
|
|
{
|
|
|
|
return lua_file[lua_nfile-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Internal function: return next global variable
|
|
|
|
*/
|
1994-11-17 00:03:48 +08:00
|
|
|
static void lua_nextvar (void)
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1994-11-17 00:03:48 +08:00
|
|
|
char *varname;
|
|
|
|
TreeNode *next;
|
1994-11-08 00:34:44 +08:00
|
|
|
lua_Object o = lua_getparam(1);
|
1994-12-16 23:55:04 +08:00
|
|
|
if (o == LUA_NOOBJECT)
|
1995-05-03 02:43:03 +08:00
|
|
|
lua_error("too few arguments to function `nextvar'");
|
1994-12-16 23:55:04 +08:00
|
|
|
if (lua_getparam(2) != LUA_NOOBJECT)
|
1995-05-03 02:43:03 +08:00
|
|
|
lua_error("too many arguments to function `nextvar'");
|
1994-11-08 00:34:44 +08:00
|
|
|
if (lua_isnil(o))
|
1994-11-11 04:41:37 +08:00
|
|
|
varname = NULL;
|
1994-11-08 00:34:44 +08:00
|
|
|
else if (!lua_isstring(o))
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
1995-05-03 02:43:03 +08:00
|
|
|
lua_error("incorrect argument to function `nextvar'");
|
1994-11-11 04:41:37 +08:00
|
|
|
return; /* to avoid warnings */
|
1994-07-20 05:27:18 +08:00
|
|
|
}
|
1994-11-11 04:41:37 +08:00
|
|
|
else
|
|
|
|
varname = lua_getstring(o);
|
1994-07-20 05:27:18 +08:00
|
|
|
next = lua_varnext(varname);
|
|
|
|
if (next == NULL)
|
|
|
|
{
|
|
|
|
lua_pushnil();
|
|
|
|
lua_pushnil();
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
1994-07-20 05:27:18 +08:00
|
|
|
else
|
1993-07-28 21:18:00 +08:00
|
|
|
{
|
|
|
|
Object name;
|
1994-11-03 04:29:09 +08:00
|
|
|
tag(&name) = LUA_T_STRING;
|
1994-11-23 22:32:00 +08:00
|
|
|
tsvalue(&name) = &(next->ts);
|
1994-11-08 00:34:44 +08:00
|
|
|
luaI_pushobject(&name);
|
1994-11-17 00:03:48 +08:00
|
|
|
luaI_pushobject(&s_object(next->varindex));
|
1993-07-28 21:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
1994-11-22 05:41:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void setglobal (void)
|
|
|
|
{
|
|
|
|
lua_Object name = lua_getparam(1);
|
|
|
|
lua_Object value = lua_getparam(2);
|
|
|
|
if (!lua_isstring(name))
|
1995-05-03 02:43:03 +08:00
|
|
|
lua_error("incorrect argument to function `setglobal'");
|
1994-11-22 05:41:09 +08: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-03 02:43:03 +08:00
|
|
|
lua_error("incorrect argument to function `getglobal'");
|
1994-11-22 05:41:09 +08:00
|
|
|
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
|
|
|
}
|