Imported from ../lua-3.0.tar.gz.
[lua.git] / src / fallback.c
blob5a0b5a5bd497861ec7ec5489e40287e1ad414321
1 /*
2 ** fallback.c
3 ** TecCGraf - PUC-Rio
4 */
6 char *rcs_fallback="$Id: fallback.c,v 2.9 1997/06/23 18:27:53 roberto Exp $";
8 #include <stdio.h>
9 #include <string.h>
11 #include "auxlib.h"
12 #include "luamem.h"
13 #include "fallback.h"
14 #include "opcode.h"
15 #include "lua.h"
16 #include "table.h"
17 #include "tree.h"
18 #include "hash.h"
22 /* -------------------------------------------
23 ** Reference routines
26 static struct ref {
27 TObject o;
28 enum {LOCK, HOLD, FREE, COLLECTED} status;
29 } *refArray = NULL;
30 static int refSize = 0;
32 int luaI_ref (TObject *object, int lock)
34 int i;
35 int oldSize;
36 if (ttype(object) == LUA_T_NIL)
37 return -1; /* special ref for nil */
38 for (i=0; i<refSize; i++)
39 if (refArray[i].status == FREE)
40 goto found;
41 /* no more empty spaces */
42 oldSize = refSize;
43 refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
44 for (i=oldSize; i<refSize; i++)
45 refArray[i].status = FREE;
46 i = oldSize;
47 found:
48 refArray[i].o = *object;
49 refArray[i].status = lock ? LOCK : HOLD;
50 return i;
54 void lua_unref (int ref)
56 if (ref >= 0 && ref < refSize)
57 refArray[ref].status = FREE;
61 TObject *luaI_getref (int ref)
63 static TObject nul = {LUA_T_NIL, {0}};
64 if (ref == -1)
65 return &nul;
66 if (ref >= 0 && ref < refSize &&
67 (refArray[ref].status == LOCK || refArray[ref].status == HOLD))
68 return &refArray[ref].o;
69 else
70 return NULL;
74 void luaI_travlock (int (*fn)(TObject *))
76 int i;
77 for (i=0; i<refSize; i++)
78 if (refArray[i].status == LOCK)
79 fn(&refArray[i].o);
83 void luaI_invalidaterefs (void)
85 int i;
86 for (i=0; i<refSize; i++)
87 if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
88 refArray[i].status = COLLECTED;
92 /* -------------------------------------------
93 * Internal Methods
96 char *luaI_eventname[] = { /* ORDER IM */
97 "gettable", "settable", "index", "getglobal", "setglobal", "add",
98 "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
99 "concat", "gc", "function",
100 NULL
105 static int luaI_checkevent (char *name, char *list[])
107 int e = luaI_findstring(name, list);
108 if (e < 0)
109 luaL_verror("`%s' is not a valid event name", name);
110 return e;
114 struct IM *luaI_IMtable = NULL;
116 static int IMtable_size = 0;
117 static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
120 /* events in LUA_T_LINE are all allowed, since this is used as a
121 * 'placeholder' for "default" fallbacks
123 static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
124 {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
125 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
126 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
127 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
128 {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
129 {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
130 {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
131 {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
132 {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
133 {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
136 static int validevent (lua_Type t, int e)
137 { /* ORDER LUA_T */
138 return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
142 static void init_entry (int tag)
144 int i;
145 for (i=0; i<IM_N; i++)
146 ttype(luaI_getim(tag, i)) = LUA_T_NIL;
149 void luaI_initfallbacks (void)
151 if (luaI_IMtable == NULL) {
152 int i;
153 IMtable_size = NUM_TYPES+10;
154 luaI_IMtable = newvector(IMtable_size, struct IM);
155 for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++)
156 init_entry(i);
160 int lua_newtag (void)
162 --last_tag;
163 if ((-last_tag) >= IMtable_size) {
164 luaI_initfallbacks();
165 IMtable_size = growvector(&luaI_IMtable, IMtable_size,
166 struct IM, memEM, MAX_INT);
168 init_entry(last_tag);
169 return last_tag;
173 static void checktag (int tag)
175 if (!(last_tag <= tag && tag <= 0))
176 luaL_verror("%d is not a valid tag", tag);
179 void luaI_realtag (int tag)
181 if (!(last_tag <= tag && tag < LUA_T_NIL))
182 luaL_verror("tag %d is not result of `newtag'", tag);
186 void luaI_settag (int tag, TObject *o)
188 luaI_realtag(tag);
189 switch (ttype(o)) {
190 case LUA_T_ARRAY:
191 o->value.a->htag = tag;
192 break;
193 case LUA_T_USERDATA:
194 o->value.ts->tag = tag;
195 break;
196 default:
197 luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
202 int luaI_efectivetag (TObject *o)
204 lua_Type t = ttype(o);
205 if (t == LUA_T_USERDATA) {
206 int tag = o->value.ts->tag;
207 return (tag >= 0) ? LUA_T_USERDATA : tag;
209 else if (t == LUA_T_ARRAY)
210 return o->value.a->htag;
211 else return t;
215 void luaI_gettagmethod (void)
217 int t = (int)luaL_check_number(1);
218 int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
219 checktag(t);
220 if (validevent(t, e))
221 luaI_pushobject(luaI_getim(t,e));
225 void luaI_settagmethod (void)
227 int t = (int)luaL_check_number(1);
228 int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
229 lua_Object func = lua_getparam(3);
230 checktag(t);
231 if (!validevent(t, e))
232 luaL_verror("cannot change internal method `%s' for tag %d",
233 luaI_eventname[e], t);
234 luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
235 3, "function expected");
236 luaI_pushobject(luaI_getim(t,e));
237 *luaI_getim(t, e) = *luaI_Address(func);
241 static void stderrorim (void)
243 lua_Object s = lua_getparam(1);
244 if (lua_isstring(s))
245 fprintf(stderr, "lua: %s\n", lua_getstring(s));
248 static TObject errorim = {LUA_T_CFUNCTION, {stderrorim}};
251 TObject *luaI_geterrorim (void)
253 return &errorim;
256 void luaI_seterrormethod (void)
258 lua_Object func = lua_getparam(1);
259 luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
260 1, "function expected");
261 luaI_pushobject(&errorim);
262 errorim = *luaI_Address(func);
265 char *luaI_travfallbacks (int (*fn)(TObject *))
267 int e;
268 if (fn(&errorim))
269 return "error";
270 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
271 int t;
272 for (t=0; t>=last_tag; t--)
273 if (fn(luaI_getim(t,e)))
274 return luaI_eventname[e];
276 return NULL;
281 * ===================================================================
282 * compatibility with old fallback system
284 #if LUA_COMPAT2_5
286 static void errorFB (void)
288 lua_Object o = lua_getparam(1);
289 if (lua_isstring(o))
290 fprintf (stderr, "lua: %s\n", lua_getstring(o));
291 else
292 fprintf(stderr, "lua: unknown error\n");
296 static void nilFB (void) { }
299 static void typeFB (void)
301 lua_error("unexpected type");
305 static void fillvalids (IMS e, TObject *func)
307 int t;
308 for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
309 if (validevent(t, e))
310 *luaI_getim(t, e) = *func;
314 void luaI_setfallback (void)
316 static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
317 TObject oldfunc;
318 lua_CFunction replace;
319 char *name = luaL_check_string(1);
320 lua_Object func = lua_getparam(2);
321 luaI_initfallbacks();
322 luaL_arg_check(lua_isfunction(func), 2, "function expected");
323 switch (luaI_findstring(name, oldnames)) {
324 case 0: /* old error fallback */
325 oldfunc = errorim;
326 errorim = *luaI_Address(func);
327 replace = errorFB;
328 break;
329 case 1: /* old getglobal fallback */
330 oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
331 *luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
332 replace = nilFB;
333 break;
334 case 2: { /* old arith fallback */
335 int i;
336 oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
337 for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
338 fillvalids(i, luaI_Address(func));
339 replace = typeFB;
340 break;
342 case 3: { /* old order fallback */
343 int i;
344 oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
345 for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
346 fillvalids(i, luaI_Address(func));
347 replace = typeFB;
348 break;
350 default: {
351 int e;
352 if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
353 oldfunc = *luaI_getim(LUA_T_LINE, e);
354 fillvalids(e, luaI_Address(func));
355 replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
357 else {
358 luaL_verror("`%s' is not a valid fallback name", name);
359 replace = NULL; /* to avoid warnings */
363 if (oldfunc.ttype != LUA_T_NIL)
364 luaI_pushobject(&oldfunc);
365 else
366 lua_pushcfunction(replace);
368 #endif