ltm.c 1.6 KB

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