use lua_mem(L, +-size) to track your additional userdata sizes. keep it balanced...
[blua.git] / src / ltm.c
blob2c5aa2a796b420aa3d6c499f6c6ee84dc5660142
1 /*
2 ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 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"
23 const char *const luaT_typenames[] = {
24 "nil", "boolean", "userdata", "number",
25 "string", "table", "function", "userdata", "thread",
26 "proto", "upval"
30 void luaT_init (lua_State *L) {
31 static const char *const luaT_eventname[] = { /* ORDER TM */
32 "__index", "__newindex",
33 "__usedindex",
34 "__gc", "__mode", "__eq",
35 "__add", "__sub", "__mul", "__div", "__mod",
36 "__pow", "__unm", "__len", "__lt", "__le",
37 "__concat", "__call"
38 ,"__strhook"
39 ,"__and", "__or", "__xor", "__shl", "__shr", "__not"
41 int i;
42 for (i=0; i<TM_N; i++) {
43 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
44 luaS_fix(G(L)->tmname[i]); /* never collect these names */
50 ** function to be used with macro "fasttm": optimized for absence of
51 ** tag methods
53 const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
54 const TValue *tm = luaH_getstr(events, ename);
55 lua_assert(event <= TM_EQ);
56 if (ttisnil(tm)) { /* no tag method? */
57 events->flags |= cast_byte(1u<<event); /* cache this fact */
58 return NULL;
60 else return tm;
64 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
65 Table *mt;
66 switch (ttype(o)) {
67 case LUA_TTABLE:
68 mt = hvalue(o)->metatable;
69 break;
70 case LUA_TUSERDATA:
71 mt = uvalue(o)->metatable;
72 break;
73 default:
74 mt = G(L)->mt[ttype(o)];
76 return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);