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 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
|
/*
* File: clua.cc
* Created by: dshaligram on Wed Aug 2 12:54:15 2006 UTC
*/
#include "AppHdr.h"
#include "clua.h"
#include "cluautil.h"
#include "dlua.h"
#include "l_libs.h"
#include "files.h"
#include "libutil.h"
#include "state.h"
#include "stuff.h"
#include <algorithm>
#define BUGGY_PCALL_ERROR "667: Malformed response to guarded pcall."
#define BUGGY_SCRIPT_ERROR "666: Killing badly-behaved Lua script."
#define CL_RESETSTACK_RETURN(ls, oldtop, retval) \
do \
{\
if (oldtop != lua_gettop(ls)) \
{ \
lua_settop(ls, oldtop); \
} \
return (retval); \
} \
while (false)
static int _clua_panic(lua_State *);
static void _clua_throttle_hook(lua_State *, lua_Debug *);
static void *_clua_allocator(void *ud, void *ptr, size_t osize, size_t nsize);
static int _clua_guarded_pcall(lua_State *);
static int _clua_require(lua_State *);
static int _clua_dofile(lua_State *);
static int _clua_loadfile(lua_State *);
CLua::CLua(bool managed)
: error(), managed_vm(managed), shutting_down(false),
throttle_unit_lines(10000),
throttle_sleep_ms(0), throttle_sleep_start(2),
throttle_sleep_end(800), n_throttle_sleeps(0), mixed_call_depth(0),
lua_call_depth(0), max_mixed_call_depth(8),
max_lua_call_depth(100), memory_used(0),
_state(NULL), sourced_files(), uniqindex(0L)
{
}
CLua::~CLua()
{
// Copy the listener vector, because listeners may remove
// themselves from the listener list when we notify them of a
// shutdown.
const std::vector<lua_shutdown_listener*> slisteners = shutdown_listeners;
for (int i = 0, size = slisteners.size(); i < size; ++i)
slisteners[i]->shutdown(*this);
shutting_down = true;
if (_state)
lua_close(_state);
}
lua_State *CLua::state()
{
if (!_state)
init_lua();
return _state;
}
void CLua::setglobal(const char *name)
{
lua_setglobal(state(), name);
}
void CLua::getglobal(const char *name)
{
lua_getglobal(state(), name);
}
std::string CLua::setuniqregistry()
{
char name[100];
snprintf(name, sizeof name, "__cru%u", uniqindex++);
lua_pushstring(state(), name);
lua_insert(state(), -2);
lua_settable(state(), LUA_REGISTRYINDEX);
return (name);
}
void CLua::setregistry(const char *name)
{
lua_pushstring(state(), name);
// Slide name round before the value
lua_insert(state(), -2);
lua_settable(state(), LUA_REGISTRYINDEX);
}
void CLua::_getregistry(lua_State *ls, const char *name)
{
lua_pushstring(ls, name);
lua_gettable(ls, LUA_REGISTRYINDEX);
}
void CLua::getregistry(const char *name)
{
_getregistry(state(), name);
}
void CLua::gc()
{
lua_gc(state(), LUA_GCCOLLECT, 0);
}
void CLua::save(const char *file)
{
if (!_state)
return;
CLuaSave clsave = { file, NULL };
callfn("c_save", "u", &clsave);
if (clsave.handle)
fclose(clsave.handle);
}
int CLua::file_write(lua_State *ls)
{
if (!lua_islightuserdata(ls, 1))
{
luaL_argerror(ls, 1, "Expected filehandle at arg 1");
return (0);
}
CLuaSave *sf = static_cast<CLuaSave *>( lua_touserdata(ls, 1) );
if (!sf)
return (0);
FILE *f = sf->get_file();
if (!f)
return (0);
const char *text = luaL_checkstring(ls, 2);
if (text)
fprintf(f, "%s", text);
return (0);
}
FILE *CLua::CLuaSave::get_file()
{
if (!handle)
handle = fopen(filename, "w");
return (handle);
}
void CLua::set_error(int err, lua_State *ls)
{
if (!err)
{
error.clear();
return;
}
if (!ls && !(ls = _state))
{
error = "<LUA not initialised>";
return;
}
const char *serr = lua_tostring(ls, -1);
lua_pop(ls, 1);
error = serr? serr : "<Unknown error>";
}
void CLua::init_throttle()
{
if (!managed_vm)
return;
if (throttle_unit_lines <= 0)
throttle_unit_lines = 500;
if (throttle_sleep_start < 1)
throttle_sleep_start = 1;
if (throttle_sleep_end < throttle_sleep_start)
throttle_sleep_end = throttle_sleep_start;
if (!mixed_call_depth)
{
lua_sethook(_state, _clua_throttle_hook,
LUA_MASKCOUNT, throttle_unit_lines);
throttle_sleep_ms = 0;
n_throttle_sleeps = 0;
}
}
int CLua::loadbuffer(const char *buf, size_t size, const char *context)
{
const int err = luaL_loadbuffer(state(), buf, size, context);
set_error(err, state());
return err;
}
int CLua::loadstring(const char *s, const char *context)
{
return loadbuffer(s, strlen(s), context);
}
int CLua::execstring(const char *s, const char *context, int nresults)
{
int err = 0;
if ((err = loadstring(s, context)))
return (err);
lua_State *ls = state();
lua_call_throttle strangler(this);
err = lua_pcall(ls, 0, nresults, 0);
set_error(err, ls);
return err;
}
bool CLua::is_path_safe(std::string s, bool trusted)
{
lowercase(s);
return (s.find("..") == std::string::npos && shell_safe(s.c_str())
&& (trusted || s.find("clua") == std::string::npos));
}
int CLua::loadfile(lua_State *ls, const char *filename, bool trusted,
bool die_on_fail)
{
if (!ls)
return (-1);
if (!is_path_safe(filename, trusted))
{
lua_pushstring(
ls,
make_stringf("invalid filename: %s", filename).c_str());
return (-1);
}
std::string file = datafile_path(filename, die_on_fail);
if (file.empty())
{
lua_pushstring(ls,
make_stringf("Can't find \"%s\"", filename).c_str());
return (-1);
}
return (luaL_loadfile(ls, file.c_str()));
}
int CLua::execfile(const char *filename, bool trusted, bool die_on_fail,
bool force)
{
if (!force && sourced_files.find(filename) != sourced_files.end())
return 0;
sourced_files.insert(filename);
lua_State *ls = state();
int err = loadfile(ls, filename, trusted || !managed_vm, die_on_fail);
lua_call_throttle strangler(this);
if (!err)
err = lua_pcall(ls, 0, 0, 0);
set_error(err);
if (die_on_fail && !error.empty())
end(1, false, "Lua execfile error (%s): %s",
filename, dlua.error.c_str());
return (err);
}
bool CLua::runhook(const char *hook, const char *params, ...)
{
error.clear();
lua_State *ls = state();
if (!ls)
return (false);
// Remember top of stack, for debugging porpoises
int stack_top = lua_gettop(ls);
pushglobal(hook);
if (!lua_istable(ls, -1))
{
lua_pop(ls, 1);
CL_RESETSTACK_RETURN( ls, stack_top, false );
}
for (int i = 1; ; ++i)
{
int currtop = lua_gettop(ls);
lua_rawgeti(ls, -1, i);
if (!lua_isfunction(ls, -1))
{
lua_pop(ls, 1);
break;
}
// So what's on top *is* a function. Call it with the args we have.
va_list args;
va_start(args, params);
calltopfn(ls, params, args);
va_end(args);
lua_settop(ls, currtop);
}
CL_RESETSTACK_RETURN( ls, stack_top, true );
}
void CLua::fnreturns(const char *format, ...)
{
lua_State *ls = _state;
if (!format || !ls)
return;
va_list args;
va_start(args, format);
vfnreturns(format, args);
va_end(args);
}
void CLua::vfnreturns(const char *format, va_list args)
{
lua_State *ls = _state;
int nrets = return_count(ls, format);
int sp = -nrets - 1;
const char *gs = strchr(format, '>');
if (gs)
format = gs + 1;
else if ((gs = strchr(format, ':')))
format = gs + 1;
for (const char *run = format; *run; ++run)
{
char argtype = *run;
++sp;
switch (argtype)
{
case 'u':
if (lua_islightuserdata(ls, sp))
*(va_arg(args, void**)) = lua_touserdata(ls, sp);
break;
case 'd':
if (lua_isnumber(ls, sp))
*(va_arg(args, int*)) = luaL_checkint(ls, sp);
break;
case 'b':
*(va_arg(args, bool *)) = lua_toboolean(ls, sp);
break;
case 's':
{
const char *s = lua_tostring(ls, sp);
if (s)
*(va_arg(args, std::string *)) = s;
break;
}
default:
break;
}
}
// Pop args off the stack
lua_pop(ls, nrets);
}
int CLua::push_args(lua_State *ls, const char *format, va_list args,
va_list *targ)
{
if (!format)
{
if (targ)
va_copy(*targ, args);
return (0);
}
const char *cs = strchr(format, ':');
if (cs)
format = cs + 1;
int argc = 0;
for (const char *run = format; *run; run++)
{
if (*run == '>')
break;
char argtype = *run;
++argc;
switch (argtype)
{
case 'u': // Light userdata
lua_pushlightuserdata(ls, va_arg(args, void*));
break;
case 'i':
clua_push_item(ls, va_arg(args, item_def*));
break;
case 's': // String
{
const char *s = va_arg(args, const char *);
if (s)
lua_pushstring(ls, s);
else
lua_pushnil(ls);
break;
}
case 'd': // Integer
lua_pushnumber(ls, va_arg(args, int));
break;
case 'L':
ASSERT("ambiguous long in Lua push_args");
lua_pushnumber(ls, va_arg(args, long));
break;
case 'b':
lua_pushboolean(ls, va_arg(args, int));
break;
case 'D':
clua_push_dgn_event(ls, va_arg(args, const dgn_event *));
break;
case 'm':
clua_push_map(ls, va_arg(args, map_def *));
break;
case 'M':
push_monster(ls, va_arg(args, monsters *));
break;
case 'I':
lua_push_moninf(ls, va_arg(args, monster_info *));
break;
case 'A':
argc += push_activity_interrupt(
ls, va_arg(args, activity_interrupt_data *));
break;
default:
--argc;
break;
}
}
if (targ)
va_copy(*targ, args);
return (argc);
}
int CLua::return_count(lua_State *ls, const char *format)
{
if (!format)
return (0);
const char *gs = strchr(format, '>');
if (gs)
return (strlen(gs + 1));
const char *cs = strchr(format, ':');
if (cs && isdigit(*format))
{
char *es = NULL;
int ci = strtol(format, &es, 10);
// We're capping return at 10 here, which is arbitrary, but avoids
// blowing the stack.
if (ci < 0)
ci = 0;
else if (ci > 10)
ci = 10;
return (ci);
}
return (0);
}
bool CLua::calltopfn(lua_State *ls, const char *params, va_list args,
int retc, va_list *copyto)
{
// We guarantee to remove the function from the stack
int argc = push_args(ls, params, args, copyto);
if (retc == -1)
retc = return_count(ls, params);
lua_call_throttle strangler(this);
int err = lua_pcall(ls, argc, retc, 0);
set_error(err, ls);
return (!err);
}
maybe_bool CLua::callmbooleanfn(const char *fn, const char *params,
va_list args)
{
error.clear();
lua_State *ls = state();
if (!ls)
return (B_MAYBE);
int stacktop = lua_gettop(ls);
pushglobal(fn);
if (!lua_isfunction(ls, -1))
{
lua_pop(ls, 1);
CL_RESETSTACK_RETURN(ls, stacktop, B_MAYBE);
}
bool ret = calltopfn(ls, params, args, 1);
if (!ret)
CL_RESETSTACK_RETURN(ls, stacktop, B_MAYBE);
maybe_bool r = frombool(lua_toboolean(ls, -1));
CL_RESETSTACK_RETURN(ls, stacktop, r);
}
maybe_bool CLua::callmbooleanfn(const char *fn, const char *params, ...)
{
va_list args;
va_start(args, params);
return (callmbooleanfn(fn, params, args));
}
bool CLua::callbooleanfn(bool def, const char *fn, const char *params, ...)
{
va_list args;
va_start(args, params);
maybe_bool r = callmbooleanfn(fn, params, args);
return (tobool(r, def));
}
bool CLua::proc_returns(const char *par) const
{
return (strchr(par, '>') != NULL);
}
// Identical to lua_getglobal for simple names, but will look up
// "a.b.c" names in tables, so you can pushglobal("dgn.point") and get
// _G['dgn']['point'], as expected.
//
// Guarantees to push exactly one value onto the stack.
//
void CLua::pushglobal(const std::string &name)
{
std::vector<std::string> pieces = split_string(".", name);
lua_State *ls(state());
if (pieces.empty())
lua_pushnil(ls);
for (unsigned i = 0, size = pieces.size(); i < size; ++i)
{
if (!i)
lua_getglobal(ls, pieces[i].c_str());
else
{
if (lua_istable(ls, -1))
{
lua_pushstring(ls, pieces[i].c_str());
lua_gettable(ls, -2);
// Swap the value we just found with the table itself.
lua_insert(ls, -2);
// And remove the table.
lua_pop(ls, 1);
}
else
{
// We expected a table here, but got something else. Fail.
lua_pop(ls, 1);
lua_pushnil(ls);
break;
}
}
}
}
bool CLua::callfn(const char *fn, const char *params, ...)
{
error.clear();
lua_State *ls = state();
if (!ls)
return (false);
pushglobal(fn);
if (!lua_isfunction(ls, -1))
{
lua_pop(ls, 1);
return (false);
}
va_list args;
va_list fnret;
va_start(args, params);
bool ret = calltopfn(ls, params, args, -1, &fnret);
if (ret)
{
// If we have a > in format, gather return params now.
if (proc_returns(params))
vfnreturns(params, fnret);
}
va_end(args);
va_end(fnret);
return (ret);
}
bool CLua::callfn(const char *fn, int nargs, int nret)
{
error.clear();
lua_State *ls = state();
if (!ls)
return (false);
// If a function is not provided on the stack, get the named function.
if (fn)
{
pushglobal(fn);
if (!lua_isfunction(ls, -1))
{
lua_settop(ls, -nargs - 2);
return (false);
}
// Slide the function in front of its args and call it.
if (nargs)
lua_insert(ls, -nargs - 1);
}
lua_call_throttle strangler(this);
int err = lua_pcall(ls, nargs, nret, 0);
set_error(err, ls);
return !err;
}
void CLua::init_lua()
{
if (_state)
return;
_state = managed_vm? lua_newstate(_clua_allocator, this) : luaL_newstate();
if (!_state)
end(1, false, "Unable to create Lua state.");
lua_stack_cleaner clean(_state);
lua_atpanic(_state, _clua_panic);
luaopen_base(_state);
luaopen_string(_state);
luaopen_table(_state);
luaopen_math(_state);
// Open Crawl bindings
cluaopen_kills(_state);
cluaopen_you(_state);
cluaopen_item(_state);
cluaopen_food(_state);
cluaopen_crawl(_state);
cluaopen_file(_state);
cluaopen_moninf(_state);
cluaopen_options(_state);
cluaopen_travel(_state);
cluaopen_view(_state);
cluaopen_globals(_state);
load_cmacro();
load_chooks();
lua_register(_state, "loadfile", _clua_loadfile);
lua_register(_state, "dofile", _clua_dofile);
lua_register(_state, "require", _clua_require);
execfile("clua/util.lua", true, true);
execfile("clua/iter.lua", true, true);
execfile("clua/init.lua", true, true);
if (managed_vm)
{
lua_register(_state, "pcall", _clua_guarded_pcall);
execfile("clua/userbase.lua", true, true);
}
lua_pushboolean(_state, managed_vm);
setregistry("lua_vm_is_managed");
lua_pushlightuserdata(_state, this);
setregistry("__clua");
}
CLua &CLua::get_vm(lua_State *ls)
{
lua_stack_cleaner clean(ls);
_getregistry(ls, "__clua");
CLua *vm = clua_get_lightuserdata<CLua>(ls, -1);
if (!vm)
luaL_error(ls, "Could not find matching clua for lua state");
return (*vm);
}
bool CLua::is_managed_vm(lua_State *ls)
{
lua_stack_cleaner clean(ls);
lua_pushstring(ls, "lua_vm_is_managed");
lua_gettable(ls, LUA_REGISTRYINDEX);
return (lua_toboolean(ls, -1));
}
void CLua::load_chooks()
{
// All hook names must be chk_????
static const char *c_hooks =
"chk_startgame = { }"
;
execstring(c_hooks, "base");
}
void CLua::load_cmacro()
{
execfile("clua/macro.lua", true, true);
}
void CLua::add_shutdown_listener(lua_shutdown_listener *listener)
{
if (std::find(shutdown_listeners.begin(), shutdown_listeners.end(),
listener) == shutdown_listeners.end())
shutdown_listeners.push_back(listener);
}
void CLua::remove_shutdown_listener(lua_shutdown_listener *listener)
{
std::vector<lua_shutdown_listener*>::iterator i =
std::find(shutdown_listeners.begin(), shutdown_listeners.end(),
listener);
if (i != shutdown_listeners.end())
shutdown_listeners.erase(i);
}
// Can be called from within a debugger to look at the current Lua
// call stack. (Borrowed from ToME 3)
void CLua::print_stack()
{
struct lua_Debug dbg;
int i = 0;
lua_State *L = state();
fprintf(stderr, "\n");
while (lua_getstack(L, i++, &dbg) == 1)
{
lua_getinfo(L, "lnuS", &dbg);
char* file = strrchr(dbg.short_src, '/');
if (file == NULL)
file = dbg.short_src;
else
file++;
fprintf(stderr, "%s, function %s, line %d\n", file,
dbg.name, dbg.currentline);
}
fprintf(stderr, "\n");
}
////////////////////////////////////////////////////////////////////////
// lua_text_pattern
// We could simplify this a great deal by just using lex and yacc, but I
// don't know if we want to introduce them.
struct lua_pat_op
{
const char *token;
const char *luatok;
bool pretext; // Does this follow a pattern?
bool posttext; // Is this followed by a pattern?
};
static lua_pat_op pat_ops[] =
{
{ "<<", " ( ", false, true },
{ ">>", " ) ", true, false },
{ "!!", " not ", false, true },
{ "==", " == ", true, true },
{ "^^", " ~= ", true, true },
{ "&&", " and ", true, true },
{ "||", " or ", true, true },
};
unsigned int lua_text_pattern::lfndx = 0;
bool lua_text_pattern::is_lua_pattern(const std::string &s)
{
for (int i = 0, size = sizeof(pat_ops) / sizeof(*pat_ops);
i < size; ++i)
{
if (s.find(pat_ops[i].token) != std::string::npos)
return (true);
}
return (false);
}
lua_text_pattern::lua_text_pattern(const std::string &_pattern)
: translated(false), isvalid(true), pattern(_pattern), lua_fn_name()
{
lua_fn_name = new_fn_name();
}
lua_text_pattern::~lua_text_pattern()
{
if (translated && !lua_fn_name.empty())
{
lua_State *ls = clua;
if (ls)
{
lua_pushnil(ls);
clua.setglobal(lua_fn_name.c_str());
}
}
}
bool lua_text_pattern::valid() const
{
return translated? isvalid : translate();
}
bool lua_text_pattern::matches( const std::string &s ) const
{
if (isvalid && !translated)
translate();
if (!isvalid)
return (false);
return clua.callbooleanfn(false, lua_fn_name.c_str(), "s", s.c_str());
}
void lua_text_pattern::pre_pattern(std::string &pat, std::string &fn) const
{
// Trim trailing spaces
pat.erase( pat.find_last_not_of(" \t\n\r") + 1 );
fn += " pmatch([[";
fn += pat;
fn += "]], text, false) ";
pat.clear();
}
void lua_text_pattern::post_pattern(std::string &pat, std::string &fn) const
{
pat.erase( 0, pat.find_first_not_of(" \t\n\r") );
fn += " pmatch([[";
fn += pat;
fn += "]], text, false) ";
pat.clear();
}
std::string lua_text_pattern::new_fn_name()
{
return (make_stringf("__ch_stash_search_%u", lfndx++));
}
bool lua_text_pattern::translate() const
{
if (translated || !isvalid)
return (false);
if (pattern.find("]]") != std::string::npos
|| pattern.find("[[") != std::string::npos)
{
return (false);
}
std::string textp;
std::string luafn;
const lua_pat_op *currop = NULL;
for (std::string::size_type i = 0; i < pattern.length(); ++i)
{
bool match = false;
for (unsigned p = 0; p < sizeof pat_ops / sizeof *pat_ops; ++p)
{
const lua_pat_op &lop = pat_ops[p];
if (pattern.find(lop.token, i) == i)
{
match = true;
if (lop.pretext && (!currop || currop->posttext))
{
if (currop)
textp.erase(0, textp.find_first_not_of(" \r\n\t"));
pre_pattern(textp, luafn);
}
currop = &lop;
luafn += lop.luatok;
i += strlen( lop.token ) - 1;
break;
}
}
if (match)
continue;
textp += pattern[i];
}
if (currop && currop->posttext)
post_pattern(textp, luafn);
luafn = "function " + lua_fn_name + "(text) return " + luafn + " end";
const_cast<lua_text_pattern *>(this)->translated = true;
int err = clua.execstring( luafn.c_str(), "stash-search" );
if (err)
{
lua_text_pattern *self = const_cast<lua_text_pattern *>(this);
self->isvalid = self->translated = false;
}
return translated;
}
//////////////////////////////////////////////////////////////////////////
lua_call_throttle::lua_clua_map lua_call_throttle::lua_map;
// A panic function for the Lua interpreter, usually called when it
// runs out of memory when trying to load a file or a chunk of Lua from
// an unprotected Lua op. The only cases of unprotected Lua loads are
// loads of Lua code from .crawlrc, which is read at start of game.
//
// If there's an inordinately large .crawlrc (we're talking seriously
// massive here) that wants more memory than we're willing to give
// Lua, then the game will save and exit until the .crawlrc is fixed.
//
// Lua can also run out of memory during protected script execution,
// such as when running a macro or some other game hook, but in such
// cases the Lua interpreter will throw an exception instead of
// panicking.
//
static int _clua_panic(lua_State *ls)
{
if (crawl_state.need_save && !crawl_state.saving_game
&& !crawl_state.updating_scores)
{
save_game(true);
}
return (0);
}
static void *_clua_allocator(void *ud, void *ptr, size_t osize, size_t nsize)
{
CLua *cl = static_cast<CLua *>( ud );
cl->memory_used += nsize - osize;
if (nsize > osize && cl->memory_used >= CLUA_MAX_MEMORY_USE * 1024
&& cl->mixed_call_depth)
{
return (NULL);
}
if (!nsize)
{
free(ptr);
return (NULL);
}
else
return (realloc(ptr, nsize));
}
static void _clua_throttle_hook(lua_State *ls, lua_Debug *dbg)
{
CLua *lua = lua_call_throttle::find_clua(ls);
// Co-routines can create a new Lua state; in such cases, we must
// fudge it.
if (!lua)
lua = &clua;
if (lua)
{
if (!lua->throttle_sleep_ms)
lua->throttle_sleep_ms = lua->throttle_sleep_start;
else if (lua->throttle_sleep_ms < lua->throttle_sleep_end)
lua->throttle_sleep_ms *= 2;
++lua->n_throttle_sleeps;
delay(lua->throttle_sleep_ms);
// Try to kill the annoying script.
if (lua->n_throttle_sleeps > CLua::MAX_THROTTLE_SLEEPS)
{
lua->n_throttle_sleeps = CLua::MAX_THROTTLE_SLEEPS;
luaL_error(ls, BUGGY_SCRIPT_ERROR);
}
}
}
lua_call_throttle::lua_call_throttle(CLua *_lua)
: lua(_lua)
{
lua->init_throttle();
if (!lua->mixed_call_depth++)
lua_map[lua->state()] = lua;
}
lua_call_throttle::~lua_call_throttle()
{
if (!--lua->mixed_call_depth)
lua_map.erase(lua->state());
}
CLua *lua_call_throttle::find_clua(lua_State *ls)
{
lua_clua_map::iterator i = lua_map.find(ls);
return (i != lua_map.end()? i->second : NULL);
}
// This function is a replacement for Lua's in-built pcall function. It behaves
// like pcall in all respects (as documented in the Lua 5.1 reference manual),
// but does not allow the Lua chunk/script to catch errors thrown by the
// Lua-throttling code. This is necessary so that we can interrupt scripts that
// are hogging CPU.
//
// If we did not intercept pcall, the script could do the equivalent
// of this:
//
// while true do
// pcall(function () while true do end end)
// end
//
// And there's a good chance we wouldn't be able to interrupt the
// deadloop because our errors would get caught by the pcall (more
// levels of nesting would just increase the chance of the script
// beating our throttling).
//
static int _clua_guarded_pcall(lua_State *ls)
{
const int nargs = lua_gettop(ls);
const int err = lua_pcall(ls, nargs - 1, LUA_MULTRET, 0);
if (err)
{
const char *errs = lua_tostring(ls, 1);
if (!errs || strstr(errs, BUGGY_SCRIPT_ERROR))
luaL_error(ls, errs? errs : BUGGY_PCALL_ERROR);
}
lua_pushboolean(ls, !err);
lua_insert(ls, 1);
return (lua_gettop(ls));
}
static int _clua_loadfile(lua_State *ls)
{
const char *file = luaL_checkstring(ls, 1);
if (!file)
return (0);
const int err = CLua::loadfile(ls, file, !CLua::is_managed_vm(ls));
if (err)
{
const int place = lua_gettop(ls);
lua_pushnil(ls);
lua_insert(ls, place);
return (2);
}
return (1);
}
static int _clua_require(lua_State *ls)
{
const char *file = luaL_checkstring(ls, 1);
if (!file)
return (0);
CLua &vm(CLua::get_vm(ls));
if (vm.execfile(file, false, false) != 0)
luaL_error(ls, vm.error.c_str());
lua_pushboolean(ls, true);
return (1);
}
static int _clua_dofile(lua_State *ls)
{
const char *file = luaL_checkstring(ls, 1);
if (!file)
return (0);
const int err = CLua::loadfile(ls, file, !CLua::is_managed_vm(ls));
if (err)
return (lua_error(ls));
lua_call(ls, 0, LUA_MULTRET);
return (lua_gettop(ls));
}
std::string quote_lua_string(const std::string &s)
{
return replace_all_of(replace_all_of(s, "\\", "\\\\"), "\"", "\\\"");
}
/////////////////////////////////////////////////////////////////////
lua_shutdown_listener::~lua_shutdown_listener()
{
}
lua_datum::lua_datum(CLua &_lua, int stackpos, bool pop)
: lua(_lua), need_cleanup(true)
{
// Store the datum in the registry indexed by "this".
lua_pushvalue(lua, stackpos);
lua_pushlightuserdata(lua, this);
// Move the key (this) before the value.
lua_insert(lua, -2);
lua_settable(lua, LUA_REGISTRYINDEX);
if (pop && stackpos < 0)
lua_pop(lua, -stackpos);
lua.add_shutdown_listener(this);
}
lua_datum::lua_datum(const lua_datum &o)
: lua(o.lua), need_cleanup(true)
{
set_from(o);
}
void lua_datum::set_from(const lua_datum &o)
{
lua_pushlightuserdata(lua, this);
o.push();
lua_settable(lua, LUA_REGISTRYINDEX);
lua.add_shutdown_listener(this);
need_cleanup = true;
}
const lua_datum &lua_datum::operator = (const lua_datum &o)
{
if (this != &o)
{
cleanup();
set_from(o);
}
return (*this);
}
void lua_datum::push() const
{
lua_pushlightuserdata(lua, const_cast<lua_datum*>(this));
lua_gettable(lua, LUA_REGISTRYINDEX);
// The value we saved is now on top of the Lua stack.
}
lua_datum::~lua_datum()
{
cleanup();
}
void lua_datum::shutdown(CLua &)
{
cleanup();
}
void lua_datum::cleanup()
{
if (need_cleanup)
{
need_cleanup = false;
lua.remove_shutdown_listener(this);
lua_pushlightuserdata(lua, this);
lua_pushnil(lua);
lua_settable(lua, LUA_REGISTRYINDEX);
}
}
#define LUA_CHECK_TYPE(check) \
lua_stack_cleaner clean(lua); \
push(); \
return check(lua, -1)
bool lua_datum::is_table() const
{
LUA_CHECK_TYPE(lua_istable);
}
bool lua_datum::is_function() const
{
LUA_CHECK_TYPE(lua_isfunction);
}
bool lua_datum::is_number() const
{
LUA_CHECK_TYPE(lua_isnumber);
}
bool lua_datum::is_string() const
{
LUA_CHECK_TYPE(lua_isstring);
}
bool lua_datum::is_udata() const
{
LUA_CHECK_TYPE(lua_isuserdata);
}
|