diff src/lua/src/ltm.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/src/ltm.c	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 +** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** Tag methods
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <string.h>
    1.12 +
    1.13 +#define ltm_c
    1.14 +#define LUA_CORE
    1.15 +
    1.16 +#include "lua.h"
    1.17 +
    1.18 +#include "lobject.h"
    1.19 +#include "lstate.h"
    1.20 +#include "lstring.h"
    1.21 +#include "ltable.h"
    1.22 +#include "ltm.h"
    1.23 +
    1.24 +
    1.25 +
    1.26 +const char *const luaT_typenames[] = {
    1.27 +  "nil", "boolean", "userdata", "number",
    1.28 +  "string", "table", "function", "userdata", "thread",
    1.29 +  "proto", "upval"
    1.30 +};
    1.31 +
    1.32 +
    1.33 +void luaT_init (lua_State *L) {
    1.34 +  static const char *const luaT_eventname[] = {  /* ORDER TM */
    1.35 +    "__index", "__newindex",
    1.36 +    "__gc", "__mode", "__eq",
    1.37 +    "__add", "__sub", "__mul", "__div", "__mod",
    1.38 +    "__pow", "__unm", "__len", "__lt", "__le",
    1.39 +    "__concat", "__call"
    1.40 +  };
    1.41 +  int i;
    1.42 +  for (i=0; i<TM_N; i++) {
    1.43 +    G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
    1.44 +    luaS_fix(G(L)->tmname[i]);  /* never collect these names */
    1.45 +  }
    1.46 +}
    1.47 +
    1.48 +
    1.49 +/*
    1.50 +** function to be used with macro "fasttm": optimized for absence of
    1.51 +** tag methods
    1.52 +*/
    1.53 +const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
    1.54 +  const TValue *tm = luaH_getstr(events, ename);
    1.55 +  lua_assert(event <= TM_EQ);
    1.56 +  if (ttisnil(tm)) {  /* no tag method? */
    1.57 +    events->flags |= cast_byte(1u<<event);  /* cache this fact */
    1.58 +    return NULL;
    1.59 +  }
    1.60 +  else return tm;
    1.61 +}
    1.62 +
    1.63 +
    1.64 +const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
    1.65 +  Table *mt;
    1.66 +  switch (ttype(o)) {
    1.67 +    case LUA_TTABLE:
    1.68 +      mt = hvalue(o)->metatable;
    1.69 +      break;
    1.70 +    case LUA_TUSERDATA:
    1.71 +      mt = uvalue(o)->metatable;
    1.72 +      break;
    1.73 +    default:
    1.74 +      mt = G(L)->mt[ttype(o)];
    1.75 +  }
    1.76 +  return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
    1.77 +}
    1.78 +