| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 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 |
|
| 70 |
void throw_lua_exception(lua_State* st, const std::string& addinfo = std::string()); |
| 71 |
|
| 72 |
|
| 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 |
|
| 91 |
void initialize(); |
| 92 |
|
| 93 |
|
| 94 |
void load_file(const char* file); |
| 95 |
|
| 96 |
|
| 97 |
void eval(const std::string& stmt, int offset = 0); |
| 98 |
|
| 99 |
|
| 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 |
|
| 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 |
|
| 129 |
int mutex; |
| 130 |
|
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
class stack |
| 137 |
{ |
| 138 |
public: |
| 139 |
|
| 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 |
|
| 151 |
int size() const |
| 152 |
{ |
| 153 |
return lua_gettop(st); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
void find(const char* name, int index = LUA_GLOBALSINDEX) |
| 158 |
{ |
| 159 |
lua_getfield(st, index, name); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 326 |
|
| 327 |
class invoke_type {}; |
| 328 |
class m_invoke_type {}; |
| 329 |
class release_type {}; |
| 330 |
|
| 331 |
extern invoke_type invoke; |
| 332 |
extern m_invoke_type m_invoke; |
| 333 |
extern release_type end; |
| 334 |
|
| 335 |
class lookup |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 410 |
void lookup(const char* name); |
| 411 |
|
| 412 |
|
| 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 |
|
| 421 |
void invoke(); |
| 422 |
|
| 423 |
|
| 424 |
void m_invoke(); |
| 425 |
|
| 426 |
|
| 427 |
void end(); |
| 428 |
|
| 429 |
|
| 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 |
|
| 441 |
template<typename T> bind& operator >> (T& v) |
| 442 |
{ |
| 443 |
if (refuse || retvals_number < 1) |
| 444 |
{ |
| 445 |
|
| 446 |
|
| 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 |
|
| 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 |