您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

81 行
1.8 KiB

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