aboutsummaryrefslogtreecommitdiff
path: root/fallback.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-31 17:59:09 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-31 17:59:09 -0300
commitefaaf99c425db615e63b6c5ee56c2878a592e2fa (patch)
treed8d027e29e98d982b67f645e42fa38c0cc2bde32 /fallback.c
parentf8a571ee356f386ed7b5af898c2d297fd6d295fd (diff)
downloadlua-efaaf99c425db615e63b6c5ee56c2878a592e2fa.tar.gz
lua-efaaf99c425db615e63b6c5ee56c2878a592e2fa.tar.bz2
lua-efaaf99c425db615e63b6c5ee56c2878a592e2fa.zip
first version of "setglobal" and "getglobal" internal methods.
Diffstat (limited to 'fallback.c')
-rw-r--r--fallback.c81
1 files changed, 40 insertions, 41 deletions
diff --git a/fallback.c b/fallback.c
index c39c115b..2e40f973 100644
--- a/fallback.c
+++ b/fallback.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_fallback="$Id: fallback.c,v 1.34 1997/03/31 14:02:58 roberto Exp roberto $"; 6char *rcs_fallback="$Id: fallback.c,v 1.35 1997/03/31 14:17:09 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
@@ -109,16 +109,13 @@ void luaI_invalidaterefs (void)
109*/ 109*/
110 110
111char *luaI_eventname[] = { /* ORDER IM */ 111char *luaI_eventname[] = { /* ORDER IM */
112 "gettable", "settable", "index", "add", "sub", "mul", "div", 112 "gettable", "settable", "index", "getglobal", "setglobal", "add",
113 "pow", "unm", "lt", "le", "gt", "ge", "concat", "gc", "function", 113 "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
114 "concat", "gc", "function",
114 NULL 115 NULL
115}; 116};
116 117
117 118
118static char *geventname[] = { /* ORDER GIM */
119 "error", "getglobal", "setglobal",
120 NULL
121};
122 119
123static int findstring (char *name, char *list[]) 120static int findstring (char *name, char *list[])
124{ 121{
@@ -147,16 +144,16 @@ static int IMtable_size = 0;
147static int last_tag = LUA_T_NIL; /* ORDER LUA_T */ 144static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
148 145
149static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */ 146static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
150{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */ 147{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
151{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */ 148{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
152{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */ 149{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
153{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */ 150{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
154{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */ 151{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
155{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */ 152{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
156{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */ 153{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
157{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */ 154{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
158{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */ 155{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
159{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */ 156{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
160}; 157};
161 158
162static int validevent (lua_Type t, int e) 159static int validevent (lua_Type t, int e)
@@ -263,32 +260,29 @@ void luaI_setintmethod (void)
263 luaI_IMtable[-t].int_method[e] = *luaI_Address(func); 260 luaI_IMtable[-t].int_method[e] = *luaI_Address(func);
264} 261}
265 262
266static TObject gmethod[GIM_N] = {
267 {LUA_T_NIL, {NULL}}, {LUA_T_NIL, {NULL}}, {LUA_T_NIL, {NULL}}
268};
269 263
270TObject *luaI_getgim (IMGS event) 264static TObject errorim = {LUA_T_NIL, {NULL}};
265
266
267TObject *luaI_geterrorim (void)
271{ 268{
272 return &gmethod[event]; 269 return &errorim;
273} 270}
274 271
275void luaI_setglobalmethod (void) 272void luaI_seterrormethod (void)
276{ 273{
277 int e = luaI_checkevent(luaL_check_string(1, "setintmethod"), geventname); 274 lua_Object func = lua_getparam(1);
278 lua_Object func = lua_getparam(2); 275 luaL_arg_check(lua_isnil(func) || lua_isfunction(func), "seterrormethod",
279 luaL_arg_check(lua_isnil(func) || lua_isfunction(func), "setintmethod", 276 1, "function expected");
280 2, "function expected"); 277 luaI_pushobject(&errorim);
281 luaI_pushobject(&gmethod[e]); 278 errorim = *luaI_Address(func);
282 gmethod[e] = *luaI_Address(func);
283} 279}
284 280
285char *luaI_travfallbacks (int (*fn)(TObject *)) 281char *luaI_travfallbacks (int (*fn)(TObject *))
286{ 282{
287 int e; 283 int e;
288 for (e=GIM_ERROR; e<=GIM_SETGLOBAL; e++) { /* ORDER GIM */ 284 if (fn(&errorim))
289 if (fn(&gmethod[e])) 285 return "error";
290 return geventname[e];
291 }
292 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ 286 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
293 int t; 287 int t;
294 for (t=0; t>=last_tag; t--) 288 for (t=0; t>=last_tag; t--)
@@ -339,28 +333,33 @@ void luaI_setfallback (void)
339 lua_CFunction replace; 333 lua_CFunction replace;
340 char *name = luaL_check_string(1, "setfallback"); 334 char *name = luaL_check_string(1, "setfallback");
341 lua_Object func = lua_getparam(2); 335 lua_Object func = lua_getparam(2);
336 luaI_initfallbacks();
342 luaL_arg_check(lua_isfunction(func), "setfallback", 2, "function expected"); 337 luaL_arg_check(lua_isfunction(func), "setfallback", 2, "function expected");
343 e = findstring(name, geventname); 338 if (strcmp(name, "error") == 0) { /* old error fallback */
344 if (e >= 0) { /* global event */ 339 oldfunc = errorim;
345 oldfunc = gmethod[e]; 340 errorim = *luaI_Address(func);
346 gmethod[e] = *luaI_Address(func); 341 replace = errorFB;
347 replace = (e == GIM_ERROR) ? errorFB : nilFB; 342 }
343 else if (strcmp(name, "getglobal") == 0) { /* old getglobal fallback */
344 oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL];
345 luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func);
346 replace = nilFB;
348 } 347 }
349 else if ((e = findstring(name, luaI_eventname)) >= 0) { 348 else if ((e = findstring(name, luaI_eventname)) >= 0) {
350 oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[e]; 349 oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e];
351 fillvalids(e, luaI_Address(func)); 350 fillvalids(e, luaI_Address(func));
352 replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; 351 replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
353 } 352 }
354 else if (strcmp(name, "arith") == 0) { /* old arith fallback */ 353 else if (strcmp(name, "arith") == 0) { /* old arith fallback */
355 int i; 354 int i;
356 oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_POW]; 355 oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW];
357 for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ 356 for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
358 fillvalids(i, luaI_Address(func)); 357 fillvalids(i, luaI_Address(func));
359 replace = typeFB; 358 replace = typeFB;
360 } 359 }
361 else if (strcmp(name, "order") == 0) { /* old order fallback */ 360 else if (strcmp(name, "order") == 0) { /* old order fallback */
362 int i; 361 int i;
363 oldfunc = luaI_IMtable[LUA_T_USERDATA].int_method[IM_LT]; 362 oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT];
364 for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ 363 for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
365 fillvalids(i, luaI_Address(func)); 364 fillvalids(i, luaI_Address(func));
366 replace = typeFB; 365 replace = typeFB;