new implementation for udata (again they are just void *);

new implementation for the API: most operations now do not disturb
structures lua2C and C2lua.
This commit is contained in:
Roberto Ierusalimschy 1997-06-09 14:28:14 -03:00
parent 5fdcfeb353
commit dd22ea4da5
7 changed files with 163 additions and 136 deletions

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_fallback="$Id: fallback.c,v 2.4 1997/04/07 14:48:53 roberto Exp roberto $"; char *rcs_fallback="$Id: fallback.c,v 2.5 1997/04/24 22:59:57 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -116,16 +116,20 @@ struct IM *luaI_IMtable = NULL;
static int IMtable_size = 0; static int IMtable_size = 0;
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */ static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
/* events in LUA_T_LINE are all allowed, since this is used as a
* 'placeholder' for "default" fallbacks
*/
static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */ static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */ {1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */ {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */ {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */ {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */ {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */ {1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */ {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
}; };
@ -186,6 +190,9 @@ void luaI_settag (int tag, TObject *o)
case LUA_T_ARRAY: case LUA_T_ARRAY:
o->value.a->htag = tag; o->value.a->htag = tag;
break; break;
case LUA_T_USERDATA:
o->value.ts->tag = tag;
break;
default: default:
luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]); luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
} }
@ -318,7 +325,7 @@ void luaI_setfallback (void)
break; break;
case 2: { /* old arith fallback */ case 2: { /* old arith fallback */
int i; int i;
oldfunc = *luaI_getim(LUA_T_USERDATA, IM_POW); oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func)); fillvalids(i, luaI_Address(func));
replace = typeFB; replace = typeFB;
@ -326,7 +333,7 @@ void luaI_setfallback (void)
} }
case 3: { /* old order fallback */ case 3: { /* old order fallback */
int i; int i;
oldfunc = *luaI_getim(LUA_T_USERDATA, IM_LT); oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func)); fillvalids(i, luaI_Address(func));
replace = typeFB; replace = typeFB;
@ -335,7 +342,7 @@ void luaI_setfallback (void)
default: { default: {
int e; int e;
if ((e = luaI_findstring(name, luaI_eventname)) >= 0) { if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
oldfunc = *luaI_getim(LUA_T_USERDATA, e); oldfunc = *luaI_getim(LUA_T_LINE, e);
fillvalids(e, luaI_Address(func)); fillvalids(e, luaI_Address(func));
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
} }

70
inout.c
View File

@ -5,7 +5,7 @@
** Also provides some predefined lua functions. ** Also provides some predefined lua functions.
*/ */
char *rcs_inout="$Id: inout.c,v 2.58 1997/04/15 17:32:47 roberto Exp roberto $"; char *rcs_inout="$Id: inout.c,v 2.59 1997/05/26 14:42:51 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -103,6 +103,7 @@ void lua_closestring (void)
} }
static int passresults (void) static int passresults (void)
{ {
int arg = 0; int arg = 0;
@ -112,6 +113,25 @@ static int passresults (void)
return arg-1; return arg-1;
} }
static void packresults (void)
{
int arg = 0;
lua_Object obj;
lua_Object table = lua_createtable();
while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
lua_pushobject(table);
lua_pushnumber(arg);
lua_pushobject(obj);
lua_rawsettable();
}
lua_pushobject(table);
lua_pushstring("n");
lua_pushnumber(arg-1);
lua_rawsettable();
lua_pushobject(table); /* final result */
}
/* /*
** Internal function: do a string ** Internal function: do a string
*/ */
@ -144,14 +164,8 @@ static char *tostring (lua_Object obj)
case LUA_T_CFUNCTION: case LUA_T_NIL: case LUA_T_CFUNCTION: case LUA_T_NIL:
return luaI_typenames[-ttype(o)]; return luaI_typenames[-ttype(o)];
case LUA_T_USERDATA: { case LUA_T_USERDATA: {
char *buff = luaI_buffer(100); char *buff = luaI_buffer(30);
int size = o->value.ts->size; sprintf(buff, "userdata: %p", o->value.ts->u.v);
int i;
strcpy(buff, "userdata: ");
if (size > 10) size = 10;
for (i=0; i<size; i++)
sprintf(buff+strlen(buff), "%.2X",
(int)(unsigned char)o->value.ts->str[i]);
return buff; return buff;
} }
default: return "<unknown object>"; default: return "<unknown object>";
@ -237,37 +251,37 @@ static void luatag (void)
lua_pushnumber(lua_tag(lua_getparam(1))); lua_pushnumber(lua_tag(lua_getparam(1)));
} }
#define MAXPARAMS 256
static int getnarg (lua_Object table)
{
lua_Object temp;
/* temp = table.n */
lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
}
static void luaI_call (void) static void luaI_call (void)
{ {
lua_Object f = lua_getparam(1); lua_Object f = lua_getparam(1);
lua_Object arg = lua_getparam(2); lua_Object arg = lua_getparam(2);
lua_Object temp, params[MAXPARAMS]; int withtable = (luaL_opt_string(3, NULL) != NULL);
int narg, i; int narg, i;
luaL_arg_check(lua_isfunction(f), 1, "function expected"); luaL_arg_check(lua_isfunction(f), 1, "function expected");
luaL_arg_check(lua_istable(arg), 2, "table expected"); luaL_arg_check(lua_istable(arg), 2, "table expected");
/* narg = arg.n */ narg = getnarg(arg);
lua_pushobject(arg); /* push arg[1...n] */
lua_pushstring("n");
temp = lua_getsubscript();
narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
/* read arg[1...n] */
for (i=0; i<narg; i++) { for (i=0; i<narg; i++) {
if (i>=MAXPARAMS) lua_Object temp;
lua_error("argument list too long in function `call'"); /* temp = arg[i+1] */
lua_pushobject(arg); lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
lua_pushnumber(i+1); if (narg == MAX_WORD && lua_isnil(temp))
params[i] = lua_getsubscript();
if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
narg = i;
break; break;
lua_pushobject(temp);
} }
}
/* push parameters and do the call */
for (i=0; i<narg; i++)
lua_pushobject(params[i]);
if (lua_callfunction(f)) if (lua_callfunction(f))
lua_error(NULL); lua_error(NULL);
else if (withtable)
packresults();
else else
passresults(); passresults();
} }

