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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
|
/*
* Copyright (C) 2009-2017 René Rebe, ExactCODE GmbH
* Copyright (C) 2008 Valentin Ziegler, ExactCODE GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2. A copy of the GNU General
* Public License can be found in the file LICENSE.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
* ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* Alternatively, commercial licensing options are available from the
* copyright holder ExactCODE GmbH Germany.
*/
#ifndef __LUA_HH
#define __LUA_HH
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#ifndef lua_pushglobaltable // Lua 5.1 et al.
#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
#endif
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
namespace LuaWrapper {
template <typename T> class typeName;
template <>
class typeName<int>
{
public:
static const char* name() {return "int"; }
};
template <>
class typeName<unsigned int>
{
public:
static const char* name() {return "unsigned int"; }
};
template <>
class typeName<double>
{
public:
static const char* name() {return "double"; }
};
template <>
class typeName<const char*>
{
public:
static const char* name() {return "const char*"; }
};
template <>
class typeName<bool>
{
public:
static const char* name() {return "bool"; }
};
template <>
class typeName<std::string>
{
public:
static const char* name() {return "std::string"; }
};
static void argError(lua_State* L, int index, const char* type_name)
{
// slightly adapted from luaL_argerror and luaL_typerror
lua_Debug ar;
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
ar.name = "?";
else
lua_getinfo(L, "n", &ar);
if (ar.name == NULL)
ar.name = "?";
luaL_error(L, "cannot convert argument #%d from %s to %s (current stackframe: " LUA_QS " )",
index, luaL_typename(L, index), type_name, ar.name);
}
template <typename T, T RET, bool MARK=false>
class DefaultConst
{
public:
DefaultConst(lua_State* L, int index) { if (MARK) lua_pushnil(L); };
static const T ret = RET;
};
template <typename T, bool MARK=false>
class DefaultInitializer
{
public:
DefaultInitializer(lua_State* L, int index) : ret() { if (MARK) lua_pushnil(L); };
T ret;
};
template <typename T>
class TypeError
{
public:
TypeError(lua_State* L, int index) : ret()
{
argError(L, index, typeName<T>::name() );
}
T ret;
};
template <typename T>
class TypeError<T&>
{
public:
TypeError(lua_State* L, int index) : ret()
{
argError(L, index, typeName<T>::name());
}
T* ret;
};
template <typename T>
class TypeError<const T&>
{
public:
TypeError(lua_State* L, int index) : ret()
{
argError(L, index, typeName<T>::name());
}
T* ret;
};
template <>
class TypeError<const std::string&>
{
public:
TypeError(lua_State* L, int index)
{
argError(L, index, typeName<std::string>::name());
}
std::string ret;
};
template <typename T, typename DEF>
T getDefault(lua_State* L, int index) {
DEF def(L, index);
return def.ret;
}
class LuaClassData
{
public:
LuaClassData(const char* name)
: handle(name) {
}
bool isCompatible(lua_State* L, int index) {
// modelled after Lua 5.2+ luaL_testudata()
if (lua_getmetatable(L, index)) { // does it have a metatable?
luaL_getmetatable(L, handle); // get correct metatable
bool ret = lua_rawequal(L, -1, -2); // if not the same userdata with wrong metatable
lua_pop(L, 2); // remove both metatables
return ret;
}
return false; // value is not a userdata with a metatable
}
const char* handle;
};
template <typename T>
class LuaClass
{
public:
LuaClass(const char* name)
{
if (!data) {
data = new LuaClassData(name);
}
}
~LuaClass()
{
if (data) {
delete data; data = 0;
}
}
static const char* luahandle()
{
return data->handle;
}
static T* getPtr(lua_State* L, int index)
{
T** p = getRawPtr(L, index);
if (p)
return (T*)*p;
//luaL_typerror(L, index, luahandle());
return 0; /* avoid warnings */
}
static T** getRawPtr(lua_State* L, int index)
{
void** p = (void**)lua_touserdata(L, index);
if (p)
if (data->isCompatible(L, index))
return (T**)p;
//luaL_typerror(L, index, luahandle());
return 0; /* avoid warnings */
}
static void packPtr(lua_State* L, T* obj)
{
T** ptr = (T**)lua_newuserdata(L, sizeof(T*));
*ptr = obj;
luaL_getmetatable(L, luahandle());
lua_setmetatable(L, -2);
}
static LuaClassData* data;
};
template <typename T>
LuaClassData* LuaClass<T>::data = 0;
class AutoReleaseItem
{
public:
AutoReleaseItem(lua_State* L);
virtual ~AutoReleaseItem() {}
AutoReleaseItem* next;
};
template <typename T>
class EncapsulatedReleaseItem : public AutoReleaseItem
{
public:
T t;
EncapsulatedReleaseItem(const T& init, lua_State* L)
: AutoReleaseItem(L), t(init) {
}
};
template <typename T>
class ReleaseReferenceReleaseItem : public AutoReleaseItem
{
public:
T t;
ReleaseReferenceReleaseItem(const T& init, lua_State* L)
: AutoReleaseItem(L), t(init) {
}
~ReleaseReferenceReleaseItem() {
t.releaseReference();
}
};
void runAutoRelease(lua_State* L);
class LuaTable
{
public:
int handle;
lua_State* my_L;
LuaTable() // stub ctor, for default initializers etc
{
// make sure misuse of this constructor in code is noticed quickly
my_L = 0;
handle = 0;
}
LuaTable(lua_State* L) // create a new table and reference
{
lua_newtable(L);
handle = luaL_ref(L, LUA_REGISTRYINDEX);
my_L = L;
}
LuaTable(lua_State* L, int stackIndex) // reference a table on stack
{
if (lua_type(L, stackIndex) == LUA_TTABLE) {
lua_pushvalue(L, stackIndex);
handle = luaL_ref(L, LUA_REGISTRYINDEX);
my_L = L;
} else {
my_L = 0;
handle = 0;
}
}
LuaTable(const LuaTable& src)
{
handle = src.handle;
my_L = src.my_L;
}
void push()
{
lua_rawgeti(my_L, LUA_REGISTRYINDEX, handle);
}
void autoRelease()
{
new ReleaseReferenceReleaseItem<LuaTable>(*this, my_L);
}
void releaseReference()
{
luaL_unref(my_L, LUA_REGISTRYINDEX, handle);
my_L = 0;
}
bool valid() const
{
return my_L != 0;
}
operator bool() const
{
return valid();
}
int size()
{
push();
int result = lua_objlen(my_L, -1);
lua_pop(my_L, 1);
return result;
}
bool exists(const char* key)
{
push();
lua_getfield(my_L, -1, key);
bool result = (lua_isnil(my_L, -1) == 0);
lua_pop(my_L, 2);
return result;
}
bool exists(int ikey)
{
push();
lua_pushinteger(my_L, ikey);
lua_gettable(my_L, -2);
bool result = (lua_isnil(my_L, -1) == 0);
lua_pop(my_L, 2);
return result;
}
template <typename T, typename DEF > T getD(const char* key);
template <typename T, typename DEF > T getD(int ikey);
template <typename T> T get(const char* key)
{
return getD <T, DefaultInitializer<T> > (key);
}
template <typename T> T get(int ikey)
{
return getD <T, DefaultInitializer<T> > (ikey);
}
template <typename T> T defaultGet(const char* key, T def);
template <typename T> T defaultGet(int ikey, T def);
template <typename T> void set(const char* key, T obj);
template <typename T> void set(int ikey, T obj);
};
class LuaFunctionBase
{
public:
virtual bool prepareStack(lua_State* L) = 0;
virtual void cleanStack(lua_State* L) = 0;
int addValues;
};
class Global : public LuaFunctionBase
{
public:
Global(const char* function_name)
{
addValues = 0;
name = function_name;
}
virtual bool prepareStack(lua_State* L)
{
lua_pushglobaltable(L);
lua_getfield(L, -1, name);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
return false;
}
return true;
}
virtual void cleanStack(lua_State* L) {}
const char* name;
};
class Method : public LuaFunctionBase
{
public:
Method(LuaTable obj, const char* function_name)
: table(obj)
{
addValues = 1;
name = function_name;
}
virtual bool prepareStack(lua_State* L)
{
if (table.my_L != L)
return false;
table.push();
lua_getfield(L, -1, name);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return false;
}
lua_pushvalue(L, -2); // first arg is self
return true;
}
virtual void cleanStack(lua_State* L)
{
lua_pop(L, 1); // remove table reference from stack
}
const char* name;
LuaTable table;
};
class LuaFunction : public LuaFunctionBase
{
public:
int handle;
lua_State* my_L;
LuaFunction() // stub ctor, for default initializers etc
{
my_L = 0; // make sure missuse of this constructor in code
// be noticed quick ;)
handle = 0;
addValues = 0;
}
LuaFunction(lua_State* L, int stackIndex) // reference a function on stack
{
if (lua_type(L, stackIndex) == LUA_TFUNCTION) {
lua_pushvalue(L, stackIndex);
handle = luaL_ref(L, LUA_REGISTRYINDEX);
my_L = L;
} else {
my_L = 0;
handle =0;
}
addValues = 0;
}
LuaFunction(const LuaFunction& src)
{
handle = src.handle;
my_L = src.my_L;
addValues = 0;
}
virtual ~LuaFunction() {};
virtual bool prepareStack(lua_State* L)
{
if (my_L != L)
return false;
lua_rawgeti(my_L, LUA_REGISTRYINDEX, handle);
return true;
}
virtual void cleanStack(lua_State* L)
{
}
void releaseReference()
{
luaL_unref(my_L, LUA_REGISTRYINDEX, handle);
my_L = 0;
}
};
template <>
class typeName<LuaTable>
{
public:
static const char* name() {return "LuaWrapper::LuaTable"; }
};
template <>
class typeName<LuaFunction>
{
public:
static const char* name() {return "LuaWrapper::LuaFunction"; }
};
template <typename T>
class typeName
{
public:
static const char* name() { return LuaClass<T>::luahandle(); }
};
template <typename T>
class typeName<T&> : public typeName<T> {};
template <typename T>
class typeName<const T&> : public typeName<T> {};
template <typename T>
class typeName<T*> : public typeName<T> {};
template <typename T, typename DEF=TypeError<T> >
class Unpack {};
template <typename DEF>
class Unpack<int, DEF>
{
public:
static int convert(lua_State* L, int index)
{
lua_Integer d = lua_tointeger(L, index);
if (d == 0 && !lua_isnumber(L, index))
return getDefault<int, DEF>(L, index);
return (int) d;
}
};
template <typename DEF>
class Unpack<unsigned int, DEF>
{
public:
static unsigned int convert(lua_State* L, int index)
{
lua_Integer d = lua_tointeger(L, index);
if (d == 0 && !lua_isnumber(L, index))
return getDefault<unsigned int, DEF>(L, index);
return (unsigned int) d;
}
};
template <typename DEF>
class Unpack<double, DEF>
{
public:
static double convert(lua_State* L, int index)
{
lua_Number d = lua_tonumber(L, index);
if (d == 0 && !lua_isnumber(L, index))
return getDefault<double, DEF>(L, index);
return (double) d;
}
};
template <typename DEF>
class Unpack<bool, DEF>
{
public:
static bool convert(lua_State* L, int index)
{
if (lua_type(L, index) != LUA_TBOOLEAN)
return getDefault<bool, DEF>(L, index);
return lua_toboolean(L, index) == 0 ? false : true;
}
};
template <typename DEF>
class Unpack<const char*, DEF>
{
public:
static const char* convert(lua_State* L, int index)
{
const char* str = lua_tostring(L, index);
if (str == 0)
return getDefault<const char*, DEF>(L, index);
return str;
}
};
template <typename DEF>
class Unpack<std::string, DEF>
{
public:
static std::string convert(lua_State* L, int index)
{
size_t strlen = 0;
const char* str = lua_tolstring(L, index, &strlen);
if (str == 0)
return getDefault<std::string, DEF>(L, index);
return std::string(str, strlen);
}
};
template <typename DEF>
class Unpack<std::string&, DEF>
{
public:
static std::string& convert(lua_State* L, int index)
{
typedef EncapsulatedReleaseItem<std::string> AutoRelease;
AutoRelease* rel = new AutoRelease(Unpack<std::string, DEF>::convert(L, index), L);
return rel->t;
}
};
template <typename DEF>
class Unpack<const std::string&, DEF>
{
public:
static const std::string& convert(lua_State* L, int index)
{
typedef EncapsulatedReleaseItem<std::string> AutoRelease;
AutoRelease* rel = new AutoRelease(Unpack<std::string, DEF>::convert(L, index), L);
return rel->t;
}
};
template <typename DEF>
class Unpack<LuaTable, DEF>
{
public:
static LuaTable convert(lua_State* L, int index)
{
LuaTable t(L, index);
if (t.my_L == 0)
return getDefault<LuaTable, DEF>(L, index);
return t;
}
};
template <typename DEF>
class Unpack<LuaFunction, DEF>
{
public:
static LuaFunction convert(lua_State* L, int index)
{
LuaFunction f(L, index);
if (f.my_L == 0)
return getDefault<LuaFunction, DEF>(L, index);
return f;
}
};
template <typename T, typename DEF>
class Unpack<T*, DEF>
{
public:
static T* convert(lua_State* L, int index)
{
T* t=LuaClass<T>::getPtr(L, index);
if (t == 0)
return getDefault<T*, DEF>(L, index);
return t;
}
};
template <typename T, typename DEF >
class Unpack<T&, DEF>
{
public:
static T& convert(lua_State* L, int index)
{
return *( Unpack<T*, DEF>::convert(L, index) );
}
};
template <typename T, typename DEF>
class Unpack<const T&, DEF>
{
public:
static const T& convert(lua_State* L, int index)
{
return *( Unpack<T*, DEF>::convert(L, index) );
}
};
template <typename T>
class Pack {};
template <>
class Pack<int>
{
public:
static void convert(lua_State* L, int value)
{
lua_pushinteger(L, value);
}
};
template <>
class Pack<unsigned int>
{
public:
static void convert(lua_State* L, unsigned int value)
{
lua_pushinteger(L, value);
}
};
template <>
class Pack<float>
{
public:
static void convert(lua_State* L, float value)
{
lua_pushnumber(L, value);
}
};
template <>
class Pack<double>
{
public:
static void convert(lua_State* L, double value)
{
lua_pushnumber(L, value);
}
};
template <>
class Pack<bool>
{
public:
static void convert(lua_State* L, bool value)
{
lua_pushboolean(L, value);
}
};
template <>
class Pack<const char*>
{
public:
static void convert(lua_State* L, const char* value)
{
lua_pushstring(L, value);
}
};
template <>
class Pack<std::string>
{
public:
static void convert(lua_State* L, std::string value)
{
lua_pushlstring(L, value.c_str(), value.size());
}
};
template <>
class Pack<std::string&>
{
public:
static void convert(lua_State* L, std::string value)
{
lua_pushlstring(L, value.c_str(), value.size());
}
};
template <>
class Pack<const std::string&>
{
public:
static void convert(lua_State* L, std::string value)
{
lua_pushlstring(L, value.c_str(), value.size());
}
};
template <>
class Pack<LuaTable>
{
public:
static void convert(lua_State* L, LuaTable value)
{
value.push();
}
};
template <>
class Pack<LuaFunction>
{
public:
static void convert(lua_State* L, LuaFunction value)
{
value.prepareStack(L);
}
};
template <typename T>
class Pack <T*>
{
public:
static void convert(lua_State* L, T* obj)
{
LuaClass<T>::packPtr(L, obj);
}
};
template <typename T>
class Pack <T&>
{
public:
static void convert(lua_State* L, T& obj)
{
LuaClass<T>::packPtr(L, *obj);
}
};
template <typename T, typename DEF> T LuaTable::getD(const char* key)
{
push();
lua_getfield(my_L, -1, key);
T ret = Unpack<T, DEF>::convert(my_L, -1);
lua_pop(my_L, 2);
return ret;
}
template <typename T, typename DEF> T LuaTable::getD(int ikey)
{
push();
lua_pushinteger(my_L, ikey);
lua_gettable(my_L, -2);
T ret = Unpack<T, DEF>::convert(my_L, -1);
lua_pop(my_L, 2);
return ret;
}
template <typename T> T LuaTable::defaultGet(const char* key, T def)
{
push();
lua_getfield(my_L, -1, key);
T ret = Unpack<T, DefaultInitializer<T, true> >::convert(my_L, -1);
if (lua_isnil(my_L, -1)) {
set(key, def);
lua_pop(my_L, 3);
return def;
}
lua_pop(my_L, 2);
return ret;
}
template <typename T> T LuaTable::defaultGet(int ikey, T def)
{
push();
lua_pushinteger(my_L, ikey);
lua_gettable(my_L, -2);
T ret = Unpack<T, DefaultInitializer<T, true> >::convert(my_L, -1);
if (lua_isnil(my_L, -1)) {
set(ikey, def);
lua_pop(my_L, 3);
return def;
}
lua_pop(my_L, 2);
return ret;
}
template <typename T> void LuaTable::set(const char* key, T obj)
{
push();
Pack<T>::convert(my_L, obj);
lua_setfield(my_L, -2, key);
lua_pop(my_L, 1);
}
template <typename T> void LuaTable::set(int ikey, T obj)
{
push();
lua_pushinteger(my_L, ikey);
Pack<T>::convert(my_L, obj);
lua_settable(my_L, -3);
lua_pop(my_L, 1);
}
template <typename ORIG, typename ALIAS>
class UnpackTypedef
{
public:
static ALIAS convert(lua_State* L, int index)
{
return (ALIAS)Unpack<ORIG>::convert(L, index);
}
};
template <typename ORIG, typename ALIAS>
class PackTypedef
{
public:
static void convert(lua_State* L, ALIAS value)
{
Pack<ORIG>::convert(L, (ORIG)value);
}
};
#include "LuaWrappers.hh"
// does not need to be expanded in LuaWrappers.hh hackery, since
// dtors never take arguments ;)
template <typename OBJ>
class DtorWrapper
{
public:
typedef OBJ myobjectT;
static int Wrapper(lua_State* L)
{
OBJ** obj = LuaClass<OBJ>::getRawPtr(L, 1);
if (*obj) {
delete *obj; *obj = 0;
}
return 0;
}
static const bool hasmeta = true;
static const bool noindex = false;
};
template <typename WRAPPER>
class ExportToLua {
public:
typedef typename WRAPPER::myobjectT objectT;
ExportToLua(lua_State* L, const char* module_name, const char* function_name)
{
luaL_Reg entry[2] = {{function_name, WRAPPER::Wrapper}, {NULL, NULL}};
if (WRAPPER::hasmeta) {
luaL_getmetatable(L, LuaClass<objectT>::luahandle());
if (strncmp(function_name, "__", 2) != 0 && !WRAPPER::noindex) {
lua_getfield(L, -1, "__index");
luaL_register(L, 0, entry);
lua_pop(L, 2);
} else { // allow direct registration of ctors and __* methods
luaL_register(L, 0, entry);
lua_pop(L, 1);
}
} else {
luaL_register(L, module_name, entry);
lua_pop(L, 1);
}
}
};
template <typename T>
void DeclareToLua(lua_State* L, const char* module_name = 0)
{
luaL_newmetatable(L, LuaClass<T>::luahandle());
if (module_name) {
// drop a reference to the metatable into the module table
lua_pushglobaltable(L);
luaL_findtable(L, -1, module_name, 1);
lua_remove(L, -2); // globaltable
lua_pushvalue(L, -2);
lua_setfield(L, -2, LuaClass<T>::luahandle());
lua_pop(L, 1);
}
// create index table
lua_newtable(L);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
}
template <typename BASE, typename DERIVED>
void InheritMeta(lua_State* L)
{
luaL_getmetatable(L, LuaClass<DERIVED>::luahandle()); // -5
lua_getfield(L, -1, "__index"); // -4
luaL_getmetatable(L, LuaClass<BASE>::luahandle()); // -3
lua_getfield(L, -1, "__index"); // -2
lua_pushnil(L); /* -1 first key */
while (lua_next(L, -2) != 0) {
lua_pushvalue(L, -2); // copy key and move down
lua_insert(L, -3);
lua_settable(L, -6); // DERIVED __index at -6
}
lua_pop(L, 5);
}
template <typename BASE, typename DERIVED>
void AllowDowncast(lua_State* L)
{
luaL_getmetatable(L, LuaClass<DERIVED>::luahandle());
// NYI: if used allow re-implement with new direct meta comparision
LuaClass<BASE>::data->insertMetaTablePtr((void*)lua_topointer(L, -1));
lua_pop(L, 1);
}
template <typename C>
void RegisterValue(lua_State* L, const char* module_name, const char* name, C value)
{
lua_pushglobaltable(L);
luaL_findtable(L, -1, module_name, 1);
lua_remove(L, -2); // globaltable
Pack<C>::convert(L, value);
lua_setfield(L, -2, name);
lua_pop(L, 1);
}
// convenient helper
inline void dumpStack (lua_State* L)
{
int top = lua_gettop(L);
for (int i = 1; i <= top; ++i)
{
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING: printf("'%s' ", lua_tostring(L, i)); break;
case LUA_TBOOLEAN: printf("%s ", lua_toboolean(L, i) ? "true" : "false"); break;
case LUA_TNUMBER: printf("%g ", lua_tonumber(L, i)); break;
default: printf("%s ", lua_typename(L, t)); break;
}
}
printf("\n");
}
inline void insertLoader(lua_State* L, int (*fptr)(lua_State* L))
{
lua_pushglobaltable(L);
luaL_findtable(L, -1, "package.loaders", 0);
LuaWrapper::LuaTable loaders(L, -1);
lua_pop(L, 2);
int i = 1;
while (loaders.exists(i))
++i;
// move all loaders by one, to insert ours at the first index
for (; i > 0; --i)
{
loaders.push(); // tables registry handle
lua_pushinteger(L, i);
if (i > 1) {
lua_pushinteger(L, i - 1);
lua_gettable(L, -3);
lua_settable(L, -3);
}
else {
lua_pushcclosure(L, fptr, 0);
lua_settable(L, -3);
}
lua_pop(L, 1); // table ref
}
loaders.releaseReference();
}
}
// some convenient defines:
#define ToLuaTypedef(ORIG, ALIAS) \
namespace LuaWrapper { \
template <> class Unpack<ALIAS>:public UnpackTypedef<ORIG, ALIAS>{}; \
template <> class Pack<ALIAS>:public PackTypedef<ORIG, ALIAS>{}; }
#endif
|