1994-11-07 07:20:56 -08:00
|
|
|
/*
|
|
|
|
** fallback.c
|
|
|
|
** TecCGraf - PUC-Rio
|
|
|
|
*/
|
|
|
|
|
1997-03-24 09:13:22 -08:00
|
|
|
char *rcs_fallback="$Id: fallback.c,v 1.32 1997/03/21 18:37:28 roberto Exp roberto $";
|
1994-11-07 07:20:56 -08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1995-02-06 11:34:03 -08:00
|
|
|
#include <string.h>
|
1994-11-07 07:20:56 -08:00
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
#include "auxlib.h"
|
1994-11-16 09:39:16 -08:00
|
|
|
#include "mem.h"
|
1994-11-07 07:20:56 -08:00
|
|
|
#include "fallback.h"
|
1994-11-08 11:56:39 -08:00
|
|
|
#include "opcode.h"
|
1994-11-07 07:20:56 -08:00
|
|
|
#include "lua.h"
|
1996-04-22 11:00:37 -07:00
|
|
|
#include "table.h"
|
1997-02-26 09:38:41 -08:00
|
|
|
#include "tree.h"
|
|
|
|
#include "hash.h"
|
1994-11-07 07:20:56 -08:00
|
|
|
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
static char *typenames[] = { /* ORDER LUA_T */
|
|
|
|
"userdata", "line", "cmark", "mark", "function",
|
|
|
|
"function", "table", "string", "number", "nil",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void luaI_type (void)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(1);
|
1997-03-24 09:13:22 -08:00
|
|
|
luaL_arg_check(o != LUA_NOOBJECT, "type", 1, "no argument");
|
1997-03-20 11:20:43 -08:00
|
|
|
lua_pushstring(typenames[-ttype(luaI_Address(o))]);
|
|
|
|
lua_pushnumber(lua_tag(o));
|
|
|
|
}
|
1994-11-21 10:22:58 -08:00
|
|
|
|
1994-11-08 11:56:39 -08:00
|
|
|
|
1997-02-26 09:38:41 -08:00
|
|
|
/* -------------------------------------------
|
1996-04-22 11:00:37 -07:00
|
|
|
** Reference routines
|
1994-11-08 11:56:39 -08:00
|
|
|
*/
|
|
|
|
|
1996-04-22 11:00:37 -07:00
|
|
|
static struct ref {
|
|
|
|
Object o;
|
|
|
|
enum {LOCK, HOLD, FREE, COLLECTED} status;
|
|
|
|
} *refArray = NULL;
|
|
|
|
static int refSize = 0;
|
1994-11-08 11:56:39 -08:00
|
|
|
|
1996-04-25 07:10:00 -07:00
|
|
|
int luaI_ref (Object *object, int lock)
|
1994-11-08 11:56:39 -08:00
|
|
|
{
|
1996-03-04 05:29:10 -08:00
|
|
|
int i;
|
|
|
|
int oldSize;
|
1997-03-11 10:44:28 -08:00
|
|
|
if (ttype(object) == LUA_T_NIL)
|
1996-04-22 11:00:37 -07:00
|
|
|
return -1; /* special ref for nil */
|
|
|
|
for (i=0; i<refSize; i++)
|
|
|
|
if (refArray[i].status == FREE)
|
|
|
|
goto found;
|
1994-11-08 11:56:39 -08:00
|
|
|
/* no more empty spaces */
|
1996-04-22 11:00:37 -07:00
|
|
|
oldSize = refSize;
|
|
|
|
refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
|
|
|
|
for (i=oldSize; i<refSize; i++)
|
|
|
|
refArray[i].status = FREE;
|
|
|
|
i = oldSize;
|
|
|
|
found:
|
|
|
|
refArray[i].o = *object;
|
|
|
|
refArray[i].status = lock ? LOCK : HOLD;
|
|
|
|
return i;
|
1994-11-08 11:56:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-04-25 07:10:00 -07:00
|
|
|
void lua_unref (int ref)
|
1994-11-08 11:56:39 -08:00
|
|
|
{
|
1996-04-22 11:00:37 -07:00
|
|
|
if (ref >= 0 && ref < refSize)
|
|
|
|
refArray[ref].status = FREE;
|
1994-11-08 11:56:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-04-25 07:10:00 -07:00
|
|
|
Object *luaI_getref (int ref)
|
1994-11-08 11:56:39 -08:00
|
|
|
{
|
1996-02-08 11:08:34 -08:00
|
|
|
static Object nul = {LUA_T_NIL, {0}};
|
1996-04-22 11:00:37 -07:00
|
|
|
if (ref == -1)
|
1996-02-08 11:08:34 -08:00
|
|
|
return &nul;
|
1996-04-22 11:00:37 -07:00
|
|
|
if (ref >= 0 && ref < refSize &&
|
|
|
|
(refArray[ref].status == LOCK || refArray[ref].status == HOLD))
|
|
|
|
return &refArray[ref].o;
|
|
|
|
else
|
|
|
|
return NULL;
|
1994-11-08 11:56:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-10-17 04:52:38 -07:00
|
|
|
void luaI_travlock (int (*fn)(Object *))
|
1994-11-08 11:56:39 -08:00
|
|
|
{
|
1996-03-04 05:29:10 -08:00
|
|
|
int i;
|
1996-04-22 11:00:37 -07:00
|
|
|
for (i=0; i<refSize; i++)
|
|
|
|
if (refArray[i].status == LOCK)
|
|
|
|
fn(&refArray[i].o);
|
1994-11-08 11:56:39 -08:00
|
|
|
}
|
|
|
|
|
1995-10-04 10:13:02 -07:00
|
|
|
|
1996-04-22 11:00:37 -07:00
|
|
|
void luaI_invalidaterefs (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<refSize; i++)
|
|
|
|
if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
|
|
|
|
refArray[i].status = COLLECTED;
|
|
|
|
}
|
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
|
|
|
|
/* -------------------------------------------
|
|
|
|
* Internal Methods
|
|
|
|
*/
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
char *luaI_eventname[] = { /* ORDER IM */
|
|
|
|
"gettable", "settable", "index", "add", "sub", "mul", "div",
|
|
|
|
"pow", "unm", "lt", "le", "gt", "ge", "concat", "gc", "function",
|
1997-03-19 11:41:10 -08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
static char *geventname[] = { /* ORDER GIM */
|
|
|
|
"error", "getglobal", "setglobal",
|
1997-03-19 11:41:10 -08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
static int findstring (char *name, char *list[])
|
1995-10-04 10:13:02 -07:00
|
|
|
{
|
1996-03-04 05:29:10 -08:00
|
|
|
int i;
|
1997-03-19 11:41:10 -08:00
|
|
|
for (i=0; list[i]; i++)
|
|
|
|
if (strcmp(list[i], name) == 0)
|
|
|
|
return i;
|
1997-03-24 09:13:22 -08:00
|
|
|
return -1; /* name not found */
|
1995-10-04 10:13:02 -07:00
|
|
|
}
|
1997-02-26 09:38:41 -08:00
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
static int luaI_checkevent (char *name, char *list[])
|
|
|
|
{
|
1997-03-20 11:20:43 -08:00
|
|
|
int e = findstring(name, list);
|
1997-03-19 11:41:10 -08:00
|
|
|
if (e < 0)
|
1997-03-24 09:13:22 -08:00
|
|
|
luaL_verror("invalid event name `%s'", name);
|
1997-03-19 11:41:10 -08:00
|
|
|
return e;
|
|
|
|
}
|
1997-02-26 09:38:41 -08:00
|
|
|
|
|
|
|
|
|
|
|
static struct IM {
|
|
|
|
lua_Type tp;
|
1997-03-19 11:41:10 -08:00
|
|
|
Object int_method[IM_N];
|
|
|
|
} *luaI_IMtable = NULL;
|
|
|
|
|
1997-02-26 09:38:41 -08:00
|
|
|
static int IMtable_size = 0;
|
1997-03-24 09:13:22 -08:00
|
|
|
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
|
1997-03-19 11:41:10 -08:00
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
|
|
|
|
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
|
|
|
|
{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}, /* LUA_T_MARK */
|
|
|
|
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
|
|
|
|
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
|
|
|
|
{0, 0, 1, 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, 1}, /* LUA_T_STRING */
|
|
|
|
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
|
1997-03-19 11:41:10 -08:00
|
|
|
};
|
1997-02-26 09:38:41 -08:00
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
static int validevent (lua_Type t, int e)
|
1997-03-19 11:41:10 -08:00
|
|
|
{
|
1997-03-20 11:20:43 -08:00
|
|
|
return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
|
1997-03-19 11:41:10 -08:00
|
|
|
}
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
static void init_entry (int tag)
|
1997-02-26 09:38:41 -08:00
|
|
|
{
|
|
|
|
int i;
|
1997-03-19 11:41:10 -08:00
|
|
|
for (i=0; i<IM_N; i++)
|
|
|
|
luaI_IMtable[-tag].int_method[i].ttype = LUA_T_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void luaI_initfallbacks (void)
|
|
|
|
{
|
1997-03-20 12:36:19 -08:00
|
|
|
if (luaI_IMtable == NULL) {
|
|
|
|
int i;
|
|
|
|
IMtable_size = NUM_TYPES+10;
|
|
|
|
luaI_IMtable = newvector(IMtable_size, struct IM);
|
|
|
|
for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++) {
|
|
|
|
luaI_IMtable[-i].tp = (lua_Type)i;
|
|
|
|
init_entry(i);
|
|
|
|
}
|
1997-03-19 11:41:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int lua_newtag (char *t)
|
|
|
|
{
|
1997-03-20 11:20:43 -08:00
|
|
|
int tp;
|
1997-03-19 11:41:10 -08:00
|
|
|
--last_tag;
|
1997-03-20 12:36:19 -08:00
|
|
|
if ((-last_tag) >= IMtable_size) {
|
|
|
|
luaI_initfallbacks();
|
1997-02-26 09:38:41 -08:00
|
|
|
IMtable_size = growvector(&luaI_IMtable, IMtable_size,
|
|
|
|
struct IM, memEM, MAX_INT);
|
1997-03-20 12:36:19 -08:00
|
|
|
}
|
1997-03-20 11:20:43 -08:00
|
|
|
tp = -findstring(t, typenames);
|
|
|
|
if (tp == LUA_T_ARRAY || tp == LUA_T_USERDATA)
|
|
|
|
luaI_IMtable[-last_tag].tp = tp;
|
1997-02-26 09:38:41 -08:00
|
|
|
else
|
|
|
|
lua_error("invalid type for new tag");
|
1997-03-19 11:41:10 -08:00
|
|
|
init_entry(last_tag);
|
1997-02-26 09:38:41 -08:00
|
|
|
return last_tag;
|
|
|
|
}
|
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
|
1997-02-26 09:38:41 -08:00
|
|
|
static void checktag (int tag)
|
|
|
|
{
|
1997-03-24 09:13:22 -08:00
|
|
|
if (!(last_tag <= (tag) && (tag) <= 0))
|
1997-02-26 09:38:41 -08:00
|
|
|
lua_error("invalid tag");
|
|
|
|
}
|
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
lua_Type luaI_typetag (int tag)
|
|
|
|
{
|
|
|
|
if (tag >= 0) return LUA_T_USERDATA;
|
|
|
|
else {
|
|
|
|
checktag(tag);
|
|
|
|
return luaI_IMtable[-tag].tp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-02-26 09:38:41 -08:00
|
|
|
void luaI_settag (int tag, Object *o)
|
|
|
|
{
|
1997-03-19 11:41:10 -08:00
|
|
|
if (ttype(o) != luaI_typetag(tag))
|
1997-02-26 09:38:41 -08:00
|
|
|
lua_error("Tag is not compatible with this type");
|
1997-03-11 10:44:28 -08:00
|
|
|
if (o->ttype == LUA_T_ARRAY)
|
1997-02-26 09:38:41 -08:00
|
|
|
o->value.a->htag = tag;
|
|
|
|
else /* must be userdata */
|
|
|
|
o->value.ts->tag = tag;
|
|
|
|
}
|
|
|
|
|
1997-03-24 09:13:22 -08:00
|
|
|
|
1997-02-26 09:38:41 -08:00
|
|
|
int luaI_tag (Object *o)
|
|
|
|
{
|
1997-03-11 10:44:28 -08:00
|
|
|
lua_Type t = ttype(o);
|
1997-02-26 09:38:41 -08:00
|
|
|
if (t == LUA_T_USERDATA)
|
|
|
|
return o->value.ts->tag;
|
|
|
|
else if (t == LUA_T_ARRAY)
|
|
|
|
return o->value.a->htag;
|
|
|
|
else return t;
|
|
|
|
}
|
|
|
|
|
1997-03-24 09:13:22 -08:00
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
Object *luaI_getim (int tag, IMS event)
|
1997-02-26 09:38:41 -08:00
|
|
|
{
|
1997-03-19 11:41:10 -08:00
|
|
|
if (tag > LUA_T_USERDATA)
|
|
|
|
tag = LUA_T_USERDATA; /* default for non-registered tags */
|
|
|
|
return &luaI_IMtable[-tag].int_method[event];
|
|
|
|
}
|
|
|
|
|
1997-02-26 09:38:41 -08:00
|
|
|
|
|
|
|
void luaI_setintmethod (void)
|
|
|
|
{
|
1997-03-19 11:41:10 -08:00
|
|
|
int t = (int)luaL_check_number(1, "setintmethod");
|
1997-03-20 11:20:43 -08:00
|
|
|
int e = luaI_checkevent(luaL_check_string(2, "setintmethod"), luaI_eventname);
|
1997-02-26 09:38:41 -08:00
|
|
|
lua_Object func = lua_getparam(3);
|
1997-03-20 11:20:43 -08:00
|
|
|
checktag(t);
|
1997-03-19 11:41:10 -08:00
|
|
|
if (!validevent(t, e))
|
|
|
|
lua_error("cannot change this internal method");
|
|
|
|
luaL_arg_check(lua_isnil(func) || lua_isfunction(func), "setintmethod",
|
|
|
|
3, "function expected");
|
1997-03-19 13:12:34 -08:00
|
|
|
luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
|
1997-03-19 11:41:10 -08:00
|
|
|
luaI_IMtable[-t].int_method[e] = *luaI_Address(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Object gmethod[GIM_N] = {
|
|
|
|
{LUA_T_NIL, {NULL}}, {LUA_T_NIL, {NULL}}, {LUA_T_NIL, {NULL}}
|
|
|
|
};
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
Object *luaI_getgim (IMGS event)
|
1997-03-19 11:41:10 -08:00
|
|
|
{
|
|
|
|
return &gmethod[event];
|
|
|
|
}
|
|
|
|
|
|
|
|
void luaI_setglobalmethod (void)
|
|
|
|
{
|
|
|
|
int e = luaI_checkevent(luaL_check_string(1, "setintmethod"), geventname);
|
|
|
|
lua_Object func = lua_getparam(2);
|
|
|
|
luaL_arg_check(lua_isnil(func) || lua_isfunction(func), "setintmethod",
|
|
|
|
2, "function expected");
|
1997-03-19 13:12:34 -08:00
|
|
|
luaI_pushobject(&gmethod[e]);
|
1997-03-19 11:41:10 -08:00
|
|
|
gmethod[e] = *luaI_Address(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *luaI_travfallbacks (int (*fn)(Object *))
|
1997-03-24 09:13:22 -08:00
|
|
|
{
|
|
|
|
int e;
|
|
|
|
for (e=GIM_ERROR; e<=GIM_SETGLOBAL; e++) { /* ORDER GIM */
|
|
|
|
if (fn(&gmethod[e]))
|
|
|
|
return geventname[e];
|
|
|
|
}
|
|
|
|
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
|
|
|
|
int t;
|
|
|
|
for (t=0; t>=last_tag; t--)
|
|
|
|
if (fn(&luaI_IMtable[-t].int_method[e]))
|
|
|
|
return luaI_eventname[e];
|
|
|
|
}
|
1997-03-19 11:41:10 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ===================================================================
|
|
|
|
* compatibility with old fallback system
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static void errorFB (void)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(1);
|
|
|
|
if (lua_isstring(o))
|
|
|
|
fprintf (stderr, "lua: %s\n", lua_getstring(o));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "lua: unknown error\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void nilFB (void) { }
|
|
|
|
|
|
|
|
|
|
|
|
static void typeFB (void)
|
|
|
|
{
|
|
|
|
lua_error("unexpected type");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-03-20 11:20:43 -08:00
|
|
|
static void fillvalids (IMS e, Object *func)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
|
|
|
|
if (validevent(t, e))
|
|
|
|
luaI_IMtable[-t].int_method[e] = *func;
|
|
|
|
}
|
|
|
|
|
1997-03-19 11:41:10 -08:00
|
|
|
void luaI_setfallback (void)
|
|
|
|
{
|
|
|
|
int e;
|
1997-03-20 11:20:43 -08:00
|
|
|
Object oldfunc;
|
|
|
|
lua_CFunction replace;
|
1997-03-19 11:41:10 -08:00
|
|
|
char *name = luaL_check_string(1, "setfallback");
|
|
|
|
lua_Object func = lua_getparam(2);
|
|
|
|
luaL_arg_check(lua_isfunction(func), "setfallback", 2, "function expected");
|
1997-03-20 11:20:43 -08:00
|
|
|
e = findstring(name, geventname);
|
1997-03-19 11:41:10 -08:00
|
|
|
if (e >= 0) { /* global event */
|
1997-03-20 11:20:43 -08:00
|
|
|
oldfunc = gmethod[e];
|
|
|
|
gmethod[e] = *luaI_Address(func);
|
|
|
|
replace = (e == GIM_ERROR) ? errorFB : nilFB;
|
1997-03-19 11:41:10 -08:00
|
|
|
}
|
1997-03-20 11:20:43 -08:00
|
|
|
else if ((e = findstring(name, luaI_eventname)) >= 0) {
|
1997-03-19 11:41:10 -08:00
|
|
|
oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[e];
|
1997-03-20 11:20:43 -08:00
|
|
|
fillvalids(e, luaI_Address(func));
|
|
|
|
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
1997-02-26 09:38:41 -08:00
|
|
|
}
|
1997-03-20 11:20:43 -08:00
|
|
|
else if (strcmp(name, "arith") == 0) { /* old arith fallback */
|
|
|
|
int i;
|
1997-03-21 10:37:28 -08:00
|
|
|
oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_POW];
|
1997-03-20 11:20:43 -08:00
|
|
|
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
|
|
|
fillvalids(i, luaI_Address(func));
|
|
|
|
replace = typeFB;
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "order") == 0) { /* old order fallback */
|
|
|
|
int i;
|
|
|
|
oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_LT];
|
|
|
|
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
|
|
|
fillvalids(i, luaI_Address(func));
|
|
|
|
replace = typeFB;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_error("invalid fallback name");
|
|
|
|
replace = NULL; /* to avoid warnings */
|
|
|
|
}
|
|
|
|
if (oldfunc.ttype != LUA_T_NIL)
|
|
|
|
luaI_pushobject(&oldfunc);
|
|
|
|
else
|
|
|
|
lua_pushcfunction(replace);
|
1997-02-26 09:38:41 -08:00
|
|
|
}
|
1997-03-19 11:41:10 -08:00
|
|
|
|