ltm.c 1.5 KB

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