ltm.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. ** $Id: ltm.c,v 1.94 2002/06/12 14:51:31 roberto Exp roberto $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  8. #include "lobject.h"
  9. #include "lstate.h"
  10. #include "lstring.h"
  11. #include "ltable.h"
  12. #include "ltm.h"
  13. const char *const luaT_typenames[] = {
  14. "nil", "number", "string", "boolean", "table",
  15. "userdata", "userdata", "function"
  16. };
  17. void luaT_init (lua_State *L) {
  18. static const char *const luaT_eventname[] = { /* ORDER TM */
  19. "__gettable", "__settable", "__index", "__newindex",
  20. "__gc", "__eq", "__weakmode",
  21. "__add", "__sub", "__mul", "__div",
  22. "__pow", "__unm", "__lt", "__le",
  23. "__concat", "__call"
  24. };
  25. int i;
  26. for (i=0; i<TM_N; i++) {
  27. G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
  28. luaS_fix(G(L)->tmname[i]); /* never collect these names */
  29. }
  30. }
  31. /*
  32. ** function to be used with macro "fasttm": optimized for absence of
  33. ** tag methods
  34. */
  35. const TObject *luaT_gettm (Table *events, TMS event, TString *ename) {
  36. const TObject *tm = luaH_getstr(events, ename);
  37. lua_assert(event <= TM_WEAKMODE);
  38. if (ttype(tm) == LUA_TNIL) { /* no tag method? */
  39. events->flags |= (1u<<event); /* cache this fact */
  40. return NULL;
  41. }
  42. else return tm;
  43. }
  44. const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
  45. TString *ename = G(L)->tmname[event];
  46. switch (ttype(o)) {
  47. case LUA_TTABLE:
  48. return luaH_getstr(hvalue(o)->metatable, ename);
  49. case LUA_TUSERDATA:
  50. return luaH_getstr(uvalue(o)->uv.metatable, ename);
  51. default:
  52. return &luaO_nilobject;
  53. }
  54. }