misc/liblua/ltm.c
author inu
Sat, 06 Nov 2010 21:00:37 -0400
changeset 4154 20b300499340
parent 2812 0a24853de796
permissions -rw-r--r--
more polish
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Tag methods
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#define ltm_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "lstring.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "ltable.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "ltm.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
const char *const luaT_typenames[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
  "nil", "boolean", "userdata", "number",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
  "string", "table", "function", "userdata", "thread",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
  "proto", "upval"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
void luaT_init (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
  static const char *const luaT_eventname[] = {  /* ORDER TM */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
    "__index", "__newindex",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
    "__gc", "__mode", "__eq",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
    "__add", "__sub", "__mul", "__div", "__mod",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
    "__pow", "__unm", "__len", "__lt", "__le",
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
    "__concat", "__call"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
  };
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
  for (i=0; i<TM_N; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
    G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
    luaS_fix(G(L)->tmname[i]);  /* never collect these names */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
** function to be used with macro "fasttm": optimized for absence of
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
** tag methods
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
  const TValue *tm = luaH_getstr(events, ename);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
  lua_assert(event <= TM_EQ);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
  if (ttisnil(tm)) {  /* no tag method? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
    events->flags |= cast_byte(1u<<event);  /* cache this fact */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
    return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
  else return tm;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
  Table *mt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
  switch (ttype(o)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
    case LUA_TTABLE:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
      mt = hvalue(o)->metatable;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
    case LUA_TUSERDATA:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
      mt = uvalue(o)->metatable;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
    default:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
      mt = G(L)->mt[ttype(o)];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
  return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75