Esempio n. 1
0
void luaT_setfallback() {
	static const char *oldnames [] = { "error", "getglobal", "arith", "order", NULL };
	TObject oldfunc;
	lua_CFunction replace;
	if (!tmFBAdded) {
		luaL_addlibtolist(tmFB, (sizeof(tmFB) / sizeof(tmFB[0])));
		tmFBAdded = true;
	}
	const char *name = luaL_check_string(1);
	lua_Object func = lua_getparam(2);
	luaL_arg_check(lua_isfunction(func), 2, "function expected");
	switch (luaO_findstring(name, oldnames)) {
	case 0:  // old error fallback
		oldfunc = errorim;
		errorim = *luaA_Address(func);
		replace = errorFB;
		break;
	case 1:  // old getglobal fallback
		oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
		*luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
		replace = nilFB;
		break;
	case 2:
		{  // old arith fallback
			int32 i;
			oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
			for (i = IM_ADD; i <= IM_UNM; i++)  // ORDER IM
				fillvalids(i, luaA_Address(func));
			replace = typeFB;
			break;
		}
	case 3:
		{  // old order fallback
			int32 i;
			oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
			for (i = IM_LT; i <= IM_GE; i++)  // ORDER IM
				fillvalids(i, luaA_Address(func));
			replace = typeFB;
			break;
		}
	default:
		{
			int32 e;
			if ((e = luaO_findstring(name, luaT_eventname)) >= 0) {
				oldfunc = *luaT_getim(LUA_T_NIL, e);
				fillvalids(e, luaA_Address(func));
				replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
			} else {
				luaL_verror("`%.50s' is not a valid fallback name", name);
				replace = NULL;  // to avoid warnings
				oldfunc.ttype = LUA_T_NIL; // to avoid warnings
			}
		}
	}
	if (oldfunc.ttype != LUA_T_NIL)
		luaA_pushobject(&oldfunc);
	else
		lua_pushcfunction(replace);
}
Esempio n. 2
0
void luaT_setfallback (void)
{
  static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
  TObject oldfunc;
  lua_CFunction replace;
  char *name = luaL_check_string(1);
  lua_Object func = lua_getparam(2);
  luaL_arg_check(lua_isfunction(func), 2, "function expected");
  switch (luaL_findstring(name, oldnames)) {
    case 0:  /* old error fallback */
      oldfunc = L->errorim;
      L->errorim = *luaA_Address(func);
      replace = errorFB;
      break;
    case 1:  /* old getglobal fallback */
      oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
      *luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
      replace = nilFB;
      break;
    case 2: {  /* old arith fallback */
      int i;
      oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
      for (i=IM_ADD; i<=IM_UNM; i++)  /* ORDER IM */
        fillvalids(i, luaA_Address(func));
      replace = typeFB;
      break;
    }
    case 3: {  /* old order fallback */
      int i;
      oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
      for (i=IM_LT; i<=IM_GE; i++)  /* ORDER IM */
        fillvalids(i, luaA_Address(func));
      replace = typeFB;
      break;
    }
    default: {
      int e;
      if ((e = luaL_findstring(name, luaT_eventname)) >= 0) {
        oldfunc = *luaT_getim(LUA_T_NIL, e);
        fillvalids(e, luaA_Address(func));
        replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
      }
      else {
        luaL_verror("`%.50s' is not a valid fallback name", name);
        replace = NULL;  /* to avoid warnings */
      }
    }
  }
  if (oldfunc.ttype != LUA_T_NIL)
    luaA_pushobject(&oldfunc);
  else
    lua_pushcfunction(replace);
}