xref: /freebsd/sys/contrib/openzfs/module/lua/ltm.c (revision 10ff414c)
1 /* BEGIN CSTYLED */
2 /*
3 ** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** Tag methods
5 ** See Copyright Notice in lua.h
6 */
7 
8 
9 #define ltm_c
10 #define LUA_CORE
11 
12 #include <sys/lua/lua.h>
13 
14 #include "lobject.h"
15 #include "lstate.h"
16 #include "lstring.h"
17 #include "ltable.h"
18 #include "ltm.h"
19 
20 
21 static const char udatatypename[] = "userdata";
22 
23 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
24   "no value",
25   "nil", "boolean", udatatypename, "number",
26   "string", "table", "function", udatatypename, "thread",
27   "proto", "upval"  /* these last two cases are used for tests only */
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", "__len", "__eq",
35     "__add", "__sub", "__mul", "__div", "__mod",
36     "__pow", "__unm", "__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 (ttypenv(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[ttypenv(o)];
73   }
74   return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
75 }
76 /* END CSTYLED */
77