~ilya-yanok/ubuntu/precise/grub2/fix-for-948716

« back to all changes in this revision

Viewing changes to script/lua/ltm.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Millan
  • Date: 2009-07-25 19:00:53 UTC
  • mfrom: (1.6.3 upstream)
  • mto: (17.4.13 sid)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: james.westby@ubuntu.com-20090725190053-uv3lm6ya3zxs77ep
ImportĀ upstreamĀ versionĀ 1.96+20090725

Show diffs side-by-side

added added

removed removed

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