]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/ned/server/src/lua_cap.h
Update
[l4.git] / l4 / pkg / l4re-core / ned / server / src / lua_cap.h
1 /*
2  * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *          Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10 #pragma once
11
12 #include <lua.h>
13 #include <lauxlib.h>
14 #include <lualib.h>
15
16 #include <l4/re/util/cap_alloc>
17
18 #if 0
19 #include <cstdio>
20 #include <typeinfo>
21 #endif
22
23 #include "lua.h"
24
25 namespace Lua {
26
27 static char const *const CAP_TYPE = "L4::Cap";
28 static char const *const CAST_TABLE = "_CAP_TYPES";
29 void set_cap_metatable(lua_State *l);
30
31 #define L4_LUA_DECLARE_KOBJECT(_cap_type)                          \
32   private:                                                         \
33     void operator = (_cap_type const &);                           \
34   public:                                                          \
35     virtual _cap_type *clone(lua_State *l) const                   \
36     {                                                              \
37       _cap_type *c = new (lua_newuserdata(l, sizeof(_cap_type)))   \
38         _cap_type(*this);                                          \
39       set_cap_metatable(l);                                        \
40       return c;                                                    \
41     }                                                              \
42   private:
43
44
45 class Cap
46 {
47   L4_LUA_DECLARE_KOBJECT(Cap)
48
49 public:
50   template< typename T >
51   struct C : public L4Re::Util::Ref_cap<T> {};
52
53   template< typename T >
54   typename C<T>::Cap cap() const
55   { return L4::cap_cast<T>(_c); }
56
57   l4_fpage_t fpage() const
58   { return _c.fpage(_rights & L4_CAP_FPAGE_RWSD); }
59
60   unsigned long ext_rights() const
61   { return _rights & 0xf0; }
62
63   unsigned flags() const { return _flags; }
64
65   unsigned all_rights() const
66   { return _rights; }
67
68   L4_cap_fpage_rights rights() const
69   { return L4_cap_fpage_rights(_rights & 0xf); }
70
71   Cap(C<void>::Cap c = C<void>::Cap(), unsigned flags = 0)
72   : _c(c), _rights(L4_FPAGE_RO), _flags(flags) {}
73
74   template< typename T >
75   void set(typename C<T>::Cap c)
76   { _c = c; }
77
78   void set(C<void>::Cap c)
79   { _c = c; }
80
81   void trim_rights(L4_cap_fpage_rights keep)
82   { _rights = _rights & (keep | (~0 << 4)); }
83
84   void set_flags(unsigned flags)
85   { _flags |= flags; }
86
87   void set_rights(unsigned char r)
88   { _rights = r | L4_FPAGE_RO; }
89
90   virtual ~Cap();
91
92   int index(lua_State *l) const;
93   int newindex(lua_State *l) const;
94   int get_method_table(lua_State *l, char const *typ) const;
95
96   void *operator new (size_t, void *p) throw() { return p; }
97   void operator delete (void *) {}
98
99   void assign(Cap *o)
100   {
101     if (o == this)
102       return;
103
104     _c = o->_c;
105     _rights = o->_rights;
106   }
107
108 public:
109   typedef void Register_methods(lua_State *l);
110   static void register_methods(lua_State *l);
111   static void create_class(lua_State *l, Register_methods *rm,
112                            char const *type);
113   static void get_class(lua_State *l);
114
115
116   static void add_class_metatable(lua_State *l)
117   {
118     lua_newtable(l);
119     get_class(l);
120     lua_setfield(l, -2, "__index");
121     lua_setmetatable(l, -2);
122   }
123
124   bool find_dynamic_type(lua_State *) const;
125
126 private:
127   C<void>::Cap _c;
128   unsigned _rights : 16;
129   unsigned _flags  : 16;
130 };
131
132
133 Cap *
134 push_new_cap(lua_State *l, bool void_cap = false);
135
136 Cap *
137 push_void_cap(lua_State *l);
138
139 inline
140 Cap *
141 check_cap(lua_State *l, int idx)
142 { return (Cap*)luaL_checkudata(l, idx, CAP_TYPE); }
143
144 void get_cap_cast_table(lua_State *l);
145
146 template< typename C, typename Register_func >
147 void
148 register_cap_type(lua_State *l, Register_func r)
149 {
150   get_cap_cast_table(l);
151   L4::Type_info const *m = L4::kobject_typeid<C>();
152   char const *class_name = m->name() ? m->name() : "<unk-class>";
153   Cap::create_class(l, r, class_name);
154
155   if (0)
156     printf("register new cap type %s: '%s'\n", typeid(C).name(), class_name);
157   long proto = m->proto();
158   if (proto)
159     {
160       lua_pushinteger(l, proto);
161       lua_pushvalue(l, -2);
162       lua_settable(l, -4);
163     }
164
165   lua_setfield(l, -2, class_name);
166   lua_pop(l, 1);
167 }
168
169 template< typename KO, typename Lua_model >
170 class Cap_type_lib : public Lib
171 {
172 public:
173   Cap_type_lib() : Lib(P_cap_type) {}
174   void init(lua_State *l)
175   { register_cap_type<KO>(l, Lua_model::register_methods); }
176 };
177
178 void init_lua_cap(lua_State *l);
179
180 }
181
182