compat-5.3.c.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. #include <sol/compatibility/compat-5.3.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. /* don't compile it again if it already is included via compat53.h */
  9. #ifndef KEPLER_PROJECT_COMPAT53_C_
  10. #define KEPLER_PROJECT_COMPAT53_C_
  11. /* definitions for Lua 5.1 only */
  12. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  13. #ifndef COMPAT53_FOPEN_NO_LOCK
  14. #if defined(_MSC_VER)
  15. #define COMPAT53_FOPEN_NO_LOCK 1
  16. #else /* otherwise */
  17. #define COMPAT53_FOPEN_NO_LOCK 0
  18. #endif /* VC++ only so far */
  19. #endif /* No-lock fopen_s usage if possible */
  20. #if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
  21. #include <share.h>
  22. #endif /* VC++ _fsopen for share-allowed file read */
  23. #ifndef COMPAT53_HAVE_STRERROR_R
  24. #if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined(__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6))
  25. #define COMPAT53_HAVE_STRERROR_R 1
  26. #else /* none of the defines matched: define to 0 */
  27. #define COMPAT53_HAVE_STRERROR_R 0
  28. #endif /* have strerror_r of some form */
  29. #endif /* strerror_r */
  30. #ifndef COMPAT53_HAVE_STRERROR_S
  31. #if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__)
  32. #define COMPAT53_HAVE_STRERROR_S 1
  33. #else /* not VC++ or C11 */
  34. #define COMPAT53_HAVE_STRERROR_S 0
  35. #endif /* strerror_s from VC++ or C11 */
  36. #endif /* strerror_s */
  37. #ifndef COMPAT53_LUA_FILE_BUFFER_SIZE
  38. #define COMPAT53_LUA_FILE_BUFFER_SIZE 4096
  39. #endif /* Lua File Buffer Size */
  40. static char* compat53_strerror(int en, char* buff, size_t sz) {
  41. #if COMPAT53_HAVE_STRERROR_R
  42. /* use strerror_r here, because it's available on these specific platforms */
  43. if (sz > 0) {
  44. buff[0] = '\0';
  45. /* we don't care whether the GNU version or the XSI version is used: */
  46. if (strerror_r(en, buff, sz)) {
  47. /* Yes, we really DO want to ignore the return value!
  48. * GCC makes that extra hard, not even a (void) cast will do. */
  49. }
  50. if (buff[0] == '\0') {
  51. /* Buffer is unchanged, so we probably have called GNU strerror_r which
  52. * returned a static constant string. Chances are that strerror will
  53. * return the same static constant string and therefore be thread-safe. */
  54. return strerror(en);
  55. }
  56. }
  57. return buff; /* sz is 0 *or* strerror_r wrote into the buffer */
  58. #elif COMPAT53_HAVE_STRERROR_S
  59. /* for MSVC and other C11 implementations, use strerror_s since it's
  60. * provided by default by the libraries */
  61. strerror_s(buff, sz, en);
  62. return buff;
  63. #else
  64. /* fallback, but strerror is not guaranteed to be threadsafe due to modifying
  65. * errno itself and some impls not locking a static buffer for it ... but most
  66. * known systems have threadsafe errno: this might only change if the locale
  67. * is changed out from under someone while this function is being called */
  68. (void)buff;
  69. (void)sz;
  70. return strerror(en);
  71. #endif
  72. }
  73. COMPAT53_API int lua_absindex(lua_State* L, int i) {
  74. if (i < 0 && i > LUA_REGISTRYINDEX)
  75. i += lua_gettop(L) + 1;
  76. return i;
  77. }
  78. static void compat53_call_lua(lua_State* L, char const code[], size_t len, int nargs, int nret) {
  79. lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
  80. if (lua_type(L, -1) != LUA_TFUNCTION) {
  81. lua_pop(L, 1);
  82. if (luaL_loadbuffer(L, code, len, "=none"))
  83. lua_error(L);
  84. lua_pushvalue(L, -1);
  85. lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
  86. }
  87. lua_insert(L, -nargs - 1);
  88. lua_call(L, nargs, nret);
  89. }
  90. COMPAT53_API void lua_arith(lua_State* L, int op) {
  91. static const char compat53_arith_code[]
  92. = "local op,a,b=...\n"
  93. "if op==0 then return a+b\n"
  94. "elseif op==1 then return a-b\n"
  95. "elseif op==2 then return a*b\n"
  96. "elseif op==3 then return a/b\n"
  97. "elseif op==4 then return a%b\n"
  98. "elseif op==5 then return a^b\n"
  99. "elseif op==6 then return -a\n"
  100. "end\n";
  101. if (op < LUA_OPADD || op > LUA_OPUNM)
  102. luaL_error(L, "invalid 'op' argument for lua_arith");
  103. luaL_checkstack(L, 5, "not enough stack slots");
  104. if (op == LUA_OPUNM)
  105. lua_pushvalue(L, -1);
  106. lua_pushnumber(L, op);
  107. lua_insert(L, -3);
  108. compat53_call_lua(L, compat53_arith_code, sizeof(compat53_arith_code) - 1, 3, 1);
  109. }
  110. COMPAT53_API int lua_compare(lua_State* L, int idx1, int idx2, int op) {
  111. static const char compat53_compare_code[]
  112. = "local a,b=...\n"
  113. "return a<=b\n";
  114. int result = 0;
  115. switch (op) {
  116. case LUA_OPEQ:
  117. return lua_equal(L, idx1, idx2);
  118. case LUA_OPLT:
  119. return lua_lessthan(L, idx1, idx2);
  120. case LUA_OPLE:
  121. luaL_checkstack(L, 5, "not enough stack slots");
  122. idx1 = lua_absindex(L, idx1);
  123. idx2 = lua_absindex(L, idx2);
  124. lua_pushvalue(L, idx1);
  125. lua_pushvalue(L, idx2);
  126. compat53_call_lua(L, compat53_compare_code, sizeof(compat53_compare_code) - 1, 2, 1);
  127. result = lua_toboolean(L, -1);
  128. lua_pop(L, 1);
  129. return result;
  130. default:
  131. luaL_error(L, "invalid 'op' argument for lua_compare");
  132. }
  133. return 0;
  134. }
  135. COMPAT53_API void lua_copy(lua_State* L, int from, int to) {
  136. int abs_to = lua_absindex(L, to);
  137. luaL_checkstack(L, 1, "not enough stack slots");
  138. lua_pushvalue(L, from);
  139. lua_replace(L, abs_to);
  140. }
  141. COMPAT53_API void lua_len(lua_State* L, int i) {
  142. switch (lua_type(L, i)) {
  143. case LUA_TSTRING:
  144. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  145. break;
  146. case LUA_TTABLE:
  147. if (!luaL_callmeta(L, i, "__len"))
  148. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  149. break;
  150. case LUA_TUSERDATA:
  151. if (luaL_callmeta(L, i, "__len"))
  152. break;
  153. /* FALLTHROUGH */
  154. default:
  155. luaL_error(L, "attempt to get length of a %s value", lua_typename(L, lua_type(L, i)));
  156. }
  157. }
  158. COMPAT53_API int lua_rawgetp(lua_State* L, int i, const void* p) {
  159. int abs_i = lua_absindex(L, i);
  160. lua_pushlightuserdata(L, (void*)p);
  161. lua_rawget(L, abs_i);
  162. return lua_type(L, -1);
  163. }
  164. COMPAT53_API void lua_rawsetp(lua_State* L, int i, const void* p) {
  165. int abs_i = lua_absindex(L, i);
  166. luaL_checkstack(L, 1, "not enough stack slots");
  167. lua_pushlightuserdata(L, (void*)p);
  168. lua_insert(L, -2);
  169. lua_rawset(L, abs_i);
  170. }
  171. COMPAT53_API lua_Number lua_tonumberx(lua_State* L, int i, int* isnum) {
  172. lua_Number n = lua_tonumber(L, i);
  173. if (isnum != NULL) {
  174. *isnum = (n != 0 || lua_isnumber(L, i));
  175. }
  176. return n;
  177. }
  178. COMPAT53_API void luaL_checkversion(lua_State* L) {
  179. (void)L;
  180. }
  181. COMPAT53_API void luaL_checkstack(lua_State* L, int sp, const char* msg) {
  182. if (!lua_checkstack(L, sp + LUA_MINSTACK)) {
  183. if (msg != NULL)
  184. luaL_error(L, "stack overflow (%s)", msg);
  185. else {
  186. lua_pushliteral(L, "stack overflow");
  187. lua_error(L);
  188. }
  189. }
  190. }
  191. COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char* name) {
  192. int abs_i = lua_absindex(L, i);
  193. luaL_checkstack(L, 3, "not enough stack slots");
  194. lua_pushstring(L, name);
  195. lua_gettable(L, abs_i);
  196. if (lua_istable(L, -1))
  197. return 1;
  198. lua_pop(L, 1);
  199. lua_newtable(L);
  200. lua_pushstring(L, name);
  201. lua_pushvalue(L, -2);
  202. lua_settable(L, abs_i);
  203. return 0;
  204. }
  205. COMPAT53_API lua_Integer luaL_len(lua_State* L, int i) {
  206. lua_Integer res = 0;
  207. int isnum = 0;
  208. luaL_checkstack(L, 1, "not enough stack slots");
  209. lua_len(L, i);
  210. res = lua_tointegerx(L, -1, &isnum);
  211. lua_pop(L, 1);
  212. if (!isnum)
  213. luaL_error(L, "object length is not an integer");
  214. return res;
  215. }
  216. COMPAT53_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
  217. luaL_checkstack(L, nup + 1, "too many upvalues");
  218. for (; l->name != NULL; l++) { /* fill the table with given functions */
  219. int i;
  220. lua_pushstring(L, l->name);
  221. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  222. lua_pushvalue(L, -(nup + 1));
  223. lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
  224. lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
  225. }
  226. lua_pop(L, nup); /* remove upvalues */
  227. }
  228. COMPAT53_API void luaL_setmetatable(lua_State* L, const char* tname) {
  229. luaL_checkstack(L, 1, "not enough stack slots");
  230. luaL_getmetatable(L, tname);
  231. lua_setmetatable(L, -2);
  232. }
  233. COMPAT53_API void* luaL_testudata(lua_State* L, int i, const char* tname) {
  234. void* p = lua_touserdata(L, i);
  235. luaL_checkstack(L, 2, "not enough stack slots");
  236. if (p == NULL || !lua_getmetatable(L, i))
  237. return NULL;
  238. else {
  239. int res = 0;
  240. luaL_getmetatable(L, tname);
  241. res = lua_rawequal(L, -1, -2);
  242. lua_pop(L, 2);
  243. if (!res)
  244. p = NULL;
  245. }
  246. return p;
  247. }
  248. static int compat53_countlevels(lua_State* L) {
  249. lua_Debug ar;
  250. int li = 1, le = 1;
  251. /* find an upper bound */
  252. while (lua_getstack(L, le, &ar)) {
  253. li = le;
  254. le *= 2;
  255. }
  256. /* do a binary search */
  257. while (li < le) {
  258. int m = (li + le) / 2;
  259. if (lua_getstack(L, m, &ar))
  260. li = m + 1;
  261. else
  262. le = m;
  263. }
  264. return le - 1;
  265. }
  266. static int compat53_findfield(lua_State* L, int objidx, int level) {
  267. if (level == 0 || !lua_istable(L, -1))
  268. return 0; /* not found */
  269. lua_pushnil(L); /* start 'next' loop */
  270. while (lua_next(L, -2)) { /* for each pair in table */
  271. if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
  272. if (lua_rawequal(L, objidx, -1)) { /* found object? */
  273. lua_pop(L, 1); /* remove value (but keep name) */
  274. return 1;
  275. }
  276. else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
  277. lua_remove(L, -2); /* remove table (but keep name) */
  278. lua_pushliteral(L, ".");
  279. lua_insert(L, -2); /* place '.' between the two names */
  280. lua_concat(L, 3);
  281. return 1;
  282. }
  283. }
  284. lua_pop(L, 1); /* remove value */
  285. }
  286. return 0; /* not found */
  287. }
  288. static int compat53_pushglobalfuncname(lua_State* L, lua_Debug* ar) {
  289. int top = lua_gettop(L);
  290. lua_getinfo(L, "f", ar); /* push function */
  291. lua_pushvalue(L, LUA_GLOBALSINDEX);
  292. if (compat53_findfield(L, top + 1, 2)) {
  293. lua_copy(L, -1, top + 1); /* move name to proper place */
  294. lua_pop(L, 2); /* remove pushed values */
  295. return 1;
  296. }
  297. else {
  298. lua_settop(L, top); /* remove function and global table */
  299. return 0;
  300. }
  301. }
  302. static void compat53_pushfuncname(lua_State* L, lua_Debug* ar) {
  303. if (*ar->namewhat != '\0') /* is there a name? */
  304. lua_pushfstring(L, "function " LUA_QS, ar->name);
  305. else if (*ar->what == 'm') /* main? */
  306. lua_pushliteral(L, "main chunk");
  307. else if (*ar->what == 'C') {
  308. if (compat53_pushglobalfuncname(L, ar)) {
  309. lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
  310. lua_remove(L, -2); /* remove name */
  311. }
  312. else
  313. lua_pushliteral(L, "?");
  314. }
  315. else
  316. lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
  317. }
  318. #define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
  319. #define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
  320. COMPAT53_API void luaL_traceback(lua_State* L, lua_State* L1, const char* msg, int level) {
  321. lua_Debug ar;
  322. int top = lua_gettop(L);
  323. int numlevels = compat53_countlevels(L1);
  324. int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
  325. if (msg)
  326. lua_pushfstring(L, "%s\n", msg);
  327. lua_pushliteral(L, "stack traceback:");
  328. while (lua_getstack(L1, level++, &ar)) {
  329. if (level == mark) { /* too many levels? */
  330. lua_pushliteral(L, "\n\t..."); /* add a '...' */
  331. level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
  332. }
  333. else {
  334. lua_getinfo(L1, "Slnt", &ar);
  335. lua_pushfstring(L, "\n\t%s:", ar.short_src);
  336. if (ar.currentline > 0)
  337. lua_pushfstring(L, "%d:", ar.currentline);
  338. lua_pushliteral(L, " in ");
  339. compat53_pushfuncname(L, &ar);
  340. lua_concat(L, lua_gettop(L) - top);
  341. }
  342. }
  343. lua_concat(L, lua_gettop(L) - top);
  344. }
  345. COMPAT53_API int luaL_fileresult(lua_State* L, int stat, const char* fname) {
  346. const char* serr = NULL;
  347. int en = errno; /* calls to Lua API may change this value */
  348. char buf[512] = { 0 };
  349. if (stat) {
  350. lua_pushboolean(L, 1);
  351. return 1;
  352. }
  353. else {
  354. lua_pushnil(L);
  355. serr = compat53_strerror(en, buf, sizeof(buf));
  356. if (fname)
  357. lua_pushfstring(L, "%s: %s", fname, serr);
  358. else
  359. lua_pushstring(L, serr);
  360. lua_pushnumber(L, (lua_Number)en);
  361. return 3;
  362. }
  363. }
  364. static int compat53_checkmode(lua_State* L, const char* mode, const char* modename, int err) {
  365. if (mode && strchr(mode, modename[0]) == NULL) {
  366. lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode);
  367. return err;
  368. }
  369. return LUA_OK;
  370. }
  371. typedef struct {
  372. lua_Reader reader;
  373. void* ud;
  374. int has_peeked_data;
  375. const char* peeked_data;
  376. size_t peeked_data_size;
  377. } compat53_reader_data;
  378. static const char* compat53_reader(lua_State* L, void* ud, size_t* size) {
  379. compat53_reader_data* data = (compat53_reader_data*)ud;
  380. if (data->has_peeked_data) {
  381. data->has_peeked_data = 0;
  382. *size = data->peeked_data_size;
  383. return data->peeked_data;
  384. }
  385. else
  386. return data->reader(L, data->ud, size);
  387. }
  388. COMPAT53_API int lua_load(lua_State* L, lua_Reader reader, void* data, const char* source, const char* mode) {
  389. int status = LUA_OK;
  390. compat53_reader_data compat53_data = { reader, data, 1, 0, 0 };
  391. compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size));
  392. if (compat53_data.peeked_data && compat53_data.peeked_data_size && compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */
  393. status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
  394. else
  395. status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
  396. if (status != LUA_OK)
  397. return status;
  398. /* we need to call the original 5.1 version of lua_load! */
  399. #undef lua_load
  400. return lua_load(L, compat53_reader, &compat53_data, source);
  401. #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
  402. }
  403. typedef struct {
  404. int n; /* number of pre-read characters */
  405. FILE* f; /* file being read */
  406. char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
  407. } compat53_LoadF;
  408. static const char* compat53_getF(lua_State* L, void* ud, size_t* size) {
  409. compat53_LoadF* lf = (compat53_LoadF*)ud;
  410. (void)L; /* not used */
  411. if (lf->n > 0) { /* are there pre-read characters to be read? */
  412. *size = lf->n; /* return them (chars already in buffer) */
  413. lf->n = 0; /* no more pre-read characters */
  414. }
  415. else { /* read a block from file */
  416. /* 'fread' can return > 0 *and* set the EOF flag. If next call to
  417. 'compat53_getF' called 'fread', it might still wait for user input.
  418. The next check avoids this problem. */
  419. if (feof(lf->f))
  420. return NULL;
  421. *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
  422. }
  423. return lf->buff;
  424. }
  425. static int compat53_errfile(lua_State* L, const char* what, int fnameindex) {
  426. char buf[512] = { 0 };
  427. const char* serr = compat53_strerror(errno, buf, sizeof(buf));
  428. const char* filename = lua_tostring(L, fnameindex) + 1;
  429. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  430. lua_remove(L, fnameindex);
  431. return LUA_ERRFILE;
  432. }
  433. static int compat53_skipBOM(compat53_LoadF* lf) {
  434. const char* p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
  435. int c;
  436. lf->n = 0;
  437. do {
  438. c = getc(lf->f);
  439. if (c == EOF || c != *(const unsigned char*)p++)
  440. return c;
  441. lf->buff[lf->n++] = (char)c; /* to be read by the parser */
  442. } while (*p != '\0');
  443. lf->n = 0; /* prefix matched; discard it */
  444. return getc(lf->f); /* return next character */
  445. }
  446. /*
  447. ** reads the first character of file 'f' and skips an optional BOM mark
  448. ** in its beginning plus its first line if it starts with '#'. Returns
  449. ** true if it skipped the first line. In any case, '*cp' has the
  450. ** first "valid" character of the file (after the optional BOM and
  451. ** a first-line comment).
  452. */
  453. static int compat53_skipcomment(compat53_LoadF* lf, int* cp) {
  454. int c = *cp = compat53_skipBOM(lf);
  455. if (c == '#') { /* first line is a comment (Unix exec. file)? */
  456. do { /* skip first line */
  457. c = getc(lf->f);
  458. } while (c != EOF && c != '\n');
  459. *cp = getc(lf->f); /* skip end-of-line, if present */
  460. return 1; /* there was a comment */
  461. }
  462. else
  463. return 0; /* no comment */
  464. }
  465. COMPAT53_API int luaL_loadfilex(lua_State* L, const char* filename, const char* mode) {
  466. compat53_LoadF lf;
  467. int status, readstatus;
  468. int c;
  469. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  470. if (filename == NULL) {
  471. lua_pushliteral(L, "=stdin");
  472. lf.f = stdin;
  473. }
  474. else {
  475. lua_pushfstring(L, "@%s", filename);
  476. #if defined(_MSC_VER)
  477. /* This code is here to stop a deprecation error that stops builds
  478. * if a certain macro is defined. While normally not caring would
  479. * be best, some header-only libraries and builds can't afford to
  480. * dictate this to the user. A quick check shows that fopen_s this
  481. * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
  482. * possibly even before that so we don't need to do any version
  483. * number checks, since this has been there since forever. */
  484. /* TO USER: if you want the behavior of typical fopen_s/fopen,
  485. * which does lock the file on VC++, define the macro used below to 0 */
  486. #if COMPAT53_FOPEN_NO_LOCK
  487. lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
  488. if (lf.f == NULL)
  489. return compat53_errfile(L, "open", fnameindex);
  490. #else /* use default locking version */
  491. if (fopen_s(&lf.f, filename, "r") != 0)
  492. return compat53_errfile(L, "open", fnameindex);
  493. #endif /* Locking vs. No-locking fopen variants */
  494. #else
  495. lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
  496. if (lf.f == NULL)
  497. return compat53_errfile(L, "open", fnameindex);
  498. #endif
  499. }
  500. if (compat53_skipcomment(&lf, &c)) /* read initial portion */
  501. lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
  502. if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
  503. #if defined(_MSC_VER)
  504. if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
  505. return compat53_errfile(L, "reopen", fnameindex);
  506. #else
  507. lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
  508. if (lf.f == NULL)
  509. return compat53_errfile(L, "reopen", fnameindex);
  510. #endif
  511. compat53_skipcomment(&lf, &c); /* re-read initial portion */
  512. }
  513. if (c != EOF)
  514. lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
  515. status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
  516. readstatus = ferror(lf.f);
  517. if (filename)
  518. fclose(lf.f); /* close file (even in case of errors) */
  519. if (readstatus) {
  520. lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
  521. return compat53_errfile(L, "read", fnameindex);
  522. }
  523. lua_remove(L, fnameindex);
  524. return status;
  525. }
  526. COMPAT53_API int luaL_loadbufferx(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode) {
  527. int status = LUA_OK;
  528. if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) {
  529. status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
  530. }
  531. else {
  532. status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
  533. }
  534. if (status != LUA_OK)
  535. return status;
  536. return luaL_loadbuffer(L, buff, sz, name);
  537. }
  538. #if !defined(l_inspectstat) \
  539. && (defined(unix) || defined(__unix) || defined(__unix__) || defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || (defined(__APPLE__) && defined(__MACH__)))
  540. /* some form of unix; check feature macros in unistd.h for details */
  541. #include <unistd.h>
  542. /* check posix version; the relevant include files and macros probably
  543. * were available before 2001, but I'm not sure */
  544. #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
  545. #include <sys/wait.h>
  546. #define l_inspectstat(stat, what) \
  547. if (WIFEXITED(stat)) { \
  548. stat = WEXITSTATUS(stat); \
  549. } \
  550. else if (WIFSIGNALED(stat)) { \
  551. stat = WTERMSIG(stat); \
  552. what = "signal"; \
  553. }
  554. #endif
  555. #endif
  556. /* provide default (no-op) version */
  557. #if !defined(l_inspectstat)
  558. #define l_inspectstat(stat, what) ((void)0)
  559. #endif
  560. COMPAT53_API int luaL_execresult(lua_State* L, int stat) {
  561. const char* what = "exit";
  562. if (stat == -1)
  563. return luaL_fileresult(L, 0, NULL);
  564. else {
  565. l_inspectstat(stat, what);
  566. if (*what == 'e' && stat == 0)
  567. lua_pushboolean(L, 1);
  568. else
  569. lua_pushnil(L);
  570. lua_pushstring(L, what);
  571. lua_pushinteger(L, stat);
  572. return 3;
  573. }
  574. }
  575. COMPAT53_API void luaL_buffinit(lua_State* L, luaL_Buffer_53* B) {
  576. /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
  577. B->b.p = NULL;
  578. B->b.L = NULL;
  579. B->b.lvl = 0;
  580. /* reuse the buffer from the 5.1-style luaL_Buffer though! */
  581. B->ptr = B->b.buffer;
  582. B->capacity = LUAL_BUFFERSIZE;
  583. B->nelems = 0;
  584. B->L2 = L;
  585. }
  586. COMPAT53_API char* luaL_prepbuffsize(luaL_Buffer_53* B, size_t s) {
  587. if (B->capacity - B->nelems < s) { /* needs to grow */
  588. char* newptr = NULL;
  589. size_t newcap = B->capacity * 2;
  590. if (newcap - B->nelems < s)
  591. newcap = B->nelems + s;
  592. if (newcap < B->capacity) /* overflow */
  593. luaL_error(B->L2, "buffer too large");
  594. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
  595. newptr = (char*)lua_newuserdatauv(B->L2, newcap, 0);
  596. #else
  597. newptr = (char*)lua_newuserdata(B->L2, newcap);
  598. #endif
  599. memcpy(newptr, B->ptr, B->nelems);
  600. if (B->ptr != B->b.buffer)
  601. lua_replace(B->L2, -2); /* remove old buffer */
  602. B->ptr = newptr;
  603. B->capacity = newcap;
  604. }
  605. return B->ptr + B->nelems;
  606. }
  607. COMPAT53_API void luaL_addlstring(luaL_Buffer_53* B, const char* s, size_t l) {
  608. memcpy(luaL_prepbuffsize(B, l), s, l);
  609. luaL_addsize(B, l);
  610. }
  611. COMPAT53_API void luaL_addvalue(luaL_Buffer_53* B) {
  612. size_t len = 0;
  613. const char* s = lua_tolstring(B->L2, -1, &len);
  614. if (!s)
  615. luaL_error(B->L2, "cannot convert value to string");
  616. if (B->ptr != B->b.buffer)
  617. lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
  618. luaL_addlstring(B, s, len);
  619. lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
  620. }
  621. void luaL_pushresult(luaL_Buffer_53* B) {
  622. lua_pushlstring(B->L2, B->ptr, B->nelems);
  623. if (B->ptr != B->b.buffer)
  624. lua_replace(B->L2, -2); /* remove userdata buffer */
  625. }
  626. #endif /* Lua 5.1 */
  627. /* definitions for Lua 5.1 and Lua 5.2 */
  628. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  629. COMPAT53_API int lua_geti(lua_State* L, int index, lua_Integer i) {
  630. index = lua_absindex(L, index);
  631. lua_pushinteger(L, i);
  632. lua_gettable(L, index);
  633. return lua_type(L, -1);
  634. }
  635. COMPAT53_API int lua_isinteger(lua_State* L, int index) {
  636. if (lua_type(L, index) == LUA_TNUMBER) {
  637. lua_Number n = lua_tonumber(L, index);
  638. lua_Integer i = lua_tointeger(L, index);
  639. if (i == n)
  640. return 1;
  641. }
  642. return 0;
  643. }
  644. COMPAT53_API lua_Integer lua_tointegerx(lua_State* L, int i, int* isnum) {
  645. int ok = 0;
  646. lua_Number n = lua_tonumberx(L, i, &ok);
  647. if (ok) {
  648. if (n == (lua_Integer)n) {
  649. if (isnum)
  650. *isnum = 1;
  651. return (lua_Integer)n;
  652. }
  653. }
  654. if (isnum)
  655. *isnum = 0;
  656. return 0;
  657. }
  658. static void compat53_reverse(lua_State* L, int a, int b) {
  659. for (; a < b; ++a, --b) {
  660. lua_pushvalue(L, a);
  661. lua_pushvalue(L, b);
  662. lua_replace(L, a);
  663. lua_replace(L, b);
  664. }
  665. }
  666. COMPAT53_API void lua_rotate(lua_State* L, int idx, int n) {
  667. int n_elems = 0;
  668. idx = lua_absindex(L, idx);
  669. n_elems = lua_gettop(L) - idx + 1;
  670. if (n < 0)
  671. n += n_elems;
  672. if (n > 0 && n < n_elems) {
  673. luaL_checkstack(L, 2, "not enough stack slots available");
  674. n = n_elems - n;
  675. compat53_reverse(L, idx, idx + n - 1);
  676. compat53_reverse(L, idx + n, idx + n_elems - 1);
  677. compat53_reverse(L, idx, idx + n_elems - 1);
  678. }
  679. }
  680. COMPAT53_API void lua_seti(lua_State* L, int index, lua_Integer i) {
  681. luaL_checkstack(L, 1, "not enough stack slots available");
  682. index = lua_absindex(L, index);
  683. lua_pushinteger(L, i);
  684. lua_insert(L, -2);
  685. lua_settable(L, index);
  686. }
  687. #if !defined(lua_str2number)
  688. #define lua_str2number(s, p) strtod((s), (p))
  689. #endif
  690. COMPAT53_API size_t lua_stringtonumber(lua_State* L, const char* s) {
  691. char* endptr;
  692. lua_Number n = lua_str2number(s, &endptr);
  693. if (endptr != s) {
  694. while (*endptr != '\0' && isspace((unsigned char)*endptr))
  695. ++endptr;
  696. if (*endptr == '\0') {
  697. lua_pushnumber(L, n);
  698. return endptr - s + 1;
  699. }
  700. }
  701. return 0;
  702. }
  703. COMPAT53_API const char* luaL_tolstring(lua_State* L, int idx, size_t* len) {
  704. if (!luaL_callmeta(L, idx, "__tostring")) {
  705. int t = lua_type(L, idx), tt = 0;
  706. char const* name = NULL;
  707. switch (t) {
  708. case LUA_TNIL:
  709. lua_pushliteral(L, "nil");
  710. break;
  711. case LUA_TSTRING:
  712. case LUA_TNUMBER:
  713. lua_pushvalue(L, idx);
  714. break;
  715. case LUA_TBOOLEAN:
  716. if (lua_toboolean(L, idx))
  717. lua_pushliteral(L, "true");
  718. else
  719. lua_pushliteral(L, "false");
  720. break;
  721. default:
  722. tt = luaL_getmetafield(L, idx, "__name");
  723. name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
  724. lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
  725. if (tt != LUA_TNIL)
  726. lua_replace(L, -2);
  727. break;
  728. }
  729. }
  730. else {
  731. if (!lua_isstring(L, -1))
  732. luaL_error(L, "'__tostring' must return a string");
  733. }
  734. return lua_tolstring(L, -1, len);
  735. }
  736. COMPAT53_API void luaL_requiref(lua_State* L, const char* modname, lua_CFunction openf, int glb) {
  737. luaL_checkstack(L, 3, "not enough stack slots available");
  738. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
  739. if (lua_getfield(L, -1, modname) == LUA_TNIL) {
  740. lua_pop(L, 1);
  741. lua_pushcfunction(L, openf);
  742. lua_pushstring(L, modname);
  743. lua_call(L, 1, 1);
  744. lua_pushvalue(L, -1);
  745. lua_setfield(L, -3, modname);
  746. }
  747. if (glb) {
  748. lua_pushvalue(L, -1);
  749. lua_setglobal(L, modname);
  750. }
  751. lua_replace(L, -2);
  752. }
  753. #endif /* Lua 5.1 and 5.2 */
  754. #endif /* KEPLER_PROJECT_COMPAT53_C_ */
  755. /*********************************************************************
  756. * This file contains parts of Lua 5.2's and Lua 5.3's source code:
  757. *
  758. * Copyright (C) 1994-2014 Lua.org, PUC-Rio.
  759. *
  760. * Permission is hereby granted, free of charge, to any person obtaining
  761. * a copy of this software and associated documentation files (the
  762. * "Software"), to deal in the Software without restriction, including
  763. * without limitation the rights to use, copy, modify, merge, publish,
  764. * distribute, sublicense, and/or sell copies of the Software, and to
  765. * permit persons to whom the Software is furnished to do so, subject to
  766. * the following conditions:
  767. *
  768. * The above copyright notice and this permission notice shall be
  769. * included in all copies or substantial portions of the Software.
  770. *
  771. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  772. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  773. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  774. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  775. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  776. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  777. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  778. *********************************************************************/