Imported from ../lua-2.1.tar.gz.
[lua.git] / src / fallback.c
blobd5bbbba126365783ca81e2c008345d0f9ef8a7e6
1 /*
2 ** fallback.c
3 ** TecCGraf - PUC-Rio
4 */
6 char *rcs_fallback="$Id: fallback.c,v 1.11 1995/02/06 19:34:03 roberto Exp $";
8 #include <stdio.h>
9 #include <string.h>
11 #include "mem.h"
12 #include "fallback.h"
13 #include "opcode.h"
14 #include "inout.h"
15 #include "lua.h"
18 static void errorFB (void);
19 static void indexFB (void);
20 static void gettableFB (void);
21 static void arithFB (void);
22 static void concatFB (void);
23 static void orderFB (void);
24 static void GDFB (void);
25 static void funcFB (void);
29 ** Warning: This list must be in the same order as the #define's
31 struct FB luaI_fallBacks[] = {
32 {"error", {LUA_T_CFUNCTION, errorFB}},
33 {"index", {LUA_T_CFUNCTION, indexFB}},
34 {"gettable", {LUA_T_CFUNCTION, gettableFB}},
35 {"arith", {LUA_T_CFUNCTION, arithFB}},
36 {"order", {LUA_T_CFUNCTION, orderFB}},
37 {"concat", {LUA_T_CFUNCTION, concatFB}},
38 {"settable", {LUA_T_CFUNCTION, gettableFB}},
39 {"gc", {LUA_T_CFUNCTION, GDFB}},
40 {"function", {LUA_T_CFUNCTION, funcFB}}
43 #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
45 void luaI_setfallback (void)
47 int i;
48 char *name = lua_getstring(lua_getparam(1));
49 lua_Object func = lua_getparam(2);
50 if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
52 lua_pushnil();
53 return;
55 for (i=0; i<N_FB; i++)
57 if (strcmp(luaI_fallBacks[i].kind, name) == 0)
59 luaI_pushobject(&luaI_fallBacks[i].function);
60 luaI_fallBacks[i].function = *luaI_Address(func);
61 return;
64 /* name not found */
65 lua_pushnil();
69 static void errorFB (void)
71 lua_Object o = lua_getparam(1);
72 if (lua_isstring(o))
73 fprintf (stderr, "lua: %s\n", lua_getstring(o));
74 else
75 fprintf(stderr, "lua: unknown error\n");
79 static void indexFB (void)
81 lua_pushnil();
85 static void gettableFB (void)
87 lua_reportbug("indexed expression not a table");
91 static void arithFB (void)
93 lua_reportbug("unexpected type at conversion to number");
96 static void concatFB (void)
98 lua_reportbug("unexpected type at conversion to string");
102 static void orderFB (void)
104 lua_reportbug("unexpected type at comparison");
107 static void GDFB (void) { }
109 static void funcFB (void)
111 lua_reportbug("call expression not a function");
116 ** Lock routines
119 static Object *lockArray = NULL;
120 static Word lockSize = 0;
122 int luaI_lock (Object *object)
124 Word i;
125 Word oldSize;
126 if (tag(object) == LUA_T_NIL)
127 return -1;
128 for (i=0; i<lockSize; i++)
129 if (tag(&lockArray[i]) == LUA_T_NIL)
131 lockArray[i] = *object;
132 return i;
134 /* no more empty spaces */
135 oldSize = lockSize;
136 if (lockArray == NULL)
138 lockSize = 10;
139 lockArray = newvector(lockSize, Object);
141 else
143 lockSize = 3*oldSize/2 + 5;
144 lockArray = growvector(lockArray, lockSize, Object);
146 for (i=oldSize; i<lockSize; i++)
147 tag(&lockArray[i]) = LUA_T_NIL;
148 lockArray[oldSize] = *object;
149 return oldSize;
153 void lua_unlock (int ref)
155 tag(&lockArray[ref]) = LUA_T_NIL;
159 Object *luaI_getlocked (int ref)
161 return &lockArray[ref];
165 void luaI_travlock (void (*fn)(Object *))
167 Word i;
168 for (i=0; i<lockSize; i++)
169 fn(&lockArray[i]);