/* ** fallback.c ** TecCGraf - PUC-Rio */ char *rcs_fallback="$Id: fallback.c,v 1.26 1997/02/26 17:38:41 roberto Unstable roberto $"; #include #include #include "mem.h" #include "fallback.h" #include "opcode.h" #include "lua.h" #include "table.h" #include "tree.h" #include "hash.h" static void errorFB (void); static void indexFB (void); static void gettableFB (void); static void arithFB (void); static void concatFB (void); static void orderFB (void); static void GDFB (void); static void funcFB (void); /* ** Warning: This list must be in the same order as the #define's */ struct FB luaI_fallBacks[] = { {"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1}, {"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1}, {"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1}, {"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1}, {"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0}, {"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0}, {"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1}, /* no fixed number of params or results */ {"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}, /* same default behavior of index FB */ {"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1}, {"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0} }; #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB)) static int luaI_findevent (char *name) { int i; for (i=0; i= 0 && ref < refSize) refArray[ref].status = FREE; } Object *luaI_getref (int ref) { static Object nul = {LUA_T_NIL, {0}}; if (ref == -1) return &nul; if (ref >= 0 && ref < refSize && (refArray[ref].status == LOCK || refArray[ref].status == HOLD)) return &refArray[ref].o; else return NULL; } void luaI_travlock (int (*fn)(Object *)) { int i; for (i=0; i= IMtable_size) IMtable_size = growvector(&luaI_IMtable, IMtable_size, struct IM, memEM, MAX_INT); if (strcmp(t, "table") == 0) luaI_IMtable[last_tag-BASE_TAG].tp = LUA_T_ARRAY; else if (strcmp(t, "userdata") == 0) luaI_IMtable[last_tag-BASE_TAG].tp = LUA_T_USERDATA; else lua_error("invalid type for new tag"); for (i=0; ittype == LUA_T_ARRAY) o->value.a->htag = tag; else /* must be userdata */ o->value.ts->tag = tag; } int luaI_tag (Object *o) { lua_Type t = ttype(o); if (t == LUA_T_USERDATA) return o->value.ts->tag; else if (t == LUA_T_ARRAY) return o->value.a->htag; else return t; } Object *luaI_getim (int tag, int event) { if (tag == 0) return &luaI_fallBacks[event].function; else if (validtag(tag)) { Object *func = &luaI_IMtable[tag-BASE_TAG].int_method[event]; if (func->ttype == LUA_T_NIL) return NULL; else return func; } else return NULL; } void luaI_setintmethod (void) { lua_Object tag = lua_getparam(1); lua_Object event = lua_getparam(2); lua_Object func = lua_getparam(3); if (!(lua_isnumber(tag) && lua_isstring(event) && lua_isfunction(func))) lua_error("incorrect arguments to function `setintmethod'"); else { int i = luaI_findevent(lua_getstring(event)); int t = lua_getnumber(tag); checktag(t); luaI_IMtable[t-BASE_TAG].int_method[i] = *luaI_Address(func); } }