/src/middleware/lua/ltm.c

https://bitbucket.org/vivkin/gam3b00bs/ · C · 75 lines · 51 code · 15 blank · 9 comment · 4 complexity · 4ef6dd80b6678def7366157b127dd801 MD5 · raw file

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