17
lua.h
View File

@ -2,7 +2,7 @@
** LUA - An Extensible Extension Language ** LUA - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br ** e-mail: lua@tecgraf.puc-rio.br
** $Id: lua.h,v 4.4 1997/05/26 14:42:51 roberto Exp roberto $ ** $Id: lua.h,v 4.5 1997/06/06 20:54:40 roberto Exp roberto $
*/ */
@ -16,6 +16,8 @@
#define LUA_NOOBJECT 0 #define LUA_NOOBJECT 0
#define LUA_ANYTAG (-1)
typedef void (*lua_CFunction) (void); typedef void (*lua_CFunction) (void);
typedef unsigned int lua_Object; typedef unsigned int lua_Object;
@ -31,8 +33,6 @@ int lua_dofile (char *filename); /* Out: returns */
int lua_dostring (char *string); /* Out: returns */ int lua_dostring (char *string); /* Out: returns */
int lua_callfunction (lua_Object f); int lua_callfunction (lua_Object f);
/* In: parameters; Out: returns */ /* In: parameters; Out: returns */
int lua_call (char *funcname);
/* In: parameters; Out: returns */
void lua_beginblock (void); void lua_beginblock (void);
void lua_endblock (void); void lua_endblock (void);
@ -52,17 +52,16 @@ int lua_isfunction (lua_Object object);
float lua_getnumber (lua_Object object); float lua_getnumber (lua_Object object);
char *lua_getstring (lua_Object object); char *lua_getstring (lua_Object object);
lua_CFunction lua_getcfunction (lua_Object object); lua_CFunction lua_getcfunction (lua_Object object);
void *lua_getbindata (lua_Object object);
int lua_getbindatasize (lua_Object object);
void lua_pushnil (void); void lua_pushnil (void);
void lua_pushnumber (float n); void lua_pushnumber (float n);
void lua_pushstring (char *s); void lua_pushstring (char *s);
void lua_pushcfunction (lua_CFunction fn); void lua_pushcfunction (lua_CFunction fn);
void lua_pushbindata (void *buff, int size, int tag);
void lua_pushusertag (void *u, int tag); void lua_pushusertag (void *u, int tag);
void lua_pushobject (lua_Object object); void lua_pushobject (lua_Object object);
lua_Object lua_pop (void);
lua_Object lua_getglobal (char *name); lua_Object lua_getglobal (char *name);
lua_Object lua_rawgetglobal (char *name); lua_Object lua_rawgetglobal (char *name);
void lua_setglobal (char *name); /* In: value */ void lua_setglobal (char *name); /* In: value */
@ -82,6 +81,8 @@ void lua_unref (int ref);
lua_Object lua_createtable (void); lua_Object lua_createtable (void);
lua_Object lua_getudata (void *u, int tag);
long lua_collectgarbage (long limit); long lua_collectgarbage (long limit);
@ -89,7 +90,9 @@ long lua_collectgarbage (long limit);
/* =============================================================== */ /* =============================================================== */
/* some useful macros */ /* some useful macros */
#define lua_pushref(ref) (lua_pushobject(lua_getref(ref))) #define lua_call(name) lua_callfunction(lua_getglobal(name))
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) #define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 4.5 1997/05/26 14:23:55 roberto Exp roberto $"; char *rcs_opcode="$Id: opcode.c,v 4.6 1997/06/06 20:54:40 roberto Exp roberto $";
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h> #include <stdio.h>
@ -54,11 +54,11 @@ static TObject *top = &initial_stack;
struct C_Lua_Stack { struct C_Lua_Stack {
StkId base; /* when Lua calls C or C calls Lua, points to */ StkId base; /* when Lua calls C or C calls Lua, points to */
/* the first slot after the last parameter. */ /* the first slot after the last parameter. */
int num; /* when Lua calls C, has the number of parameters; */ StkId lua2C; /* points to first element of "array" lua2C */
/* when C calls Lua, has the number of results. */ int num; /* size of "array" lua2C */
}; };
static struct C_Lua_Stack CLS_current = {0, 0}; static struct C_Lua_Stack CLS_current = {0, 0, 0};
static jmp_buf *errorJmp = NULL; /* current error recover point */ static jmp_buf *errorJmp = NULL; /* current error recover point */
@ -228,6 +228,13 @@ static lua_Object put_luaObjectonTop (void)
} }
lua_Object lua_pop (void)
{
checkCparams(1);
return put_luaObjectonTop();
}
/* /*
** call Line hook ** call Line hook
@ -235,7 +242,7 @@ static lua_Object put_luaObjectonTop (void)
static void lineHook (int line) static void lineHook (int line)
{ {
struct C_Lua_Stack oldCLS = CLS_current; struct C_Lua_Stack oldCLS = CLS_current;
StkId old_top = CLS_current.base = top-stack; StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
CLS_current.num = 0; CLS_current.num = 0;
(*lua_linehook)(line); (*lua_linehook)(line);
top = stack+old_top; top = stack+old_top;
@ -250,7 +257,7 @@ static void lineHook (int line)
static void callHook (StkId base, lua_Type type, int isreturn) static void callHook (StkId base, lua_Type type, int isreturn)
{ {
struct C_Lua_Stack oldCLS = CLS_current; struct C_Lua_Stack oldCLS = CLS_current;
StkId old_top = CLS_current.base = top-stack; StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
CLS_current.num = 0; CLS_current.num = 0;
if (isreturn) if (isreturn)
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0); (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
@ -278,6 +285,7 @@ static StkId callC (lua_CFunction func, StkId base)
StkId firstResult; StkId firstResult;
CLS_current.num = (top-stack) - base; CLS_current.num = (top-stack) - base;
/* incorporate parameters on the stack */ /* incorporate parameters on the stack */
CLS_current.lua2C = base;
CLS_current.base = base+CLS_current.num; /* == top-stack */ CLS_current.base = base+CLS_current.num; /* == top-stack */
if (lua_callhook) if (lua_callhook)
callHook(base, LUA_T_CMARK, 0); callHook(base, LUA_T_CMARK, 0);
@ -518,7 +526,7 @@ int lua_setlocal (lua_Function func, int local_number)
{ {
TObject *f = Address(func); TObject *f = Address(func);
char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func)); char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
adjustC(1); checkCparams(1);
--top; --top;
if (name) if (name)
{ {
@ -537,9 +545,11 @@ int lua_setlocal (lua_Function func, int local_number)
*/ */
static void do_callinc (int nResults) static void do_callinc (int nResults)
{ {
do_call(CLS_current.base+1, nResults); StkId base = CLS_current.base;
CLS_current.num = (top-stack) - CLS_current.base; /* number of results */ do_call(base+1, nResults);
CLS_current.base += CLS_current.num; /* incorporate results on the stack */ CLS_current.lua2C = base; /* position of the new results */
CLS_current.num = (top-stack) - base; /* number of results */
CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
} }
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
@ -628,15 +638,6 @@ int lua_callfunction (lua_Object function)
} }
int lua_call (char *funcname)
{
Word n = luaI_findsymbolbyname(funcname);
open_stack((top-stack)-CLS_current.base);
stack[CLS_current.base] = s_object(n);
return do_protectedrun(MULT_RET);
}
/* /*
** Open file, generate opcode and execute global statement. Return 0 on ** Open file, generate opcode and execute global statement. Return 0 on
** success or non 0 on error. ** success or non 0 on error.
@ -791,9 +792,9 @@ lua_Object lua_createtable (void)
lua_Object lua_lua2C (int number) lua_Object lua_lua2C (int number)
{ {
if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT; if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
/* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) == /* Ref(stack+(CLS_current.lua2C+number-1)) ==
stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */ stack+(CLS_current.lua2C+number-1)-stack+1 == */
return CLS_current.base-CLS_current.num+number; return CLS_current.lua2C+number;
} }
int lua_isnil (lua_Object o) int lua_isnil (lua_Object o)
@ -850,33 +851,20 @@ real lua_getnumber (lua_Object object)
*/ */
char *lua_getstring (lua_Object object) char *lua_getstring (lua_Object object)
{ {
if (object == LUA_NOOBJECT) return NULL; if (object == LUA_NOOBJECT || tostring (Address(object)))
if (tostring (Address(object))) return NULL; return NULL;
else return (svalue(Address(object))); else return (svalue(Address(object)));
} }
void *lua_getbindata (lua_Object object)
{
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
return NULL;
else return svalue(Address(object));
}
void *lua_getuserdata (lua_Object object) void *lua_getuserdata (lua_Object object)
{
void *add = lua_getbindata(object);
if (add == NULL) return NULL;
else return *(void **)add;
}
int lua_getbindatasize (lua_Object object)
{ {
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
return 0; return NULL;
else return (Address(object))->value.ts->size; else return tsvalue(Address(object))->u.v;
} }
/* /*
** Given an object handle, return its cfuntion pointer. On error, return NULL. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
*/ */
@ -1000,25 +988,14 @@ void lua_pushcfunction (lua_CFunction fn)
incr_top; incr_top;
} }
void lua_pushbindata (void *buff, int size, int tag)
{
if (buff == NULL)
ttype(top) = LUA_T_NIL;
else {
if (tag < 0)
luaI_realtag(tag);
tsvalue(top) = luaI_createuserdata(buff, size, tag);
ttype(top) = LUA_T_USERDATA;
}
incr_top;
}
/*
** Push an object (ttype=userdata) to stack.
*/
void lua_pushusertag (void *u, int tag) void lua_pushusertag (void *u, int tag)
{ {
lua_pushbindata(&u, sizeof(void *), tag); if (tag < 0) luaI_realtag(tag); /* error if tag is not valid */
tsvalue(top) = luaI_createudata(u, tag);
ttype(top) = LUA_T_USERDATA;
incr_top;
} }
/* /*

17
table.c
View File

@ -3,7 +3,7 @@
** Module to control static tables ** Module to control static tables
*/ */
char *rcs_table="$Id: table.c,v 2.69 1997/05/14 18:38:29 roberto Exp roberto $"; char *rcs_table="$Id: table.c,v 2.70 1997/05/26 14:42:51 roberto Exp roberto $";
#include "luamem.h" #include "luamem.h"
#include "auxlib.h" #include "auxlib.h"
@ -59,17 +59,17 @@ void luaI_initconstant (void)
*/ */
Word luaI_findsymbol (TaggedString *t) Word luaI_findsymbol (TaggedString *t)
{ {
if (t->varindex == NOT_USED) if (t->u.s.varindex == NOT_USED)
{ {
if (lua_ntable == lua_maxsymbol) if (lua_ntable == lua_maxsymbol)
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol, lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
symbolEM, MAX_WORD); symbolEM, MAX_WORD);
t->varindex = lua_ntable; t->u.s.varindex = lua_ntable;
lua_table[lua_ntable].varname = t; lua_table[lua_ntable].varname = t;
s_ttype(lua_ntable) = LUA_T_NIL; s_ttype(lua_ntable) = LUA_T_NIL;
lua_ntable++; lua_ntable++;
} }
return t->varindex; return t->u.s.varindex;
} }
@ -85,16 +85,16 @@ Word luaI_findsymbolbyname (char *name)
*/ */
Word luaI_findconstant (TaggedString *t) Word luaI_findconstant (TaggedString *t)
{ {
if (t->constindex == NOT_USED) if (t->u.s.constindex == NOT_USED)
{ {
if (lua_nconstant == lua_maxconstant) if (lua_nconstant == lua_maxconstant)
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *, lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
constantEM, MAX_WORD); constantEM, MAX_WORD);
t->constindex = lua_nconstant; t->u.s.constindex = lua_nconstant;
lua_constant[lua_nconstant] = t; lua_constant[lua_nconstant] = t;
lua_nconstant++; lua_nconstant++;
} }
return t->constindex; return t->u.s.constindex;
} }
@ -154,7 +154,7 @@ int luaI_ismarked (TObject *o)
{ {
switch (o->ttype) switch (o->ttype)
{ {
case LUA_T_STRING: case LUA_T_STRING: case LUA_T_USERDATA:
return o->value.ts->marked; return o->value.ts->marked;
case LUA_T_FUNCTION: case LUA_T_FUNCTION:
return o->value.tf->marked; return o->value.tf->marked;
@ -196,6 +196,7 @@ long lua_collectgarbage (long limit)
TaggedString *freestr; TaggedString *freestr;
TFunc *freefunc; TFunc *freefunc;
markall(); markall();
luaI_invalidaterefs();
freetable = luaI_hashcollector(&recovered); freetable = luaI_hashcollector(&recovered);
freestr = luaI_strcollector(&recovered); freestr = luaI_strcollector(&recovered);
freefunc = luaI_funccollector(&recovered); freefunc = luaI_funccollector(&recovered);

59
tree.c
View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_tree="$Id: tree.c,v 1.25 1997/05/05 20:21:23 roberto Exp roberto $"; char *rcs_tree="$Id: tree.c,v 1.26 1997/05/14 18:38:29 roberto Exp roberto $";
#include <string.h> #include <string.h>
@ -29,15 +29,21 @@ static int initialized = 0;
static stringtable string_root[NUM_HASHS]; static stringtable string_root[NUM_HASHS];
static TaggedString EMPTY = {LUA_T_STRING, NULL, 0, NOT_USED, NOT_USED, static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
0, 2, {0}}; 0, 2, {0}};
static unsigned long hash (char *buff, long size) static unsigned long hash (char *s, int tag)
{ {
unsigned long h = 0; unsigned long h;
if (tag != LUA_T_STRING)
h = (unsigned long)s;
else {
long size = strlen(s);
h = 0;
while (size--) while (size--)
h = ((h<<5)-h)^(unsigned char)*(buff++); h = ((h<<5)-h)^(unsigned char)*(s++);
}
return h; return h;
} }
@ -86,10 +92,30 @@ static void grow (stringtable *tb)
tb->hash = newhash; tb->hash = newhash;
} }
static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
static TaggedString *newone(char *buff, int tag, unsigned long h)
{ {
TaggedString *ts; TaggedString *ts;
unsigned long h = hash(buff, size); if (tag == LUA_T_STRING) {
ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
strcpy(ts->str, buff);
ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
ts->tag = LUA_T_STRING;
}
else {
ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
ts->u.v = buff;
ts->tag = tag == LUA_ANYTAG ? 0 : tag;
}
ts->marked = 0;
ts->hash = h;
return ts;
}
static TaggedString *insert (char *buff, int tag, stringtable *tb)
{
TaggedString *ts;
unsigned long h = hash(buff, tag);
int i; int i;
int j = -1; int j = -1;
if ((Long)tb->nuse*3 >= (Long)tb->size*2) if ((Long)tb->nuse*3 >= (Long)tb->size*2)
@ -103,8 +129,9 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
{ {
if (ts == &EMPTY) if (ts == &EMPTY)
j = i; j = i;
else if (ts->size == size && ts->tag == tag && else if ((ts->tag == LUA_T_STRING) ?
memcmp(buff, ts->str, size) == 0) (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
return ts; return ts;
i = (i+1)%tb->size; i = (i+1)%tb->size;
} }
@ -114,24 +141,18 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
i = j; i = j;
else else
tb->nuse++; tb->nuse++;
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1); ts = tb->hash[i] = newone(buff, tag, h);
memcpy(ts->str, buff, size);
ts->tag = tag;
ts->size = size;
ts->marked = 0;
ts->hash = h;
ts->varindex = ts->constindex = NOT_USED;
return ts; return ts;
} }
TaggedString *luaI_createuserdata (char *buff, long size, int tag) TaggedString *luaI_createudata (void *udata, int tag)
{ {
return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]); return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
} }
TaggedString *lua_createstring (char *str) TaggedString *lua_createstring (char *str)
{ {
return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING); return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
} }

