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