ltm.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. ** $Id: ltm.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  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. "userdata", "nil", "number", "boolean", "string", "table", "function"
  16. };
  17. void luaT_init (lua_State *L) {
  18. static const char *const luaT_eventname[] = { /* ORDER TM */
  19. "gettable", "settable", "index",
  20. "gc", "weakmode",
  21. "add", "sub", "mul", "div",
  22. "pow", "unm", "lt", "concat",
  23. "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. G(L)->tmname[i]->tsv.marked = FIXMARK; /* 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. if (ttype(tm) == LUA_TNIL) { /* no tag method? */
  38. events->flags |= (1<<event); /* cache this fact */
  39. return NULL;
  40. }
  41. else return tm;
  42. }
  43. const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
  44. TString *ename = G(L)->tmname[event];
  45. switch (ttype(o)) {
  46. case LUA_TTABLE:
  47. return luaH_getstr(hvalue(o)->metatable, ename);
  48. case LUA_TUSERDATA:
  49. return luaH_getstr(uvalue(o)->uv.metatable, ename);
  50. default:
  51. return &luaO_nilobject;
  52. }
  53. }