Debian Codegraph @

src/bnetd/luawrapper.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Anton Burdinuk <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the organization nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifdef WITH_LUA
29 #ifndef INCLUDED_LUAWRAPPER_H
30 #define INCLUDED_LUAWRAPPER_H
31
32 extern "C"
33 {
34 #include <lua.h>
35 #include <lauxlib.h>
36 #include <lualib.h>
37 }
38
39 #include <cstring>
40 #include <map>
41 #include <stdexcept>
42 #include <string>
43 #include <typeinfo>
44 #include <vector>
45
46 namespace lua
47 {
48 // lua exception
49
50 class exception : public std::exception
51 {
52 public:
53 explicit exception(const std::string s) : m_what(s)
54 {
55 }
56
57 virtual ~exception() throw()
58 {
59 }
60
61 virtual const char* what() const throw()
62 {
63 return m_what.c_str();
64 }
65 protected:
66 std::string m_what;
67 };
68
69 // check Lua VM for errors and throw C++ lua::exception
70 void throw_lua_exception(lua_State* st, const std::string& addinfo = std::string());
71
72 // Lua VM instance
73
74 class vm
75 {
76 public:
77 vm() : st(nullptr), mutex(0)
78 {
79 }
80
81 explicit vm(lua_State* _st) : st(_st), mutex(0)
82 {
83 }
84
85 ~vm()
86 {
87 this->done();
88 }
89
90 // create new Lua VM instance
91 void initialize();
92
93 // load and execute Lua script from file
94 void load_file(const char* file);
95
96 // load and execute Lua statement from string
97 void eval(const std::string& stmt, int offset = 0);
98
99 // register external C function
100 void reg(const char* name, lua_CFunction func)
101 {
102 lua_register(st, name, func);
103 }
104 void reg(const char* package, const luaL_Reg* members)
105 {
106 luaL_register(st, package, members);
107 }
108
109 // destroy Lua VM instance
110 void done()
111 {
112 if (st)
113 {
114 lua_close(st);
115 }
116 }
117
118 lua_State* get_st() const
119 {
120 return st;
121 }
122
123 friend class stack;
124 friend class bind;
125 protected:
126 lua_State* st;
127
128 // mutex for lua::bind
129 int mutex;
130
131 };
132
133
134 // Lua stack wrapper (use in custom CFunctions only)
135
136 class stack
137 {
138 public:
139 // initialize stack
140 stack() : st(nullptr), st_top(0)
141 {
142 }
143 explicit stack(lua_State* _st) : st(_st), st_top(_st ? lua_gettop(_st) : 0)
144 {
145 }
146 explicit stack(const vm& _vm) : st(_vm.st), st_top(_vm.st ? lua_gettop(_vm.st) : 0)
147 {
148 }
149
150 // return current stack size
151 int size() const
152 {
153 return lua_gettop(st);
154 }
155
156 // find field in table
157 void find(const char* name, int index = LUA_GLOBALSINDEX)
158 {
159 lua_getfield(st, index, name);
160 }
161
162 // push to stack scalar
163 void push(unsigned int v)
164 {
165 lua_pushinteger(st, v);
166 }
167 void push(int v)
168 {
169 lua_pushinteger(st, v);
170 }
171 void push_boolean(int v)
172 {
173 lua_pushboolean(st, v);
174 }
175 void push(double v)
176 {
177 lua_pushnumber(st, v);
178 }
179 void push(const std::string& v)
180 {
181 lua_pushlstring(st, v.c_str(), v.length());
182 }
183 void push(const char* v)
184 {
185 lua_pushlstring(st, v, std::strlen(v));
186 }
187
188 // push to stack std::map
189 template<typename _key, typename _val, typename _comp, typename _alloc>
190 void push(const std::map<_key, _val, _comp, _alloc>& v)
191 {
192 lua_newtable(st);
193
194 for (typename std::map<_key, _val, _comp, _alloc>::const_iterator i = v.begin(); i != v.end(); ++i)
195 {
196 push(i->first);
197 push(i->second);
198 lua_rawset(st, -3);
199 }
200 }
201
202 // push to stack std::vector
203 template<typename _val, typename _alloc>
204 void push(const std::vector<_val, _alloc>& v)
205 {
206 lua_newtable(st);
207
208 for (int i = 0; i < v.size(); ++i)
209 {
210 push(i);
211 push(v[i]);
212 lua_rawset(st, -3);
213 }
214 }
215
216
217 // recv from stack scalar
218 void get(unsigned int& v, int index)
219 {
220 v = lua_tointeger(st, index);
221 }
222 void get(int& v, int index)
223 {
224 v = lua_tointeger(st, index);
225 }
226 void get(bool& v, int index)
227 {
228 v = lua_toboolean(st, index);
229 }
230 void get(double& v, int index)
231 {
232 v = lua_tonumber(st, index);
233 }
234 void get(const char*& v, int index)
235 {
236 size_t len = 0;
237 v = lua_tolstring(st, index, &len);
238 }
239 void get(std::string& v, int index)
240 {
241 size_t len = 0;
242
243 const char* p = lua_tolstring(st, index, &len);
244 if (p)
245 v.assign(p, len);
246 }
247
248 // recv from stack std::map
249 template<typename _key, typename _val, typename _comp, typename _alloc>
250 void get(std::map<_key, _val, _comp, _alloc>& v, int index)
251 {
252 if (lua_type(st, index) == LUA_TTABLE)
253 {
254 if (index < 0)
255 index = lua_gettop(st) + 1 + index;
256
257 lua_pushnil(st);
258
259 while (lua_next(st, index))
260 {
261 _val _v; get(_v, -1);
262 _key _k; get(_k, -2);
263 v[_k] = _v;
264 pop();
265 }
266 }
267 }
268
269 // recv from stack std::vector (use std::map instead)
270 template<typename _val, typename _alloc>
271 void get(std::vector<_val, _alloc>& v, int index)
272 {
273 if (lua_type(st, index) == LUA_TTABLE)
274 {
275 if (index < 0)
276 index = lua_gettop(st) + 1 + index;
277
278 lua_pushnil(st);
279 while (lua_next(st, index))
280 {
281 _val _v; get(_v, -1);
282
283 if (lua_type(st, -2) == LUA_TNUMBER)
284 {
285 int _k = lua_tointeger(st, -2);
286 if (_k >= 0)
287 {
288 if (_k >= v.size())
289 v.resize(_k + 1);
290 v[_k] = _v;
291 }
292 }
293
294 pop();
295 }
296 }
297 }
298
299
300 // get value of stack with index check
301 template<typename T>
302 void at(int index, T& v)
303 {
304 if (index > 0 && index <= size())
305 get(v, index);
306 else
307 v = T();
308 }
309
310 // pop last values from stack
311 void pop()
312 {
313 lua_pop(st, 1);
314 }
315 void popn(int n)
316 {
317 lua_pop(st, n);
318 }
319 protected:
320 lua_State* st;
321 int st_top;
322 };
323
324
325 // manipulators for lua::bind
326
327 class invoke_type {};
328 class m_invoke_type {};
329 class release_type {};
330
331 extern invoke_type invoke; // execute function
332 extern m_invoke_type m_invoke; // execute function as method (insert this pointer before argument list)
333 extern release_type end; // release lua::bind (pop staff from Lua stack)
334
335 class lookup // find field in last table (or LUA_GLOBALSINDEX if first)
336 {
337 public:
338 lookup(const std::string& _name) :name(_name)
339 {
340 }
341
342 friend class bind;
343 protected:
344 std::string name;
345 };
346
347
348 // class for direct access to Lua table fields
349
350 class table : protected stack
351 {
352 public:
353 table() : stack(nullptr), index(0)
354 {
355 }
356 explicit table(lua_State* _st, int _index) : stack(_st), index(_index)
357 {
358 }
359 explicit table(vm& _vm) :stack(_vm.get_st()), index(LUA_GLOBALSINDEX)
360 {
361 }
362
363 // get field value
364 template<typename _VAL>
365 void query(const char* name, _VAL& val)
366 {
367 if (st)
368 {
369 lua_getfield(st, index, name);
370 stack::get(val, -1);
371 stack::pop();
372 }
373 }
374
375 // set field value
376 template<typename _VAL>
377 void update(const char* name, const _VAL& val)
378 {
379 if (st)
380 {
381 stack::push(val);
382 lua_setfield(st, index, name);
383 }
384 }
385 protected:
386 int index;
387 };
388
389
390 // Lua transaction class (only one per vm instance)
391
392 class bind : protected stack
393 {
394 public:
395 explicit bind(vm& _vm) : stack(_vm), refuse(0), args_number(0), retvals_number(0),
396 cur_index(LUA_GLOBALSINDEX), mutex(_vm.mutex)
397 {
398 if (mutex)
399 refuse++;
400 else
401 mutex++;
402 }
403
404 ~bind()
405 {
406 end();
407 }
408
409 // find field in last table
410 void lookup(const char* name);
411
412 // return lua::table object for last table
413 lua::table table()
414 {
415 if (lua_type(st, -1) == LUA_TTABLE)
416 return lua::table(st, lua_gettop(st));
417 return lua::table();
418 }
419
420 // execute last function
421 void invoke();
422
423 // execute last function as method
424 void m_invoke();
425
426 // end transaction (free Lua stack)
427 void end();
428
429 // push function argument to Lua stack
430 template<typename T> bind& operator<<(const T& v)
431 {
432 if (!refuse)
433 {
434 push(v);
435 args_number++;
436 }
437 return *this;
438 }
439
440 // pop function return value from stack
441 template<typename T> bind& operator >> (T& v)
442 {
443 if (refuse || retvals_number < 1)
444 {
445 // INFO: (HarpyWar) -2 is default value if Lua code failed to run due to error
446 // it's needed for lua_handle_command()
447 if (typeid(T) == typeid(int))
448 v = T(-2);
449 else
450 v = T();
451 }
452 else
453 {
454 get(v, -retvals_number); retvals_number--;
455 }
456
457 return *this;
458 }
459
460 // for manipulators
461 bind& operator<<(const lua::lookup& f)
462 {
463 bind::lookup(f.name.c_str());
464 return *this;
465 }
466 bind& operator<<(const lua::invoke_type&)
467 {
468 bind::invoke();
469 return *this;
470 }
471 bind& operator<<(const lua::m_invoke_type&)
472 {
473 bind::m_invoke();
474 return *this;
475 }
476 bind& operator<<(const lua::release_type&)
477 {
478 bind::end();
479 return *this;
480 }
481
482 protected:
483 int args_number;
484 int retvals_number;
485 int cur_index;
486
487 private:
488 int& mutex;
489 int refuse;
490 };
491
492 using transaction = bind;
493 using function = lookup;
494 using field = lookup;
495 }
496
497
498 #endif
499 #endif