Committed lnum patch 031007
[lua.git] / src / ltm.c
blob4b22bf44d1e1f0fadd1ded1c126ec5d0b282efe4
1 /*
2 ** $Id: ltm.c,v 2.8 2006/01/10 12:50:00 roberto Exp $
3 ** Tag methods
4 ** See Copyright Notice in lua.h
5 */
8 #include <string.h>
10 #define ltm_c
11 #define LUA_CORE
13 #include "lua.h"
15 #include "lobject.h"
16 #include "lstate.h"
17 #include "lstring.h"
18 #include "ltable.h"
19 #include "ltm.h"
22 const char *const luaT_typenames[] = {
23 "nil", "boolean", "userdata", "number",
24 "string", "table", "function", "userdata", "thread",
25 "proto", "upval"
29 void luaT_init (lua_State *L) {
30 static const char *const luaT_eventname[] = { /* ORDER TM */
31 "__index", "__newindex",
32 "__gc", "__mode", "__eq",
33 "__add", "__sub", "__mul", "__div", "__mod",
34 "__pow", "__unm", "__len", "__lt", "__le",
35 "__concat", "__call"
37 int i;
38 for (i=0; i<TM_N; i++) {
39 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
40 luaS_fix(G(L)->tmname[i]); /* never collect these names */
46 ** function to be used with macro "fasttm": optimized for absence of
47 ** tag methods
49 const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
50 const TValue *tm = luaH_getstr(events, ename);
51 lua_assert(event <= TM_EQ);
52 if (ttisnil(tm)) { /* no tag method? */
53 events->flags |= cast_byte(1u<<event); /* cache this fact */
54 return NULL;
56 else return tm;
60 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
61 Table *mt;
62 switch (ttype(o)) {
63 case LUA_TTABLE:
64 mt = hvalue(o)->metatable;
65 break;
66 case LUA_TUSERDATA:
67 mt = uvalue(o)->metatable;
68 break;
69 default:
70 mt = G(L)->mt[ttype(o)];
72 return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);