12
tree.h
View File

@ -1,7 +1,7 @@
/* /*
** tree.h ** tree.h
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
** $Id: tree.h,v 1.16 1997/03/19 19:41:10 roberto Exp roberto $ ** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $
*/ */
#ifndef tree_h #ifndef tree_h
@ -16,17 +16,21 @@ typedef struct TaggedString
{ {
int tag; /* if != LUA_T_STRING, this is a userdata */ int tag; /* if != LUA_T_STRING, this is a userdata */
struct TaggedString *next; struct TaggedString *next;
long size; union {
struct {
Word varindex; /* != NOT_USED if this is a symbol */ Word varindex; /* != NOT_USED if this is a symbol */
Word constindex; /* != NOT_USED if this is a constant */ Word constindex; /* != NOT_USED if this is a constant */
} s;
void *v; /* if this is a userdata, here is its value */
} u;
unsigned long hash; /* 0 if not initialized */ unsigned long hash; /* 0 if not initialized */
int marked; /* for garbage collection; never collect (nor change) if > 1 */ int marked; /* for garbage collection; never collect (nor change) if > 1 */
char str[1]; /* \0 byte already reserved; MAY BE NOT 0 TERMINATED!! */ char str[1]; /* \0 byte already reserved */
} TaggedString; } TaggedString;
TaggedString *lua_createstring (char *str); TaggedString *lua_createstring (char *str);
TaggedString *luaI_createuserdata (char *buff, long size, int tag); TaggedString *luaI_createudata (void *udata, int tag);
TaggedString *luaI_strcollector (long *cont); TaggedString *luaI_strcollector (long *cont);
void luaI_strfree (TaggedString *l); void luaI_strfree (TaggedString *l);
void luaI_strcallIM (TaggedString *l); void luaI_strcallIM (TaggedString *l);