lua/fallback.c

185 lines
3.7 KiB
C
Raw Normal View History

/*
** fallback.c
** TecCGraf - PUC-Rio
*/
char *rcs_fallback="$Id: fallback.c,v 1.18 1996/01/30 15:25:23 roberto Exp roberto $";
#include <stdio.h>
#include <string.h>
1994-11-16 20:39:16 +03:00
#include "mem.h"
#include "fallback.h"
1994-11-08 22:56:39 +03:00
#include "opcode.h"
#include "lua.h"
1994-11-10 20:36:54 +03:00
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);
1994-11-10 20:36:54 +03:00
/*
** Warning: This list must be in the same order as the #define's
*/
struct FB luaI_fallBacks[] = {
{"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0},
{"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
{"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},
1996-01-30 18:25:23 +03:00
{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
/* no fixed number of params or results */
1996-01-30 18:25:23 +03:00
{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
/* same default behavior of index FB */
};
#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
void luaI_setfallback (void)
{
int i;
char *name = lua_getstring(lua_getparam(1));
lua_Object func = lua_getparam(2);
if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
lua_error("incorrect argument to function `setfallback'");
for (i=0; i<N_FB; i++)
{
if (strcmp(luaI_fallBacks[i].kind, name) == 0)
{
luaI_pushobject(&luaI_fallBacks[i].function);
luaI_fallBacks[i].function = *luaI_Address(func);
return;
}
}
/* name not found */
lua_error("incorrect argument to function `setfallback'");
}
1994-11-10 20:36:54 +03:00
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");
}
1994-11-10 20:36:54 +03:00
static void indexFB (void)
{
lua_pushnil();
}
1994-11-10 20:36:54 +03:00
static void gettableFB (void)
{
lua_error("indexed expression not a table");
}
1994-11-10 20:36:54 +03:00
static void arithFB (void)
{
lua_error("unexpected type at conversion to number");
}
1994-11-10 20:36:54 +03:00
static void concatFB (void)
{
lua_error("unexpected type at conversion to string");
}
1994-11-10 20:36:54 +03:00
static void orderFB (void)
{
lua_error("unexpected type at comparison");
}
1994-11-10 20:36:54 +03:00
static void GDFB (void) { }
static void funcFB (void)
{
lua_error("call expression not a function");
}
1994-11-08 22:56:39 +03:00
/*
** Lock routines
*/
static Object *lockArray = NULL;
static Word lockSize = 0;
1994-11-08 22:56:39 +03:00
int luaI_lock (Object *object)
1994-11-08 22:56:39 +03:00
{
Word i;
Word oldSize;
if (tag(object) == LUA_T_NIL)
return -1; /* special lock ref for nil */
1994-11-08 22:56:39 +03:00
for (i=0; i<lockSize; i++)
if (tag(&lockArray[i]) == LUA_T_NIL)
{
lockArray[i] = *object;
1994-11-08 22:56:39 +03:00
return i;
}
/* no more empty spaces */
oldSize = lockSize;
if (lockArray == NULL)
{
lockSize = 10;
1994-11-16 20:39:16 +03:00
lockArray = newvector(lockSize, Object);
1994-11-08 22:56:39 +03:00
}
else
{
lockSize = 3*oldSize/2 + 5;
1994-11-16 20:39:16 +03:00
lockArray = growvector(lockArray, lockSize, Object);
1994-11-08 22:56:39 +03:00
}
for (i=oldSize; i<lockSize; i++)
tag(&lockArray[i]) = LUA_T_NIL;
lockArray[oldSize] = *object;
1994-11-08 22:56:39 +03:00
return oldSize;
}
void lua_unlock (int ref)
{
if (ref >= 0 && ref < lockSize)
tag(&lockArray[ref]) = LUA_T_NIL;
1994-11-08 22:56:39 +03:00
}
Object *luaI_getlocked (int ref)
{
static Object nul = {LUA_T_NIL, {0}};
if (ref >= 0 && ref < lockSize)
return &lockArray[ref];
else
return &nul;
1994-11-08 22:56:39 +03:00
}
void luaI_travlock (int (*fn)(Object *))
1994-11-08 22:56:39 +03:00
{
Word i;
1994-11-08 22:56:39 +03:00
for (i=0; i<lockSize; i++)
fn(&lockArray[i]);
}
char *luaI_travfallbacks (int (*fn)(Object *))
{
Word i;
for (i=0; i<N_FB; i++)
if (fn(&luaI_fallBacks[i].function))
return luaI_fallBacks[i].kind;
return NULL;
}